Path: blob/master/test/jdk/java/lang/Math/AbsTests.java
41152 views
/*1* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.function.*;2425/*26* @test27* @bug 824137428* @summary Test abs and absExact for Math and StrictMath29*/30public class AbsTests {31private static int errors = 0;3233public static void main(String... args) {34errors += testInRangeIntAbs();35errors += testIntMinValue();36errors += testInRangeLongAbs();37errors += testLongMinValue();3839if (errors > 0) {40throw new RuntimeException(errors + " errors found testing abs.");41}42}4344private static int testInRangeIntAbs() {45int errors = 0;46int[][] testCases = {47// Argument to abs, expected result48{+0, 0},49{+1, 1},50{-1, 1},51{-2, 2},52{+2, 2},53{-Integer.MAX_VALUE, Integer.MAX_VALUE},54{+Integer.MAX_VALUE, Integer.MAX_VALUE}55};5657for(var testCase : testCases) {58errors += testIntAbs(Math::abs, testCase[0], testCase[1]);59errors += testIntAbs(Math::absExact, testCase[0], testCase[1]);60}61return errors;62}6364private static int testIntMinValue() {65int errors = 0;66// Strange but true67errors += testIntAbs(Math::abs, Integer.MIN_VALUE, Integer.MIN_VALUE);6869// Test exceptional behavior for absExact70try {71int result = Math.absExact(Integer.MIN_VALUE);72System.err.printf("Bad return value %d from Math.absExact(MIN_VALUE)%n",73result);74errors++;75} catch (ArithmeticException ae) {76; // Expected77}78return errors;79}8081private static int testIntAbs(IntUnaryOperator absFunc,82int argument, int expected) {83int result = absFunc.applyAsInt(argument);84if (result != expected) {85System.err.printf("Unexpected int abs result %d for argument %d%n",86result, argument);87return 1;88} else {89return 0;90}91}9293// --------------------------------------------------------------------9495private static long testInRangeLongAbs() {96int errors = 0;97long[][] testCases = {98// Argument to abs, expected result99{+0L, 0L},100{+1L, 1L},101{-1L, 1L},102{-2L, 2L},103{+2L, 2L},104{-Integer.MAX_VALUE, Integer.MAX_VALUE},105{+Integer.MAX_VALUE, Integer.MAX_VALUE},106{ Integer.MIN_VALUE, -((long)Integer.MIN_VALUE)},107{-Long.MAX_VALUE, Long.MAX_VALUE},108};109110for(var testCase : testCases) {111errors += testLongAbs(Math::abs, testCase[0], testCase[1]);112errors += testLongAbs(Math::absExact, testCase[0], testCase[1]);113}114return errors;115}116117private static int testLongMinValue() {118int errors = 0;119// Strange but true120errors += testLongAbs(Math::abs, Long.MIN_VALUE, Long.MIN_VALUE);121122// Test exceptional behavior for absExact123try {124long result = Math.absExact(Long.MIN_VALUE);125System.err.printf("Bad return value %d from Math.absExact(MIN_VALUE)%n",126result);127errors++;128} catch (ArithmeticException ae) {129; // Expected130}131return errors;132}133134private static int testLongAbs(LongUnaryOperator absFunc,135long argument, long expected) {136long result = absFunc.applyAsLong(argument);137if (result != expected) {138System.err.printf("Unexpected long abs result %d for argument %d%n",139result, argument);140return 1;141} else {142return 0;143}144}145}146147148