1 /*
2  * Copyright (c) 2016, 2018, 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 
24 
25 package org.graalvm.compiler.replacements.test;
26 
27 import org.junit.Assert;
28 import org.junit.Test;
29 
30 import org.graalvm.compiler.core.common.type.IntegerStamp;
31 import org.graalvm.compiler.core.common.type.StampFactory;
32 
33 public class IntegerSubOverflowsTest {
34 
35     @Test
testOverflowCheck()36     public void testOverflowCheck() {
37         long a = Integer.MIN_VALUE;
38         long b = -1;
39         Assert.assertFalse(IntegerStamp.subtractionOverflows(a, b, 32));
40     }
41 
42     @Test
testOverflowCheck01()43     public void testOverflowCheck01() {
44         long a = Integer.MAX_VALUE;
45         long b = Integer.MAX_VALUE;
46         Assert.assertFalse(IntegerStamp.subtractionOverflows(a, b, 32));
47     }
48 
49     @Test
testOverflowCheck02()50     public void testOverflowCheck02() {
51         long a = Integer.MIN_VALUE;
52         long b = Integer.MIN_VALUE;
53         Assert.assertFalse(IntegerStamp.subtractionOverflows(a, b, 32));
54     }
55 
56     @Test
testOverflowCheck03()57     public void testOverflowCheck03() {
58         long a = Integer.MIN_VALUE;
59         long b = 1;
60         Assert.assertTrue(IntegerStamp.subtractionOverflows(a, b, 32));
61     }
62 
63     @Test
testOverflowCheck04()64     public void testOverflowCheck04() {
65         long a = Integer.MAX_VALUE;
66         long b = 1;
67         Assert.assertFalse(IntegerStamp.subtractionOverflows(a, b, 32));
68     }
69 
70     @Test
testOverflowCheck05()71     public void testOverflowCheck05() {
72         long a = Integer.MAX_VALUE;
73         long b = Integer.MIN_VALUE;
74         Assert.assertTrue(IntegerStamp.subtractionOverflows(a, b, 32));
75     }
76 
77     @Test
testOverflowCheck06()78     public void testOverflowCheck06() {
79         long a = Integer.MAX_VALUE;
80         long b = Integer.MAX_VALUE;
81         Assert.assertFalse(IntegerStamp.subtractionOverflows(a, b, 64));
82     }
83 
84     @Test
testOverflowCheck07()85     public void testOverflowCheck07() {
86         long a = Long.MAX_VALUE;
87         long b = 2;
88         Assert.assertFalse(IntegerStamp.subtractionOverflows(a, b, 64));
89     }
90 
91     @Test
testOverflowCheck08()92     public void testOverflowCheck08() {
93         long a = Long.MAX_VALUE;
94         long b = Long.MAX_VALUE;
95         Assert.assertFalse(IntegerStamp.subtractionOverflows(a, b, 64));
96     }
97 
98     @Test
testOverflowCheck09()99     public void testOverflowCheck09() {
100         long a = -Long.MAX_VALUE;
101         long b = Long.MAX_VALUE;
102         Assert.assertTrue(IntegerStamp.subtractionOverflows(a, b, 64));
103     }
104 
105     @Test
testOverflowCheck10()106     public void testOverflowCheck10() {
107         long a = -Long.MAX_VALUE;
108         long b = -Long.MAX_VALUE;
109         Assert.assertFalse(IntegerStamp.subtractionOverflows(a, b, 64));
110     }
111 
112     @Test
testOverflowCheck11()113     public void testOverflowCheck11() {
114         long a = Long.MAX_VALUE;
115         long b = -Long.MAX_VALUE;
116         Assert.assertTrue(IntegerStamp.subtractionOverflows(a, b, 64));
117     }
118 
119     @Test
testOverflowCheckStamp()120     public void testOverflowCheckStamp() {
121         IntegerStamp s1 = StampFactory.forInteger(32, Integer.MIN_VALUE, Integer.MIN_VALUE);
122         IntegerStamp s2 = StampFactory.forInteger(32, -1, -1);
123         Assert.assertFalse(IntegerStamp.subtractionCanOverflow(s1, s2));
124     }
125 
126     @Test
testOverflowCheckStamp01()127     public void testOverflowCheckStamp01() {
128         IntegerStamp s1 = StampFactory.forInteger(32, Integer.MAX_VALUE, Integer.MAX_VALUE);
129         IntegerStamp s2 = StampFactory.forInteger(32, Integer.MAX_VALUE, Integer.MAX_VALUE);
130         Assert.assertFalse(IntegerStamp.subtractionCanOverflow(s1, s2));
131     }
132 
133     @Test
testOverflowCheckStamp02()134     public void testOverflowCheckStamp02() {
135         IntegerStamp s1 = StampFactory.forInteger(32, Integer.MIN_VALUE, Integer.MIN_VALUE);
136         IntegerStamp s2 = StampFactory.forInteger(32, Integer.MIN_VALUE, Integer.MIN_VALUE);
137         Assert.assertFalse(IntegerStamp.subtractionCanOverflow(s1, s2));
138     }
139 
140     @Test
testOverflowCheckStamp03()141     public void testOverflowCheckStamp03() {
142         IntegerStamp s1 = StampFactory.forInteger(32, Integer.MIN_VALUE, Integer.MIN_VALUE);
143         IntegerStamp s2 = StampFactory.forInteger(32, 1, 1);
144         Assert.assertTrue(IntegerStamp.subtractionCanOverflow(s1, s2));
145     }
146 
147     @Test
testOverflowCheckStamp04()148     public void testOverflowCheckStamp04() {
149         IntegerStamp s1 = StampFactory.forInteger(8, Byte.MIN_VALUE, Byte.MIN_VALUE);
150         IntegerStamp s2 = StampFactory.forInteger(8, -1, -1);
151         Assert.assertFalse(IntegerStamp.subtractionCanOverflow(s1, s2));
152     }
153 
154     @Test
testOverflowCheckStamp05()155     public void testOverflowCheckStamp05() {
156         IntegerStamp s1 = StampFactory.forInteger(8, Byte.MAX_VALUE, Byte.MAX_VALUE);
157         IntegerStamp s2 = StampFactory.forInteger(8, Byte.MAX_VALUE, Byte.MAX_VALUE);
158         Assert.assertFalse(IntegerStamp.subtractionCanOverflow(s1, s2));
159     }
160 
161     @Test
testOverflowCheckStamp06()162     public void testOverflowCheckStamp06() {
163         IntegerStamp s1 = StampFactory.forInteger(8, Byte.MIN_VALUE, Byte.MIN_VALUE);
164         IntegerStamp s2 = StampFactory.forInteger(8, Byte.MIN_VALUE, Byte.MIN_VALUE);
165         Assert.assertFalse(IntegerStamp.subtractionCanOverflow(s1, s2));
166     }
167 
168     @Test
testOverflowCheckStamp07()169     public void testOverflowCheckStamp07() {
170         IntegerStamp s1 = StampFactory.forInteger(8, Byte.MIN_VALUE, Byte.MIN_VALUE);
171         IntegerStamp s2 = StampFactory.forInteger(8, 1, 1);
172         Assert.assertTrue(IntegerStamp.subtractionCanOverflow(s1, s2));
173     }
174 
175     @Test
testOverflowCheckStamp08()176     public void testOverflowCheckStamp08() {
177         IntegerStamp s1 = StampFactory.forInteger(64, Long.MIN_VALUE, Long.MIN_VALUE);
178         IntegerStamp s2 = StampFactory.forInteger(64, -1, -1);
179         Assert.assertFalse(IntegerStamp.subtractionCanOverflow(s1, s2));
180     }
181 
182     @Test
testOverflowCheckStamp09()183     public void testOverflowCheckStamp09() {
184         IntegerStamp s1 = StampFactory.forInteger(64, Long.MAX_VALUE, Long.MAX_VALUE);
185         IntegerStamp s2 = StampFactory.forInteger(64, Long.MAX_VALUE, Long.MAX_VALUE);
186         Assert.assertFalse(IntegerStamp.subtractionCanOverflow(s1, s2));
187     }
188 
189     @Test
testOverflowCheckStamp10()190     public void testOverflowCheckStamp10() {
191         IntegerStamp s1 = StampFactory.forInteger(64, Long.MIN_VALUE, Long.MIN_VALUE);
192         IntegerStamp s2 = StampFactory.forInteger(64, Long.MIN_VALUE, Long.MIN_VALUE);
193         Assert.assertFalse(IntegerStamp.subtractionCanOverflow(s1, s2));
194     }
195 
196     @Test
testOverflowCheckStamp11()197     public void testOverflowCheckStamp11() {
198         IntegerStamp s1 = StampFactory.forInteger(64, Long.MIN_VALUE, Long.MIN_VALUE);
199         IntegerStamp s2 = StampFactory.forInteger(64, 1, 1);
200         Assert.assertTrue(IntegerStamp.subtractionCanOverflow(s1, s2));
201     }
202 
203     @Test
testOverflowBIgStamps01()204     public void testOverflowBIgStamps01() {
205         IntegerStamp s1 = StampFactory.forInteger(64, Long.MIN_VALUE, Long.MAX_VALUE);
206         IntegerStamp s2 = StampFactory.forInteger(64, Long.MIN_VALUE, Long.MAX_VALUE);
207         Assert.assertTrue(IntegerStamp.subtractionCanOverflow(s1, s2));
208     }
209 
210     @Test
testOverflowBIgStamps02()211     public void testOverflowBIgStamps02() {
212         IntegerStamp s1 = StampFactory.forInteger(64, Long.MIN_VALUE, Long.MAX_VALUE);
213         IntegerStamp s2 = StampFactory.forInteger(64, Long.MIN_VALUE, Long.MIN_VALUE);
214         Assert.assertTrue(IntegerStamp.subtractionCanOverflow(s1, s2));
215     }
216 }
217