/* * Copyright (c) 2012, 2021, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * @test Test Math and StrictMath Floor and Ceil Div / Modulo operations. * @bug 6282196 8271602 * @summary Basic tests for Floor and Ceil division and modulo methods for both Math * and StrictMath for int and long datatypes.
*/ publicclass DivModTests {
/** * The count of test errors.
*/ privatestaticint errors = 0;
/** * @param args the command line arguments are unused
*/ publicstaticvoid main(String[] args) {
errors = 0;
testIntFloorDivMod();
testLongIntFloorDivMod();
testLongFloorDivMod();
testIntCeilDivMod();
testLongIntCeilDivMod();
testLongCeilDivMod();
if (errors > 0) { thrownew RuntimeException(errors + " errors found in DivMod methods.");
}
}
/** * Report a test failure and increment the error count. * @param message the formatting string * @param args the variable number of arguments for the message.
*/ staticvoid fail(String message, Object... args) {
errors++;
System.out.printf(message, args);
}
/** * Test FloorDiv and then FloorMod with int data.
*/ staticvoid testIntFloorDivMod(int x, int y, Object divExpected, Object modExpected) {
testIntFloorDiv(x, y, divExpected);
testIntFloorMod(x, y, modExpected);
}
/** * Test FloorDiv with int data.
*/ staticvoid testIntFloorDiv(int x, int y, Object expected) {
Object result = doFloorDiv(x, y); if (!resultEquals(result, expected)) {
fail("FAIL: Math.floorDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);
}
/** * Test FloorMod with int data.
*/ staticvoid testIntFloorMod(int x, int y, Object expected) {
Object result = doFloorMod(x, y); if (!resultEquals(result, expected)) {
fail("FAIL: Math.floorMod(%d, %d) = %s; expected %s%n", x, y, result, expected);
}
try { // Verify result against double precision floor function int tmp = x / y; // Force ArithmeticException for divide by zero double ff = x - Math.floor((double)x / (double)y) * y; int fr = (int)ff; boolean t = (fr == ((Integer)result)); if (!result.equals(fr)) {
fail("FAIL: Math.floorMod(%d, %d) = %s differs from Math.floor(x, y): %d%n", x, y, result, fr);
}
} catch (ArithmeticException ae) { if (y != 0) {
fail("FAIL: Math.floorMod(%d, %d); unexpected %s%n", x, y, ae);
}
}
}
/** * Test the long floorDiv and floorMod methods. * Math and StrictMath are tested and the same results are expected for both.
*/ staticvoid testLongFloorDivMod(long x, long y, Object divExpected, Object modExpected) {
testLongFloorDiv(x, y, divExpected);
testLongFloorMod(x, y, modExpected);
}
/** * Test FloorDiv with long arguments against expected value. * The expected value is usually a Long but in some cases is * an ArithmeticException. * * @param x dividend * @param y modulus * @param expected expected value,
*/ staticvoid testLongFloorDiv(long x, long y, Object expected) {
Object result = doFloorDiv(x, y); if (!resultEquals(result, expected)) {
fail("FAIL: long Math.floorDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);
}
Object strict_result = doStrictFloorDiv(x, y); if (!resultEquals(strict_result, expected)) {
fail("FAIL: long StrictMath.floorDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
}
}
/** * Test FloorMod of long arguments against expected value. * The expected value is usually a Long but in some cases is * an ArithmeticException. * * @param x dividend * @param y modulus * @param expected expected value
*/ staticvoid testLongFloorMod(long x, long y, Object expected) {
Object result = doFloorMod(x, y); if (!resultEquals(result, expected)) {
fail("FAIL: long Math.floorMod(%d, %d) = %s; expected %s%n", x, y, result, expected);
}
Object strict_result = doStrictFloorMod(x, y); if (!resultEquals(strict_result, expected)) {
fail("FAIL: long StrictMath.floorMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
}
try { // Verify the result against BigDecimal rounding mode.
BigDecimal xD = new BigDecimal(x);
BigDecimal yD = new BigDecimal(y);
BigDecimal resultD = xD.divide(yD, RoundingMode.FLOOR);
resultD = resultD.multiply(yD);
resultD = xD.subtract(resultD); long fr = resultD.longValue(); if (!result.equals(fr)) {
fail("FAIL: Long.floorMod(%d, %d) = %d is different than BigDecimal result: %d%n", x, y, result, fr);
}
} catch (ArithmeticException ae) { if (y != 0) {
fail("FAIL: long Math.floorMod(%d, %d); unexpected ArithmeticException from bigdecimal");
}
}
}
/** * Test the integer floorDiv and floorMod methods. * Math and StrictMath are tested and the same results are expected for both.
*/ staticvoid testLongIntFloorDivMod(long x, int y, Object divExpected, Object modExpected) {
testLongIntFloorDiv(x, y, divExpected);
testLongIntFloorMod(x, y, modExpected);
}
/** * Test FloorDiv with long arguments against expected value. * The expected value is usually a Long but in some cases is * an ArithmeticException. * * @param x dividend * @param y modulus * @param expected expected value,
*/ staticvoid testLongIntFloorDiv(long x, int y, Object expected) {
Object result = doFloorDiv(x, y); if (!resultEquals(result, expected)) {
fail("FAIL: long Math.floorDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);
}
Object strict_result = doStrictFloorDiv(x, y); if (!resultEquals(strict_result, expected)) {
fail("FAIL: long StrictMath.floorDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
}
}
/** * Test FloorMod of long arguments against expected value. * The expected value is usually a Long but in some cases is * an ArithmeticException. * * @param x dividend * @param y modulus * @param expected expected value
*/ staticvoid testLongIntFloorMod(long x, int y, Object expected) {
Object result = doFloorMod(x, y); if (!resultEquals(result, expected)) {
fail("FAIL: int Math.floorMod(%d, %d) = %s; expected %s%n", x, y, result, expected);
}
Object strict_result = doStrictFloorMod(x, y); if (!resultEquals(strict_result, expected)) {
fail("FAIL: int StrictMath.floorMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
}
try { // Verify the result against BigDecimal rounding mode.
BigDecimal xD = new BigDecimal(x);
BigDecimal yD = new BigDecimal(y);
BigDecimal resultD = xD.divide(yD, RoundingMode.FLOOR);
resultD = resultD.multiply(yD);
resultD = xD.subtract(resultD); int fr = resultD.intValue(); if (!result.equals(fr)) {
fail("FAIL: Long.floorMod(%d, %d) = %d is different than BigDecimal result: %d%n", x, y, result, fr);
}
} catch (ArithmeticException ae) { if (y != 0) {
fail("FAIL: long Math.floorMod(%d, %d); unexpected ArithmeticException from bigdecimal");
}
}
}
/** * Invoke floorDiv and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doFloorDiv(int x, int y) { try { return Math.floorDiv(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke floorDiv and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doFloorDiv(long x, int y) { try { return Math.floorDiv(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke floorDiv and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doFloorDiv(long x, long y) { try { return Math.floorDiv(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke floorMod and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doFloorMod(int x, int y) { try { return Math.floorMod(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke floorMod and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doFloorMod(long x, int y) { try { return Math.floorMod(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke floorMod and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doFloorMod(long x, long y) { try { return Math.floorMod(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke floorDiv and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doStrictFloorDiv(int x, int y) { try { return StrictMath.floorDiv(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke floorDiv and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doStrictFloorDiv(long x, int y) { try { return StrictMath.floorDiv(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke floorDiv and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doStrictFloorDiv(long x, long y) { try { return StrictMath.floorDiv(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke floorMod and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doStrictFloorMod(int x, int y) { try { return StrictMath.floorMod(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke floorMod and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doStrictFloorMod(long x, int y) { try { return StrictMath.floorMod(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke floorMod and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doStrictFloorMod(long x, long y) { try { return StrictMath.floorMod(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Test CeilDiv and then CeilMod with int data.
*/ staticvoid testIntCeilDivMod(int x, int y, Object divExpected, Object modExpected) {
testIntCeilDiv(x, y, divExpected);
testIntCeilMod(x, y, modExpected);
}
/** * Test CeilDiv with int data.
*/ staticvoid testIntCeilDiv(int x, int y, Object expected) {
Object result = doCeilDiv(x, y); if (!resultEquals(result, expected)) {
fail("FAIL: Math.ceilDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);
}
/** * Test CeilMod with int data.
*/ staticvoid testIntCeilMod(int x, int y, Object expected) {
Object result = doCeilMod(x, y); if (!resultEquals(result, expected)) {
fail("FAIL: Math.ceilMod(%d, %d) = %s; expected %s%n", x, y, result, expected);
}
try { // Verify result against double precision ceil function int tmp = x / y; // Force ArithmeticException for divide by zero double ff = x - Math.ceil((double)x / (double)y) * y; int fr = (int)ff; boolean t = (fr == ((Integer)result)); if (!result.equals(fr)) {
fail("FAIL: Math.ceilMod(%d, %d) = %s differs from Math.ceil(x, y): %d%n", x, y, result, fr);
}
} catch (ArithmeticException ae) { if (y != 0) {
fail("FAIL: Math.ceilMod(%d, %d); unexpected %s%n", x, y, ae);
}
}
}
/** * Test the long ceilDiv and ceilMod methods. * Math and StrictMath are tested and the same results are expected for both.
*/ staticvoid testLongCeilDivMod(long x, long y, Object divExpected, Object modExpected) {
testLongCeilDiv(x, y, divExpected);
testLongCeilMod(x, y, modExpected);
}
/** * Test CeilDiv with long arguments against expected value. * The expected value is usually a Long but in some cases is * an ArithmeticException. * * @param x dividend * @param y modulus * @param expected expected value,
*/ staticvoid testLongCeilDiv(long x, long y, Object expected) {
Object result = doCeilDiv(x, y); if (!resultEquals(result, expected)) {
fail("FAIL: long Math.ceilDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);
}
Object strict_result = doStrictCeilDiv(x, y); if (!resultEquals(strict_result, expected)) {
fail("FAIL: long StrictMath.ceilDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
}
}
/** * Test CeilMod of long arguments against expected value. * The expected value is usually a Long but in some cases is * an ArithmeticException. * * @param x dividend * @param y modulus * @param expected expected value
*/ staticvoid testLongCeilMod(long x, long y, Object expected) {
Object result = doCeilMod(x, y); if (!resultEquals(result, expected)) {
fail("FAIL: long Math.ceilMod(%d, %d) = %s; expected %s%n", x, y, result, expected);
}
Object strict_result = doStrictCeilMod(x, y); if (!resultEquals(strict_result, expected)) {
fail("FAIL: long StrictMath.ceilMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
}
try { // Verify the result against BigDecimal rounding mode.
BigDecimal xD = new BigDecimal(x);
BigDecimal yD = new BigDecimal(y);
BigDecimal resultD = xD.divide(yD, RoundingMode.CEILING);
resultD = resultD.multiply(yD);
resultD = xD.subtract(resultD); long fr = resultD.longValue(); if (!result.equals(fr)) {
fail("FAIL: Long.ceilMod(%d, %d) = %d is different than BigDecimal result: %d%n", x, y, result, fr);
}
} catch (ArithmeticException ae) { if (y != 0) {
fail("FAIL: long Math.ceilMod(%d, %d); unexpected ArithmeticException from bigdecimal");
}
}
}
/** * Test the integer ceilDiv and ceilMod methods. * Math and StrictMath are tested and the same results are expected for both.
*/ staticvoid testLongIntCeilDivMod(long x, int y, Object divExpected, Object modExpected) {
testLongIntCeilDiv(x, y, divExpected);
testLongIntCeilMod(x, y, modExpected);
}
/** * Test CeilDiv with long arguments against expected value. * The expected value is usually a Long but in some cases is * an ArithmeticException. * * @param x dividend * @param y modulus * @param expected expected value,
*/ staticvoid testLongIntCeilDiv(long x, int y, Object expected) {
Object result = doCeilDiv(x, y); if (!resultEquals(result, expected)) {
fail("FAIL: long Math.ceilDiv(%d, %d) = %s; expected %s%n", x, y, result, expected);
}
Object strict_result = doStrictCeilDiv(x, y); if (!resultEquals(strict_result, expected)) {
fail("FAIL: long StrictMath.ceilDiv(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
}
}
/** * Test CeilMod of long arguments against expected value. * The expected value is usually a Long but in some cases is * an ArithmeticException. * * @param x dividend * @param y modulus * @param expected expected value
*/ staticvoid testLongIntCeilMod(long x, int y, Object expected) {
Object result = doCeilMod(x, y); if (!resultEquals(result, expected)) {
fail("FAIL: int Math.ceilMod(%d, %d) = %s; expected %s%n", x, y, result, expected);
}
Object strict_result = doStrictCeilMod(x, y); if (!resultEquals(strict_result, expected)) {
fail("FAIL: int StrictMath.ceilMod(%d, %d) = %s; expected %s%n", x, y, strict_result, expected);
}
try { // Verify the result against BigDecimal rounding mode.
BigDecimal xD = new BigDecimal(x);
BigDecimal yD = new BigDecimal(y);
BigDecimal resultD = xD.divide(yD, RoundingMode.CEILING);
resultD = resultD.multiply(yD);
resultD = xD.subtract(resultD); int fr = resultD.intValue(); if (!result.equals(fr)) {
fail("FAIL: Long.ceilMod(%d, %d) = %d is different than BigDecimal result: %d%n", x, y, result, fr);
}
} catch (ArithmeticException ae) { if (y != 0) {
fail("FAIL: long Math.ceilMod(%d, %d); unexpected ArithmeticException from bigdecimal");
}
}
}
/** * Invoke ceilDiv and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doCeilDiv(int x, int y) { try { return Math.ceilDiv(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke ceilDiv and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doCeilDiv(long x, int y) { try { return Math.ceilDiv(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke ceilDiv and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doCeilDiv(long x, long y) { try { return Math.ceilDiv(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke ceilMod and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doCeilMod(int x, int y) { try { return Math.ceilMod(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke ceilMod and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doCeilMod(long x, int y) { try { return Math.ceilMod(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke ceilMod and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doCeilMod(long x, long y) { try { return Math.ceilMod(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke ceilDiv and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doStrictCeilDiv(int x, int y) { try { return StrictMath.ceilDiv(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke ceilDiv and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doStrictCeilDiv(long x, int y) { try { return StrictMath.ceilDiv(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke ceilDiv and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doStrictCeilDiv(long x, long y) { try { return StrictMath.ceilDiv(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke ceilMod and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doStrictCeilMod(int x, int y) { try { return StrictMath.ceilMod(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke ceilMod and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doStrictCeilMod(long x, int y) { try { return StrictMath.ceilMod(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Invoke ceilMod and return the result or any exception. * @param x the x value * @param y the y value * @return the result Integer or an exception.
*/ static Object doStrictCeilMod(long x, long y) { try { return StrictMath.ceilMod(x, y);
} catch (ArithmeticException ae) { return ae;
}
}
/** * Returns a boolean by comparing the result and the expected value. * The equals method is not defined for ArithmeticException but it is * desirable to have equals return true if the expected and the result * both threw the same exception (class and message.) * * @param result the result from testing the method * @param expected the expected value * @return true if the result is equal to the expected values; false otherwise.
*/ staticboolean resultEquals(Object result, Object expected) { if (result.getClass() != expected.getClass()) {
fail("FAIL: Result type mismatch, %s; expected: %s%n",
result.getClass().getName(), expected.getClass().getName()); returnfalse;
}
if (result.equals(expected)) { returntrue;
} // Handle special case to compare ArithmeticExceptions if (result instanceof ArithmeticException && expected instanceof ArithmeticException) { returntrue;
} returnfalse;
}
}
¤ Dauer der Verarbeitung: 0.68 Sekunden
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.