Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/Math/DivModTests.java
38812 views
/*1* Copyright (c) 2012, 2013, 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);93// Special case of integer overflow94testIntFloorDivMod(Integer.MIN_VALUE, -1, Integer.MIN_VALUE, 0);95}9697/**98* Test FloorDiv and then FloorMod with int data.99*/100static void testIntFloorDivMod(int x, int y, Object divExpected, Object modExpected) {101testIntFloorDiv(x, y, divExpected);102testIntFloorMod(x, y, modExpected);103}104105/**106* Test FloorDiv with int data.107*/108static void testIntFloorDiv(int x, int y, Object expected) {109Object result = doFloorDiv(x, y);110if (!resultEquals(result, expected)) {111fail("FAIL: Math.floorDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);112}113114Object strict_result = doStrictFloorDiv(x, y);115if (!resultEquals(strict_result, expected)) {116fail("FAIL: StrictMath.floorDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);117}118}119120/**121* Test FloorMod with int data.122*/123static void testIntFloorMod(int x, int y, Object expected) {124Object result = doFloorMod(x, y);125if (!resultEquals(result, expected)) {126fail("FAIL: Math.floorMod(%d, %d) = %s; expected %s%n", x, y, result, expected);127}128129Object strict_result = doStrictFloorMod(x, y);130if (!resultEquals(strict_result, expected)) {131fail("FAIL: StrictMath.floorMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);132}133134try {135// Verify result against double precision floor function136int tmp = x / y; // Force ArithmeticException for divide by zero137double ff = x - Math.floor((double)x / (double)y) * y;138int fr = (int)ff;139boolean t = (fr == ((Integer)result));140if (!result.equals(fr)) {141fail("FAIL: Math.floorMod(%d, %d) = %s differs from Math.floor(x, y): %d%n", x, y, result, fr);142}143} catch (ArithmeticException ae) {144if (y != 0) {145fail("FAIL: Math.floorMod(%d, %d); unexpected %s%n", x, y, ae);146}147}148}149150/**151* Test the floorDiv and floorMod methods for primitive long.152*/153static void testLongFloorDivMod() {154testLongFloorDivMod(4L, 0L, new ArithmeticException(), new ArithmeticException()); // Should throw ArithmeticException155testLongFloorDivMod(4L, 3L, 1L, 1L);156testLongFloorDivMod(3L, 3L, 1L, 0L);157testLongFloorDivMod(2L, 3L, 0L, 2L);158testLongFloorDivMod(1L, 3L, 0L, 1L);159testLongFloorDivMod(0L, 3L, 0L, 0L);160testLongFloorDivMod(4L, -3L, -2L, -2L);161testLongFloorDivMod(3L, -3L, -1L, 0l);162testLongFloorDivMod(2L, -3L, -1L, -1L);163testLongFloorDivMod(1L, -3L, -1L, -2L);164testLongFloorDivMod(0L, -3L, 0L, 0L);165testLongFloorDivMod(-1L, 3L, -1L, 2L);166testLongFloorDivMod(-2L, 3L, -1L, 1L);167testLongFloorDivMod(-3L, 3L, -1L, 0L);168testLongFloorDivMod(-4L, 3L, -2L, 2L);169testLongFloorDivMod(-1L, -3L, 0L, -1L);170testLongFloorDivMod(-2L, -3L, 0L, -2L);171testLongFloorDivMod(-3L, -3L, 1L, 0L);172testLongFloorDivMod(-4L, -3L, 1L, -1L);173174testLongFloorDivMod(Long.MAX_VALUE, 1, Long.MAX_VALUE, 0L);175testLongFloorDivMod(Long.MAX_VALUE, -1, -Long.MAX_VALUE, 0L);176testLongFloorDivMod(Long.MAX_VALUE, 3L, Long.MAX_VALUE / 3L, 1L);177testLongFloorDivMod(Long.MAX_VALUE - 1L, 3L, (Long.MAX_VALUE - 1L) / 3L, 0L);178testLongFloorDivMod(Long.MIN_VALUE, 3L, Long.MIN_VALUE / 3L - 1L, 1L);179testLongFloorDivMod(Long.MIN_VALUE + 1L, 3L, Long.MIN_VALUE / 3L - 1L, 2L);180testLongFloorDivMod(Long.MIN_VALUE + 1, -1, Long.MAX_VALUE, 0L);181// Special case of integer overflow182testLongFloorDivMod(Long.MIN_VALUE, -1, Long.MIN_VALUE, 0L);183}184185/**186* Test the integer floorDiv and floorMod methods.187* Math and StrictMath are tested and the same results are expected for both.188*/189static void testLongFloorDivMod(long x, long y, Object divExpected, Object modExpected) {190testLongFloorDiv(x, y, divExpected);191testLongFloorMod(x, y, modExpected);192}193194/**195* Test FloorDiv with long arguments against expected value.196* The expected value is usually a Long but in some cases is197* an ArithmeticException.198*199* @param x dividend200* @param y modulus201* @param expected expected value,202*/203static void testLongFloorDiv(long x, long y, Object expected) {204Object result = doFloorDiv(x, y);205if (!resultEquals(result, expected)) {206fail("FAIL: long Math.floorDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);207}208209Object strict_result = doStrictFloorDiv(x, y);210if (!resultEquals(strict_result, expected)) {211fail("FAIL: long StrictMath.floorDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);212}213}214215/**216* Test FloorMod of long arguments against expected value.217* The expected value is usually a Long but in some cases is218* an ArithmeticException.219*220* @param x dividend221* @param y modulus222* @param expected expected value223*/224static void testLongFloorMod(long x, long y, Object expected) {225Object result = doFloorMod(x, y);226if (!resultEquals(result, expected)) {227fail("FAIL: long Math.floorMod(%d, %d) = %s; expected %s%n", x, y, result, expected);228}229230Object strict_result = doStrictFloorMod(x, y);231if (!resultEquals(strict_result, expected)) {232fail("FAIL: long StrictMath.floorMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);233}234235try {236// Verify the result against BigDecimal rounding mode.237BigDecimal xD = new BigDecimal(x);238BigDecimal yD = new BigDecimal(y);239BigDecimal resultD = xD.divide(yD, RoundingMode.FLOOR);240resultD = resultD.multiply(yD);241resultD = xD.subtract(resultD);242long fr = resultD.longValue();243if (!result.equals(fr)) {244fail("FAIL: Long.floorMod(%d, %d) = %d is different than BigDecimal result: %d%n", x, y, result, fr);245246}247} catch (ArithmeticException ae) {248if (y != 0) {249fail("FAIL: long Math.floorMod(%d, %d); unexpected ArithmeticException from bigdecimal");250}251}252}253254/**255* Invoke floorDiv and return the result or any exception.256* @param x the x value257* @param y the y value258* @return the result Integer or an exception.259*/260static Object doFloorDiv(int x, int y) {261try {262return Math.floorDiv(x, y);263} catch (ArithmeticException ae) {264return ae;265}266}267268/**269* Invoke floorDiv and return the result or any exception.270* @param x the x value271* @param y the y value272* @return the result Integer or an exception.273*/274static Object doFloorDiv(long x, long y) {275try {276return Math.floorDiv(x, y);277} catch (ArithmeticException ae) {278return ae;279}280}281282/**283* Invoke floorDiv and return the result or any exception.284* @param x the x value285* @param y the y value286* @return the result Integer or an exception.287*/288static Object doFloorMod(int x, int y) {289try {290return Math.floorMod(x, y);291} catch (ArithmeticException ae) {292return ae;293}294}295296/**297* Invoke floorDiv and return the result or any exception.298* @param x the x value299* @param y the y value300* @return the result Integer or an exception.301*/302static Object doFloorMod(long x, long y) {303try {304return Math.floorMod(x, y);305} catch (ArithmeticException ae) {306return ae;307}308}309310/**311* Invoke floorDiv and return the result or any exception.312* @param x the x value313* @param y the y value314* @return the result Integer or an exception.315*/316static Object doStrictFloorDiv(int x, int y) {317try {318return StrictMath.floorDiv(x, y);319} catch (ArithmeticException ae) {320return ae;321}322}323324/**325* Invoke floorDiv and return the result or any exception.326* @param x the x value327* @param y the y value328* @return the result Integer or an exception.329*/330static Object doStrictFloorDiv(long x, long y) {331try {332return StrictMath.floorDiv(x, y);333} catch (ArithmeticException ae) {334return ae;335}336}337338/**339* Invoke floorDiv and return the result or any exception.340* @param x the x value341* @param y the y value342* @return the result Integer or an exception.343*/344static Object doStrictFloorMod(int x, int y) {345try {346return StrictMath.floorMod(x, y);347} catch (ArithmeticException ae) {348return ae;349}350}351352/**353* Invoke floorDiv and return the result or any exception.354* @param x the x value355* @param y the y value356* @return the result Integer or an exception.357*/358static Object doStrictFloorMod(long x, long y) {359try {360return StrictMath.floorMod(x, y);361} catch (ArithmeticException ae) {362return ae;363}364}365366/**367* Returns a boolean by comparing the result and the expected value.368* The equals method is not defined for ArithmeticException but it is369* desirable to have equals return true if the expected and the result370* both threw the same exception (class and message.)371*372* @param result the result from testing the method373* @param expected the expected value374* @return true if the result is equal to the expected values; false otherwise.375*/376static boolean resultEquals(Object result, Object expected) {377if (result.getClass() != expected.getClass()) {378fail("FAIL: Result type mismatch, %s; expected: %s%n",379result.getClass().getName(), expected.getClass().getName());380return false;381}382383if (result.equals(expected)) {384return true;385}386// Handle special case to compare ArithmeticExceptions387if (result instanceof ArithmeticException && expected instanceof ArithmeticException) {388return true;389}390return false;391}392393}394395396