Path: blob/master/test/jdk/java/lang/Math/DivModTests.java
41149 views
/*1* Copyright (c) 2012, 2016, 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.BigDecimal;24import java.math.RoundingMode;2526/**27* @test Test Math and StrictMath Floor Div / Modulo operations.28* @bug 628219629* @summary Basic tests for Floor division and modulo methods for both Math30* and StrictMath for int and long datatypes.31*/32public class DivModTests {3334/**35* The count of test errors.36*/37private static int errors = 0;3839/**40* @param args the command line arguments are unused41*/42public static void main(String[] args) {43errors = 0;44testIntFloorDivMod();45testLongFloorDivMod();4647if (errors > 0) {48throw new RuntimeException(errors + " errors found in DivMod methods.");49}50}5152/**53* Report a test failure and increment the error count.54* @param message the formatting string55* @param args the variable number of arguments for the message.56*/57static void fail(String message, Object... args) {58errors++;59System.out.printf(message, args);60}6162/**63* Test the integer floorDiv and floorMod methods.64* Math and StrictMath tested and the same results are expected for both.65*/66static void testIntFloorDivMod() {67testIntFloorDivMod(4, 0, new ArithmeticException(), new ArithmeticException()); // Should throw ArithmeticException68testIntFloorDivMod(4, 3, 1, 1);69testIntFloorDivMod(3, 3, 1, 0);70testIntFloorDivMod(2, 3, 0, 2);71testIntFloorDivMod(1, 3, 0, 1);72testIntFloorDivMod(0, 3, 0, 0);73testIntFloorDivMod(4, -3, -2, -2);74testIntFloorDivMod(3, -3, -1, 0);75testIntFloorDivMod(2, -3, -1, -1);76testIntFloorDivMod(1, -3, -1, -2);77testIntFloorDivMod(0, -3, 0, 0);78testIntFloorDivMod(-1, 3, -1, 2);79testIntFloorDivMod(-2, 3, -1, 1);80testIntFloorDivMod(-3, 3, -1, 0);81testIntFloorDivMod(-4, 3, -2, 2);82testIntFloorDivMod(-1, -3, 0, -1);83testIntFloorDivMod(-2, -3, 0, -2);84testIntFloorDivMod(-3, -3, 1, 0);85testIntFloorDivMod(-4, -3, 1, -1);86testIntFloorDivMod(Integer.MAX_VALUE, 1, Integer.MAX_VALUE, 0);87testIntFloorDivMod(Integer.MAX_VALUE, -1, -Integer.MAX_VALUE, 0);88testIntFloorDivMod(Integer.MAX_VALUE, 3, 715827882, 1);89testIntFloorDivMod(Integer.MAX_VALUE - 1, 3, 715827882, 0);90testIntFloorDivMod(Integer.MIN_VALUE, 3, -715827883, 1);91testIntFloorDivMod(Integer.MIN_VALUE + 1, 3, -715827883, 2);92testIntFloorDivMod(Integer.MIN_VALUE + 1, -1, Integer.MAX_VALUE, 0);93testIntFloorDivMod(Integer.MAX_VALUE, Integer.MAX_VALUE, 1, 0);94testIntFloorDivMod(Integer.MAX_VALUE, Integer.MIN_VALUE, -1, -1);95testIntFloorDivMod(Integer.MIN_VALUE, Integer.MIN_VALUE, 1, 0);96testIntFloorDivMod(Integer.MIN_VALUE, Integer.MAX_VALUE, -2, 2147483646);97// Special case of integer overflow98testIntFloorDivMod(Integer.MIN_VALUE, -1, Integer.MIN_VALUE, 0);99}100101/**102* Test FloorDiv and then FloorMod with int data.103*/104static void testIntFloorDivMod(int x, int y, Object divExpected, Object modExpected) {105testIntFloorDiv(x, y, divExpected);106testIntFloorMod(x, y, modExpected);107}108109/**110* Test FloorDiv with int data.111*/112static void testIntFloorDiv(int x, int y, Object expected) {113Object result = doFloorDiv(x, y);114if (!resultEquals(result, expected)) {115fail("FAIL: Math.floorDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);116}117118Object strict_result = doStrictFloorDiv(x, y);119if (!resultEquals(strict_result, expected)) {120fail("FAIL: StrictMath.floorDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);121}122}123124/**125* Test FloorMod with int data.126*/127static void testIntFloorMod(int x, int y, Object expected) {128Object result = doFloorMod(x, y);129if (!resultEquals(result, expected)) {130fail("FAIL: Math.floorMod(%d, %d) = %s; expected %s%n", x, y, result, expected);131}132133Object strict_result = doStrictFloorMod(x, y);134if (!resultEquals(strict_result, expected)) {135fail("FAIL: StrictMath.floorMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);136}137138try {139// Verify result against double precision floor function140int tmp = x / y; // Force ArithmeticException for divide by zero141double ff = x - Math.floor((double)x / (double)y) * y;142int fr = (int)ff;143boolean t = (fr == ((Integer)result));144if (!result.equals(fr)) {145fail("FAIL: Math.floorMod(%d, %d) = %s differs from Math.floor(x, y): %d%n", x, y, result, fr);146}147} catch (ArithmeticException ae) {148if (y != 0) {149fail("FAIL: Math.floorMod(%d, %d); unexpected %s%n", x, y, ae);150}151}152}153154/**155* Test the floorDiv and floorMod methods for primitive long.156*/157static void testLongFloorDivMod() {158testLongFloorDivMod(4L, 0L, new ArithmeticException(), new ArithmeticException()); // Should throw ArithmeticException159testLongFloorDivMod(4L, 3L, 1L, 1L);160testLongFloorDivMod(3L, 3L, 1L, 0L);161testLongFloorDivMod(2L, 3L, 0L, 2L);162testLongFloorDivMod(1L, 3L, 0L, 1L);163testLongFloorDivMod(0L, 3L, 0L, 0L);164testLongFloorDivMod(4L, -3L, -2L, -2L);165testLongFloorDivMod(3L, -3L, -1L, 0l);166testLongFloorDivMod(2L, -3L, -1L, -1L);167testLongFloorDivMod(1L, -3L, -1L, -2L);168testLongFloorDivMod(0L, -3L, 0L, 0L);169testLongFloorDivMod(-1L, 3L, -1L, 2L);170testLongFloorDivMod(-2L, 3L, -1L, 1L);171testLongFloorDivMod(-3L, 3L, -1L, 0L);172testLongFloorDivMod(-4L, 3L, -2L, 2L);173testLongFloorDivMod(-1L, -3L, 0L, -1L);174testLongFloorDivMod(-2L, -3L, 0L, -2L);175testLongFloorDivMod(-3L, -3L, 1L, 0L);176testLongFloorDivMod(-4L, -3L, 1L, -1L);177178testLongFloorDivMod(Long.MAX_VALUE, 1, Long.MAX_VALUE, 0L);179testLongFloorDivMod(Long.MAX_VALUE, -1, -Long.MAX_VALUE, 0L);180testLongFloorDivMod(Long.MAX_VALUE, 3L, Long.MAX_VALUE / 3L, 1L);181testLongFloorDivMod(Long.MAX_VALUE - 1L, 3L, (Long.MAX_VALUE - 1L) / 3L, 0L);182testLongFloorDivMod(Long.MIN_VALUE, 3L, Long.MIN_VALUE / 3L - 1L, 1L);183testLongFloorDivMod(Long.MIN_VALUE + 1L, 3L, Long.MIN_VALUE / 3L - 1L, 2L);184testLongFloorDivMod(Long.MIN_VALUE + 1, -1, Long.MAX_VALUE, 0L);185testLongFloorDivMod(Long.MAX_VALUE, Long.MAX_VALUE, 1L, 0L);186testLongFloorDivMod(Long.MAX_VALUE, Long.MIN_VALUE, -1L, -1L);187testLongFloorDivMod(Long.MIN_VALUE, Long.MIN_VALUE, 1L, 0L);188testLongFloorDivMod(Long.MIN_VALUE, Long.MAX_VALUE, -2L, 9223372036854775806L);189// Special case of integer overflow190testLongFloorDivMod(Long.MIN_VALUE, -1, Long.MIN_VALUE, 0L);191}192193/**194* Test the long floorDiv and floorMod methods.195* Math and StrictMath are tested and the same results are expected for both.196*/197static void testLongFloorDivMod(long x, long y, Object divExpected, Object modExpected) {198testLongFloorDiv(x, y, divExpected);199testLongFloorMod(x, y, modExpected);200}201202/**203* Test FloorDiv with long arguments against expected value.204* The expected value is usually a Long but in some cases is205* an ArithmeticException.206*207* @param x dividend208* @param y modulus209* @param expected expected value,210*/211static void testLongFloorDiv(long x, long y, Object expected) {212Object result = doFloorDiv(x, y);213if (!resultEquals(result, expected)) {214fail("FAIL: long Math.floorDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);215}216217Object strict_result = doStrictFloorDiv(x, y);218if (!resultEquals(strict_result, expected)) {219fail("FAIL: long StrictMath.floorDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);220}221}222223/**224* Test FloorMod of long arguments against expected value.225* The expected value is usually a Long but in some cases is226* an ArithmeticException.227*228* @param x dividend229* @param y modulus230* @param expected expected value231*/232static void testLongFloorMod(long x, long y, Object expected) {233Object result = doFloorMod(x, y);234if (!resultEquals(result, expected)) {235fail("FAIL: long Math.floorMod(%d, %d) = %s; expected %s%n", x, y, result, expected);236}237238Object strict_result = doStrictFloorMod(x, y);239if (!resultEquals(strict_result, expected)) {240fail("FAIL: long StrictMath.floorMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);241}242243try {244// Verify the result against BigDecimal rounding mode.245BigDecimal xD = new BigDecimal(x);246BigDecimal yD = new BigDecimal(y);247BigDecimal resultD = xD.divide(yD, RoundingMode.FLOOR);248resultD = resultD.multiply(yD);249resultD = xD.subtract(resultD);250long fr = resultD.longValue();251if (!result.equals(fr)) {252fail("FAIL: Long.floorMod(%d, %d) = %d is different than BigDecimal result: %d%n", x, y, result, fr);253254}255} catch (ArithmeticException ae) {256if (y != 0) {257fail("FAIL: long Math.floorMod(%d, %d); unexpected ArithmeticException from bigdecimal");258}259}260}261262/**263* Test the floorDiv and floorMod methods for mixed long and int.264*/265static void testLongIntFloorDivMod() {266testLongIntFloorDivMod(4L, 0, new ArithmeticException(), new ArithmeticException()); // Should throw ArithmeticException267testLongIntFloorDivMod(4L, 3, 1L, 1);268testLongIntFloorDivMod(3L, 3, 1L, 0);269testLongIntFloorDivMod(2L, 3, 0L, 2);270testLongIntFloorDivMod(1L, 3, 0L, 1);271testLongIntFloorDivMod(0L, 3, 0L, 0);272testLongIntFloorDivMod(4L, -3, -2L, -2);273testLongIntFloorDivMod(3L, -3, -1L, 0);274testLongIntFloorDivMod(2L, -3, -1L, -1);275testLongIntFloorDivMod(1L, -3, -1L, -2);276testLongIntFloorDivMod(0L, -3, 0L, 0);277testLongIntFloorDivMod(-1L, 3, -1L, 2);278testLongIntFloorDivMod(-2L, 3, -1L, 1);279testLongIntFloorDivMod(-3L, 3, -1L, 0);280testLongIntFloorDivMod(-4L, 3, -2L, 2);281testLongIntFloorDivMod(-1L, -3, 0L, -1);282testLongIntFloorDivMod(-2L, -3, 0L, -2);283testLongIntFloorDivMod(-3L, -3, 1L, 0);284testLongIntFloorDivMod(-4L, -3, 1L, -1);285286testLongIntFloorDivMod(Long.MAX_VALUE, 1, Long.MAX_VALUE, 0L);287testLongIntFloorDivMod(Long.MAX_VALUE, -1, -Long.MAX_VALUE, 0L);288testLongIntFloorDivMod(Long.MAX_VALUE, 3, Long.MAX_VALUE / 3L, 1L);289testLongIntFloorDivMod(Long.MAX_VALUE - 1L, 3, (Long.MAX_VALUE - 1L) / 3L, 0L);290testLongIntFloorDivMod(Long.MIN_VALUE, 3, Long.MIN_VALUE / 3L - 1L, 1L);291testLongIntFloorDivMod(Long.MIN_VALUE + 1L, 3, Long.MIN_VALUE / 3L - 1L, 2L);292testLongIntFloorDivMod(Long.MIN_VALUE + 1, -1, Long.MAX_VALUE, 0L);293testLongIntFloorDivMod(Long.MAX_VALUE, Integer.MAX_VALUE, 4294967298L, 1);294testLongIntFloorDivMod(Long.MAX_VALUE, Integer.MIN_VALUE, -4294967296L, -1);295testLongIntFloorDivMod(Long.MIN_VALUE, Integer.MIN_VALUE, 4294967296L, 0);296testLongIntFloorDivMod(Long.MIN_VALUE, Integer.MAX_VALUE, -4294967299L, 2147483645);297// Special case of integer overflow298testLongIntFloorDivMod(Long.MIN_VALUE, -1, Long.MIN_VALUE, 0L);299}300301/**302* Test the integer floorDiv and floorMod methods.303* Math and StrictMath are tested and the same results are expected for both.304*/305static void testLongIntFloorDivMod(long x, int y, Object divExpected, Object modExpected) {306testLongIntFloorDiv(x, y, divExpected);307testLongIntFloorMod(x, y, modExpected);308}309310/**311* Test FloorDiv with long arguments against expected value.312* The expected value is usually a Long but in some cases is313* an ArithmeticException.314*315* @param x dividend316* @param y modulus317* @param expected expected value,318*/319static void testLongIntFloorDiv(long x, int y, Object expected) {320Object result = doFloorDiv(x, y);321if (!resultEquals(result, expected)) {322fail("FAIL: long Math.floorDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);323}324325Object strict_result = doStrictFloorDiv(x, y);326if (!resultEquals(strict_result, expected)) {327fail("FAIL: long StrictMath.floorDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);328}329}330331/**332* Test FloorMod of long arguments against expected value.333* The expected value is usually a Long but in some cases is334* an ArithmeticException.335*336* @param x dividend337* @param y modulus338* @param expected expected value339*/340static void testLongIntFloorMod(long x, int y, Object expected) {341Object result = doFloorMod(x, y);342if (!resultEquals(result, expected)) {343fail("FAIL: long Math.floorMod(%d, %d) = %s; expected %s%n", x, y, result, expected);344}345346Object strict_result = doStrictFloorMod(x, y);347if (!resultEquals(strict_result, expected)) {348fail("FAIL: long StrictMath.floorMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);349}350351try {352// Verify the result against BigDecimal rounding mode.353BigDecimal xD = new BigDecimal(x);354BigDecimal yD = new BigDecimal(y);355BigDecimal resultD = xD.divide(yD, RoundingMode.FLOOR);356resultD = resultD.multiply(yD);357resultD = xD.subtract(resultD);358long fr = resultD.longValue();359if (!result.equals(fr)) {360fail("FAIL: Long.floorMod(%d, %d) = %d is different than BigDecimal result: %d%n", x, y, result, fr);361362}363} catch (ArithmeticException ae) {364if (y != 0) {365fail("FAIL: long Math.floorMod(%d, %d); unexpected ArithmeticException from bigdecimal");366}367}368}369370/**371* Invoke floorDiv and return the result or any exception.372* @param x the x value373* @param y the y value374* @return the result Integer or an exception.375*/376static Object doFloorDiv(int x, int y) {377try {378return Math.floorDiv(x, y);379} catch (ArithmeticException ae) {380return ae;381}382}383384/**385* Invoke floorDiv and return the result or any exception.386* @param x the x value387* @param y the y value388* @return the result Integer or an exception.389*/390static Object doFloorDiv(long x, int y) {391try {392return Math.floorDiv(x, y);393} catch (ArithmeticException ae) {394return ae;395}396}397398/**399* Invoke floorDiv and return the result or any exception.400* @param x the x value401* @param y the y value402* @return the result Integer or an exception.403*/404static Object doFloorDiv(long x, long y) {405try {406return Math.floorDiv(x, y);407} catch (ArithmeticException ae) {408return ae;409}410}411412/**413* Invoke floorDiv and return the result or any exception.414* @param x the x value415* @param y the y value416* @return the result Integer or an exception.417*/418static Object doFloorMod(int x, int y) {419try {420return Math.floorMod(x, y);421} catch (ArithmeticException ae) {422return ae;423}424}425426/**427* Invoke floorDiv and return the result or any exception.428* @param x the x value429* @param y the y value430* @return the result Integer or an exception.431*/432static Object doFloorMod(long x, int y) {433try {434return Math.floorMod(x, y);435} catch (ArithmeticException ae) {436return ae;437}438}439440/**441* Invoke floorDiv and return the result or any exception.442* @param x the x value443* @param y the y value444* @return the result Integer or an exception.445*/446static Object doFloorMod(long x, long y) {447try {448return Math.floorMod(x, y);449} catch (ArithmeticException ae) {450return ae;451}452}453454/**455* Invoke floorDiv and return the result or any exception.456* @param x the x value457* @param y the y value458* @return the result Integer or an exception.459*/460static Object doStrictFloorDiv(int x, int y) {461try {462return StrictMath.floorDiv(x, y);463} catch (ArithmeticException ae) {464return ae;465}466}467468/**469* Invoke floorDiv and return the result or any exception.470* @param x the x value471* @param y the y value472* @return the result Integer or an exception.473*/474static Object doStrictFloorDiv(long x, int y) {475try {476return StrictMath.floorDiv(x, y);477} catch (ArithmeticException ae) {478return ae;479}480}481482/**483* Invoke floorDiv and return the result or any exception.484* @param x the x value485* @param y the y value486* @return the result Integer or an exception.487*/488static Object doStrictFloorDiv(long x, long y) {489try {490return StrictMath.floorDiv(x, y);491} catch (ArithmeticException ae) {492return ae;493}494}495496/**497* Invoke floorDiv and return the result or any exception.498* @param x the x value499* @param y the y value500* @return the result Integer or an exception.501*/502static Object doStrictFloorMod(int x, int y) {503try {504return StrictMath.floorMod(x, y);505} catch (ArithmeticException ae) {506return ae;507}508}509510/**511* Invoke floorDiv and return the result or any exception.512* @param x the x value513* @param y the y value514* @return the result Integer or an exception.515*/516static Object doStrictFloorMod(long x, int y) {517try {518return StrictMath.floorMod(x, y);519} catch (ArithmeticException ae) {520return ae;521}522}523524/**525* Invoke floorDiv and return the result or any exception.526* @param x the x value527* @param y the y value528* @return the result Integer or an exception.529*/530static Object doStrictFloorMod(long x, long y) {531try {532return StrictMath.floorMod(x, y);533} catch (ArithmeticException ae) {534return ae;535}536}537538/**539* Returns a boolean by comparing the result and the expected value.540* The equals method is not defined for ArithmeticException but it is541* desirable to have equals return true if the expected and the result542* both threw the same exception (class and message.)543*544* @param result the result from testing the method545* @param expected the expected value546* @return true if the result is equal to the expected values; false otherwise.547*/548static boolean resultEquals(Object result, Object expected) {549if (result.getClass() != expected.getClass()) {550fail("FAIL: Result type mismatch, %s; expected: %s%n",551result.getClass().getName(), expected.getClass().getName());552return false;553}554555if (result.equals(expected)) {556return true;557}558// Handle special case to compare ArithmeticExceptions559if (result instanceof ArithmeticException && expected instanceof ArithmeticException) {560return true;561}562return false;563}564565}566567568