1 /* |
1 /* |
2 * Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved. |
2 * Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 * |
4 * |
5 * This code is free software; you can redistribute it and/or modify it |
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 |
6 * under the terms of the GNU General Public License version 2 only, as |
7 * published by the Free Software Foundation. |
7 * published by the Free Software Foundation. |
54 errors++; |
54 errors++; |
55 System.err.println(message); |
55 System.err.println(message); |
56 } |
56 } |
57 |
57 |
58 /** |
58 /** |
59 * Test Math.addExact, multiplyExact, subtractExact, toIntValue methods |
59 * Test Math.addExact, multiplyExact, subtractExact, incrementExact, |
60 * with {@code int} arguments. |
60 * decrementExact, negateExact methods with {@code int} arguments. |
61 */ |
61 */ |
62 static void testIntegerExact() { |
62 static void testIntegerExact() { |
63 testIntegerExact(0, 0); |
63 testIntegerExact(0, 0); |
64 testIntegerExact(1, 1); |
64 testIntegerExact(1, 1); |
65 testIntegerExact(1, -1); |
65 testIntegerExact(1, -1); |
74 testIntegerExact(Integer.MAX_VALUE, 2); |
74 testIntegerExact(Integer.MAX_VALUE, 2); |
75 testIntegerExact(Integer.MIN_VALUE, -1); |
75 testIntegerExact(Integer.MIN_VALUE, -1); |
76 testIntegerExact(Integer.MAX_VALUE, -1); |
76 testIntegerExact(Integer.MAX_VALUE, -1); |
77 testIntegerExact(Integer.MIN_VALUE, -2); |
77 testIntegerExact(Integer.MIN_VALUE, -2); |
78 testIntegerExact(Integer.MAX_VALUE, -2); |
78 testIntegerExact(Integer.MAX_VALUE, -2); |
79 |
|
80 } |
79 } |
81 |
80 |
82 /** |
81 /** |
83 * Test exact arithmetic by comparing with the same operations using long |
82 * Test exact arithmetic by comparing with the same operations using long |
84 * and checking that the result is the same as the integer truncation. |
83 * and checking that the result is the same as the integer truncation. |
99 } |
98 } |
100 } catch (ArithmeticException ex) { |
99 } catch (ArithmeticException ex) { |
101 long sum2 = (long) x + (long) y; |
100 long sum2 = (long) x + (long) y; |
102 if ((int) sum2 == sum2) { |
101 if ((int) sum2 == sum2) { |
103 fail("FAIL: int Math.addExact(" + x + " + " + y + ")" + "; Unexpected exception: " + ex); |
102 fail("FAIL: int Math.addExact(" + x + " + " + y + ")" + "; Unexpected exception: " + ex); |
104 |
|
105 } |
103 } |
106 } |
104 } |
107 |
105 |
108 try { |
106 try { |
109 // Test subtractExact |
107 // Test subtractExact |
145 } |
143 } |
146 } catch (ArithmeticException ex) { |
144 } catch (ArithmeticException ex) { |
147 long inc2 = (long) x + 1L; |
145 long inc2 = (long) x + 1L; |
148 if ((int) inc2 == inc2) { |
146 if ((int) inc2 == inc2) { |
149 fail("FAIL: int Math.incrementExact(" + x + ")" + "; Unexpected exception: " + ex); |
147 fail("FAIL: int Math.incrementExact(" + x + ")" + "; Unexpected exception: " + ex); |
150 |
|
151 } |
148 } |
152 } |
149 } |
153 |
150 |
154 try { |
151 try { |
155 // Test decrementExact |
152 // Test decrementExact |
162 } |
159 } |
163 } catch (ArithmeticException ex) { |
160 } catch (ArithmeticException ex) { |
164 long dec2 = (long) x - 1L; |
161 long dec2 = (long) x - 1L; |
165 if ((int) dec2 == dec2) { |
162 if ((int) dec2 == dec2) { |
166 fail("FAIL: int Math.decrementExact(" + x + ")" + "; Unexpected exception: " + ex); |
163 fail("FAIL: int Math.decrementExact(" + x + ")" + "; Unexpected exception: " + ex); |
167 |
|
168 } |
164 } |
169 } |
165 } |
170 |
166 |
171 try { |
167 try { |
172 // Test negateExact |
168 // Test negateExact |
173 int neg = Math.negateExact(x); |
169 int neg = Math.negateExact(x); |
174 long neg2 = -((long)x) ; |
170 long neg2 = -((long)x); |
175 if ((int) neg2 != neg2) { |
171 if ((int) neg2 != neg2) { |
176 fail("FAIL: int Math.negateExact(" + x + ") = " + neg + "; expected Arithmetic exception"); |
172 fail("FAIL: int Math.negateExact(" + x + ") = " + neg + "; expected Arithmetic exception"); |
177 } else if (neg != neg2) { |
173 } else if (neg != neg2) { |
178 fail("FAIL: long Math.negateExact(" + x + ") = " + neg + "; expected: " + neg2); |
174 fail("FAIL: long Math.negateExact(" + x + ") = " + neg + "; expected: " + neg2); |
179 } |
175 } |
180 } catch (ArithmeticException ex) { |
176 } catch (ArithmeticException ex) { |
181 long neg2 = (long) x - 1L; |
177 long neg2 = -((long)x); |
182 if ((int) neg2 == neg2) { |
178 if ((int) neg2 == neg2) { |
183 fail("FAIL: int Math.negateExact(" + x + ")" + "; Unexpected exception: " + ex); |
179 fail("FAIL: int Math.negateExact(" + x + ")" + "; Unexpected exception: " + ex); |
184 |
180 } |
185 } |
181 } |
186 } |
182 } |
187 } |
183 |
188 |
184 /** |
189 /** |
185 * Test Math.addExact, multiplyExact, subtractExact, incrementExact, |
190 * Test Math.addExact, multiplyExact, subtractExact, toIntExact methods |
186 * decrementExact, negateExact, toIntExact methods with {@code long} arguments. |
191 * with {@code long} arguments. |
|
192 */ |
187 */ |
193 static void testLongExact() { |
188 static void testLongExact() { |
194 testLongExactTwice(0, 0); |
189 testLongExactTwice(0, 0); |
195 testLongExactTwice(1, 1); |
190 testLongExactTwice(1, 1); |
196 testLongExactTwice(1, -1); |
191 testLongExactTwice(1, -1); |
213 testLongExactTwice(Integer.MAX_VALUE+1, Integer.MAX_VALUE+1); |
208 testLongExactTwice(Integer.MAX_VALUE+1, Integer.MAX_VALUE+1); |
214 testLongExactTwice(Integer.MAX_VALUE+1, -Integer.MAX_VALUE+1); |
209 testLongExactTwice(Integer.MAX_VALUE+1, -Integer.MAX_VALUE+1); |
215 testLongExactTwice(Integer.MIN_VALUE-1, Integer.MIN_VALUE-1); |
210 testLongExactTwice(Integer.MIN_VALUE-1, Integer.MIN_VALUE-1); |
216 testLongExactTwice(Integer.MIN_VALUE-1, -Integer.MIN_VALUE-1); |
211 testLongExactTwice(Integer.MIN_VALUE-1, -Integer.MIN_VALUE-1); |
217 testLongExactTwice(Integer.MIN_VALUE/2, 2); |
212 testLongExactTwice(Integer.MIN_VALUE/2, 2); |
218 |
|
219 } |
213 } |
220 |
214 |
221 /** |
215 /** |
222 * Test each of the exact operations with the arguments and |
216 * Test each of the exact operations with the arguments and |
223 * with the arguments reversed. |
217 * with the arguments reversed. |
317 } catch (ArithmeticException ex) { |
311 } catch (ArithmeticException ex) { |
318 if (resultBig.bitLength() <= 32) { |
312 if (resultBig.bitLength() <= 32) { |
319 fail("FAIL: long Math.toIntExact(" + x + ")" + "; Unexpected exception: " + ex); |
313 fail("FAIL: long Math.toIntExact(" + x + ")" + "; Unexpected exception: " + ex); |
320 } |
314 } |
321 } |
315 } |
322 |
|
323 } |
316 } |
324 |
317 |
325 /** |
318 /** |
326 * Compare the expected and actual results. |
319 * Compare the expected and actual results. |
327 * @param message message for the error |
320 * @param message message for the error |