Clément Béra | 390f4a4 | 2023-02-21 09:16:57 +0100 | [diff] [blame] | 1 | // Copyright (c) 2023, the R8 project authors. Please see the AUTHORS file |
| 2 | // for details. All rights reserved. Use of this source code is governed by a |
| 3 | // BSD-style license that can be found in the LICENSE file. |
| 4 | |
| 5 | package backport; |
| 6 | |
| 7 | public class StrictMathBackportJava17Main { |
| 8 | |
| 9 | public static void main(String[] args) { |
| 10 | // The methods are actually from Java 15, but we can test them from Java 17. |
| 11 | testAbsExactInteger(); |
| 12 | testAbsExactLong(); |
| 13 | // The methods are actually from Java 14, but we can test them from Java 17. |
| 14 | testDecrementExactInteger(); |
| 15 | testDecrementExactLong(); |
| 16 | testIncrementExactInteger(); |
| 17 | testIncrementExactLong(); |
| 18 | testNegateExactInteger(); |
| 19 | testNegateExactLong(); |
| 20 | } |
| 21 | |
| 22 | private static void testAbsExactInteger() { |
| 23 | assertEquals(42, StrictMath.absExact(42)); |
| 24 | assertEquals(42, StrictMath.absExact(-42)); |
| 25 | assertEquals(Integer.MAX_VALUE, StrictMath.absExact(Integer.MAX_VALUE)); |
| 26 | try { |
| 27 | throw new AssertionError(StrictMath.absExact(Integer.MIN_VALUE)); |
| 28 | } catch (ArithmeticException expected) { |
| 29 | |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | private static void testAbsExactLong() { |
| 34 | assertEquals(42L, StrictMath.absExact(42L)); |
| 35 | assertEquals(42L, StrictMath.absExact(-42L)); |
| 36 | assertEquals(Long.MAX_VALUE, StrictMath.absExact(Long.MAX_VALUE)); |
| 37 | try { |
| 38 | throw new AssertionError(StrictMath.absExact(Long.MIN_VALUE)); |
| 39 | } catch (ArithmeticException expected) { |
| 40 | |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | private static void testDecrementExactInteger() { |
| 45 | assertEquals(-1, StrictMath.decrementExact(0)); |
| 46 | assertEquals(Integer.MIN_VALUE, StrictMath.decrementExact(Integer.MIN_VALUE + 1)); |
| 47 | |
| 48 | try { |
| 49 | throw new AssertionError(StrictMath.decrementExact(Integer.MIN_VALUE)); |
| 50 | } catch (ArithmeticException expected) { |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | private static void testDecrementExactLong() { |
| 55 | assertEquals(-1L, StrictMath.decrementExact(0L)); |
| 56 | assertEquals(Long.MIN_VALUE, StrictMath.decrementExact(Long.MIN_VALUE + 1L)); |
| 57 | |
| 58 | try { |
| 59 | throw new AssertionError(StrictMath.decrementExact(Long.MIN_VALUE)); |
| 60 | } catch (ArithmeticException expected) { |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | private static void testIncrementExactInteger() { |
| 65 | assertEquals(1, StrictMath.incrementExact(0)); |
| 66 | assertEquals(Integer.MAX_VALUE, StrictMath.incrementExact(Integer.MAX_VALUE - 1)); |
| 67 | |
| 68 | try { |
| 69 | throw new AssertionError(StrictMath.incrementExact(Integer.MAX_VALUE)); |
| 70 | } catch (ArithmeticException expected) { |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | private static void testIncrementExactLong() { |
| 75 | assertEquals(1L, StrictMath.incrementExact(0L)); |
| 76 | assertEquals(Long.MAX_VALUE, StrictMath.incrementExact(Long.MAX_VALUE - 1L)); |
| 77 | |
| 78 | try { |
| 79 | throw new AssertionError(StrictMath.incrementExact(Long.MAX_VALUE)); |
| 80 | } catch (ArithmeticException expected) { |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | private static void testNegateExactInteger() { |
| 85 | assertEquals(0, StrictMath.negateExact(0)); |
| 86 | assertEquals(-1, StrictMath.negateExact(1)); |
| 87 | assertEquals(1, StrictMath.negateExact(-1)); |
| 88 | assertEquals(-2_147_483_647, StrictMath.negateExact(Integer.MAX_VALUE)); |
| 89 | |
| 90 | try { |
| 91 | throw new AssertionError(StrictMath.negateExact(Integer.MIN_VALUE)); |
| 92 | } catch (ArithmeticException expected) { |
| 93 | } |
| 94 | } |
| 95 | |
| 96 | private static void testNegateExactLong() { |
| 97 | assertEquals(0L, StrictMath.negateExact(0L)); |
| 98 | assertEquals(-1L, StrictMath.negateExact(1L)); |
| 99 | assertEquals(1L, StrictMath.negateExact(-1L)); |
| 100 | assertEquals(-9_223_372_036_854_775_807L, StrictMath.negateExact(Long.MAX_VALUE)); |
| 101 | |
| 102 | try { |
| 103 | throw new AssertionError(StrictMath.negateExact(Long.MIN_VALUE)); |
| 104 | } catch (ArithmeticException expected) { |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | private static void assertEquals(int expected, int actual) { |
| 109 | if (expected != actual) { |
| 110 | throw new AssertionError("Expected <" + expected + "> but was <" + actual + '>'); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | private static void assertEquals(long expected, long actual) { |
| 115 | if (expected != actual) { |
| 116 | throw new AssertionError("Expected <" + expected + "> but was <" + actual + '>'); |
| 117 | } |
| 118 | } |
| 119 | } |