Path: blob/master/test/jdk/java/lang/StrictMath/ExactArithTests.java
41149 views
/*1* Copyright (c) 2012, 2019, 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.math.BigInteger;2425/**26* @test Test for StrictMath.*Exact integer and long methods.27* @bug 670839828* @summary Basic tests for StrictMath exact arithmetic operations.29*30* @author Roger Riggs31*/32public class ExactArithTests {3334/**35* The count of test errors.36*/37private static int errors = 0;3839/**40* @param args the command line arguments41*/42public static void main(String[] args) {43testIntegerExact();44testLongExact();4546if (errors > 0) {47throw new RuntimeException(errors + " errors found in ExactArithTests.");48}49}5051static void fail(String message) {52errors++;53System.err.println(message);54}5556/**57* Test StrictMath.addExact, multiplyExact, subtractExact, incrementExact,58* decrementExact, negateExact methods with {@code int} arguments.59*/60static void testIntegerExact() {61testIntegerExact(0, 0);62testIntegerExact(1, 1);63testIntegerExact(1, -1);64testIntegerExact(-1, 1);65testIntegerExact(1000, 2000);6667testIntegerExact(Integer.MIN_VALUE, Integer.MIN_VALUE);68testIntegerExact(Integer.MAX_VALUE, Integer.MAX_VALUE);69testIntegerExact(Integer.MIN_VALUE, 1);70testIntegerExact(Integer.MAX_VALUE, 1);71testIntegerExact(Integer.MIN_VALUE, 2);72testIntegerExact(Integer.MAX_VALUE, 2);73testIntegerExact(Integer.MIN_VALUE, -1);74testIntegerExact(Integer.MAX_VALUE, -1);75testIntegerExact(Integer.MIN_VALUE, -2);76testIntegerExact(Integer.MAX_VALUE, -2);77}7879/**80* Test exact arithmetic by comparing with the same operations using long81* and checking that the result is the same as the integer truncation.82* Errors are reported with {@link fail}.83*84* @param x first parameter85* @param y second parameter86*/87static void testIntegerExact(int x, int y) {88try {89// Test addExact90int sum = StrictMath.addExact(x, y);91long sum2 = (long) x + (long) y;92if ((int) sum2 != sum2) {93fail("FAIL: int StrictMath.addExact(" + x + " + " + y + ") = " + sum + "; expected Arithmetic exception");94} else if (sum != sum2) {95fail("FAIL: long StrictMath.addExact(" + x + " + " + y + ") = " + sum + "; expected: " + sum2);96}97} catch (ArithmeticException ex) {98long sum2 = (long) x + (long) y;99if ((int) sum2 == sum2) {100fail("FAIL: int StrictMath.addExact(" + x + " + " + y + ")" + "; Unexpected exception: " + ex);101}102}103104try {105// Test subtractExact106int diff = StrictMath.subtractExact(x, y);107long diff2 = (long) x - (long) y;108if ((int) diff2 != diff2) {109fail("FAIL: int StrictMath.subtractExact(" + x + " - " + y + ") = " + diff + "; expected: " + diff2);110}111112} catch (ArithmeticException ex) {113long diff2 = (long) x - (long) y;114if ((int) diff2 == diff2) {115fail("FAIL: int StrictMath.subtractExact(" + x + " - " + y + ")" + "; Unexpected exception: " + ex);116}117}118119try {120// Test multiplyExact121int product = StrictMath.multiplyExact(x, y);122long m2 = (long) x * (long) y;123if ((int) m2 != m2) {124fail("FAIL: int StrictMath.multiplyExact(" + x + " * " + y + ") = " + product + "; expected: " + m2);125}126} catch (ArithmeticException ex) {127long m2 = (long) x * (long) y;128if ((int) m2 == m2) {129fail("FAIL: int StrictMath.multiplyExact(" + x + " * " + y + ")" + "; Unexpected exception: " + ex);130}131}132133try {134// Test incrementExact135int inc = StrictMath.incrementExact(x);136long inc2 = (long) x + 1L;137if ((int) inc2 != inc2) {138fail("FAIL: int StrictMath.incrementExact(" + x + ") = " + inc + "; expected Arithmetic exception");139} else if (inc != inc2) {140fail("FAIL: long StrictMath.incrementExact(" + x + ") = " + inc + "; expected: " + inc2);141}142} catch (ArithmeticException ex) {143long inc2 = (long) x + 1L;144if ((int) inc2 == inc2) {145fail("FAIL: int StrictMath.incrementExact(" + x + ")" + "; Unexpected exception: " + ex);146}147}148149try {150// Test decrementExact151int dec = StrictMath.decrementExact(x);152long dec2 = (long) x - 1L;153if ((int) dec2 != dec2) {154fail("FAIL: int StrictMath.decrementExact(" + x + ") = " + dec + "; expected Arithmetic exception");155} else if (dec != dec2) {156fail("FAIL: long StrictMath.decrementExact(" + x + ") = " + dec + "; expected: " + dec2);157}158} catch (ArithmeticException ex) {159long dec2 = (long) x - 1L;160if ((int) dec2 == dec2) {161fail("FAIL: int StrictMath.decrementExact(" + x + ")" + "; Unexpected exception: " + ex);162}163}164165try {166// Test negateExact167int neg = StrictMath.negateExact(x);168long neg2 = -((long)x);169if ((int) neg2 != neg2) {170fail("FAIL: int StrictMath.negateExact(" + x + ") = " + neg + "; expected Arithmetic exception");171} else if (neg != neg2) {172fail("FAIL: long StrictMath.negateExact(" + x + ") = " + neg + "; expected: " + neg2);173}174} catch (ArithmeticException ex) {175long neg2 = -((long)x);176if ((int) neg2 == neg2) {177fail("FAIL: int StrictMath.negateExact(" + x + ")" + "; Unexpected exception: " + ex);178}179}180}181182/**183* Test StrictMath.addExact, multiplyExact, subtractExact, incrementExact,184* decrementExact, negateExact, toIntExact methods with {@code long} arguments.185*/186static void testLongExact() {187testLongExactTwice(0, 0);188testLongExactTwice(1, 1);189testLongExactTwice(1, -1);190testLongExactTwice(1000, 2000);191192testLongExactTwice(Long.MIN_VALUE, Long.MIN_VALUE);193testLongExactTwice(Long.MAX_VALUE, Long.MAX_VALUE);194testLongExactTwice(Long.MIN_VALUE, 1);195testLongExactTwice(Long.MAX_VALUE, 1);196testLongExactTwice(Long.MIN_VALUE, 2);197testLongExactTwice(Long.MAX_VALUE, 2);198testLongExactTwice(Long.MIN_VALUE, -1);199testLongExactTwice(Long.MAX_VALUE, -1);200testLongExactTwice(Long.MIN_VALUE, -2);201testLongExactTwice(Long.MAX_VALUE, -2);202testLongExactTwice(Long.MIN_VALUE/2, 2);203testLongExactTwice(Long.MAX_VALUE, 2);204testLongExactTwice(Integer.MAX_VALUE, Integer.MAX_VALUE);205testLongExactTwice(Integer.MAX_VALUE, -Integer.MAX_VALUE);206testLongExactTwice(Integer.MAX_VALUE+1, Integer.MAX_VALUE+1);207testLongExactTwice(Integer.MAX_VALUE+1, -Integer.MAX_VALUE+1);208testLongExactTwice(Integer.MIN_VALUE-1, Integer.MIN_VALUE-1);209testLongExactTwice(Integer.MIN_VALUE-1, -Integer.MIN_VALUE-1);210testLongExactTwice(Integer.MIN_VALUE/2, 2);211}212213/**214* Test each of the exact operations with the arguments and215* with the arguments reversed.216* @param x217* @param y218*/219static void testLongExactTwice(long x, long y) {220testLongExact(x, y);221testLongExact(y, x);222}223224225/**226* Test long exact arithmetic by comparing with the same operations using BigInteger227* and checking that the result is the same as the long truncation.228* Errors are reported with {@link fail}.229*230* @param x first parameter231* @param y second parameter232*/233static void testLongExact(long x, long y) {234BigInteger resultBig = null;235final BigInteger xBig = BigInteger.valueOf(x);236final BigInteger yBig = BigInteger.valueOf(y);237try {238// Test addExact239resultBig = xBig.add(yBig);240long sum = StrictMath.addExact(x, y);241checkResult("long StrictMath.addExact", x, y, sum, resultBig);242} catch (ArithmeticException ex) {243if (inLongRange(resultBig)) {244fail("FAIL: long StrictMath.addExact(" + x + " + " + y + "); Unexpected exception: " + ex);245}246}247248try {249// Test subtractExact250resultBig = xBig.subtract(yBig);251long diff = StrictMath.subtractExact(x, y);252checkResult("long StrictMath.subtractExact", x, y, diff, resultBig);253} catch (ArithmeticException ex) {254if (inLongRange(resultBig)) {255fail("FAIL: long StrictMath.subtractExact(" + x + " - " + y + ")" + "; Unexpected exception: " + ex);256}257}258259try {260// Test multiplyExact261resultBig = xBig.multiply(yBig);262long product = StrictMath.multiplyExact(x, y);263checkResult("long StrictMath.multiplyExact", x, y, product, resultBig);264} catch (ArithmeticException ex) {265if (inLongRange(resultBig)) {266fail("FAIL: long StrictMath.multiplyExact(" + x + " * " + y + ")" + "; Unexpected exception: " + ex);267}268}269270try {271// Test incrementExact272resultBig = xBig.add(BigInteger.ONE);273long inc = StrictMath.incrementExact(x);274checkResult("long Math.incrementExact", x, 1L, inc, resultBig);275} catch (ArithmeticException ex) {276if (inLongRange(resultBig)) {277fail("FAIL: long Math.incrementExact(" + x + "); Unexpected exception: " + ex);278}279}280281try {282// Test decrementExact283resultBig = xBig.subtract(BigInteger.ONE);284long dec = StrictMath.decrementExact(x);285checkResult("long Math.decrementExact", x, 1L, dec, resultBig);286} catch (ArithmeticException ex) {287if (inLongRange(resultBig)) {288fail("FAIL: long Math.decrementExact(" + x + "); Unexpected exception: " + ex);289}290}291292try {293// Test negateExact294resultBig = xBig.negate();295long dec = StrictMath.negateExact(x);296checkResult("long Math.negateExact", x, 0L, dec, resultBig);297} catch (ArithmeticException ex) {298if (inLongRange(resultBig)) {299fail("FAIL: long Math.negateExact(" + x + "); Unexpected exception: " + ex);300}301}302303try {304// Test toIntExact305int value = StrictMath.toIntExact(x);306if ((long)value != x) {307fail("FAIL: " + "long StrictMath.toIntExact" + "(" + x + ") = " + value + "; expected an arithmetic exception: ");308}309} catch (ArithmeticException ex) {310if (resultBig.bitLength() <= 32) {311fail("FAIL: long StrictMath.toIntExact(" + x + ")" + "; Unexpected exception: " + ex);312}313}314}315316/**317* Compare the expected and actual results.318* @param message message for the error319* @param x first argument320* @param y second argument321* @param result actual result value322* @param expected expected result value323*/324static void checkResult(String message, long x, long y, long result, BigInteger expected) {325BigInteger resultBig = BigInteger.valueOf(result);326if (!inLongRange(expected)) {327fail("FAIL: " + message + "(" + x + ", " + y + ") = " + result + "; expected an arithmetic exception: ");328} else if (!resultBig.equals(expected)) {329fail("FAIL: " + message + "(" + x + ", " + y + ") = " + result + "; expected " + expected);330}331}332333/**334* Check if the value fits in 64 bits (a long).335* @param value336* @return true if the value fits in 64 bits (including the sign).337*/338static boolean inLongRange(BigInteger value) {339return value.bitLength() <= 63;340}341}342343344