Path: blob/master/test/jdk/java/lang/Math/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 Math.*Exact integer and long methods.27* @bug 670839828* @summary Basic tests for Math 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();45testLongIntExact();4647if (errors > 0) {48throw new RuntimeException(errors + " errors found in ExactArithTests.");49}50}5152static void fail(String message) {53errors++;54System.err.println(message);55}5657/**58* Test Math.addExact, multiplyExact, subtractExact, incrementExact,59* decrementExact, negateExact methods with {@code int} arguments.60*/61static void testIntegerExact() {62testIntegerExact(0, 0);63testIntegerExact(1, 1);64testIntegerExact(1, -1);65testIntegerExact(-1, 1);66testIntegerExact(1000, 2000);6768testIntegerExact(Integer.MIN_VALUE, Integer.MIN_VALUE);69testIntegerExact(Integer.MAX_VALUE, Integer.MAX_VALUE);70testIntegerExact(Integer.MIN_VALUE, 1);71testIntegerExact(Integer.MAX_VALUE, 1);72testIntegerExact(Integer.MIN_VALUE, 2);73testIntegerExact(Integer.MAX_VALUE, 2);74testIntegerExact(Integer.MIN_VALUE, -1);75testIntegerExact(Integer.MAX_VALUE, -1);76testIntegerExact(Integer.MIN_VALUE, -2);77testIntegerExact(Integer.MAX_VALUE, -2);78}7980/**81* Test exact arithmetic by comparing with the same operations using long82* and checking that the result is the same as the integer truncation.83* Errors are reported with {@link fail}.84*85* @param x first parameter86* @param y second parameter87*/88static void testIntegerExact(int x, int y) {89try {90// Test addExact91int sum = Math.addExact(x, y);92long sum2 = (long) x + (long) y;93if ((int) sum2 != sum2) {94fail("FAIL: int Math.addExact(" + x + " + " + y + ") = " + sum + "; expected Arithmetic exception");95} else if (sum != sum2) {96fail("FAIL: long Math.addExact(" + x + " + " + y + ") = " + sum + "; expected: " + sum2);97}98} catch (ArithmeticException ex) {99long sum2 = (long) x + (long) y;100if ((int) sum2 == sum2) {101fail("FAIL: int Math.addExact(" + x + " + " + y + ")" + "; Unexpected exception: " + ex);102}103}104105try {106// Test subtractExact107int diff = Math.subtractExact(x, y);108long diff2 = (long) x - (long) y;109if ((int) diff2 != diff2) {110fail("FAIL: int Math.subtractExact(" + x + " - " + y + ") = " + diff + "; expected: " + diff2);111}112113} catch (ArithmeticException ex) {114long diff2 = (long) x - (long) y;115if ((int) diff2 == diff2) {116fail("FAIL: int Math.subtractExact(" + x + " - " + y + ")" + "; Unexpected exception: " + ex);117}118}119120try {121// Test multiplyExact122int product = Math.multiplyExact(x, y);123long m2 = (long) x * (long) y;124if ((int) m2 != m2) {125fail("FAIL: int Math.multiplyExact(" + x + " * " + y + ") = " + product + "; expected: " + m2);126}127} catch (ArithmeticException ex) {128long m2 = (long) x * (long) y;129if ((int) m2 == m2) {130fail("FAIL: int Math.multiplyExact(" + x + " * " + y + ")" + "; Unexpected exception: " + ex);131}132}133134try {135// Test incrementExact136int inc = Math.incrementExact(x);137long inc2 = (long) x + 1L;138if ((int) inc2 != inc2) {139fail("FAIL: int Math.incrementExact(" + x + ") = " + inc + "; expected Arithmetic exception");140} else if (inc != inc2) {141fail("FAIL: long Math.incrementExact(" + x + ") = " + inc + "; expected: " + inc2);142}143} catch (ArithmeticException ex) {144long inc2 = (long) x + 1L;145if ((int) inc2 == inc2) {146fail("FAIL: int Math.incrementExact(" + x + ")" + "; Unexpected exception: " + ex);147}148}149150try {151// Test decrementExact152int dec = Math.decrementExact(x);153long dec2 = (long) x - 1L;154if ((int) dec2 != dec2) {155fail("FAIL: int Math.decrementExact(" + x + ") = " + dec + "; expected Arithmetic exception");156} else if (dec != dec2) {157fail("FAIL: long Math.decrementExact(" + x + ") = " + dec + "; expected: " + dec2);158}159} catch (ArithmeticException ex) {160long dec2 = (long) x - 1L;161if ((int) dec2 == dec2) {162fail("FAIL: int Math.decrementExact(" + x + ")" + "; Unexpected exception: " + ex);163}164}165166try {167// Test negateExact168int neg = Math.negateExact(x);169long neg2 = -((long)x);170if ((int) neg2 != neg2) {171fail("FAIL: int Math.negateExact(" + x + ") = " + neg + "; expected Arithmetic exception");172} else if (neg != neg2) {173fail("FAIL: long Math.negateExact(" + x + ") = " + neg + "; expected: " + neg2);174}175} catch (ArithmeticException ex) {176long neg2 = -((long)x);177if ((int) neg2 == neg2) {178fail("FAIL: int Math.negateExact(" + x + ")" + "; Unexpected exception: " + ex);179}180}181}182183/**184* Test Math.addExact, multiplyExact, subtractExact, incrementExact,185* decrementExact, negateExact, toIntExact methods with {@code long} arguments.186*/187static void testLongExact() {188testLongExactTwice(0, 0);189testLongExactTwice(1, 1);190testLongExactTwice(1, -1);191testLongExactTwice(1000, 2000);192193testLongExactTwice(Long.MIN_VALUE, Long.MIN_VALUE);194testLongExactTwice(Long.MAX_VALUE, Long.MAX_VALUE);195testLongExactTwice(Long.MIN_VALUE, 1);196testLongExactTwice(Long.MAX_VALUE, 1);197testLongExactTwice(Long.MIN_VALUE, 2);198testLongExactTwice(Long.MAX_VALUE, 2);199testLongExactTwice(Long.MIN_VALUE, -1);200testLongExactTwice(Long.MAX_VALUE, -1);201testLongExactTwice(Long.MIN_VALUE, -2);202testLongExactTwice(Long.MAX_VALUE, -2);203testLongExactTwice(Long.MIN_VALUE/2, 2);204testLongExactTwice(Long.MAX_VALUE, 2);205testLongExactTwice(Integer.MAX_VALUE, Integer.MAX_VALUE);206testLongExactTwice(Integer.MAX_VALUE, -Integer.MAX_VALUE);207testLongExactTwice(Integer.MAX_VALUE+1, Integer.MAX_VALUE+1);208testLongExactTwice(Integer.MAX_VALUE+1, -Integer.MAX_VALUE+1);209testLongExactTwice(Integer.MIN_VALUE-1, Integer.MIN_VALUE-1);210testLongExactTwice(Integer.MIN_VALUE-1, -Integer.MIN_VALUE-1);211testLongExactTwice(Integer.MIN_VALUE/2, 2);212}213214/**215* Test each of the exact operations with the arguments and216* with the arguments reversed.217* @param x218* @param y219*/220static void testLongExactTwice(long x, long y) {221testLongExact(x, y);222testLongExact(y, x);223}224225226/**227* Test long exact arithmetic by comparing with the same operations using BigInteger228* and checking that the result is the same as the long truncation.229* Errors are reported with {@link fail}.230*231* @param x first parameter232* @param y second parameter233*/234static void testLongExact(long x, long y) {235BigInteger resultBig = null;236final BigInteger xBig = BigInteger.valueOf(x);237final BigInteger yBig = BigInteger.valueOf(y);238try {239// Test addExact240resultBig = xBig.add(yBig);241long sum = Math.addExact(x, y);242checkResult("long Math.addExact", x, y, sum, resultBig);243} catch (ArithmeticException ex) {244if (inLongRange(resultBig)) {245fail("FAIL: long Math.addExact(" + x + " + " + y + "); Unexpected exception: " + ex);246}247}248249try {250// Test subtractExact251resultBig = xBig.subtract(yBig);252long diff = Math.subtractExact(x, y);253checkResult("long Math.subtractExact", x, y, diff, resultBig);254} catch (ArithmeticException ex) {255if (inLongRange(resultBig)) {256fail("FAIL: long Math.subtractExact(" + x + " - " + y + ")" + "; Unexpected exception: " + ex);257}258}259260try {261// Test multiplyExact262resultBig = xBig.multiply(yBig);263long product = Math.multiplyExact(x, y);264checkResult("long Math.multiplyExact", x, y, product, resultBig);265} catch (ArithmeticException ex) {266if (inLongRange(resultBig)) {267fail("FAIL: long Math.multiplyExact(" + x + " * " + y + ")" + "; Unexpected exception: " + ex);268}269}270271try {272// Test incrementExact273resultBig = xBig.add(BigInteger.ONE);274long inc = Math.incrementExact(x);275checkResult("long Math.incrementExact", x, 1L, inc, resultBig);276} catch (ArithmeticException ex) {277if (inLongRange(resultBig)) {278fail("FAIL: long Math.incrementExact(" + x + "); Unexpected exception: " + ex);279}280}281282try {283// Test decrementExact284resultBig = xBig.subtract(BigInteger.ONE);285long dec = Math.decrementExact(x);286checkResult("long Math.decrementExact", x, 1L, dec, resultBig);287} catch (ArithmeticException ex) {288if (inLongRange(resultBig)) {289fail("FAIL: long Math.decrementExact(" + x + "); Unexpected exception: " + ex);290}291}292293try {294// Test negateExact295resultBig = xBig.negate();296long dec = Math.negateExact(x);297checkResult("long Math.negateExact", x, 0L, dec, resultBig);298} catch (ArithmeticException ex) {299if (inLongRange(resultBig)) {300fail("FAIL: long Math.negateExact(" + x + "); Unexpected exception: " + ex);301}302}303304try {305// Test toIntExact306int value = Math.toIntExact(x);307if ((long)value != x) {308fail("FAIL: " + "long Math.toIntExact" + "(" + x + ") = " + value + "; expected an arithmetic exception: ");309}310} catch (ArithmeticException ex) {311if (resultBig.bitLength() <= 32) {312fail("FAIL: long Math.toIntExact(" + x + ")" + "; Unexpected exception: " + ex);313}314}315}316317/**318* Compare the expected and actual results.319* @param message message for the error320* @param x first argument321* @param y second argument322* @param result actual result value323* @param expected expected result value324*/325static void checkResult(String message, long x, long y, long result, BigInteger expected) {326BigInteger resultBig = BigInteger.valueOf(result);327if (!inLongRange(expected)) {328fail("FAIL: " + message + "(" + x + ", " + y + ") = " + result + "; expected an arithmetic exception: ");329} else if (!resultBig.equals(expected)) {330fail("FAIL: " + message + "(" + x + ", " + y + ") = " + result + "; expected " + expected);331}332}333334/**335* Check if the value fits in 64 bits (a long).336* @param value337* @return true if the value fits in 64 bits (including the sign).338*/339static boolean inLongRange(BigInteger value) {340return value.bitLength() <= 63;341}342343/**344* Test Math.multiplyExact method with {@code long} and {@code int}345* arguments.346*/347static void testLongIntExact() {348testLongIntExact(0, 0);349testLongIntExact(1, 1);350testLongIntExact(1, -1);351testLongIntExact(1000, 2000);352353testLongIntExact(Long.MIN_VALUE, Integer.MIN_VALUE);354testLongIntExact(Long.MAX_VALUE, Integer.MAX_VALUE);355testLongIntExact(Long.MIN_VALUE, 1);356testLongIntExact(Long.MAX_VALUE, 1);357testLongIntExact(Long.MIN_VALUE, 2);358testLongIntExact(Long.MAX_VALUE, 2);359testLongIntExact(Long.MIN_VALUE, -1);360testLongIntExact(Long.MAX_VALUE, -1);361testLongIntExact(Long.MIN_VALUE, -2);362testLongIntExact(Long.MAX_VALUE, -2);363testLongIntExact(Long.MIN_VALUE/2, 2);364testLongIntExact(Long.MAX_VALUE, 2);365testLongIntExact(Integer.MAX_VALUE, Integer.MAX_VALUE);366testLongIntExact(Integer.MAX_VALUE, -Integer.MAX_VALUE);367testLongIntExact((long)Integer.MAX_VALUE+1L, Integer.MAX_VALUE);368testLongIntExact((long)Integer.MAX_VALUE+1L, -Integer.MAX_VALUE+1);369testLongIntExact((long)Integer.MIN_VALUE-1L, Integer.MIN_VALUE);370testLongIntExact((long)Integer.MIN_VALUE-1, Integer.MAX_VALUE);371testLongIntExact(Integer.MIN_VALUE/2, 2);372}373374/**375* Test long-int exact arithmetic by comparing with the same operations using BigInteger376* and checking that the result is the same as the long truncation.377* Errors are reported with {@link fail}.378*379* @param x first parameter380* @param y second parameter381*/382static void testLongIntExact(long x, int y) {383BigInteger resultBig = null;384final BigInteger xBig = BigInteger.valueOf(x);385final BigInteger yBig = BigInteger.valueOf(y);386387try {388// Test multiplyExact389resultBig = xBig.multiply(yBig);390long product = Math.multiplyExact(x, y);391checkResult("long Math.multiplyExact", x, y, product, resultBig);392} catch (ArithmeticException ex) {393if (inLongRange(resultBig)) {394fail("FAIL: long Math.multiplyExact(" + x + " * " + y + ")" + "; Unexpected exception: " + ex);395}396}397}398}399400401