1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 package org.graalvm.compiler.replacements.test;
  24 
  25 import org.junit.Assert;
  26 import org.junit.Test;
  27 
  28 import org.graalvm.compiler.core.common.type.IntegerStamp;
  29 import org.graalvm.compiler.core.common.type.StampFactory;
  30 
  31 public class IntegerSubOverflowsTest {
  32 
  33     @Test
  34     public void testOverflowCheck() {
  35         long a = Integer.MIN_VALUE;
  36         long b = -1;
  37         Assert.assertFalse(IntegerStamp.subtractionOverflows(a, b, 32));
  38     }
  39 
  40     @Test
  41     public void testOverflowCheck01() {
  42         long a = Integer.MAX_VALUE;
  43         long b = Integer.MAX_VALUE;
  44         Assert.assertFalse(IntegerStamp.subtractionOverflows(a, b, 32));
  45     }
  46 
  47     @Test
  48     public void testOverflowCheck02() {
  49         long a = Integer.MIN_VALUE;
  50         long b = Integer.MIN_VALUE;
  51         Assert.assertFalse(IntegerStamp.subtractionOverflows(a, b, 32));
  52     }
  53 
  54     @Test
  55     public void testOverflowCheck03() {
  56         long a = Integer.MIN_VALUE;
  57         long b = 1;
  58         Assert.assertTrue(IntegerStamp.subtractionOverflows(a, b, 32));
  59     }
  60 
  61     @Test
  62     public void testOverflowCheck04() {
  63         long a = Integer.MAX_VALUE;
  64         long b = 1;
  65         Assert.assertFalse(IntegerStamp.subtractionOverflows(a, b, 32));
  66     }
  67 
  68     @Test
  69     public void testOverflowCheck05() {
  70         long a = Integer.MAX_VALUE;
  71         long b = Integer.MIN_VALUE;
  72         Assert.assertTrue(IntegerStamp.subtractionOverflows(a, b, 32));
  73     }
  74 
  75     @Test
  76     public void testOverflowCheck06() {
  77         long a = Integer.MAX_VALUE;
  78         long b = Integer.MAX_VALUE;
  79         Assert.assertFalse(IntegerStamp.subtractionOverflows(a, b, 64));
  80     }
  81 
  82     @Test
  83     public void testOverflowCheck07() {
  84         long a = Long.MAX_VALUE;
  85         long b = 2;
  86         Assert.assertFalse(IntegerStamp.subtractionOverflows(a, b, 64));
  87     }
  88 
  89     @Test
  90     public void testOverflowCheck08() {
  91         long a = Long.MAX_VALUE;
  92         long b = Long.MAX_VALUE;
  93         Assert.assertFalse(IntegerStamp.subtractionOverflows(a, b, 64));
  94     }
  95 
  96     @Test
  97     public void testOverflowCheck09() {
  98         long a = -Long.MAX_VALUE;
  99         long b = Long.MAX_VALUE;
 100         Assert.assertTrue(IntegerStamp.subtractionOverflows(a, b, 64));
 101     }
 102 
 103     @Test
 104     public void testOverflowCheck10() {
 105         long a = -Long.MAX_VALUE;
 106         long b = -Long.MAX_VALUE;
 107         Assert.assertFalse(IntegerStamp.subtractionOverflows(a, b, 64));
 108     }
 109 
 110     @Test
 111     public void testOverflowCheck11() {
 112         long a = Long.MAX_VALUE;
 113         long b = -Long.MAX_VALUE;
 114         Assert.assertTrue(IntegerStamp.subtractionOverflows(a, b, 64));
 115     }
 116 
 117     @Test
 118     public void testOverflowCheckStamp() {
 119         IntegerStamp s1 = StampFactory.forInteger(32, Integer.MIN_VALUE, Integer.MIN_VALUE);
 120         IntegerStamp s2 = StampFactory.forInteger(32, -1, -1);
 121         Assert.assertFalse(IntegerStamp.subtractionCanOverflow(s1, s2));
 122     }
 123 
 124     @Test
 125     public void testOverflowCheckStamp01() {
 126         IntegerStamp s1 = StampFactory.forInteger(32, Integer.MAX_VALUE, Integer.MAX_VALUE);
 127         IntegerStamp s2 = StampFactory.forInteger(32, Integer.MAX_VALUE, Integer.MAX_VALUE);
 128         Assert.assertFalse(IntegerStamp.subtractionCanOverflow(s1, s2));
 129     }
 130 
 131     @Test
 132     public void testOverflowCheckStamp02() {
 133         IntegerStamp s1 = StampFactory.forInteger(32, Integer.MIN_VALUE, Integer.MIN_VALUE);
 134         IntegerStamp s2 = StampFactory.forInteger(32, Integer.MIN_VALUE, Integer.MIN_VALUE);
 135         Assert.assertFalse(IntegerStamp.subtractionCanOverflow(s1, s2));
 136     }
 137 
 138     @Test
 139     public void testOverflowCheckStamp03() {
 140         IntegerStamp s1 = StampFactory.forInteger(32, Integer.MIN_VALUE, Integer.MIN_VALUE);
 141         IntegerStamp s2 = StampFactory.forInteger(32, 1, 1);
 142         Assert.assertTrue(IntegerStamp.subtractionCanOverflow(s1, s2));
 143     }
 144 
 145     @Test
 146     public void testOverflowCheckStamp04() {
 147         IntegerStamp s1 = StampFactory.forInteger(8, Byte.MIN_VALUE, Byte.MIN_VALUE);
 148         IntegerStamp s2 = StampFactory.forInteger(8, -1, -1);
 149         Assert.assertFalse(IntegerStamp.subtractionCanOverflow(s1, s2));
 150     }
 151 
 152     @Test
 153     public void testOverflowCheckStamp05() {
 154         IntegerStamp s1 = StampFactory.forInteger(8, Byte.MAX_VALUE, Byte.MAX_VALUE);
 155         IntegerStamp s2 = StampFactory.forInteger(8, Byte.MAX_VALUE, Byte.MAX_VALUE);
 156         Assert.assertFalse(IntegerStamp.subtractionCanOverflow(s1, s2));
 157     }
 158 
 159     @Test
 160     public void testOverflowCheckStamp06() {
 161         IntegerStamp s1 = StampFactory.forInteger(8, Byte.MIN_VALUE, Byte.MIN_VALUE);
 162         IntegerStamp s2 = StampFactory.forInteger(8, Byte.MIN_VALUE, Byte.MIN_VALUE);
 163         Assert.assertFalse(IntegerStamp.subtractionCanOverflow(s1, s2));
 164     }
 165 
 166     @Test
 167     public void testOverflowCheckStamp07() {
 168         IntegerStamp s1 = StampFactory.forInteger(8, Byte.MIN_VALUE, Byte.MIN_VALUE);
 169         IntegerStamp s2 = StampFactory.forInteger(8, 1, 1);
 170         Assert.assertTrue(IntegerStamp.subtractionCanOverflow(s1, s2));
 171     }
 172 
 173     @Test
 174     public void testOverflowCheckStamp08() {
 175         IntegerStamp s1 = StampFactory.forInteger(64, Long.MIN_VALUE, Long.MIN_VALUE);
 176         IntegerStamp s2 = StampFactory.forInteger(64, -1, -1);
 177         Assert.assertFalse(IntegerStamp.subtractionCanOverflow(s1, s2));
 178     }
 179 
 180     @Test
 181     public void testOverflowCheckStamp09() {
 182         IntegerStamp s1 = StampFactory.forInteger(64, Long.MAX_VALUE, Long.MAX_VALUE);
 183         IntegerStamp s2 = StampFactory.forInteger(64, Long.MAX_VALUE, Long.MAX_VALUE);
 184         Assert.assertFalse(IntegerStamp.subtractionCanOverflow(s1, s2));
 185     }
 186 
 187     @Test
 188     public void testOverflowCheckStamp10() {
 189         IntegerStamp s1 = StampFactory.forInteger(64, Long.MIN_VALUE, Long.MIN_VALUE);
 190         IntegerStamp s2 = StampFactory.forInteger(64, Long.MIN_VALUE, Long.MIN_VALUE);
 191         Assert.assertFalse(IntegerStamp.subtractionCanOverflow(s1, s2));
 192     }
 193 
 194     @Test
 195     public void testOverflowCheckStamp11() {
 196         IntegerStamp s1 = StampFactory.forInteger(64, Long.MIN_VALUE, Long.MIN_VALUE);
 197         IntegerStamp s2 = StampFactory.forInteger(64, 1, 1);
 198         Assert.assertTrue(IntegerStamp.subtractionCanOverflow(s1, s2));
 199     }
 200 
 201     @Test
 202     public void testOverflowBIgStamps01() {
 203         IntegerStamp s1 = StampFactory.forInteger(64, Long.MIN_VALUE, Long.MAX_VALUE);
 204         IntegerStamp s2 = StampFactory.forInteger(64, Long.MIN_VALUE, Long.MAX_VALUE);
 205         Assert.assertTrue(IntegerStamp.subtractionCanOverflow(s1, s2));
 206     }
 207 
 208     @Test
 209     public void testOverflowBIgStamps02() {
 210         IntegerStamp s1 = StampFactory.forInteger(64, Long.MIN_VALUE, Long.MAX_VALUE);
 211         IntegerStamp s2 = StampFactory.forInteger(64, Long.MIN_VALUE, Long.MIN_VALUE);
 212         Assert.assertTrue(IntegerStamp.subtractionCanOverflow(s1, s2));
 213     }
 214 }