Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/StrictMath/ExactArithTests.java
47192 views
/*1* Copyright (c) 2012, 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, toIntValue methods58* 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);7778}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 = StrictMath.addExact(x, y);92long sum2 = (long) x + (long) y;93if ((int) sum2 != sum2) {94fail("FAIL: int StrictMath.addExact(" + x + " + " + y + ") = " + sum + "; expected Arithmetic exception");95} else if (sum != sum2) {96fail("FAIL: long StrictMath.addExact(" + x + " + " + y + ") = " + sum + "; expected: " + sum2);97}98} catch (ArithmeticException ex) {99long sum2 = (long) x + (long) y;100if ((int) sum2 == sum2) {101fail("FAIL: int StrictMath.addExact(" + x + " + " + y + ")" + "; Unexpected exception: " + ex);102103}104}105106try {107// Test subtractExact108int diff = StrictMath.subtractExact(x, y);109long diff2 = (long) x - (long) y;110if ((int) diff2 != diff2) {111fail("FAIL: int StrictMath.subtractExact(" + x + " - " + y + ") = " + diff + "; expected: " + diff2);112}113114} catch (ArithmeticException ex) {115long diff2 = (long) x - (long) y;116if ((int) diff2 == diff2) {117fail("FAIL: int StrictMath.subtractExact(" + x + " - " + y + ")" + "; Unexpected exception: " + ex);118}119}120121try {122// Test multiplyExact123int product = StrictMath.multiplyExact(x, y);124long m2 = (long) x * (long) y;125if ((int) m2 != m2) {126fail("FAIL: int StrictMath.multiplyExact(" + x + " * " + y + ") = " + product + "; expected: " + m2);127}128} catch (ArithmeticException ex) {129long m2 = (long) x * (long) y;130if ((int) m2 == m2) {131fail("FAIL: int StrictMath.multiplyExact(" + x + " * " + y + ")" + "; Unexpected exception: " + ex);132}133}134135}136137/**138* Test StrictMath.addExact, multiplyExact, subtractExact, toIntExact methods139* with {@code long} arguments.140*/141static void testLongExact() {142testLongExactTwice(0, 0);143testLongExactTwice(1, 1);144testLongExactTwice(1, -1);145testLongExactTwice(1000, 2000);146147testLongExactTwice(Long.MIN_VALUE, Long.MIN_VALUE);148testLongExactTwice(Long.MAX_VALUE, Long.MAX_VALUE);149testLongExactTwice(Long.MIN_VALUE, 1);150testLongExactTwice(Long.MAX_VALUE, 1);151testLongExactTwice(Long.MIN_VALUE, 2);152testLongExactTwice(Long.MAX_VALUE, 2);153testLongExactTwice(Long.MIN_VALUE, -1);154testLongExactTwice(Long.MAX_VALUE, -1);155testLongExactTwice(Long.MIN_VALUE, -2);156testLongExactTwice(Long.MAX_VALUE, -2);157testLongExactTwice(Long.MIN_VALUE/2, 2);158testLongExactTwice(Long.MAX_VALUE, 2);159testLongExactTwice(Integer.MAX_VALUE, Integer.MAX_VALUE);160testLongExactTwice(Integer.MAX_VALUE, -Integer.MAX_VALUE);161testLongExactTwice(Integer.MAX_VALUE+1, Integer.MAX_VALUE+1);162testLongExactTwice(Integer.MAX_VALUE+1, -Integer.MAX_VALUE+1);163testLongExactTwice(Integer.MIN_VALUE-1, Integer.MIN_VALUE-1);164testLongExactTwice(Integer.MIN_VALUE-1, -Integer.MIN_VALUE-1);165testLongExactTwice(Integer.MIN_VALUE/2, 2);166167}168169/**170* Test each of the exact operations with the arguments and171* with the arguments reversed.172* @param x173* @param y174*/175static void testLongExactTwice(long x, long y) {176testLongExact(x, y);177testLongExact(y, x);178}179180181/**182* Test long exact arithmetic by comparing with the same operations using BigInteger183* and checking that the result is the same as the long truncation.184* Errors are reported with {@link fail}.185*186* @param x first parameter187* @param y second parameter188*/189static void testLongExact(long x, long y) {190BigInteger resultBig = null;191final BigInteger xBig = BigInteger.valueOf(x);192final BigInteger yBig = BigInteger.valueOf(y);193try {194// Test addExact195resultBig = xBig.add(yBig);196long sum = StrictMath.addExact(x, y);197checkResult("long StrictMath.addExact", x, y, sum, resultBig);198} catch (ArithmeticException ex) {199if (inLongRange(resultBig)) {200fail("FAIL: long StrictMath.addExact(" + x + " + " + y + "); Unexpected exception: " + ex);201}202}203204try {205// Test subtractExact206resultBig = xBig.subtract(yBig);207long diff = StrictMath.subtractExact(x, y);208checkResult("long StrictMath.subtractExact", x, y, diff, resultBig);209} catch (ArithmeticException ex) {210if (inLongRange(resultBig)) {211fail("FAIL: long StrictMath.subtractExact(" + x + " - " + y + ")" + "; Unexpected exception: " + ex);212}213}214215try {216// Test multiplyExact217resultBig = xBig.multiply(yBig);218long product = StrictMath.multiplyExact(x, y);219checkResult("long StrictMath.multiplyExact", x, y, product, resultBig);220} catch (ArithmeticException ex) {221if (inLongRange(resultBig)) {222fail("FAIL: long StrictMath.multiplyExact(" + x + " * " + y + ")" + "; Unexpected exception: " + ex);223}224}225226try {227// Test toIntExact228int value = StrictMath.toIntExact(x);229if ((long)value != x) {230fail("FAIL: " + "long StrictMath.toIntExact" + "(" + x + ") = " + value + "; expected an arithmetic exception: ");231}232} catch (ArithmeticException ex) {233if (resultBig.bitLength() <= 32) {234fail("FAIL: long StrictMath.toIntExact(" + x + ")" + "; Unexpected exception: " + ex);235}236}237238}239240/**241* Compare the expected and actual results.242* @param message message for the error243* @param x first argument244* @param y second argument245* @param result actual result value246* @param expected expected result value247*/248static void checkResult(String message, long x, long y, long result, BigInteger expected) {249BigInteger resultBig = BigInteger.valueOf(result);250if (!inLongRange(expected)) {251fail("FAIL: " + message + "(" + x + ", " + y + ") = " + result + "; expected an arithmetic exception: ");252} else if (!resultBig.equals(expected)) {253fail("FAIL: " + message + "(" + x + ", " + y + ") = " + result + "; expected " + expected);254}255}256257/**258* Check if the value fits in 64 bits (a long).259* @param value260* @return true if the value fits in 64 bits (including the sign).261*/262static boolean inLongRange(BigInteger value) {263return value.bitLength() <= 63;264}265}266267268