1 /* 2 * Copyright (c) 2020, 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 test.java.lang.Math; 24 25 import java.util.function.*; 26 27 import static org.testng.Assert.assertEquals; 28 import static org.testng.Assert.assertThrows; 29 import org.testng.annotations.Test; 30 /* 31 * @test 32 * @bug 8241374 33 * @summary Test abs and absExact for Math and StrictMath 34 */ 35 public class AbsTests { 36 37 @Test testInRangeIntAbs()38 public void testInRangeIntAbs() { 39 int[][] testCases = { 40 // Argument to abs, expected result 41 {+0, 0}, 42 {+1, 1}, 43 {-1, 1}, 44 {-2, 2}, 45 {+2, 2}, 46 {-Integer.MAX_VALUE, Integer.MAX_VALUE}, 47 {+Integer.MAX_VALUE, Integer.MAX_VALUE} 48 }; 49 50 for(var testCase : testCases) { 51 testIntAbs(Math::abs, testCase[0], testCase[1]); 52 testIntAbs(Math::absExact, testCase[0], testCase[1]); 53 } 54 } 55 56 @Test testIntMinValue()57 public void testIntMinValue() { 58 // Strange but true 59 testIntAbs(Math::abs, Integer.MIN_VALUE, Integer.MIN_VALUE); 60 61 // Test exceptional behavior for absExact 62 assertThrows(ArithmeticException.class, () -> Math.absExact(Integer.MIN_VALUE)); 63 } 64 testIntAbs(IntUnaryOperator absFunc, int argument, int expected)65 private static void testIntAbs(IntUnaryOperator absFunc, 66 int argument, int expected) { 67 int result = absFunc.applyAsInt(argument); 68 assertEquals(result, expected, 69 String.format("Unexpected int abs result %d for argument %d%n", 70 result, argument)); 71 } 72 73 // -------------------------------------------------------------------- 74 75 @Test testInRangeLongAbs()76 public void testInRangeLongAbs() { 77 long[][] testCases = { 78 // Argument to abs, expected result 79 {+0L, 0L}, 80 {+1L, 1L}, 81 {-1L, 1L}, 82 {-2L, 2L}, 83 {+2L, 2L}, 84 {-Integer.MAX_VALUE, Integer.MAX_VALUE}, 85 {+Integer.MAX_VALUE, Integer.MAX_VALUE}, 86 { Integer.MIN_VALUE, -((long)Integer.MIN_VALUE)}, 87 {-Long.MAX_VALUE, Long.MAX_VALUE}, 88 }; 89 90 for(var testCase : testCases) { 91 testLongAbs(Math::abs, testCase[0], testCase[1]); 92 testLongAbs(Math::absExact, testCase[0], testCase[1]); 93 } 94 } 95 96 @Test testLongMinValue()97 public void testLongMinValue() { 98 // Strange but true 99 testLongAbs(Math::abs, Long.MIN_VALUE, Long.MIN_VALUE); 100 101 // Test exceptional behavior for absExact 102 assertThrows(ArithmeticException.class, () -> Math.absExact(Long.MIN_VALUE)); 103 } 104 testLongAbs(LongUnaryOperator absFunc, long argument, long expected)105 private static void testLongAbs(LongUnaryOperator absFunc, 106 long argument, long expected) { 107 long result = absFunc.applyAsLong(argument); 108 assertEquals(result, expected, 109 String.format("Unexpected long abs result %d for argument %d%n", 110 result, argument)); 111 } 112 } 113