From e9f71f37bad7001070d933a6cf4c200393ca3c06 Mon Sep 17 00:00:00 2001 From: Jesse Wilson Date: Sun, 3 Oct 2010 11:55:43 -0700 Subject: [PATCH] Remove math tests duplicated from Harmony. Change-Id: Ia7b24e2f7637b8036b646182cc0411ea83a4b09b --- .../java/math/OldBigDecimalArithmeticTest.java | 669 ++++++ .../java/math/OldBigDecimalCompareTest.java | 63 + .../java/math/OldBigDecimalConvertTest.java | 451 ++++ .../math/OldBigDecimalScaleOperationsTest.java | 99 + .../java/math/OldBigIntegerToStringTest.java | 83 + .../tests/java/math/BigDecimalArithmeticTest.java | 2275 -------------------- .../tests/java/math/BigDecimalCompareTest.java | 531 ----- .../tests/java/math/BigDecimalConvertTest.java | 1166 ---------- .../java/math/BigDecimalScaleOperationsTest.java | 451 ---- .../math/tests/java/math/BigIntegerAddTest.java | 497 ----- .../math/tests/java/math/BigIntegerAndTest.java | 434 ---- .../tests/java/math/BigIntegerCompareTest.java | 535 ----- .../tests/java/math/BigIntegerConvertTest.java | 794 ------- .../tests/java/math/BigIntegerHashCodeTest.java | 81 - .../math/tests/java/math/BigIntegerNotTest.java | 194 -- .../math/tests/java/math/BigIntegerOrTest.java | 421 ---- .../tests/java/math/BigIntegerSubtractTest.java | 548 ----- .../tests/java/math/BigIntegerToStringTest.java | 228 -- .../math/tests/java/math/BigIntegerXorTest.java | 279 --- 19 files changed, 1365 insertions(+), 8434 deletions(-) create mode 100644 luni/src/test/java/libcore/java/math/OldBigDecimalArithmeticTest.java create mode 100644 luni/src/test/java/libcore/java/math/OldBigDecimalCompareTest.java create mode 100644 luni/src/test/java/libcore/java/math/OldBigDecimalConvertTest.java create mode 100644 luni/src/test/java/libcore/java/math/OldBigDecimalScaleOperationsTest.java create mode 100644 luni/src/test/java/libcore/java/math/OldBigIntegerToStringTest.java delete mode 100644 luni/src/test/java/org/apache/harmony/math/tests/java/math/BigDecimalArithmeticTest.java delete mode 100644 luni/src/test/java/org/apache/harmony/math/tests/java/math/BigDecimalCompareTest.java delete mode 100644 luni/src/test/java/org/apache/harmony/math/tests/java/math/BigDecimalConvertTest.java delete mode 100644 luni/src/test/java/org/apache/harmony/math/tests/java/math/BigDecimalScaleOperationsTest.java delete mode 100644 luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerAddTest.java delete mode 100644 luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerAndTest.java delete mode 100644 luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerCompareTest.java delete mode 100644 luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerConvertTest.java delete mode 100644 luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerHashCodeTest.java delete mode 100644 luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerNotTest.java delete mode 100644 luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerOrTest.java delete mode 100644 luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerSubtractTest.java delete mode 100644 luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerToStringTest.java delete mode 100644 luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerXorTest.java diff --git a/luni/src/test/java/libcore/java/math/OldBigDecimalArithmeticTest.java b/luni/src/test/java/libcore/java/math/OldBigDecimalArithmeticTest.java new file mode 100644 index 00000000..6e22c635 --- /dev/null +++ b/luni/src/test/java/libcore/java/math/OldBigDecimalArithmeticTest.java @@ -0,0 +1,669 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package libcore.java.math; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.math.MathContext; +import java.math.RoundingMode; +import junit.framework.TestCase; + +public class OldBigDecimalArithmeticTest extends TestCase { + + public void testAddMathContextNonTrivial() { + MathContext mc; + BigDecimal a, b, res; + + mc = new MathContext(17, RoundingMode.FLOOR); + a = new BigDecimal("123456789012345.678"); + b = new BigDecimal("100000000000000.009"); + assertEquals("incorrect value", "123456789012345.67", + a.round(mc).toString()); + assertEquals("incorrect value", "100000000000000.00", + b.round(mc).toString()); + assertEquals("incorrect value", "223456789012345.67", + a.round(mc).add(b.round(mc)).toString()); + res = a.add(b, mc); + assertEquals("incorrect value", "223456789012345.68", res.toString()); + + mc = new MathContext(33, RoundingMode.UNNECESSARY); + a = new BigDecimal("1234567890123456789012345678.9012395"); + b = new BigDecimal("1000000000000000090000000000.0000005"); + res = a.add(b, mc); + assertEquals("Incorrect value!", "2234567890123456879012345678.90124", res.toString()); + assertEquals("Incorrect scale!", 5, res.scale()); + assertEquals("Incorrect precision!", 33, res.precision()); + } + + public void testSubtractMathContextNonTrivial() { + MathContext mc; + BigDecimal a, b, res; + + mc = new MathContext(17, RoundingMode.FLOOR); + a = new BigDecimal("12345678901234567.8"); + b = new BigDecimal("10000000000000000.9"); + assertEquals("incorrect value", "2345678901234567", + a.round(mc).subtract(b.round(mc)).toString()); + res = a.subtract(b, mc); + assertEquals("incorrect value", "2345678901234566.9", res.toString()); + assertEquals("Incorrect scale!", 1, res.scale()); + assertEquals("Incorrect precision!", 17, res.precision()); + + mc = new MathContext(33, RoundingMode.UNNECESSARY); + a = new BigDecimal("1234567890123456789012345678.9012395"); + b = new BigDecimal("1000000000000000090000000000.0000005"); + res = a.subtract(b, mc); + assertEquals("incorrect value", "234567890123456699012345678.901239", res.toString()); + assertEquals("Incorrect scale!", 6, res.scale()); + assertEquals("Incorrect precision!", 33, res.precision()); + } + + public void testMultiplyMathContextNonTrivial() { + MathContext mc; + BigDecimal a, b, res; + + mc = new MathContext(17, RoundingMode.FLOOR); + a = new BigDecimal("92345678901234567.8"); + b = new BigDecimal("10000000000000000.9"); + res = a.round(mc).multiply(b.round(mc)); + assertEquals("incorrect value", "923456789012345670000000000000000", res.toString()); + res = res.round(mc); + assertEquals("incorrect value", "9.2345678901234567E+32", res.toString()); + res = a.multiply(b, mc); + assertEquals("incorrect value", "9.2345678901234576E+32", res.toString()); + assertEquals("Incorrect scale!", -16, res.scale()); + assertEquals("Incorrect precision!", 17, res.precision()); + } + + public void testPowNonTrivial() { + BigDecimal a, b, res; + + a = new BigDecimal("100.9"); + try { + res = a.pow(-1); + fail("ArithmeticException is not thrown for negative exponent"); + } catch (ArithmeticException e) { + // expected + } + try { + res = a.pow(-103); + fail("ArithmeticException is not thrown for negative exponent"); + } catch (ArithmeticException e) { + // expected + } + } + + public void testPowMathContext() { + String a = "123121247898748298842980"; + int aScale = 10; + int exp = 10; + String c = "8.0044E+130"; + int cScale = -126; + BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); + MathContext mc = new MathContext(5, RoundingMode.HALF_UP); + BigDecimal result = aNumber.pow(exp, mc); + assertEquals("incorrect value", c, result.toString()); + assertEquals("incorrect scale", cScale, result.scale()); + } + + public void testPowMathContextNonTrivial() { + MathContext mc; + BigDecimal a, b, res; + + mc = new MathContext(7, RoundingMode.FLOOR); + a = new BigDecimal("1000000.9"); + assertEquals("incorrect value", "1.000000E+6000", + a.round(mc).pow(1000).round(mc).toString()); + res = a.pow(1000, mc); + assertEquals("incorrect value", "1.000900E+6000", res.toString()); + + mc = new MathContext(4, RoundingMode.FLOOR); + a = new BigDecimal("1000.9"); + assertEquals("incorrect value", "1.000E+3000", + a.round(mc).pow(1000).round(mc).toString()); + res = a.pow(1000, mc); + assertEquals("incorrect value", "2.458E+3000", res.toString()); + + mc = new MathContext(2, RoundingMode.UNNECESSARY); + a = new BigDecimal("1234"); + try { + res = a.pow(-2, mc); + fail("ArithmeticException is not thrown"); + } catch (ArithmeticException e) { + // expected + } + + a = new BigDecimal("100"); + mc = new MathContext(4, RoundingMode.UNNECESSARY); + res = a.pow(-2, mc); + assertEquals("incorrect value", "0.0001", res.toString()); + + a = new BigDecimal("1000.9"); + try { + mc = new MathContext(0, RoundingMode.FLOOR); + res = a.pow(-1, mc); + fail("ArithmeticException is not thrown for negative exponent and precision = 0"); + } catch (ArithmeticException e) { + // expected + } + + a = new BigDecimal("000.0001"); + try { + mc = new MathContext(0, RoundingMode.FLOOR); + res = a.pow(-1, mc); + fail("ArithmeticException is not thrown for negative exponent and precision = 0"); + } catch (ArithmeticException e) { + // expected + } + + a = new BigDecimal("1E-400"); + mc = new MathContext(4, RoundingMode.UNNECESSARY); + res = a.pow(-1, mc); + assertEquals("incorrect value", "1E+400", res.toString()); + +// Doesn't succeed against JDK of Sun!: +// mc = new MathContext(3, RoundingMode.FLOOR); +// a = new BigDecimal("100.9"); +// assertEquals("incorrect value", "1.00E+2000", +// a.round(mc).pow(1000).round(mc).toString()); +// res = a.pow(1000).round(mc); +// res = a.pow(1000, mc); +// assertEquals("incorrect value", "7.783E+2003", res.toString()); + } + + public void testDivideINonTrivial() { + MathContext mc; + BigDecimal a, b, res; + + mc = new MathContext(17, RoundingMode.FLOOR); + a = new BigDecimal("12345678901234567E1234"); + b = new BigDecimal("1.23456789012345679"); + assertEquals("incorrect value", "1E+1250", + a.round(mc).divide(b.round(mc)).toString()); + res = a.divide(b, BigDecimal.ROUND_FLOOR); + assertEquals("incorrect value", "9.999999999999999E+1249", res.toString()); + + a = new BigDecimal("1234567890123456789012345678.9012395"); + b = new BigDecimal("6172839450617283945061728394.5061975"); + res = a.divide(b, BigDecimal.ROUND_UNNECESSARY); + assertEquals("incorrect value", "0.2000000", res.toString()); + + a = new BigDecimal("1234567890123456789012345678.9012395"); + b = new BigDecimal("1000000000000000090000000000.0000005"); + try { + res = a.divide(b, BigDecimal.ROUND_UNNECESSARY); + fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY"); + } catch (ArithmeticException e) { + // expected + } + } + + public void testDivideIINonTrivial() { + MathContext mc; + BigDecimal a, b, res; + + mc = new MathContext(17, RoundingMode.FLOOR); + a = new BigDecimal("12345678901234567E1234"); + b = new BigDecimal("1.23456789012345679"); + res = a.divide(b, -1220, BigDecimal.ROUND_FLOOR); + assertEquals("incorrect value", "9.99999999999999927099999343899E+1249", res.toString()); + + a = new BigDecimal("1234567890123456789012345678.9012395"); + b = new BigDecimal("6172839450617283945061728394.5061975"); + res = a.divide(b, 1, BigDecimal.ROUND_UNNECESSARY); + assertEquals("incorrect value", "0.2", res.toString()); + + a = new BigDecimal("1234567890123456789012345678.9012395"); + b = new BigDecimal("6172839450617283945061728394.5061975"); + try { + res = a.divide(b, 0, BigDecimal.ROUND_UNNECESSARY); + fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY"); + } catch (ArithmeticException e) { + // expected + } + } + + // Has a rounding problem. seems like the precision is + // 1 too small and cuts off the last digit. also this test might + // not be correct. The name implies that scale should be used. + public void testDivideScaleRoundingModeNonTrivial() { + MathContext mc; + BigDecimal a, b, res; + + mc = new MathContext(17, RoundingMode.FLOOR); + a = new BigDecimal("12345678901234567.1"); + b = new BigDecimal("12345678901234567.9"); + assertEquals("incorrect value", "1", + a.round(mc).divide(b.round(mc)).toString()); + res = a.divide(b, mc); + assertEquals("incorrect value", "0.99999999999999993", res.toString()); + + mc = new MathContext(13, RoundingMode.UNNECESSARY); + a = new BigDecimal("1234567890123456789012345678.9012395"); + b = new BigDecimal("6172839450617283945061728394.5061975"); + res = a.divide(b, mc); + assertEquals("incorrect value", "0.2", res.toString()); + + mc = new MathContext(33, RoundingMode.UNNECESSARY); + a = new BigDecimal("1234567890123456789012345678.9012395"); + b = new BigDecimal("1000000000000000090000000000.0000005"); + try { + res = a.divide(b, mc); + fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY"); + } catch (ArithmeticException e) { + // expected + } + } + + // The same test and the same problem like testDivideScaleRoundingModeNonTrivial. + public void testDivideMathContextNonTrivial() { + MathContext mc; + BigDecimal a, b, res; + +// FAILS AGAINST RI!: +// mc = new MathContext(6, RoundingMode.FLOOR); +// a = new BigDecimal("12345.1"); +// b = new BigDecimal("12345.9"); +// assertEquals("incorrect value", "1", +// a.round(mc).divide(b.round(mc)).toString()); +// res = a.divide(b, mc); +// assertEquals("incorrect value", "0.99993", res.toString()); + + mc = new MathContext(5, RoundingMode.FLOOR); + a = new BigDecimal("12345.1"); + b = new BigDecimal("12345.9"); + assertEquals("incorrect value", "1", + a.round(mc).divide(b.round(mc)).toString()); + res = a.divide(b, mc); + assertEquals("incorrect value", "0.99993", res.toString()); + + mc = new MathContext(17, RoundingMode.FLOOR); + a = new BigDecimal("12345678901234567.1"); + b = new BigDecimal("12345678901234567.9"); + assertEquals("incorrect value", "1", + a.round(mc).divide(b.round(mc)).toString()); + res = a.divide(b, mc); + assertEquals("incorrect value", "0.99999999999999993", res.toString()); + assertEquals("incorrect value", res.round(mc).toString(), res.toString()); + + mc = new MathContext(13, RoundingMode.UNNECESSARY); + a = new BigDecimal("1234567890123456789012345678.9012395"); + b = new BigDecimal("6172839450617283945061728394.5061975"); + res = a.divide(b, mc); + assertEquals("incorrect value", "0.2", res.toString()); + + mc = new MathContext(33, RoundingMode.UNNECESSARY); + a = new BigDecimal("1234567890123456789012345678.9012395"); + b = new BigDecimal("1000000000000000090000000000.0000005"); + try { + res = a.divide(b, mc); + fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY"); + } catch (ArithmeticException e) { + // expected + } + } + + public void testDivideToIntegralValueByZero() { + String a = "3736186567876876578956958765675671119238118911893939591735"; + int aScale = 45; + String b = "0"; + int bScale = -70; + String res = "277923185514690367474770683"; + int resScale = 0; + String rem = "1.3032693871288309587558885943391070087960319452465789990E-15"; + int remScale = 70; + BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); + BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); + try { + BigDecimal result = aNumber.divideToIntegralValue(bNumber); + fail("ArithmeticException not thrown for division by 0"); + } catch (ArithmeticException e) { + // expected + } + } + + public void testDivideToIntegralValueMathContextNonTrivial() { + MathContext mc; + BigDecimal a, b, res; + + a = new BigDecimal("92345678901234567.8"); + b = new BigDecimal("43"); + res = a.multiply(b); + assertEquals("incorrect value", "3970864192753086415.4", res.toString()); + + mc = new MathContext(20, RoundingMode.DOWN); + a = new BigDecimal("3970864192753086415.4"); + b = new BigDecimal("92345678901234567.8"); + b = new BigDecimal("92345678901234567.8001"); + assertEquals("incorrect value", "43", + a.round(mc).divideToIntegralValue(b.round(mc)).toString()); + res = a.divideToIntegralValue(b, mc); + assertEquals("incorrect value", "42", res.toString()); + +// mc = new MathContext(1, RoundingMode.DOWN); +// res = a.divideToIntegralValue(b, mc); +// assertEquals("incorrect value", "42", res.toString()); + + + mc = new MathContext(17, RoundingMode.FLOOR); + a = new BigDecimal("518518513851851830"); + b = new BigDecimal("12345678901234567.9"); + assertEquals("incorrect value", "42", + a.round(mc).divideToIntegralValue(b.round(mc)).toString()); + res = a.divideToIntegralValue(b, mc); + assertEquals("incorrect value", "41", res.toString()); + } + + /** + * divideAndRemainder(BigDecimal) + */ + public void testDivideAndRemainderByZero() { + String a = "3736186567876876578956958765675671119238118911893939591735"; + int aScale = 45; + String b = "0"; + int bScale = -70; + String res = "277923185514690367474770683"; + int resScale = 0; + String rem = "1.3032693871288309587558885943391070087960319452465789990E-15"; + int remScale = 70; + BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); + BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); + try { + BigDecimal result[] = aNumber.divideAndRemainder(bNumber); + fail("ArithmeticException not thrown for division by 0"); + } catch (ArithmeticException e) { + // expected + } + } + + public void testDivideAndRemainderMathContextNonTrivial() { + MathContext mc; + BigDecimal a, b, res[]; + + mc = new MathContext(13, RoundingMode.FLOOR); + a = new BigDecimal("12345678901234567.1"); + b = new BigDecimal("12345678901234567.9"); + assertEquals("incorrect value", "0E+4", + a.round(mc).divideAndRemainder(b.round(mc))[1].toString()); + res = a.divideAndRemainder(b, mc); + assertEquals("incorrect value", "12345678901234567.1", res[1].toString()); + + mc = new MathContext(1, RoundingMode.UNNECESSARY); + a = new BigDecimal("6172839450617283945061728394.5061976"); + b = new BigDecimal("1234567890123456789012345678.9012395"); + res = a.divideAndRemainder(b, mc); + assertEquals("incorrect value", "1E-7", res[1].toString()); + + mc = new MathContext(3, RoundingMode.UNNECESSARY); + a = new BigDecimal("6172839450617283945061728394.6000000"); + b = new BigDecimal("1234567890123456789012345678.9012395"); + try { + res = a.divideAndRemainder(b, mc); + assertEquals("incorrect value", "0.0938025", res[1].toString()); + assertEquals("incorrect value", "0.09", res[1].round(mc).toString()); +// fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY"); + } catch (ArithmeticException e) { + // expected + } + } + public void testRemainderByZero() { + String a = "3736186567876876578956958765675671119238118911893939591735"; + int aScale = 45; + String b = "0"; + int bScale = -70; + String res = "277923185514690367474770683"; + int resScale = 0; + String rem = "1.3032693871288309587558885943391070087960319452465789990E-15"; + int remScale = 70; + BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); + BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); + try { + BigDecimal result = aNumber.remainder(bNumber); + fail("ArithmeticException not thrown for division by 0"); + } catch (ArithmeticException e) { + // expected + } + } + + public void testRemainderMathContextNonTrivial() { + MathContext mc; + BigDecimal a, b, res; + + mc = new MathContext(13, RoundingMode.DOWN); + a = new BigDecimal("12345678901234567.1"); + b = new BigDecimal("12345678901234567.9"); + assertEquals("incorrect value", "0E+4", + a.round(mc).divideAndRemainder(b.round(mc))[1].toString()); + res = a.remainder(b, mc); + assertEquals("incorrect value", "12345678901234567.1", res.toString()); + + mc = new MathContext(1, RoundingMode.UNNECESSARY); + a = new BigDecimal("6172839450617283945061728394.5061976"); + b = new BigDecimal("1234567890123456789012345678.9012395"); + res = a.remainder(b, mc); + assertEquals("incorrect value", "1E-7", res.toString()); + + mc = new MathContext(3, RoundingMode.UNNECESSARY); + a = new BigDecimal("6172839450617283945061728394.6000000"); + b = new BigDecimal("1234567890123456789012345678.9012395"); + try { + res = a.remainder(b, mc); + assertEquals("incorrect value", "0.0938025", res.toString()); + assertEquals("incorrect value", "0.09", res.round(mc).toString()); +// fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY"); + } catch (ArithmeticException e) { + // expected + } + } + public void testRoundNonTrivial() { + MathContext mc; + String biStr = new String( "12345678901234567890123456789012345.0E+10"); + String nbiStr = new String("-12345678901234567890123456789012345.E+10"); + BigDecimal bd; + + mc = new MathContext(17, RoundingMode.FLOOR); + bd = new BigDecimal(new BigInteger("123456789012345678"), 3, mc); + assertEquals("incorrect value", "123456789012345.67", bd.toString()); + + mc = new MathContext(31, RoundingMode.UP); + bd = (new BigDecimal(biStr)).round(mc); + assertEquals("incorrect value", "1.234567890123456789012345678902E+44", bd.toString()); + bd = (new BigDecimal(nbiStr)).round(mc); + assertEquals("incorrect value", "-1.234567890123456789012345678902E+44", bd.toString()); + + mc = new MathContext(28, RoundingMode.DOWN); + bd = (new BigDecimal(biStr)).round(mc); + assertEquals("incorrect value", "1.234567890123456789012345678E+44", bd.toString()); + bd = (new BigDecimal(nbiStr)).round(mc); + assertEquals("incorrect value", "-1.234567890123456789012345678E+44", bd.toString()); + + mc = new MathContext(33, RoundingMode.CEILING); + bd = (new BigDecimal(biStr)).round(mc); + assertEquals("incorrect value", "1.23456789012345678901234567890124E+44", bd.toString()); + bd = (new BigDecimal(nbiStr)).round(mc); + assertEquals("incorrect value", "-1.23456789012345678901234567890123E+44", bd.toString()); + + mc = new MathContext(34, RoundingMode.UNNECESSARY); + try { + bd = (new BigDecimal(biStr)).round(mc); + fail("No ArithmeticException for RoundingMode.UNNECESSARY"); + } catch (ArithmeticException e) { + // expected + } + try { + bd = (new BigDecimal(nbiStr)).round(mc); + fail("No ArithmeticException for RoundingMode.UNNECESSARY"); + } catch (ArithmeticException e) { + // expected + } + + mc = new MathContext(7, RoundingMode.FLOOR); + bd = new BigDecimal("1000000.9", mc); + assertEquals("incorrect value", "1000000", bd.toString()); + } + /** + * @tests java.math.BigDecimal#add(java.math.BigDecimal) + */ + public void test_addBigDecimal() { + BigDecimal add1 = new BigDecimal("23.456"); + BigDecimal add2 = new BigDecimal("3849.235"); + BigDecimal sum = add1.add(add2); + assertTrue("the sum of 23.456 + 3849.235 is wrong", sum.unscaledValue().toString().equals( + "3872691") + && sum.scale() == 3); + assertTrue("the sum of 23.456 + 3849.235 is not printed correctly", sum.toString().equals( + "3872.691")); + BigDecimal add3 = new BigDecimal(12.34E02D); + assertTrue("the sum of 23.456 + 12.34E02 is not printed correctly", (add1.add(add3)) + .toString().equals("1257.456")); + } + + /** + * @tests java.math.BigDecimal#divide(java.math.BigDecimal, + * java.math.MathContext) divide(BigDecimal, RoundingMode) + */ + public void test_DivideBigDecimalRoundingModeUP() { + String a = "-37361671119238118911893939591735"; + String b = "74723342238476237823787879183470"; + RoundingMode rm = RoundingMode.UP; + String c = "-1"; + BigDecimal aNumber = new BigDecimal(new BigInteger(a)); + BigDecimal bNumber = new BigDecimal(new BigInteger(b)); + BigDecimal result = aNumber.divide(bNumber, rm); + assertEquals("incorrect value", c, result.toString()); + } + + /** + * @tests java.math.BigDecimal#divide(java.math.BigDecimal, + * java.math.RoundingMode) divide(BigDecimal, RoundingMode) + */ + public void test_DivideBigDecimalRoundingModeDOWN() { + String a = "-37361671119238118911893939591735"; + String b = "74723342238476237823787879183470"; + RoundingMode rm = RoundingMode.DOWN; + String c = "0"; + BigDecimal aNumber = new BigDecimal(new BigInteger(a)); + BigDecimal bNumber = new BigDecimal(new BigInteger(b)); + BigDecimal result = aNumber.divide(bNumber, rm); + assertEquals("incorrect value", c, result.toString()); + } + + /** + * @tests java.math.BigDecimal#divide(java.math.BigDecimal, + * java.math.RoundingMode) divide(BigDecimal, RoundingMode) + */ + public void test_DivideBigDecimalRoundingModeCEILING() { + String a = "3736186567876876578956958765675671119238118911893939591735"; + String b = "74723342238476237823787879183470"; + RoundingMode rm = RoundingMode.CEILING; + String c = "50000260373164286401361914"; + BigDecimal aNumber = new BigDecimal(new BigInteger(a)); + BigDecimal bNumber = new BigDecimal(new BigInteger(b)); + BigDecimal result = aNumber.divide(bNumber, rm); + assertEquals("incorrect value", c, result.toString()); + } + + /** + * @tests java.math.BigDecimal#divide(java.math.BigDecimal, + * java.math.RoundingMode) divide(BigDecimal, RoundingMode) + */ + public void test_DivideBigDecimalRoundingModeFLOOR() { + String a = "3736186567876876578956958765675671119238118911893939591735"; + String b = "74723342238476237823787879183470"; + RoundingMode rm = RoundingMode.FLOOR; + String c = "50000260373164286401361913"; + BigDecimal aNumber = new BigDecimal(new BigInteger(a)); + BigDecimal bNumber = new BigDecimal(new BigInteger(b)); + BigDecimal result = aNumber.divide(bNumber, rm); + assertEquals("incorrect value", c, result.toString()); + } + + /** + * @tests java.math.BigDecimal#divide(java.math.BigDecimal, + * java.math.RoundingMode) divide(BigDecimal, RoundingMode) + */ + public void test_DivideBigDecimalRoundingModeHALF_UP() { + String a = "3736186567876876578956958765675671119238118911893939591735"; + String b = "74723342238476237823787879183470"; + RoundingMode rm = RoundingMode.HALF_UP; + String c = "50000260373164286401361913"; + BigDecimal aNumber = new BigDecimal(new BigInteger(a)); + BigDecimal bNumber = new BigDecimal(new BigInteger(b)); + BigDecimal result = aNumber.divide(bNumber, rm); + assertEquals("incorrect value", c, result.toString()); + } + + /** + * @tests java.math.BigDecimal#divide(java.math.BigDecimal, + * java.math.RoundingMode) divide(BigDecimal, RoundingMode) + */ + public void test_DivideBigDecimalRoundingModeHALF_DOWN() { + String a = "3736186567876876578956958765675671119238118911893939591735"; + int aScale = 5; + String b = "74723342238476237823787879183470"; + int bScale = 15; + int newScale = 7; + RoundingMode rm = RoundingMode.HALF_DOWN; + String c = "500002603731642864013619132621009722.1803810"; + BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); + BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); + BigDecimal result = aNumber.divide(bNumber, newScale, rm); + assertEquals("incorrect value", c, result.toString()); + assertEquals("incorrect scale", newScale, result.scale()); + } + + /** + * @tests java.math.BigDecimal#divide(java.math.BigDecimal, + * java.math.RoundingMode) divide(BigDecimal, RoundingMode) + */ + public void test_DivideBigDecimalRoundingModeHALF_EVEN() { + String a = "3736186567876876578956958765675671119238118911893939591735"; + String b = "74723342238476237823787879183470"; + RoundingMode rm = RoundingMode.HALF_EVEN; + String c = "50000260373164286401361913"; + BigDecimal aNumber = new BigDecimal(new BigInteger(a)); + BigDecimal bNumber = new BigDecimal(new BigInteger(b)); + BigDecimal result = aNumber.divide(bNumber, rm); + assertEquals("incorrect value", c, result.toString()); + } + + /** + * @tests java.math.BigDecimal#divide(java.math.BigDecimal, + * java.math.RoundingMode) divide(BigDecimal, RoundingMode) + */ + public void test_DivideBigDecimalRoundingExc() { + String a = "3736186567876876578956958765675671119238118911893939591735"; + String b = "74723342238476237823787879183470"; + RoundingMode rm = RoundingMode.UNNECESSARY; + BigDecimal aNumber = new BigDecimal(new BigInteger(a)); + BigDecimal bNumber = new BigDecimal(new BigInteger(b)); + try { + aNumber.divide(bNumber, rm); + fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY divider"); + } catch (java.lang.ArithmeticException ae) { + // expected + } + try { + bNumber = new BigDecimal(0); + aNumber.divide(bNumber, rm); + fail("ArithmeticException is not thrown for zero divider"); + } catch (java.lang.ArithmeticException ae) { + // expected + } + } +} diff --git a/luni/src/test/java/libcore/java/math/OldBigDecimalCompareTest.java b/luni/src/test/java/libcore/java/math/OldBigDecimalCompareTest.java new file mode 100644 index 00000000..73a6d5e4 --- /dev/null +++ b/luni/src/test/java/libcore/java/math/OldBigDecimalCompareTest.java @@ -0,0 +1,63 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @author Elena Semukhina + * @version $Revision$ + */ + +package libcore.java.math; + +import java.math.BigDecimal; +import java.math.BigInteger; +import java.math.MathContext; +import java.math.RoundingMode; +import junit.framework.TestCase; + +public class OldBigDecimalCompareTest extends TestCase { + + public void testAbsMathContextNeg() { + String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; + BigDecimal aNumber = new BigDecimal(a); + MathContext mc = new MathContext(34, RoundingMode.UP); + assertEquals("incorrect value", "1.238096483923847545735673567457357E+53", aNumber.abs(mc).toString()); + + mc = new MathContext(34, RoundingMode.DOWN); + assertEquals("incorrect value", "1.238096483923847545735673567457356E+53", aNumber.abs(mc).toString()); + + mc = new MathContext(34, RoundingMode.FLOOR); + assertEquals("incorrect value", "1.238096483923847545735673567457356E+53", aNumber.abs(mc).toString()); + + mc = new MathContext(34, RoundingMode.CEILING); + assertEquals("incorrect value", "1.238096483923847545735673567457357E+53", aNumber.abs(mc).toString()); + + mc = new MathContext(34, RoundingMode.UNNECESSARY); + try { + aNumber.abs(mc); + fail("No ArithmeticException for RoundingMode.UNNECESSARY"); + } catch (ArithmeticException expected) { + } + } + + public void testNegateMathContextPositive() { + String a = "92948782094488478231212478987482988429808779810457634781384756794987"; + MathContext mc = new MathContext(37, RoundingMode.FLOOR); + BigDecimal aNumber = new BigDecimal(new BigInteger(a), 41); + BigDecimal res = aNumber.negate(mc); + assertEquals("incorrect value", "-929487820944884782312124789.8748298843", res.toString()); + assertEquals("incorrect scale", 10, res.scale()); + } +} diff --git a/luni/src/test/java/libcore/java/math/OldBigDecimalConvertTest.java b/luni/src/test/java/libcore/java/math/OldBigDecimalConvertTest.java new file mode 100644 index 00000000..3ce40a63 --- /dev/null +++ b/luni/src/test/java/libcore/java/math/OldBigDecimalConvertTest.java @@ -0,0 +1,451 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @author Elena Semukhina + * @version $Revision$ + */ + +package libcore.java.math; + +import java.math.BigDecimal; +import java.math.MathContext; +import java.math.RoundingMode; +import junit.framework.TestCase; + +public class OldBigDecimalConvertTest extends TestCase { + + public void test_IntValueExactNeg() { + String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; + BigDecimal aNumber = new BigDecimal(a); + try { + aNumber.intValueExact(); + fail("java.lang.ArithmeticException isn't thrown after calling intValueExact"); + } catch (java.lang.ArithmeticException ae) { + // expected; + } + } + + public void test_IntValueExactPos() { + String a = "123809648392384754573567356745735.63567890295784902768787678287E+21"; + BigDecimal aNumber = new BigDecimal(a); + try { + aNumber.intValueExact(); + fail("java.lang.ArithmeticException isn't thrown after calling intValueExact"); + } catch (java.lang.ArithmeticException ae) { + // expected; + } + } + + public void test_IntValueExactFloatNeg() { + BigDecimal aNumber = new BigDecimal("-2147483647.999"); + try { + aNumber.intValueExact(); + fail("java.lang.ArithmeticException isn't thrown after calling intValueExact"); + } catch (java.lang.ArithmeticException ae) { + // expected; + } + } + + public void test_IntValueExactFloatPos() { + float a = 2147483646.99999F; + BigDecimal aNumber = new BigDecimal(a); + try { + aNumber.intValueExact(); + fail("java.lang.ArithmeticException isn't thrown after calling intValueExact"); + } catch (java.lang.ArithmeticException ae) { + // expected; + } + } + + public void test_IntValueExactLongPos() { + long a = 2147483647L; + BigDecimal aNumber = new BigDecimal(a); + int iNumber = aNumber.intValueExact(); + assertTrue("incorrect value", iNumber == a); + } + + public void test_IntValueExactLongNeg() { + long a = -2147483648L; + BigDecimal aNumber = new BigDecimal(a); + int iNumber = aNumber.intValueExact(); + assertTrue("incorrect value", iNumber == a); + } + + public void test_LongValueExactNeg() { + String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; + BigDecimal aNumber = new BigDecimal(a); + try { + aNumber.longValueExact(); + fail("java.lang.ArithmeticException isn't thrown after calling longValueExact"); + } catch (java.lang.ArithmeticException ae) { + // expected; + } + } + + public void test_LongValueExactPos() { + String a = "123809648392384754573567356745735.63567890295784902768787678287E+21"; + BigDecimal aNumber = new BigDecimal(a); + try { + aNumber.longValueExact(); + fail("java.lang.ArithmeticException isn't thrown after calling longValueExact"); + } catch (java.lang.ArithmeticException ae) { + // expected; + } + } + + public void test_LongValueExactFloatNeg() { + BigDecimal aNumber = new BigDecimal("-9223372036854775807.99999"); + try { + aNumber.longValueExact(); + fail("java.lang.ArithmeticException isn't thrown after calling longValueExact"); + } catch (java.lang.ArithmeticException ae) { + // expected; + } + } + + /** + * @tests java.math.BigDecimal#longValueExact() Long value of a positive + * BigDecimal + */ + public void test_LongValueExactFloatPos() { + float a = 9223372036854775806.99999F; + BigDecimal aNumber = new BigDecimal(a); + try { + aNumber.longValueExact(); + fail("java.lang.ArithmeticException isn't thrown after calling longValueExact"); + } catch (java.lang.ArithmeticException ae) { + // expected; + } + } + + public void test_ByteValueExactPos() { + int i = 127; + BigDecimal bdNumber = new BigDecimal(i); + byte bNumber = bdNumber.byteValueExact(); + assertTrue("incorrect byteValueExact", i == bNumber); + } + + public void test_ByteValueExactNeg() { + String sNumber = "-127.56789"; + int iNumber = -128; + int iPresition = 3; + MathContext mc = new MathContext(iPresition, RoundingMode.UP); + BigDecimal bdNumber = new BigDecimal(sNumber, mc); + byte bNumber = bdNumber.byteValueExact(); + assertTrue("incorrect byteValueExact", iNumber == bNumber); + } + + public void test_ByteValueExactCharZero() { + char[] cNumber = { + '-', '0', '.', '0' + }; + int iNumber = 0; + int iPresition = 5; + MathContext mc = new MathContext(iPresition, RoundingMode.HALF_DOWN); + BigDecimal bdNumber = new BigDecimal(cNumber, mc); + byte bNumber = bdNumber.byteValueExact(); + assertTrue("incorrect byteValueExact", iNumber == bNumber); + } + + public void test_ByteValueExactStringZero() { + String sNumber = "00000000000000"; + int iNumber = 0; + int iPresition = 0; + MathContext mc = new MathContext(iPresition, RoundingMode.HALF_UP); + BigDecimal bdNumber = new BigDecimal(sNumber, mc); + byte bNumber = bdNumber.byteValueExact(); + assertTrue("incorrect byteValueExact", iNumber == bNumber); + } + + public void test_ByteValueExactDoubleMax() { + double dNumber = Double.MAX_VALUE; + BigDecimal bdNumber = new BigDecimal(dNumber); + try { + bdNumber.byteValueExact(); + fail("java.lang.ArithmeticException isn't thrown after calling byteValueExact"); + } catch (java.lang.ArithmeticException ae) { + // expected + } + } + + public void test_ByteValueExactDoubleMin() { + double dNumber = Double.MIN_VALUE; + BigDecimal bdNumber = new BigDecimal(dNumber); + try { + bdNumber.byteValueExact(); + fail("java.lang.ArithmeticException isn't thrown after calling byteValueExact"); + } catch (java.lang.ArithmeticException ae) { + // expected + } + } + + public void test_ByteValueExactFloatPos() { + float fNumber = 123.5445F; + BigDecimal bdNumber = new BigDecimal(fNumber); + try { + bdNumber.byteValueExact(); + fail("java.lang.ArithmeticException isn't thrown after calling byteValueExact"); + } catch (java.lang.ArithmeticException ae) { + // expected + } + } + + public void test_ByteValueExactFloatNeg() { + float fNumber = -12.987654321F; + BigDecimal bdNumber = new BigDecimal(fNumber); + try { + bdNumber.byteValueExact(); + fail("java.lang.ArithmeticException isn't thrown after calling byteValueExact"); + } catch (java.lang.ArithmeticException ae) { + // expected + } + } + + public void test_ByteValueExactDouble() { + double dNumber = 123.0000D; + BigDecimal bdNumber = new BigDecimal(dNumber); + byte bNumber = bdNumber.byteValueExact(); + assertTrue("incorrect byteValueExact", dNumber == bNumber); + } + + public void test_ByteValueExactLongMin() { + long lNumber = Long.MIN_VALUE; + BigDecimal bdNumber = new BigDecimal(lNumber); + try { + bdNumber.byteValueExact(); + fail("java.lang.ArithmeticException isn't thrown after calling byteValueExact"); + } catch (java.lang.ArithmeticException ae) { + // expected + } + } + + public void test_ByteValueExactIntMax() { + int iNumber = Integer.MAX_VALUE; + BigDecimal bdNumber = new BigDecimal(iNumber); + try { + bdNumber.byteValueExact(); + fail("java.lang.ArithmeticException isn't thrown after calling byteValueExact"); + } catch (java.lang.ArithmeticException ae) { + // expected + } + } + + public void test_ByteValuePos() { + int i = 127; + BigDecimal bdNumber = new BigDecimal(i); + byte bNumber = bdNumber.byteValue(); + assertTrue("incorrect byteValue", i == bNumber); + } + + /** + * @test java.math.BigDecimal#byteValue() Convert negative BigDesimal to + * byte type + */ + public void test_ByteValueNeg() { + String sNumber = "-127.56789"; + int iNumber = -128; + int iPresition = 3; + MathContext mc = new MathContext(iPresition, RoundingMode.UP); + BigDecimal bdNumber = new BigDecimal(sNumber, mc); + byte bNumber = bdNumber.byteValue(); + assertTrue("incorrect byteValueExact", iNumber == bNumber); + } + + public void test_ByteValueCharZero() { + char[] cNumber = { + '-', '0', '.', '0' + }; + int iNumber = 0; + int iPresition = 0; + MathContext mc = new MathContext(iPresition, RoundingMode.HALF_UP); + BigDecimal bdNumber = new BigDecimal(cNumber, mc); + byte bNumber = bdNumber.byteValue(); + assertTrue("incorrect byteValue", iNumber == bNumber); + } + + public void test_ByteValueStringZero() { + String sNumber = "00000"; + int iNumber = 0; + int iPresition = 0; + MathContext mc = new MathContext(iPresition, RoundingMode.HALF_UP); + BigDecimal bdNumber = new BigDecimal(sNumber, mc); + byte bNumber = bdNumber.byteValue(); + assertTrue("incorrect byteValue", iNumber == bNumber); + } + + public void test_ByteValueDoubleMax() { + double dNumber = Double.MAX_VALUE; + BigDecimal bdNumber = new BigDecimal(dNumber); + int result = 0; + byte bNumber = bdNumber.byteValue(); + assertTrue("incorrect byteValue", bNumber == result); + } + + public void test_ByteValueDoubleMin() { + double dNumber = Double.MIN_VALUE; + BigDecimal bdNumber = new BigDecimal(dNumber); + int result = 0; + byte bNumber = bdNumber.byteValue(); + assertTrue("incorrect byteValue", bNumber == result); + } + + public void test_ByteValueFloatNeg() { + float fNumber = -12.987654321F; + byte bValue = -12; + BigDecimal bdNumber = new BigDecimal(fNumber); + byte bNumber = bdNumber.byteValue(); + assertTrue("incorrect byteValue", bNumber == bValue); + } + + public void test_ByteValueDouble() { + double dNumber = 123.0000D; + BigDecimal bdNumber = new BigDecimal(dNumber); + byte bNumber = bdNumber.byteValue(); + assertTrue("incorrect byteValue", dNumber == bNumber); + } + + public void test_ByteValueLongMin() { + long lNumber = Long.MIN_VALUE; + int result = 0; + BigDecimal bdNumber = new BigDecimal(lNumber); + byte bNumber = bdNumber.byteValue(); + assertTrue("incorrect byteValue", bNumber == result); + } + + public void test_ByteValueIntMin() { + int iNumber = Integer.MIN_VALUE; + int result = 0; + BigDecimal bdNumber = new BigDecimal(iNumber); + byte bNumber = bdNumber.byteValue(); + assertTrue("incorrect byteValue", bNumber == result); + } + + public void test_ByteValueIntMax() { + int iNumber = Integer.MAX_VALUE; + int result = -1; + BigDecimal bdNumber = new BigDecimal(iNumber); + byte bNumber = bdNumber.byteValue(); + assertTrue("incorrect byteValue", bNumber == result); + } + + public void test_ShortValueNeg() { + String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; + BigDecimal aNumber = new BigDecimal(a); + int result = 23449; + assertTrue("incorrect value", aNumber.shortValue() == result); + } + + public void test_ShortValuePos() { + String a = "123809648392384754573567356745735.63567890295784902768787678287E+21"; + BigDecimal aNumber = new BigDecimal(a); + int result = -23449; + assertTrue("incorrect value", aNumber.shortValue() == result); + } + + public void test_ShortValueExactNeg() { + String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; + BigDecimal aNumber = new BigDecimal(a); + try { + aNumber.shortValueExact(); + fail("java.lang.ArithmeticException isn't thrown after calling intValueExact"); + } catch (java.lang.ArithmeticException ae) { + // expected; + } + } + + public void test_ShortValueExactPos() { + String a = "123809648392384754573567356745735.63567890295784902768787678287E+21"; + BigDecimal aNumber = new BigDecimal(a); + try { + aNumber.shortValueExact(); + fail("java.lang.ArithmeticException isn't thrown after calling intValueExact"); + } catch (java.lang.ArithmeticException ae) { + // expected; + } + } + + public void test_ShortValueExactFloatNeg() { + BigDecimal aNumber = new BigDecimal("-32766.99999"); + try { + aNumber.shortValueExact(); + fail("java.lang.ArithmeticException isn't thrown after calling intValueExact"); + } catch (java.lang.ArithmeticException ae) { + // expected; + } + } + + public void test_ShortValueExactFloatPos() { + float a = 32767.99999F; + BigDecimal aNumber = new BigDecimal(a); + try { + aNumber.shortValueExact(); + fail("java.lang.ArithmeticException isn't thrown after calling intValueExact"); + } catch (java.lang.ArithmeticException ae) { + // expected; + } + } + + public void test_ShortValueExactLongPos() { + long a = 12345L; + BigDecimal aNumber = new BigDecimal(a); + short shNumber = aNumber.shortValueExact(); + assertTrue("incorrect value", shNumber == a); + } + + public void test_ShortValueExactLongNeg() { + long a = -12345L; + BigDecimal aNumber = new BigDecimal(a); + int iNumber = aNumber.shortValueExact(); + assertTrue("incorrect value", iNumber == a); + } + + public void test_stripTrailingZerosZeros() { + + BigDecimal bdNumber = new BigDecimal("0000000"); + BigDecimal result = bdNumber.stripTrailingZeros(); + assertEquals("incorrect value", result.unscaledValue(), bdNumber.unscaledValue()); + assertTrue("incorrect value", result.scale() == 0); + + bdNumber = new BigDecimal(0); + result = bdNumber.stripTrailingZeros(); + assertEquals("incorrect value", result.unscaledValue(), bdNumber.unscaledValue()); + assertTrue("incorrect value", result.scale() == 0); + + bdNumber = new BigDecimal(0.000000); + result = bdNumber.stripTrailingZeros(); + assertEquals("incorrect value", result.unscaledValue(), bdNumber.unscaledValue()); + assertTrue("incorrect value", result.scale() == 0); + } + + public void test_stripTrailingZeros() { + String s = "00000000100000000100000000.000000000100000000"; + int iScale = 10; + BigDecimal bdValue = new BigDecimal("1000000001000000000000000001"); + BigDecimal bdNumber = new BigDecimal(s); + BigDecimal bdResult = bdNumber.stripTrailingZeros(); + assertEquals("incorrect value", bdResult.unscaledValue(), bdValue.unscaledValue()); + assertTrue("incorrect value", bdResult.scale() == iScale); + + s = "1000.0"; + iScale = -3; + BigDecimal bd = new BigDecimal("1"); + bdNumber = new BigDecimal(s); + bdResult = bdNumber.stripTrailingZeros(); + assertEquals("incorrect value", bdResult.unscaledValue(), bd.unscaledValue()); + assertTrue("incorrect value", bdResult.scale() == iScale); + } +} diff --git a/luni/src/test/java/libcore/java/math/OldBigDecimalScaleOperationsTest.java b/luni/src/test/java/libcore/java/math/OldBigDecimalScaleOperationsTest.java new file mode 100644 index 00000000..b39899ce --- /dev/null +++ b/luni/src/test/java/libcore/java/math/OldBigDecimalScaleOperationsTest.java @@ -0,0 +1,99 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @author Elena Semukhina + * @version $Revision$ + */ + +package libcore.java.math; + +import java.math.BigDecimal; +import junit.framework.TestCase; + +public class OldBigDecimalScaleOperationsTest extends TestCase { + + public void testMovePointRightEx() { + BigDecimal a = new BigDecimal("12345.6789012345678901234567890123456789"); + BigDecimal res = a.movePointRight(10); + assertEquals("incorrect scale", 24, res.scale()); + assertEquals("incorrect value", "123456789012345.678901234567890123456789", res.toString()); + res = a.movePointRight(-50); + assertEquals("incorrect scale", 84, res.scale()); + assertEquals("incorrect value", "1.23456789012345678901234567890123456789E-46", res.toString()); + try { + a.movePointRight(Integer.MIN_VALUE + 2); + fail("ArithmeticException is not thrown"); + } catch (ArithmeticException expected) { + } + } + + // Throws OutOfMemoryError instead of ArithmeticException! + public void testMovePointRightEx2() { + BigDecimal a = new BigDecimal("123456789012345678901234567890123456789E25"); + try { + a.movePointRight(Integer.MAX_VALUE - 2); + fail("ArithmeticException is not thrown"); + } catch (ArithmeticException expected) { + } + } + + public void testScaleByPowerOfTenEx() { + BigDecimal a = new BigDecimal("12345.6789012345678901234567890123456789"); + BigDecimal res = a.movePointRight(10); + assertEquals("incorrect scale", 24, res.scale()); + assertEquals("incorrect value", "123456789012345.678901234567890123456789", res.toString()); + res = a.scaleByPowerOfTen(-50); + assertEquals("incorrect scale", 84, res.scale()); + assertEquals("incorrect value", "1.23456789012345678901234567890123456789E-46", res.toString()); + res = a.scaleByPowerOfTen(50); + assertEquals("incorrect scale", -16, res.scale()); + assertEquals("incorrect value", "1.23456789012345678901234567890123456789E+54", res.toString()); + try { + a.scaleByPowerOfTen(Integer.MIN_VALUE + 2); + fail("ArithmeticException is not thrown"); + } catch (ArithmeticException expected) { + } + a = new BigDecimal("123456789012345678901234567890123456789E25"); + try { + a.scaleByPowerOfTen(Integer.MAX_VALUE - 2); + fail("ArithmeticException is not thrown"); + } catch (ArithmeticException expected) { + } + } + + /** + * check that setScale with a scale greater to the existing scale does not + * change the value. + */ + public void testSetScale() { + BigDecimal x1 = new BigDecimal(1.23400); + BigDecimal x2 = x1.setScale(75); + + assertEquals(0, x1.compareTo(x2)); + assertEquals(0, x2.compareTo(x1)); + + x1.precision(); + + assertEquals(0, x1.compareTo(x2)); + assertEquals(0, x2.compareTo(x1)); + + x2.precision(); + + assertEquals(0, x1.compareTo(x2)); + assertEquals(0, x2.compareTo(x1)); + } +} diff --git a/luni/src/test/java/libcore/java/math/OldBigIntegerToStringTest.java b/luni/src/test/java/libcore/java/math/OldBigIntegerToStringTest.java new file mode 100644 index 00000000..cc761bcc --- /dev/null +++ b/luni/src/test/java/libcore/java/math/OldBigIntegerToStringTest.java @@ -0,0 +1,83 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +/** + * @author Elena Semukhina + * @version $Revision$ + */ + +package libcore.java.math; + +import java.math.BigInteger; +import junit.framework.TestCase; + +public class OldBigIntegerToStringTest extends TestCase { + + public void test_toString1() { + + String s = "0000000000"; + BigInteger bi = new BigInteger(s); + String sBI = bi.toString(); + assertEquals("toString method returns incorrect value instead of " + s, sBI, "0"); + } + + public void test_toString2() { + String s = "1234567890987654321"; + BigInteger bi = new BigInteger(s); + String sBI = bi.toString(); + assertEquals("toString method returns incorrect value instead of " + s, sBI, s); + } + + public void test_toString3() { + String s = "-1234567890987654321"; + BigInteger bi = new BigInteger(s); + String sBI = bi.toString(); + assertEquals("toString method returns incorrect value instead of " + s, sBI, s); + } + + public void test_toString4() { + String s = "12345678901234"; + long l = 12345678901234L; + BigInteger bi = BigInteger.valueOf(l); + String sBI = bi.toString(); + assertEquals("toString method returns incorrect value instead of " + s, sBI, s); + } + + public void test_toString5() { + String s = "-12345678901234"; + long l = -12345678901234L; + BigInteger bi = BigInteger.valueOf(l); + String sBI = bi.toString(); + assertEquals("toString method returns incorrect value instead of " + s, sBI, s); + } + + public void test_toString() { + byte aBytes[] = { + 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91 + }; + String s = "247856948764430159964673417020251"; + BigInteger bi = new BigInteger(aBytes); + String sBI = bi.toString(); + assertEquals("toString method returns incorrect value instead of " + s, sBI, s); + byte aBytes_[] = { + -12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91 + }; + s = "-238920881723209930210060613844133"; + bi = new BigInteger(aBytes_); + sBI = bi.toString(); + assertEquals("toString method returns incorrect value instead of " + s, sBI, s); + } +} diff --git a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigDecimalArithmeticTest.java b/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigDecimalArithmeticTest.java deleted file mode 100644 index 3ed940e0..00000000 --- a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigDecimalArithmeticTest.java +++ /dev/null @@ -1,2275 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package org.apache.harmony.math.tests.java.math; - -import junit.framework.TestCase; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.math.MathContext; -import java.math.RoundingMode; - -/** - * Class: java.math.BigDecimal - * Methods: add, subtract, multiply, divide - */ -public class BigDecimalArithmeticTest extends TestCase { - - /** - * Add two numbers of equal positive scales - */ - public void testAddEqualScalePosPos() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 10; - String b = "747233429293018787918347987234564568"; - int bScale = 10; - String c = "123121247898748373566323807282924555312937.1991359555"; - int cScale = 10; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.add(bNumber); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Add two numbers of equal positive scales using MathContext - */ - public void testAddMathContextEqualScalePosPos() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 10; - String b = "747233429293018787918347987234564568"; - int bScale = 10; - String c = "1.2313E+41"; - int cScale = -37; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - MathContext mc = new MathContext(5, RoundingMode.UP); - BigDecimal result = aNumber.add(bNumber, mc); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Add two numbers of equal negative scales - */ - public void testAddEqualScaleNegNeg() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = -10; - String b = "747233429293018787918347987234564568"; - int bScale = -10; - String c = "1.231212478987483735663238072829245553129371991359555E+61"; - int cScale = -10; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.add(bNumber); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Add two numbers of equal negative scales using MathContext - */ - public void testAddMathContextEqualScaleNegNeg() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = -10; - String b = "747233429293018787918347987234564568"; - int bScale = -10; - String c = "1.2312E+61"; - int cScale = -57; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - MathContext mc = new MathContext(5, RoundingMode.FLOOR); - BigDecimal result = aNumber.add(bNumber, mc); - assertEquals("incorrect value ", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Add two numbers of different scales; the first is positive - */ - public void testAddDiffScalePosNeg() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 15; - String b = "747233429293018787918347987234564568"; - int bScale = -10; - String c = "7472334294161400358170962860775454459810457634.781384756794987"; - int cScale = 15; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.add(bNumber); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Add two numbers of different scales using MathContext; the first is positive - */ - public void testAddMathContextDiffScalePosNeg() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 15; - String b = "747233429293018787918347987234564568"; - int bScale = -10; - String c = "7.47233429416141E+45"; - int cScale = -31; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - MathContext mc = new MathContext(15, RoundingMode.CEILING); - BigDecimal result = aNumber.add(bNumber, mc); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Add two numbers of different scales; the first is negative - */ - public void testAddDiffScaleNegPos() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = -15; - String b = "747233429293018787918347987234564568"; - int bScale = 10; - String c = "1231212478987482988429808779810457634781459480137916301878791834798.7234564568"; - int cScale = 10; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.add(bNumber); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Add two zeroes of different scales; the first is negative - */ - public void testAddDiffScaleZeroZero() { - String a = "0"; - int aScale = -15; - String b = "0"; - int bScale = 10; - String c = "0E-10"; - int cScale = 10; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.add(bNumber); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Non-trivial tests using MathContext: - */ - public void testAddMathContextNonTrivial() { - MathContext mc; - BigDecimal a, b, res; - - mc = new MathContext(17, RoundingMode.FLOOR); - a = new BigDecimal("123456789012345.678"); - b = new BigDecimal("100000000000000.009"); - assertEquals("incorrect value", "123456789012345.67", - a.round(mc).toString()); - assertEquals("incorrect value", "100000000000000.00", - b.round(mc).toString()); - assertEquals("incorrect value", "223456789012345.67", - a.round(mc).add(b.round(mc)).toString()); - res = a.add(b, mc); - assertEquals("incorrect value", "223456789012345.68", res.toString()); - - mc = new MathContext(33, RoundingMode.UNNECESSARY); - a = new BigDecimal("1234567890123456789012345678.9012395"); - b = new BigDecimal("1000000000000000090000000000.0000005"); - res = a.add(b, mc); - assertEquals("Incorrect value!", "2234567890123456879012345678.90124", res.toString()); - assertEquals("Incorrect scale!", 5, res.scale()); - assertEquals("Incorrect precision!", 33, res.precision()); - } - - /** - * Subtract two numbers of equal positive scales - */ - public void testSubtractEqualScalePosPos() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 10; - String b = "747233429293018787918347987234564568"; - int bScale = 10; - String c = "123121247898748224119637948679166971643339.7522230419"; - int cScale = 10; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.subtract(bNumber); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Subtract two numbers of equal positive scales using MathContext - */ - public void testSubtractMathContextEqualScalePosPos() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 10; - String b = "747233429293018787918347987234564568"; - int bScale = 10; - String c = "1.23121247898749E+41"; - int cScale = -27; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - MathContext mc = new MathContext(15, RoundingMode.CEILING); - BigDecimal result = aNumber.subtract(bNumber, mc); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Subtract two numbers of equal negative scales - */ - public void testSubtractEqualScaleNegNeg() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = -10; - String b = "747233429293018787918347987234564568"; - int bScale = -10; - String c = "1.231212478987482241196379486791669716433397522230419E+61"; - int cScale = -10; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.subtract(bNumber); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Subtract two numbers of different scales; the first is positive - */ - public void testSubtractDiffScalePosNeg() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 15; - String b = "747233429293018787918347987234564568"; - int bScale = -10; - String c = "-7472334291698975400195996883915836900189542365.218615243205013"; - int cScale = 15; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.subtract(bNumber); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Subtract two numbers of different scales using MathContext; - * the first is positive - */ - public void testSubtractMathContextDiffScalePosNeg() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 15; - String b = "747233429293018787918347987234564568"; - int bScale = -10; - String c = "-7.4723342916989754E+45"; - int cScale = -29; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - MathContext mc = new MathContext(17, RoundingMode.DOWN); - BigDecimal result = aNumber.subtract(bNumber, mc); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Subtract two numbers of different scales; the first is negative - */ - public void testSubtractDiffScaleNegPos() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = -15; - String b = "747233429293018787918347987234564568"; - int bScale = 10; - String c = "1231212478987482988429808779810457634781310033452057698121208165201.2765435432"; - int cScale = 10; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.subtract(bNumber); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Subtract two numbers of different scales using MathContext; - * the first is negative - */ - public void testSubtractMathContextDiffScaleNegPos() { - String a = "986798656676789766678767876078779810457634781384756794987"; - int aScale = -15; - String b = "747233429293018787918347987234564568"; - int bScale = 40; - String c = "9.867986566767897666787678760787798104576347813847567949870000000000000E+71"; - int cScale = -2; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - MathContext mc = new MathContext(70, RoundingMode.HALF_DOWN); - BigDecimal result = aNumber.subtract(bNumber, mc); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Non-trivial tests using MathContext: - */ - public void testSubtractMathContextNonTrivial() { - MathContext mc; - BigDecimal a, b, res; - - mc = new MathContext(17, RoundingMode.FLOOR); - a = new BigDecimal("12345678901234567.8"); - b = new BigDecimal("10000000000000000.9"); - assertEquals("incorrect value", "2345678901234567", - a.round(mc).subtract(b.round(mc)).toString()); - res = a.subtract(b, mc); - assertEquals("incorrect value", "2345678901234566.9", res.toString()); - assertEquals("Incorrect scale!", 1, res.scale()); - assertEquals("Incorrect precision!", 17, res.precision()); - - mc = new MathContext(33, RoundingMode.UNNECESSARY); - a = new BigDecimal("1234567890123456789012345678.9012395"); - b = new BigDecimal("1000000000000000090000000000.0000005"); - res = a.subtract(b, mc); - assertEquals("incorrect value", "234567890123456699012345678.901239", res.toString()); - assertEquals("Incorrect scale!", 6, res.scale()); - assertEquals("Incorrect precision!", 33, res.precision()); - } - - /** - * Multiply two numbers of positive scales - */ - public void testMultiplyScalePosPos() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 15; - String b = "747233429293018787918347987234564568"; - int bScale = 10; - String c = "92000312286217574978643009574114545567010139156902666284589309.1880727173060570190220616"; - int cScale = 25; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.multiply(bNumber); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Multiply two numbers of positive scales using MathContext - */ - public void testMultiplyMathContextScalePosPos() { - String a = "97665696756578755423325476545428779810457634781384756794987"; - int aScale = -25; - String b = "87656965586786097685674786576598865"; - int bScale = 10; - String c = "8.561078619600910561431314228543672720908E+108"; - int cScale = -69; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - MathContext mc = new MathContext(40, RoundingMode.HALF_DOWN); - BigDecimal result = aNumber.multiply(bNumber, mc); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Multiply two numbers of negative scales - */ - public void testMultiplyEqualScaleNegNeg() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = -15; - String b = "747233429293018787918347987234564568"; - int bScale = -10; - String c = "9.20003122862175749786430095741145455670101391569026662845893091880727173060570190220616E+111"; - int cScale = -25; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.multiply(bNumber); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Multiply two numbers of different scales - */ - public void testMultiplyDiffScalePosNeg() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 10; - String b = "747233429293018787918347987234564568"; - int bScale = -10; - String c = "920003122862175749786430095741145455670101391569026662845893091880727173060570190220616"; - int cScale = 0; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.multiply(bNumber); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Multiply two numbers of different scales using MathContext - */ - public void testMultiplyMathContextDiffScalePosNeg() { - String a = "987667796597975765768768767866756808779810457634781384756794987"; - int aScale = 100; - String b = "747233429293018787918347987234564568"; - int bScale = -70; - String c = "7.3801839465418518653942222612429081498248509257207477E+68"; - int cScale = -16; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - MathContext mc = new MathContext(53, RoundingMode.HALF_UP); - BigDecimal result = aNumber.multiply(bNumber, mc); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Multiply two numbers of different scales - */ - public void testMultiplyDiffScaleNegPos() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = -15; - String b = "747233429293018787918347987234564568"; - int bScale = 10; - String c = "9.20003122862175749786430095741145455670101391569026662845893091880727173060570190220616E+91"; - int cScale = -5; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.multiply(bNumber); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Multiply two numbers of different scales using MathContext - */ - public void testMultiplyMathContextDiffScaleNegPos() { - String a = "488757458676796558668876576576579097029810457634781384756794987"; - int aScale = -63; - String b = "747233429293018787918347987234564568"; - int bScale = 63; - String c = "3.6521591193960361339707130098174381429788164316E+98"; - int cScale = -52; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - MathContext mc = new MathContext(47, RoundingMode.HALF_UP); - BigDecimal result = aNumber.multiply(bNumber, mc); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Non-trivial tests using MathContext: - */ - public void testMultiplyMathContextNonTrivial() { - MathContext mc; - BigDecimal a, b, res; - - mc = new MathContext(17, RoundingMode.FLOOR); - a = new BigDecimal("92345678901234567.8"); - b = new BigDecimal("10000000000000000.9"); - res = a.round(mc).multiply(b.round(mc)); - assertEquals("incorrect value", "923456789012345670000000000000000", res.toString()); - res = res.round(mc); - assertEquals("incorrect value", "9.2345678901234567E+32", res.toString()); - res = a.multiply(b, mc); - assertEquals("incorrect value", "9.2345678901234576E+32", res.toString()); - assertEquals("Incorrect scale!", -16, res.scale()); - assertEquals("Incorrect precision!", 17, res.precision()); - } - - /** - * pow(int) - */ - public void testPow() { - String a = "123121247898748298842980"; - int aScale = 10; - int exp = 10; - String c = "8004424019039195734129783677098845174704975003788210729597" + - "4875206425711159855030832837132149513512555214958035390490" + - "798520842025826.594316163502809818340013610490541783276343" + - "6514490899700151256484355936102754469438371850240000000000"; - int cScale = 100; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal result = aNumber.pow(exp); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * pow(0) - */ - public void testPow0() { - String a = "123121247898748298842980"; - int aScale = 10; - int exp = 0; - String c = "1"; - int cScale = 0; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal result = aNumber.pow(exp); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * ZERO.pow(0) - */ - public void testZeroPow0() { - String c = "1"; - int cScale = 0; - BigDecimal result = BigDecimal.ZERO.pow(0); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Non-trivial tests using MathContext: - */ - public void testPowNonTrivial() { - BigDecimal a, b, res; - - a = new BigDecimal("100.9"); - try { - res = a.pow(-1); - fail("ArithmeticException is not thrown for negative exponent"); - } catch (ArithmeticException e) { - // expected - } - try { - res = a.pow(-103); - fail("ArithmeticException is not thrown for negative exponent"); - } catch (ArithmeticException e) { - // expected - } - } - - /** - * pow(int, MathContext) - */ - public void testPowMathContext() { - String a = "123121247898748298842980"; - int aScale = 10; - int exp = 10; - String c = "8.0044E+130"; - int cScale = -126; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - MathContext mc = new MathContext(5, RoundingMode.HALF_UP); - BigDecimal result = aNumber.pow(exp, mc); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", cScale, result.scale()); - } - - /** - * Non-trivial tests using MathContext: - */ - public void testPowMathContextNonTrivial() { - MathContext mc; - BigDecimal a, b, res; - - mc = new MathContext(7, RoundingMode.FLOOR); - a = new BigDecimal("1000000.9"); - assertEquals("incorrect value", "1.000000E+6000", - a.round(mc).pow(1000).round(mc).toString()); - res = a.pow(1000, mc); - assertEquals("incorrect value", "1.000900E+6000", res.toString()); - - mc = new MathContext(4, RoundingMode.FLOOR); - a = new BigDecimal("1000.9"); - assertEquals("incorrect value", "1.000E+3000", - a.round(mc).pow(1000).round(mc).toString()); - res = a.pow(1000, mc); - assertEquals("incorrect value", "2.458E+3000", res.toString()); - - mc = new MathContext(2, RoundingMode.UNNECESSARY); - a = new BigDecimal("1234"); - try { - res = a.pow(-2, mc); - fail("ArithmeticException is not thrown"); - } catch (ArithmeticException e) { - // expected - } - - a = new BigDecimal("100"); - mc = new MathContext(4, RoundingMode.UNNECESSARY); - res = a.pow(-2, mc); - assertEquals("incorrect value", "0.0001", res.toString()); - - a = new BigDecimal("1000.9"); - try { - mc = new MathContext(0, RoundingMode.FLOOR); - res = a.pow(-1, mc); - fail("ArithmeticException is not thrown for negative exponent and precision = 0"); - } catch (ArithmeticException e) { - // expected - } - - a = new BigDecimal("000.0001"); - try { - mc = new MathContext(0, RoundingMode.FLOOR); - res = a.pow(-1, mc); - fail("ArithmeticException is not thrown for negative exponent and precision = 0"); - } catch (ArithmeticException e) { - // expected - } - - a = new BigDecimal("1E-400"); - mc = new MathContext(4, RoundingMode.UNNECESSARY); - res = a.pow(-1, mc); - assertEquals("incorrect value", "1E+400", res.toString()); - -// Doesn't succeed against JDK of Sun!: -// mc = new MathContext(3, RoundingMode.FLOOR); -// a = new BigDecimal("100.9"); -// assertEquals("incorrect value", "1.00E+2000", -// a.round(mc).pow(1000).round(mc).toString()); -// res = a.pow(1000).round(mc); -// res = a.pow(1000, mc); -// assertEquals("incorrect value", "7.783E+2003", res.toString()); - } - - /** - * Divide by zero - */ - public void testDivideByZero() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 15; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = BigDecimal.valueOf(0L); - try { - aNumber.divide(bNumber); - fail("ArithmeticException has not been caught"); - } catch (ArithmeticException e) { - assertEquals("Improper exception message", "Division by zero", e.getMessage()); - } - } - - /** - * Divide with ROUND_UNNECESSARY - */ - public void testDivideExceptionRM() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 15; - String b = "747233429293018787918347987234564568"; - int bScale = 10; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - try { - aNumber.divide(bNumber, BigDecimal.ROUND_UNNECESSARY); - fail("ArithmeticException has not been caught"); - } catch (ArithmeticException e) { - assertEquals("Improper exception message", "Rounding necessary", e.getMessage()); - } - } - - /** - * Divide with invalid rounding mode - */ - public void testDivideExceptionInvalidRM() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 15; - String b = "747233429293018787918347987234564568"; - int bScale = 10; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - try { - aNumber.divide(bNumber, 100); - fail("IllegalArgumentException has not been caught"); - } catch (IllegalArgumentException e) { - assertEquals("Improper exception message", "Invalid rounding mode", e.getMessage()); - } - } - - /** - * Non-trivial tests using MathContext: - */ - public void testDivideINonTrivial() { - MathContext mc; - BigDecimal a, b, res; - - mc = new MathContext(17, RoundingMode.FLOOR); - a = new BigDecimal("12345678901234567E1234"); - b = new BigDecimal("1.23456789012345679"); - assertEquals("incorrect value", "1E+1250", - a.round(mc).divide(b.round(mc)).toString()); - res = a.divide(b, BigDecimal.ROUND_FLOOR); - assertEquals("incorrect value", "9.999999999999999E+1249", res.toString()); - - a = new BigDecimal("1234567890123456789012345678.9012395"); - b = new BigDecimal("6172839450617283945061728394.5061975"); - res = a.divide(b, BigDecimal.ROUND_UNNECESSARY); - assertEquals("incorrect value", "0.2000000", res.toString()); - - a = new BigDecimal("1234567890123456789012345678.9012395"); - b = new BigDecimal("1000000000000000090000000000.0000005"); - try { - res = a.divide(b, BigDecimal.ROUND_UNNECESSARY); - fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY"); - } catch (ArithmeticException e) { - // expected - } - } - - /** - * Divide: local variable exponent is less than zero - */ - public void testDivideExpLessZero() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 15; - String b = "747233429293018787918347987234564568"; - int bScale = 10; - String c = "1.64770E+10"; - int resScale = -5; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_CEILING); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: local variable exponent is equal to zero - */ - public void testDivideExpEqualsZero() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = -15; - String b = "747233429293018787918347987234564568"; - int bScale = 10; - String c = "1.64769459009933764189139568605273529E+40"; - int resScale = -5; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_CEILING); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: local variable exponent is greater than zero - */ - public void testDivideExpGreaterZero() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = -15; - String b = "747233429293018787918347987234564568"; - int bScale = 20; - String c = "1.647694590099337641891395686052735285121058381E+50"; - int resScale = -5; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_CEILING); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: remainder is zero - */ - public void testDivideRemainderIsZero() { - String a = "8311389578904553209874735431110"; - int aScale = -15; - String b = "237468273682987234567849583746"; - int bScale = 20; - String c = "3.5000000000000000000000000000000E+36"; - int resScale = -5; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_CEILING); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_UP, result is negative - */ - public void testDivideRoundUpNeg() { - String a = "-92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "7472334223847623782375469293018787918347987234564568"; - int bScale = 13; - String c = "-1.24390557635720517122423359799284E+53"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_UP); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_UP, result is positive - */ - public void testDivideRoundUpPos() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "7472334223847623782375469293018787918347987234564568"; - int bScale = 13; - String c = "1.24390557635720517122423359799284E+53"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_UP); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_DOWN, result is negative - */ - public void testDivideRoundDownNeg() { - String a = "-92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "7472334223847623782375469293018787918347987234564568"; - int bScale = 13; - String c = "-1.24390557635720517122423359799283E+53"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_DOWN); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_DOWN, result is positive - */ - public void testDivideRoundDownPos() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "7472334223847623782375469293018787918347987234564568"; - int bScale = 13; - String c = "1.24390557635720517122423359799283E+53"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_DOWN); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_FLOOR, result is positive - */ - public void testDivideRoundFloorPos() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "7472334223847623782375469293018787918347987234564568"; - int bScale = 13; - String c = "1.24390557635720517122423359799283E+53"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_FLOOR); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_FLOOR, result is negative - */ - public void testDivideRoundFloorNeg() { - String a = "-92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "7472334223847623782375469293018787918347987234564568"; - int bScale = 13; - String c = "-1.24390557635720517122423359799284E+53"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_FLOOR); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_CEILING, result is positive - */ - public void testDivideRoundCeilingPos() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "7472334223847623782375469293018787918347987234564568"; - int bScale = 13; - String c = "1.24390557635720517122423359799284E+53"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_CEILING); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_CEILING, result is negative - */ - public void testDivideRoundCeilingNeg() { - String a = "-92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "7472334223847623782375469293018787918347987234564568"; - int bScale = 13; - String c = "-1.24390557635720517122423359799283E+53"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_CEILING); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_HALF_UP, result is positive; distance = -1 - */ - public void testDivideRoundHalfUpPos() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "7472334223847623782375469293018787918347987234564568"; - int bScale = 13; - String c = "1.24390557635720517122423359799284E+53"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_HALF_UP); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_HALF_UP, result is negative; distance = -1 - */ - public void testDivideRoundHalfUpNeg() { - String a = "-92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "7472334223847623782375469293018787918347987234564568"; - int bScale = 13; - String c = "-1.24390557635720517122423359799284E+53"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_HALF_UP); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_HALF_UP, result is positive; distance = 1 - */ - public void testDivideRoundHalfUpPos1() { - String a = "92948782094488478231212478987482988798104576347813847567949855464535634534563456"; - int aScale = -24; - String b = "74723342238476237823754692930187879183479"; - int bScale = 13; - String c = "1.2439055763572051712242335979928354832010167729111113605E+76"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_HALF_UP); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_HALF_UP, result is negative; distance = 1 - */ - public void testDivideRoundHalfUpNeg1() { - String a = "-92948782094488478231212478987482988798104576347813847567949855464535634534563456"; - int aScale = -24; - String b = "74723342238476237823754692930187879183479"; - int bScale = 13; - String c = "-1.2439055763572051712242335979928354832010167729111113605E+76"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_HALF_UP); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_HALF_UP, result is negative; equidistant - */ - public void testDivideRoundHalfUpNeg2() { - String a = "-37361671119238118911893939591735"; - int aScale = 10; - String b = "74723342238476237823787879183470"; - int bScale = 15; - String c = "-1E+5"; - int resScale = -5; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_HALF_UP); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_HALF_DOWN, result is positive; distance = -1 - */ - public void testDivideRoundHalfDownPos() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "7472334223847623782375469293018787918347987234564568"; - int bScale = 13; - String c = "1.24390557635720517122423359799284E+53"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_HALF_DOWN); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_HALF_DOWN, result is negative; distance = -1 - */ - public void testDivideRoundHalfDownNeg() { - String a = "-92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "7472334223847623782375469293018787918347987234564568"; - int bScale = 13; - String c = "-1.24390557635720517122423359799284E+53"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_HALF_DOWN); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_HALF_DOWN, result is positive; distance = 1 - */ - public void testDivideRoundHalfDownPos1() { - String a = "92948782094488478231212478987482988798104576347813847567949855464535634534563456"; - int aScale = -24; - String b = "74723342238476237823754692930187879183479"; - int bScale = 13; - String c = "1.2439055763572051712242335979928354832010167729111113605E+76"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_HALF_DOWN); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_HALF_DOWN, result is negative; distance = 1 - */ - public void testDivideRoundHalfDownNeg1() { - String a = "-92948782094488478231212478987482988798104576347813847567949855464535634534563456"; - int aScale = -24; - String b = "74723342238476237823754692930187879183479"; - int bScale = 13; - String c = "-1.2439055763572051712242335979928354832010167729111113605E+76"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_HALF_DOWN); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_HALF_UP, result is negative; equidistant - */ - public void testDivideRoundHalfDownNeg2() { - String a = "-37361671119238118911893939591735"; - int aScale = 10; - String b = "74723342238476237823787879183470"; - int bScale = 15; - String c = "0E+5"; - int resScale = -5; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_HALF_DOWN); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_HALF_EVEN, result is positive; distance = -1 - */ - public void testDivideRoundHalfEvenPos() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "7472334223847623782375469293018787918347987234564568"; - int bScale = 13; - String c = "1.24390557635720517122423359799284E+53"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_HALF_EVEN); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_HALF_EVEN, result is negative; distance = -1 - */ - public void testDivideRoundHalfEvenNeg() { - String a = "-92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "7472334223847623782375469293018787918347987234564568"; - int bScale = 13; - String c = "-1.24390557635720517122423359799284E+53"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_HALF_EVEN); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_HALF_EVEN, result is positive; distance = 1 - */ - public void testDivideRoundHalfEvenPos1() { - String a = "92948782094488478231212478987482988798104576347813847567949855464535634534563456"; - int aScale = -24; - String b = "74723342238476237823754692930187879183479"; - int bScale = 13; - String c = "1.2439055763572051712242335979928354832010167729111113605E+76"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_HALF_EVEN); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_HALF_EVEN, result is negative; distance = 1 - */ - public void testDivideRoundHalfEvenNeg1() { - String a = "-92948782094488478231212478987482988798104576347813847567949855464535634534563456"; - int aScale = -24; - String b = "74723342238476237823754692930187879183479"; - int bScale = 13; - String c = "-1.2439055763572051712242335979928354832010167729111113605E+76"; - int resScale = -21; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_HALF_EVEN); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide: rounding mode is ROUND_HALF_EVEN, result is negative; equidistant - */ - public void testDivideRoundHalfEvenNeg2() { - String a = "-37361671119238118911893939591735"; - int aScale = 10; - String b = "74723342238476237823787879183470"; - int bScale = 15; - String c = "0E+5"; - int resScale = -5; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, resScale, BigDecimal.ROUND_HALF_EVEN); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Non-trivial tests using MathContext: - */ - public void testDivideIINonTrivial() { - MathContext mc; - BigDecimal a, b, res; - - mc = new MathContext(17, RoundingMode.FLOOR); - a = new BigDecimal("12345678901234567E1234"); - b = new BigDecimal("1.23456789012345679"); - res = a.divide(b, -1220, BigDecimal.ROUND_FLOOR); - assertEquals("incorrect value", "9.99999999999999927099999343899E+1249", res.toString()); - - a = new BigDecimal("1234567890123456789012345678.9012395"); - b = new BigDecimal("6172839450617283945061728394.5061975"); - res = a.divide(b, 1, BigDecimal.ROUND_UNNECESSARY); - assertEquals("incorrect value", "0.2", res.toString()); - - a = new BigDecimal("1234567890123456789012345678.9012395"); - b = new BigDecimal("6172839450617283945061728394.5061975"); - try { - res = a.divide(b, 0, BigDecimal.ROUND_UNNECESSARY); - fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY"); - } catch (ArithmeticException e) { - // expected - } - } - - /** - * Divide to BigDecimal - */ - public void testDivideBigDecimal1() { - String a = "-37361671119238118911893939591735"; - int aScale = 10; - String b = "74723342238476237823787879183470"; - int bScale = 15; - String c = "-5E+4"; - int resScale = -4; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Divide to BigDecimal - */ - public void testDivideBigDecimal2() { - String a = "-37361671119238118911893939591735"; - int aScale = 10; - String b = "74723342238476237823787879183470"; - int bScale = -15; - String c = "-5E-26"; - int resScale = 26; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * divide(BigDecimal, scale, RoundingMode) - */ - public void testDivideBigDecimalScaleRoundingModeUP() { - String a = "-37361671119238118911893939591735"; - int aScale = 10; - String b = "74723342238476237823787879183470"; - int bScale = -15; - int newScale = 31; - RoundingMode rm = RoundingMode.UP; - String c = "-5.00000E-26"; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, newScale, rm); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", newScale, result.scale()); - } - - /** - * divide(BigDecimal, scale, RoundingMode) - */ - public void testDivideBigDecimalScaleRoundingModeDOWN() { - String a = "-37361671119238118911893939591735"; - int aScale = 10; - String b = "74723342238476237823787879183470"; - int bScale = 15; - int newScale = 31; - RoundingMode rm = RoundingMode.DOWN; - String c = "-50000.0000000000000000000000000000000"; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, newScale, rm); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", newScale, result.scale()); - } - - /** - * divide(BigDecimal, scale, RoundingMode) - */ - public void testDivideBigDecimalScaleRoundingModeCEILING() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 100; - String b = "74723342238476237823787879183470"; - int bScale = 15; - int newScale = 45; - RoundingMode rm = RoundingMode.CEILING; - String c = "1E-45"; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, newScale, rm); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", newScale, result.scale()); - } - - /** - * divide(BigDecimal, scale, RoundingMode) - */ - public void testDivideBigDecimalScaleRoundingModeFLOOR() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 100; - String b = "74723342238476237823787879183470"; - int bScale = 15; - int newScale = 45; - RoundingMode rm = RoundingMode.FLOOR; - String c = "0E-45"; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, newScale, rm); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", newScale, result.scale()); - } - - /** - * divide(BigDecimal, scale, RoundingMode) - */ - public void testDivideBigDecimalScaleRoundingModeHALF_UP() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = -51; - String b = "74723342238476237823787879183470"; - int bScale = 45; - int newScale = 3; - RoundingMode rm = RoundingMode.HALF_UP; - String c = "50000260373164286401361913262100972218038099522752460421" + - "05959924024355721031761947728703598332749334086415670525" + - "3761096961.670"; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, newScale, rm); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", newScale, result.scale()); - } - - /** - * divide(BigDecimal, scale, RoundingMode) - */ - public void testDivideBigDecimalScaleRoundingModeHALF_DOWN() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 5; - String b = "74723342238476237823787879183470"; - int bScale = 15; - int newScale = 7; - RoundingMode rm = RoundingMode.HALF_DOWN; - String c = "500002603731642864013619132621009722.1803810"; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, newScale, rm); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", newScale, result.scale()); - } - - /** - * divide(BigDecimal, scale, RoundingMode) - */ - public void testDivideBigDecimalScaleRoundingModeHALF_EVEN() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 5; - String b = "74723342238476237823787879183470"; - int bScale = 15; - int newScale = 7; - RoundingMode rm = RoundingMode.HALF_EVEN; - String c = "500002603731642864013619132621009722.1803810"; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, newScale, rm); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", newScale, result.scale()); - } - - /** - * Non-trivial tests using MathContext: - */ - // Has a rounding problem. seems like the precision is - // 1 too small and cuts off the last digit. also this test might - // not be correct. The name implies that scale should be used. - public void testDivideScaleRoundingModeNonTrivial() { - MathContext mc; - BigDecimal a, b, res; - - mc = new MathContext(17, RoundingMode.FLOOR); - a = new BigDecimal("12345678901234567.1"); - b = new BigDecimal("12345678901234567.9"); - assertEquals("incorrect value", "1", - a.round(mc).divide(b.round(mc)).toString()); - res = a.divide(b, mc); - assertEquals("incorrect value", "0.99999999999999993", res.toString()); - - mc = new MathContext(13, RoundingMode.UNNECESSARY); - a = new BigDecimal("1234567890123456789012345678.9012395"); - b = new BigDecimal("6172839450617283945061728394.5061975"); - res = a.divide(b, mc); - assertEquals("incorrect value", "0.2", res.toString()); - - mc = new MathContext(33, RoundingMode.UNNECESSARY); - a = new BigDecimal("1234567890123456789012345678.9012395"); - b = new BigDecimal("1000000000000000090000000000.0000005"); - try { - res = a.divide(b, mc); - fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY"); - } catch (ArithmeticException e) { - // expected - } - } - - /** - * divide(BigDecimal, MathContext) - */ - public void testDivideBigDecimalScaleMathContextUP() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 15; - String b = "748766876876723342238476237823787879183470"; - int bScale = 10; - int precision = 21; - RoundingMode rm = RoundingMode.UP; - MathContext mc = new MathContext(precision, rm); - String c = "49897861180.2562512996"; - int resScale = 10; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, mc); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * divide(BigDecimal, MathContext) - */ - public void testDivideBigDecimalScaleMathContextDOWN() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 15; - String b = "748766876876723342238476237823787879183470"; - int bScale = 70; - int precision = 21; - RoundingMode rm = RoundingMode.DOWN; - MathContext mc = new MathContext(precision, rm); - String c = "4.98978611802562512995E+70"; - int resScale = -50; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, mc); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * divide(BigDecimal, MathContext) - */ - public void testDivideBigDecimalScaleMathContextCEILING() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 15; - String b = "748766876876723342238476237823787879183470"; - int bScale = 70; - int precision = 21; - RoundingMode rm = RoundingMode.CEILING; - MathContext mc = new MathContext(precision, rm); - String c = "4.98978611802562512996E+70"; - int resScale = -50; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, mc); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * divide(BigDecimal, MathContext) - */ - public void testDivideBigDecimalScaleMathContextFLOOR() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 15; - String b = "748766876876723342238476237823787879183470"; - int bScale = 70; - int precision = 21; - RoundingMode rm = RoundingMode.FLOOR; - MathContext mc = new MathContext(precision, rm); - String c = "4.98978611802562512995E+70"; - int resScale = -50; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, mc); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * divide(BigDecimal, MathContext) - */ - public void testDivideBigDecimalScaleMathContextHALF_UP() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 45; - String b = "134432345432345748766876876723342238476237823787879183470"; - int bScale = 70; - int precision = 21; - RoundingMode rm = RoundingMode.HALF_UP; - MathContext mc = new MathContext(precision, rm); - String c = "2.77923185514690367475E+26"; - int resScale = -6; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, mc); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * divide(BigDecimal, MathContext) - */ - public void testDivideBigDecimalScaleMathContextHALF_DOWN() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 45; - String b = "134432345432345748766876876723342238476237823787879183470"; - int bScale = 70; - int precision = 21; - RoundingMode rm = RoundingMode.HALF_DOWN; - MathContext mc = new MathContext(precision, rm); - String c = "2.77923185514690367475E+26"; - int resScale = -6; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, mc); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * divide(BigDecimal, MathContext) - */ - public void testDivideBigDecimalScaleMathContextHALF_EVEN() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 45; - String b = "134432345432345748766876876723342238476237823787879183470"; - int bScale = 70; - int precision = 21; - RoundingMode rm = RoundingMode.HALF_EVEN; - MathContext mc = new MathContext(precision, rm); - String c = "2.77923185514690367475E+26"; - int resScale = -6; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, mc); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Non-trivial tests using MathContext: - */ - // The same test and the same problem like testDivideScaleRoundingModeNonTrivial. - public void testDivideMathContextNonTrivial() { - MathContext mc; - BigDecimal a, b, res; - -// FAILS AGAINST RI!: -// mc = new MathContext(6, RoundingMode.FLOOR); -// a = new BigDecimal("12345.1"); -// b = new BigDecimal("12345.9"); -// assertEquals("incorrect value", "1", -// a.round(mc).divide(b.round(mc)).toString()); -// res = a.divide(b, mc); -// assertEquals("incorrect value", "0.99993", res.toString()); - - mc = new MathContext(5, RoundingMode.FLOOR); - a = new BigDecimal("12345.1"); - b = new BigDecimal("12345.9"); - assertEquals("incorrect value", "1", - a.round(mc).divide(b.round(mc)).toString()); - res = a.divide(b, mc); - assertEquals("incorrect value", "0.99993", res.toString()); - - mc = new MathContext(17, RoundingMode.FLOOR); - a = new BigDecimal("12345678901234567.1"); - b = new BigDecimal("12345678901234567.9"); - assertEquals("incorrect value", "1", - a.round(mc).divide(b.round(mc)).toString()); - res = a.divide(b, mc); - assertEquals("incorrect value", "0.99999999999999993", res.toString()); - assertEquals("incorrect value", res.round(mc).toString(), res.toString()); - - mc = new MathContext(13, RoundingMode.UNNECESSARY); - a = new BigDecimal("1234567890123456789012345678.9012395"); - b = new BigDecimal("6172839450617283945061728394.5061975"); - res = a.divide(b, mc); - assertEquals("incorrect value", "0.2", res.toString()); - - mc = new MathContext(33, RoundingMode.UNNECESSARY); - a = new BigDecimal("1234567890123456789012345678.9012395"); - b = new BigDecimal("1000000000000000090000000000.0000005"); - try { - res = a.divide(b, mc); - fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY"); - } catch (ArithmeticException e) { - // expected - } - } - - /** - * divideToIntegralValue(BigDecimal) - */ - public void testDivideToIntegralValue() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 45; - String b = "134432345432345748766876876723342238476237823787879183470"; - int bScale = 70; - String c = "277923185514690367474770683"; - int resScale = 0; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divideToIntegralValue(bNumber); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - public void testDivideToIntegralValueByZero() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 45; - String b = "0"; - int bScale = -70; - String res = "277923185514690367474770683"; - int resScale = 0; - String rem = "1.3032693871288309587558885943391070087960319452465789990E-15"; - int remScale = 70; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - try { - BigDecimal result = aNumber.divideToIntegralValue(bNumber); - fail("ArithmeticException not thrown for division by 0"); - } catch (ArithmeticException e) { - // expected - } - } - - /** - * divideToIntegralValue(BigDecimal, MathContext) - */ - public void testDivideToIntegralValueMathContextUP() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 45; - String b = "134432345432345748766876876723342238476237823787879183470"; - int bScale = 70; - int precision = 32; - RoundingMode rm = RoundingMode.UP; - MathContext mc = new MathContext(precision, rm); - String c = "277923185514690367474770683"; - int resScale = 0; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divideToIntegralValue(bNumber, mc); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * divideToIntegralValue(BigDecimal, MathContext) - */ - public void testDivideToIntegralValueMathContextDOWN() { - String a = "3736186567876876578956958769675785435673453453653543654354365435675671119238118911893939591735"; - int aScale = 45; - String b = "134432345432345748766876876723342238476237823787879183470"; - int bScale = 70; - int precision = 75; - RoundingMode rm = RoundingMode.DOWN; - MathContext mc = new MathContext(precision, rm); - String c = "2.7792318551469036747477068339450205874992634417590178670822889E+62"; - int resScale = -1; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divideToIntegralValue(bNumber, mc); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Non-trivial tests using MathContext: - */ - public void testDivideToIntegralValueMathContextNonTrivial() { - MathContext mc; - BigDecimal a, b, res; - - a = new BigDecimal("92345678901234567.8"); - b = new BigDecimal("43"); - res = a.multiply(b); - assertEquals("incorrect value", "3970864192753086415.4", res.toString()); - - mc = new MathContext(20, RoundingMode.DOWN); - a = new BigDecimal("3970864192753086415.4"); - b = new BigDecimal("92345678901234567.8"); - b = new BigDecimal("92345678901234567.8001"); - assertEquals("incorrect value", "43", - a.round(mc).divideToIntegralValue(b.round(mc)).toString()); - res = a.divideToIntegralValue(b, mc); - assertEquals("incorrect value", "42", res.toString()); - -// mc = new MathContext(1, RoundingMode.DOWN); -// res = a.divideToIntegralValue(b, mc); -// assertEquals("incorrect value", "42", res.toString()); - - - mc = new MathContext(17, RoundingMode.FLOOR); - a = new BigDecimal("518518513851851830"); - b = new BigDecimal("12345678901234567.9"); - assertEquals("incorrect value", "42", - a.round(mc).divideToIntegralValue(b.round(mc)).toString()); - res = a.divideToIntegralValue(b, mc); - assertEquals("incorrect value", "41", res.toString()); - } - - /** - * divideAndRemainder(BigDecimal) - */ - public void testDivideAndRemainder1() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 45; - String b = "134432345432345748766876876723342238476237823787879183470"; - int bScale = 70; - String res = "277923185514690367474770683"; - int resScale = 0; - String rem = "1.3032693871288309587558885943391070087960319452465789990E-15"; - int remScale = 70; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result[] = aNumber.divideAndRemainder(bNumber); - assertEquals("incorrect quotient value", res, result[0].toString()); - assertEquals("incorrect quotient scale", resScale, result[0].scale()); - assertEquals("incorrect remainder value", rem, result[1].toString()); - assertEquals("incorrect remainder scale", remScale, result[1].scale()); - } - - /** - * divideAndRemainder(BigDecimal) - */ - public void testDivideAndRemainder2() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = -45; - String b = "134432345432345748766876876723342238476237823787879183470"; - int bScale = 70; - String res = "2779231855146903674747706830969461168692256919247547952" + - "2608549363170374005512836303475980101168105698072946555" + - "6862849"; - int resScale = 0; - String rem = "3.4935796954060524114470681810486417234751682675102093970E-15"; - int remScale = 70; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result[] = aNumber.divideAndRemainder(bNumber); - assertEquals("incorrect quotient value", res, result[0].toString()); - assertEquals("incorrect quotient scale", resScale, result[0].scale()); - assertEquals("incorrect remainder value", rem, result[1].toString()); - assertEquals("incorrect remainder scale", remScale, result[1].scale()); - } - - /** - * divideAndRemainder(BigDecimal) - */ - public void testDivideAndRemainderByZero() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 45; - String b = "0"; - int bScale = -70; - String res = "277923185514690367474770683"; - int resScale = 0; - String rem = "1.3032693871288309587558885943391070087960319452465789990E-15"; - int remScale = 70; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - try { - BigDecimal result[] = aNumber.divideAndRemainder(bNumber); - fail("ArithmeticException not thrown for division by 0"); - } catch (ArithmeticException e) { - // expected - } - } - - /** - * divideAndRemainder(BigDecimal, MathContext) - */ - public void testDivideAndRemainderMathContextUP() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 45; - String b = "134432345432345748766876876723342238476237823787879183470"; - int bScale = 70; - int precision = 75; - RoundingMode rm = RoundingMode.UP; - MathContext mc = new MathContext(precision, rm); - String res = "277923185514690367474770683"; - int resScale = 0; - String rem = "1.3032693871288309587558885943391070087960319452465789990E-15"; - int remScale = 70; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result[] = aNumber.divideAndRemainder(bNumber, mc); - assertEquals("incorrect quotient value", res, result[0].toString()); - assertEquals("incorrect quotient scale", resScale, result[0].scale()); - assertEquals("incorrect remainder value", rem, result[1].toString()); - assertEquals("incorrect remainder scale", remScale, result[1].scale()); - } - - /** - * divideAndRemainder(BigDecimal, MathContext) - */ - public void testDivideAndRemainderMathContextDOWN() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 45; - String b = "134432345432345748766876876723342238476237823787879183470"; - int bScale = 20; - int precision = 15; - RoundingMode rm = RoundingMode.DOWN; - MathContext mc = new MathContext(precision, rm); - String res = "0E-25"; - int resScale = 25; - String rem = "3736186567876.876578956958765675671119238118911893939591735"; - int remScale = 45; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result[] = aNumber.divideAndRemainder(bNumber, mc); - assertEquals("incorrect quotient value", res, result[0].toString()); - assertEquals("incorrect quotient scale", resScale, result[0].scale()); - assertEquals("incorrect remainder value", rem, result[1].toString()); - assertEquals("incorrect remainder scale", remScale, result[1].scale()); - } - - /** - * Non-trivial tests using MathContext: - */ - public void testDivideAndRemainderMathContextNonTrivial() { - MathContext mc; - BigDecimal a, b, res[]; - - mc = new MathContext(13, RoundingMode.FLOOR); - a = new BigDecimal("12345678901234567.1"); - b = new BigDecimal("12345678901234567.9"); - assertEquals("incorrect value", "0E+4", - a.round(mc).divideAndRemainder(b.round(mc))[1].toString()); - res = a.divideAndRemainder(b, mc); - assertEquals("incorrect value", "12345678901234567.1", res[1].toString()); - - mc = new MathContext(1, RoundingMode.UNNECESSARY); - a = new BigDecimal("6172839450617283945061728394.5061976"); - b = new BigDecimal("1234567890123456789012345678.9012395"); - res = a.divideAndRemainder(b, mc); - assertEquals("incorrect value", "1E-7", res[1].toString()); - - mc = new MathContext(3, RoundingMode.UNNECESSARY); - a = new BigDecimal("6172839450617283945061728394.6000000"); - b = new BigDecimal("1234567890123456789012345678.9012395"); - try { - res = a.divideAndRemainder(b, mc); - assertEquals("incorrect value", "0.0938025", res[1].toString()); - assertEquals("incorrect value", "0.09", res[1].round(mc).toString()); -// fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY"); - } catch (ArithmeticException e) { - // expected - } - } - - /** - * remainder(BigDecimal) - */ - public void testRemainder1() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 45; - String b = "134432345432345748766876876723342238476237823787879183470"; - int bScale = 10; - String res = "3736186567876.876578956958765675671119238118911893939591735"; - int resScale = 45; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.remainder(bNumber); - assertEquals("incorrect quotient value", res, result.toString()); - assertEquals("incorrect quotient scale", resScale, result.scale()); - } - - /** - * remainder(BigDecimal) - */ - public void testRemainder2() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = -45; - String b = "134432345432345748766876876723342238476237823787879183470"; - int bScale = 10; - String res = "1149310942946292909508821656680979993738625937.2065885780"; - int resScale = 10; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.remainder(bNumber); - assertEquals("incorrect quotient value", res, result.toString()); - assertEquals("incorrect quotient scale", resScale, result.scale()); - } - - public void testRemainderByZero() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 45; - String b = "0"; - int bScale = -70; - String res = "277923185514690367474770683"; - int resScale = 0; - String rem = "1.3032693871288309587558885943391070087960319452465789990E-15"; - int remScale = 70; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - try { - BigDecimal result = aNumber.remainder(bNumber); - fail("ArithmeticException not thrown for division by 0"); - } catch (ArithmeticException e) { - // expected - } - } - /** - * remainder(BigDecimal, MathContext) - */ - public void testRemainderMathContextHALF_UP() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 45; - String b = "134432345432345748766876876723342238476237823787879183470"; - int bScale = 10; - int precision = 15; - RoundingMode rm = RoundingMode.HALF_UP; - MathContext mc = new MathContext(precision, rm); - String res = "3736186567876.876578956958765675671119238118911893939591735"; - int resScale = 45; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.remainder(bNumber, mc); - assertEquals("incorrect quotient value", res, result.toString()); - assertEquals("incorrect quotient scale", resScale, result.scale()); - } - - /** - * remainder(BigDecimal, MathContext) - */ - public void testRemainderMathContextHALF_DOWN() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = -45; - String b = "134432345432345748766876876723342238476237823787879183470"; - int bScale = 10; - int precision = 75; - RoundingMode rm = RoundingMode.HALF_DOWN; - MathContext mc = new MathContext(precision, rm); - String res = "1149310942946292909508821656680979993738625937.2065885780"; - int resScale = 10; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.remainder(bNumber, mc); - assertEquals("incorrect quotient value", res, result.toString()); - assertEquals("incorrect quotient scale", resScale, result.scale()); - } - - /** - * Non-trivial tests using MathContext: - */ - public void testRemainderMathContextNonTrivial() { - MathContext mc; - BigDecimal a, b, res; - - mc = new MathContext(13, RoundingMode.DOWN); - a = new BigDecimal("12345678901234567.1"); - b = new BigDecimal("12345678901234567.9"); - assertEquals("incorrect value", "0E+4", - a.round(mc).divideAndRemainder(b.round(mc))[1].toString()); - res = a.remainder(b, mc); - assertEquals("incorrect value", "12345678901234567.1", res.toString()); - - mc = new MathContext(1, RoundingMode.UNNECESSARY); - a = new BigDecimal("6172839450617283945061728394.5061976"); - b = new BigDecimal("1234567890123456789012345678.9012395"); - res = a.remainder(b, mc); - assertEquals("incorrect value", "1E-7", res.toString()); - - mc = new MathContext(3, RoundingMode.UNNECESSARY); - a = new BigDecimal("6172839450617283945061728394.6000000"); - b = new BigDecimal("1234567890123456789012345678.9012395"); - try { - res = a.remainder(b, mc); - assertEquals("incorrect value", "0.0938025", res.toString()); - assertEquals("incorrect value", "0.09", res.round(mc).toString()); -// fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY"); - } catch (ArithmeticException e) { - // expected - } - } - - /** - * round(BigDecimal, MathContext) - */ - public void testRoundMathContextHALF_DOWN() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = -45; - int precision = 75; - RoundingMode rm = RoundingMode.HALF_DOWN; - MathContext mc = new MathContext(precision, rm); - String res = "3.736186567876876578956958765675671119238118911893939591735E+102"; - int resScale = -45; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal result = aNumber.round(mc); - assertEquals("incorrect quotient value", res, result.toString()); - assertEquals("incorrect quotient scale", resScale, result.scale()); - } - - /** - * round(BigDecimal, MathContext) - */ - public void testRoundMathContextHALF_UP() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 45; - int precision = 15; - RoundingMode rm = RoundingMode.HALF_UP; - MathContext mc = new MathContext(precision, rm); - String res = "3736186567876.88"; - int resScale = 2; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal result = aNumber.round(mc); - assertEquals("incorrect quotient value", res, result.toString()); - assertEquals("incorrect quotient scale", resScale, result.scale()); - } - - /** - * round(BigDecimal, MathContext) when precision = 0 - */ - public void testRoundMathContextPrecision0() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 45; - int precision = 0; - RoundingMode rm = RoundingMode.HALF_UP; - MathContext mc = new MathContext(precision, rm); - String res = "3736186567876.876578956958765675671119238118911893939591735"; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal result = aNumber.round(mc); - assertEquals("incorrect quotient value", res, result.toString()); - assertEquals("incorrect quotient scale", aScale, result.scale()); - } - - public void testRoundNonTrivial() { - MathContext mc; - String biStr = new String( "12345678901234567890123456789012345.0E+10"); - String nbiStr = new String("-12345678901234567890123456789012345.E+10"); - BigDecimal bd; - - mc = new MathContext(17, RoundingMode.FLOOR); - bd = new BigDecimal(new BigInteger("123456789012345678"), 3, mc); - assertEquals("incorrect value", "123456789012345.67", bd.toString()); - - mc = new MathContext(31, RoundingMode.UP); - bd = (new BigDecimal(biStr)).round(mc); - assertEquals("incorrect value", "1.234567890123456789012345678902E+44", bd.toString()); - bd = (new BigDecimal(nbiStr)).round(mc); - assertEquals("incorrect value", "-1.234567890123456789012345678902E+44", bd.toString()); - - mc = new MathContext(28, RoundingMode.DOWN); - bd = (new BigDecimal(biStr)).round(mc); - assertEquals("incorrect value", "1.234567890123456789012345678E+44", bd.toString()); - bd = (new BigDecimal(nbiStr)).round(mc); - assertEquals("incorrect value", "-1.234567890123456789012345678E+44", bd.toString()); - - mc = new MathContext(33, RoundingMode.CEILING); - bd = (new BigDecimal(biStr)).round(mc); - assertEquals("incorrect value", "1.23456789012345678901234567890124E+44", bd.toString()); - bd = (new BigDecimal(nbiStr)).round(mc); - assertEquals("incorrect value", "-1.23456789012345678901234567890123E+44", bd.toString()); - - mc = new MathContext(34, RoundingMode.UNNECESSARY); - try { - bd = (new BigDecimal(biStr)).round(mc); - fail("No ArithmeticException for RoundingMode.UNNECESSARY"); - } catch (ArithmeticException e) { - // expected - } - try { - bd = (new BigDecimal(nbiStr)).round(mc); - fail("No ArithmeticException for RoundingMode.UNNECESSARY"); - } catch (ArithmeticException e) { - // expected - } - - mc = new MathContext(7, RoundingMode.FLOOR); - bd = new BigDecimal("1000000.9", mc); - assertEquals("incorrect value", "1000000", bd.toString()); - } - - /** - * ulp() of a positive BigDecimal - */ - public void testUlpPos() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = -45; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal result = aNumber.ulp(); - String res = "1E+45"; - int resScale = -45; - assertEquals("incorrect value", res, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * ulp() of a negative BigDecimal - */ - public void testUlpNeg() { - String a = "-3736186567876876578956958765675671119238118911893939591735"; - int aScale = 45; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal result = aNumber.ulp(); - String res = "1E-45"; - int resScale = 45; - assertEquals("incorrect value", res, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * ulp() of a negative BigDecimal - */ - public void testUlpZero() { - String a = "0"; - int aScale = 2; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal result = aNumber.ulp(); - String res = "0.01"; - int resScale = 2; - assertEquals("incorrect value", res, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - -// ANDROID ADDED - - /** - * @tests java.math.BigDecimal#add(java.math.BigDecimal) - */ - public void test_addBigDecimal() { - BigDecimal add1 = new BigDecimal("23.456"); - BigDecimal add2 = new BigDecimal("3849.235"); - BigDecimal sum = add1.add(add2); - assertTrue("the sum of 23.456 + 3849.235 is wrong", sum.unscaledValue().toString().equals( - "3872691") - && sum.scale() == 3); - assertTrue("the sum of 23.456 + 3849.235 is not printed correctly", sum.toString().equals( - "3872.691")); - BigDecimal add3 = new BigDecimal(12.34E02D); - assertTrue("the sum of 23.456 + 12.34E02 is not printed correctly", (add1.add(add3)) - .toString().equals("1257.456")); - } - - /** - * @tests java.math.BigDecimal#divide(java.math.BigDecimal, - * java.math.MathContext) divide(BigDecimal, RoundingMode) - */ - public void test_DivideBigDecimalRoundingModeUP() { - String a = "-37361671119238118911893939591735"; - String b = "74723342238476237823787879183470"; - RoundingMode rm = RoundingMode.UP; - String c = "-1"; - BigDecimal aNumber = new BigDecimal(new BigInteger(a)); - BigDecimal bNumber = new BigDecimal(new BigInteger(b)); - BigDecimal result = aNumber.divide(bNumber, rm); - assertEquals("incorrect value", c, result.toString()); - } - - /** - * @tests java.math.BigDecimal#divide(java.math.BigDecimal, - * java.math.RoundingMode) divide(BigDecimal, RoundingMode) - */ - public void test_DivideBigDecimalRoundingModeDOWN() { - String a = "-37361671119238118911893939591735"; - String b = "74723342238476237823787879183470"; - RoundingMode rm = RoundingMode.DOWN; - String c = "0"; - BigDecimal aNumber = new BigDecimal(new BigInteger(a)); - BigDecimal bNumber = new BigDecimal(new BigInteger(b)); - BigDecimal result = aNumber.divide(bNumber, rm); - assertEquals("incorrect value", c, result.toString()); - } - - /** - * @tests java.math.BigDecimal#divide(java.math.BigDecimal, - * java.math.RoundingMode) divide(BigDecimal, RoundingMode) - */ - public void test_DivideBigDecimalRoundingModeCEILING() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - String b = "74723342238476237823787879183470"; - RoundingMode rm = RoundingMode.CEILING; - String c = "50000260373164286401361914"; - BigDecimal aNumber = new BigDecimal(new BigInteger(a)); - BigDecimal bNumber = new BigDecimal(new BigInteger(b)); - BigDecimal result = aNumber.divide(bNumber, rm); - assertEquals("incorrect value", c, result.toString()); - } - - /** - * @tests java.math.BigDecimal#divide(java.math.BigDecimal, - * java.math.RoundingMode) divide(BigDecimal, RoundingMode) - */ - public void test_DivideBigDecimalRoundingModeFLOOR() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - String b = "74723342238476237823787879183470"; - RoundingMode rm = RoundingMode.FLOOR; - String c = "50000260373164286401361913"; - BigDecimal aNumber = new BigDecimal(new BigInteger(a)); - BigDecimal bNumber = new BigDecimal(new BigInteger(b)); - BigDecimal result = aNumber.divide(bNumber, rm); - assertEquals("incorrect value", c, result.toString()); - } - - /** - * @tests java.math.BigDecimal#divide(java.math.BigDecimal, - * java.math.RoundingMode) divide(BigDecimal, RoundingMode) - */ - public void test_DivideBigDecimalRoundingModeHALF_UP() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - String b = "74723342238476237823787879183470"; - RoundingMode rm = RoundingMode.HALF_UP; - String c = "50000260373164286401361913"; - BigDecimal aNumber = new BigDecimal(new BigInteger(a)); - BigDecimal bNumber = new BigDecimal(new BigInteger(b)); - BigDecimal result = aNumber.divide(bNumber, rm); - assertEquals("incorrect value", c, result.toString()); - } - - /** - * @tests java.math.BigDecimal#divide(java.math.BigDecimal, - * java.math.RoundingMode) divide(BigDecimal, RoundingMode) - */ - public void test_DivideBigDecimalRoundingModeHALF_DOWN() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - int aScale = 5; - String b = "74723342238476237823787879183470"; - int bScale = 15; - int newScale = 7; - RoundingMode rm = RoundingMode.HALF_DOWN; - String c = "500002603731642864013619132621009722.1803810"; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal result = aNumber.divide(bNumber, newScale, rm); - assertEquals("incorrect value", c, result.toString()); - assertEquals("incorrect scale", newScale, result.scale()); - } - - /** - * @tests java.math.BigDecimal#divide(java.math.BigDecimal, - * java.math.RoundingMode) divide(BigDecimal, RoundingMode) - */ - public void test_DivideBigDecimalRoundingModeHALF_EVEN() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - String b = "74723342238476237823787879183470"; - RoundingMode rm = RoundingMode.HALF_EVEN; - String c = "50000260373164286401361913"; - BigDecimal aNumber = new BigDecimal(new BigInteger(a)); - BigDecimal bNumber = new BigDecimal(new BigInteger(b)); - BigDecimal result = aNumber.divide(bNumber, rm); - assertEquals("incorrect value", c, result.toString()); - } - - /** - * @tests java.math.BigDecimal#divide(java.math.BigDecimal, - * java.math.RoundingMode) divide(BigDecimal, RoundingMode) - */ - public void test_DivideBigDecimalRoundingExc() { - String a = "3736186567876876578956958765675671119238118911893939591735"; - String b = "74723342238476237823787879183470"; - RoundingMode rm = RoundingMode.UNNECESSARY; - BigDecimal aNumber = new BigDecimal(new BigInteger(a)); - BigDecimal bNumber = new BigDecimal(new BigInteger(b)); - try { - aNumber.divide(bNumber, rm); - fail("ArithmeticException is not thrown for RoundingMode.UNNECESSARY divider"); - } catch (java.lang.ArithmeticException ae) { - // expected - } - try { - bNumber = new BigDecimal(0); - aNumber.divide(bNumber, rm); - fail("ArithmeticException is not thrown for zero divider"); - } catch (java.lang.ArithmeticException ae) { - // expected - } - } - -} diff --git a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigDecimalCompareTest.java b/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigDecimalCompareTest.java deleted file mode 100644 index 1d7b10ab..00000000 --- a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigDecimalCompareTest.java +++ /dev/null @@ -1,531 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @author Elena Semukhina - * @version $Revision$ - */ - -package org.apache.harmony.math.tests.java.math; - -import junit.framework.TestCase; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.math.MathContext; -import java.math.RoundingMode; - -/** - * Class: java.math.BigDecimal - * Methods: abs, compareTo, equals, hashCode, - * max, min, negate, signum - */ -public class BigDecimalCompareTest extends TestCase { - /** - * Abs() of a negative BigDecimal - */ - public void testAbsNeg() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigDecimal aNumber = new BigDecimal(a); - String result = "123809648392384754573567356745735635678902957849027687.87678287"; - assertEquals("incorrect value", result, aNumber.abs().toString()); - } - - /** - * Abs() of a positive BigDecimal - */ - public void testAbsPos() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigDecimal aNumber = new BigDecimal(a); - String result = "123809648392384754573567356745735635678902957849027687.87678287"; - assertEquals("incorrect value", result, aNumber.abs().toString()); - } - - /** - * Abs(MathContext) of a negative BigDecimal - */ - public void testAbsMathContextNeg() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigDecimal aNumber = new BigDecimal(a); - int precision = 15; - RoundingMode rm = RoundingMode.HALF_DOWN; - MathContext mc = new MathContext(precision, rm); - String result = "1.23809648392385E+53"; - int resScale = -39; - BigDecimal res = aNumber.abs(mc); - assertEquals("incorrect value", result, res.toString()); - assertEquals("incorrect scale", resScale, res.scale()); - - mc = new MathContext(34, RoundingMode.UP); - assertEquals("incorrect value", "1.238096483923847545735673567457357E+53", aNumber.abs(mc).toString()); - - mc = new MathContext(34, RoundingMode.DOWN); - assertEquals("incorrect value", "1.238096483923847545735673567457356E+53", aNumber.abs(mc).toString()); - - mc = new MathContext(34, RoundingMode.FLOOR); - assertEquals("incorrect value", "1.238096483923847545735673567457356E+53", aNumber.abs(mc).toString()); - - mc = new MathContext(34, RoundingMode.CEILING); - assertEquals("incorrect value", "1.238096483923847545735673567457357E+53", aNumber.abs(mc).toString()); - - mc = new MathContext(34, RoundingMode.UNNECESSARY); - try { - res = aNumber.abs(mc); - fail("No ArithmeticException for RoundingMode.UNNECESSARY"); - } catch (ArithmeticException e) { - // expected - } - } - - /** - * Abs(MathContext) of a positive BigDecimal - */ - public void testAbsMathContextPos() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigDecimal aNumber = new BigDecimal(a); - int precision = 41; - RoundingMode rm = RoundingMode.HALF_EVEN; - MathContext mc = new MathContext(precision, rm); - String result = "1.2380964839238475457356735674573563567890E+53"; - int resScale = -13; - BigDecimal res = aNumber.abs(mc); - assertEquals("incorrect value", result, res.toString()); - assertEquals("incorrect scale", resScale, res.scale()); - } - - /** - * Compare to a number of an equal scale - */ - public void testCompareEqualScale1() { - String a = "12380964839238475457356735674573563567890295784902768787678287"; - int aScale = 18; - String b = "4573563567890295784902768787678287"; - int bScale = 18; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - int result = 1; - assertEquals("incorrect result", result, aNumber.compareTo(bNumber)); - } - - /** - * Compare to a number of an equal scale - */ - public void testCompareEqualScale2() { - String a = "12380964839238475457356735674573563567890295784902768787678287"; - int aScale = 18; - String b = "4573563923487289357829759278282992758247567890295784902768787678287"; - int bScale = 18; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - int result = -1; - assertEquals("incorrect result", result, aNumber.compareTo(bNumber)); - } - - /** - * Compare to a number of an greater scale - */ - public void testCompareGreaterScale1() { - String a = "12380964839238475457356735674573563567890295784902768787678287"; - int aScale = 28; - String b = "4573563567890295784902768787678287"; - int bScale = 18; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - int result = 1; - assertEquals("incorrect result", result, aNumber.compareTo(bNumber)); - } - - /** - * Compare to a number of an greater scale - */ - public void testCompareGreaterScale2() { - String a = "12380964839238475457356735674573563567890295784902768787678287"; - int aScale = 48; - String b = "4573563567890295784902768787678287"; - int bScale = 2; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - int result = -1; - assertEquals("incorrect result", result, aNumber.compareTo(bNumber)); - } - - /** - * Compare to a number of an less scale - */ - public void testCompareLessScale1() { - String a = "12380964839238475457356735674573563567890295784902768787678287"; - int aScale = 18; - String b = "4573563567890295784902768787678287"; - int bScale = 28; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - int result = 1; - assertEquals("incorrect result", result, aNumber.compareTo(bNumber)); - } - - /** - * Compare to a number of an less scale - */ - public void testCompareLessScale2() { - String a = "12380964839238475457356735674573"; - int aScale = 36; - String b = "45735635948573894578349572001798379183767890295784902768787678287"; - int bScale = 48; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - int result = -1; - assertEquals("incorrect result", result, aNumber.compareTo(bNumber)); - } - - /** - * Equals() for unequal BigDecimals - */ - public void testEqualsUnequal1() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "7472334223847623782375469293018787918347987234564568"; - int bScale = 13; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - assertFalse(aNumber.equals(bNumber)); - } - - /** - * Equals() for unequal BigDecimals - */ - public void testEqualsUnequal2() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "92948782094488478231212478987482988429808779810457634781384756794987"; - int bScale = 13; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - assertFalse(aNumber.equals(bNumber)); - } - - /** - * Equals() for unequal BigDecimals - */ - public void testEqualsUnequal3() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "92948782094488478231212478987482988429808779810457634781384756794987"; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - assertFalse(aNumber.equals(b)); - } - - /** - * equals() for equal BigDecimals - */ - public void testEqualsEqual() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "92948782094488478231212478987482988429808779810457634781384756794987"; - int bScale = -24; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - assertEquals(aNumber, bNumber); - } - - /** - * equals() for equal BigDecimals - */ - public void testEqualsNull() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - assertFalse(aNumber.equals(null)); - } - - /** - * hashCode() for equal BigDecimals - */ - public void testHashCodeEqual() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = -24; - String b = "92948782094488478231212478987482988429808779810457634781384756794987"; - int bScale = -24; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - assertEquals("incorrect value", aNumber.hashCode(), bNumber.hashCode()); - } - - /** - * hashCode() for unequal BigDecimals - */ - public void testHashCodeUnequal() { - String a = "8478231212478987482988429808779810457634781384756794987"; - int aScale = 41; - String b = "92948782094488478231212478987482988429808779810457634781384756794987"; - int bScale = -24; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - assertTrue("incorrect value", aNumber.hashCode() != bNumber.hashCode()); - } - - /** - * max() for equal BigDecimals - */ - public void testMaxEqual() { - String a = "8478231212478987482988429808779810457634781384756794987"; - int aScale = 41; - String b = "8478231212478987482988429808779810457634781384756794987"; - int bScale = 41; - String c = "8478231212478987482988429808779810457634781384756794987"; - int cScale = 41; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale); - assertEquals("incorrect value", cNumber, aNumber.max(bNumber)); - } - - /** - * max() for unequal BigDecimals - */ - public void testMaxUnequal1() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = 24; - String b = "92948782094488478231212478987482988429808779810457634781384756794987"; - int bScale = 41; - String c = "92948782094488478231212478987482988429808779810457634781384756794987"; - int cScale = 24; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale); - assertEquals("incorrect value", cNumber, aNumber.max(bNumber)); - } - - /** - * max() for unequal BigDecimals - */ - public void testMaxUnequal2() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = 41; - String b = "94488478231212478987482988429808779810457634781384756794987"; - int bScale = 41; - String c = "92948782094488478231212478987482988429808779810457634781384756794987"; - int cScale = 41; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale); - assertEquals("incorrect value", cNumber, aNumber.max(bNumber)); - } - - /** - * min() for equal BigDecimals - */ - public void testMinEqual() { - String a = "8478231212478987482988429808779810457634781384756794987"; - int aScale = 41; - String b = "8478231212478987482988429808779810457634781384756794987"; - int bScale = 41; - String c = "8478231212478987482988429808779810457634781384756794987"; - int cScale = 41; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale); - assertEquals("incorrect value", cNumber, aNumber.min(bNumber)); - } - - /** - * min() for unequal BigDecimals - */ - public void testMinUnequal1() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = 24; - String b = "92948782094488478231212478987482988429808779810457634781384756794987"; - int bScale = 41; - String c = "92948782094488478231212478987482988429808779810457634781384756794987"; - int cScale = 41; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale); - assertEquals("incorrect value", cNumber, aNumber.min(bNumber)); - } - - /** - * min() for unequal BigDecimals - */ - public void testMinUnequal2() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = 41; - String b = "94488478231212478987482988429808779810457634781384756794987"; - int bScale = 41; - String c = "94488478231212478987482988429808779810457634781384756794987"; - int cScale = 41; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = new BigDecimal(new BigInteger(b), bScale); - BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale); - assertEquals("incorrect value", cNumber, aNumber.min(bNumber)); - } - - /** - * plus() for a positive BigDecimal - */ - public void testPlusPositive() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = 41; - String c = "92948782094488478231212478987482988429808779810457634781384756794987"; - int cScale = 41; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale); - assertEquals("incorrect value", cNumber, aNumber.plus()); - } - - /** - * plus(MathContext) for a positive BigDecimal - */ - public void testPlusMathContextPositive() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = 41; - int precision = 37; - RoundingMode rm = RoundingMode.FLOOR; - MathContext mc = new MathContext(precision, rm); - String c = "929487820944884782312124789.8748298842"; - int cScale = 10; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal res = aNumber.plus(mc); - assertEquals("incorrect value", c, res.toString()); - assertEquals("incorrect scale", cScale, res.scale()); - } - - /** - * plus() for a negative BigDecimal - */ - public void testPlusNegative() { - String a = "-92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = 41; - String c = "-92948782094488478231212478987482988429808779810457634781384756794987"; - int cScale = 41; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale); - assertEquals("incorrect value", cNumber, aNumber.plus()); - } - - /** - * plus(MathContext) for a negative BigDecimal - */ - public void testPlusMathContextNegative() { - String a = "-92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = 49; - int precision = 46; - RoundingMode rm = RoundingMode.CEILING; - MathContext mc = new MathContext(precision, rm); - String c = "-9294878209448847823.121247898748298842980877981"; - int cScale = 27; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal res = aNumber.plus(mc); - assertEquals("incorrect value", c, res.toString()); - assertEquals("incorrect scale", cScale, res.scale()); - } - - /** - * negate() for a positive BigDecimal - */ - public void testNegatePositive() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = 41; - String c = "-92948782094488478231212478987482988429808779810457634781384756794987"; - int cScale = 41; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale); - assertEquals("incorrect value", cNumber, aNumber.negate()); - } - - /** - * negate(MathContext) for a positive BigDecimal - */ - public void testNegateMathContextPositive() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - MathContext mc = new MathContext(37, RoundingMode.FLOOR); - BigDecimal aNumber = new BigDecimal(new BigInteger(a), 41); - BigDecimal res = aNumber.negate(mc); - assertEquals("incorrect value", "-929487820944884782312124789.8748298843", res.toString()); - assertEquals("incorrect scale", 10, res.scale()); - -// String a = "92948782094488478231212478987482988429808779810457634781384756794987"; -// int aScale = 41; -// int precision = 37; -// RoundingMode rm = RoundingMode.FLOOR; -// MathContext mc = new MathContext(precision, rm); -// String c = "-929487820944884782312124789.8748298842"; -// int cScale = 10; -// BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); -// BigDecimal res = aNumber.negate(mc); -// assertEquals("incorrect value", c, res.toString()); -// assertEquals("incorrect scale", cScale, res.scale()); - } - - /** - * negate() for a negative BigDecimal - */ - public void testNegateNegative() { - String a = "-92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = 41; - String c = "92948782094488478231212478987482988429808779810457634781384756794987"; - int cScale = 41; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal cNumber = new BigDecimal(new BigInteger(c), cScale); - assertEquals("incorrect value", cNumber, aNumber.negate()); - } - - /** - * negate(MathContext) for a negative BigDecimal - */ - public void testNegateMathContextNegative() { - String a = "-92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = 49; - int precision = 46; - RoundingMode rm = RoundingMode.CEILING; - MathContext mc = new MathContext(precision, rm); - String c = "9294878209448847823.121247898748298842980877982"; - int cScale = 27; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal res = aNumber.negate(mc); - assertEquals("incorrect value", c, res.toString()); - assertEquals("incorrect scale", cScale, res.scale()); - } - - /** - * signum() for a positive BigDecimal - */ - public void testSignumPositive() { - String a = "92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = 41; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - assertEquals("incorrect value", 1, aNumber.signum()); - } - - /** - * signum() for a negative BigDecimal - */ - public void testSignumNegative() { - String a = "-92948782094488478231212478987482988429808779810457634781384756794987"; - int aScale = 41; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - assertEquals("incorrect value", -1, aNumber.signum()); - } - - /** - * signum() for zero - */ - public void testSignumZero() { - String a = "0"; - int aScale = 41; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - assertEquals("incorrect value", 0, aNumber.signum()); - } -} diff --git a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigDecimalConvertTest.java b/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigDecimalConvertTest.java deleted file mode 100644 index ccd3e15a..00000000 --- a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigDecimalConvertTest.java +++ /dev/null @@ -1,1166 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @author Elena Semukhina - * @version $Revision$ - */ - -package org.apache.harmony.math.tests.java.math; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.math.RoundingMode; -import java.math.MathContext; - -import junit.framework.TestCase; - -/** - * Class: java.math.BigDecimal - * Methods: doubleValue, floatValue, intValue, longValue, - * valueOf, toString, toBigInteger - */ -public class BigDecimalConvertTest extends TestCase { - /** - * Double value of a negative BigDecimal - */ - public void testDoubleValueNeg() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigDecimal aNumber = new BigDecimal(a); - double result = -1.2380964839238476E53; - assertEquals("incorrect value", result, aNumber.doubleValue(), 0); - } - - /** - * Double value of a positive BigDecimal - */ - public void testDoubleValuePos() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigDecimal aNumber = new BigDecimal(a); - double result = 1.2380964839238476E53; - assertEquals("incorrect value", result, aNumber.doubleValue(), 0); - } - - /** - * Double value of a large positive BigDecimal - */ - public void testDoubleValuePosInfinity() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E+400"; - BigDecimal aNumber = new BigDecimal(a); - double result = Double.POSITIVE_INFINITY; - assertEquals("incorrect value", result, aNumber.doubleValue(), 0); - } - - /** - * Double value of a large negative BigDecimal - */ - public void testDoubleValueNegInfinity() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E+400"; - BigDecimal aNumber = new BigDecimal(a); - double result = Double.NEGATIVE_INFINITY; - assertEquals("incorrect value", result, aNumber.doubleValue(), 0); - } - - /** - * Double value of a small negative BigDecimal - */ - public void testDoubleValueMinusZero() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E-400"; - BigDecimal aNumber = new BigDecimal(a); - long minusZero = -9223372036854775808L; - double result = aNumber.doubleValue(); - assertTrue("incorrect value", Double.doubleToLongBits(result) == minusZero); - } - - /** - * Double value of a small positive BigDecimal - */ - public void testDoubleValuePlusZero() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E-400"; - BigDecimal aNumber = new BigDecimal(a); - long zero = 0; - double result = aNumber.doubleValue(); - assertTrue("incorrect value", Double.doubleToLongBits(result) == zero); - } - - /** - * Float value of a negative BigDecimal - */ - public void testFloatValueNeg() { - String a = "-1238096483923847.6356789029578E+21"; - BigDecimal aNumber = new BigDecimal(a); - float result = -1.2380965E36F; - assertTrue("incorrect value", aNumber.floatValue() == result); - } - - /** - * Float value of a positive BigDecimal - */ - public void testFloatValuePos() { - String a = "1238096483923847.6356789029578E+21"; - BigDecimal aNumber = new BigDecimal(a); - float result = 1.2380965E36F; - assertTrue("incorrect value", aNumber.floatValue() == result); - } - - /** - * Float value of a large positive BigDecimal - */ - public void testFloatValuePosInfinity() { - String a = "123809648373567356745735.6356789787678287E+200"; - BigDecimal aNumber = new BigDecimal(a); - float result = Float.POSITIVE_INFINITY; - assertTrue("incorrect value", aNumber.floatValue() == result); - } - - /** - * Float value of a large negative BigDecimal - */ - public void testFloatValueNegInfinity() { - String a = "-123809648392384755735.63567887678287E+200"; - BigDecimal aNumber = new BigDecimal(a); - float result = Float.NEGATIVE_INFINITY; - assertTrue("incorrect value", aNumber.floatValue() == result); - } - - /** - * Float value of a small negative BigDecimal - */ - public void testFloatValueMinusZero() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E-400"; - BigDecimal aNumber = new BigDecimal(a); - int minusZero = -2147483648; - float result = aNumber.floatValue(); - assertTrue("incorrect value", Float.floatToIntBits(result) == minusZero); - } - - /** - * Float value of a small positive BigDecimal - */ - public void testFloatValuePlusZero() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E-400"; - BigDecimal aNumber = new BigDecimal(a); - int zero = 0; - float result = aNumber.floatValue(); - assertTrue("incorrect value", Float.floatToIntBits(result) == zero); - } - - /** - * Integer value of a negative BigDecimal - */ - public void testIntValueNeg() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigDecimal aNumber = new BigDecimal(a); - int result = 218520473; - assertTrue("incorrect value", aNumber.intValue() == result); - } - - /** - * Integer value of a positive BigDecimal - */ - public void testIntValuePos() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigDecimal aNumber = new BigDecimal(a); - int result = -218520473; - assertTrue("incorrect value", aNumber.intValue() == result); - } - - /** - * Long value of a negative BigDecimal - */ - public void testLongValueNeg() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigDecimal aNumber = new BigDecimal(a); - long result = -1246043477766677607L; - assertTrue("incorrect value", aNumber.longValue() == result); - } - - /** - * Long value of a positive BigDecimal - */ - public void testLongValuePos() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigDecimal aNumber = new BigDecimal(a); - long result = 1246043477766677607L; - assertTrue("incorrect value", aNumber.longValue() == result); - } - - /** - * scaleByPowerOfTen(int n) - */ - public void testScaleByPowerOfTen1() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 13; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal result = aNumber.scaleByPowerOfTen(10); - String res = "1231212478987482988429808779810457634781384756794.987"; - int resScale = 3; - assertEquals("incorrect value", res, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * scaleByPowerOfTen(int n) - */ - public void testScaleByPowerOfTen2() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = -13; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal result = aNumber.scaleByPowerOfTen(10); - String res = "1.231212478987482988429808779810457634781384756794987E+74"; - int resScale = -23; - assertEquals("incorrect value", res, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Convert a positive BigDecimal to BigInteger - */ - public void testToBigIntegerPos1() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigInteger bNumber = new BigInteger("123809648392384754573567356745735635678902957849027687"); - BigDecimal aNumber = new BigDecimal(a); - BigInteger result = aNumber.toBigInteger(); - assertTrue("incorrect value", result.equals(bNumber)); - } - - /** - * Convert a positive BigDecimal to BigInteger - */ - public void testToBigIntegerPos2() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E+15"; - BigInteger bNumber = new BigInteger("123809648392384754573567356745735635678902957849"); - BigDecimal aNumber = new BigDecimal(a); - BigInteger result = aNumber.toBigInteger(); - assertTrue("incorrect value", result.equals(bNumber)); - } - - /** - * Convert a positive BigDecimal to BigInteger - */ - public void testToBigIntegerPos3() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E+45"; - BigInteger bNumber = new BigInteger("123809648392384754573567356745735635678902957849027687876782870000000000000000"); - BigDecimal aNumber = new BigDecimal(a); - BigInteger result = aNumber.toBigInteger(); - assertTrue("incorrect value", result.equals(bNumber)); - } - - /** - * Convert a negative BigDecimal to BigInteger - */ - public void testToBigIntegerNeg1() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigInteger bNumber = new BigInteger("-123809648392384754573567356745735635678902957849027687"); - BigDecimal aNumber = new BigDecimal(a); - BigInteger result = aNumber.toBigInteger(); - assertTrue("incorrect value", result.equals(bNumber)); - } - - /** - * Convert a negative BigDecimal to BigInteger - */ - public void testToBigIntegerNeg2() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E+15"; - BigInteger bNumber = new BigInteger("-123809648392384754573567356745735635678902957849"); - BigDecimal aNumber = new BigDecimal(a); - BigInteger result = aNumber.toBigInteger(); - assertTrue("incorrect value", result.equals(bNumber)); - } - - /** - * Convert a negative BigDecimal to BigInteger - */ - public void testToBigIntegerNeg3() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E+45"; - BigInteger bNumber = new BigInteger("-123809648392384754573567356745735635678902957849027687876782870000000000000000"); - BigDecimal aNumber = new BigDecimal(a); - BigInteger result = aNumber.toBigInteger(); - assertTrue("incorrect value", result.equals(bNumber)); - } - - /** - * Convert a small BigDecimal to BigInteger - */ - public void testToBigIntegerZero() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E-500"; - BigInteger bNumber = new BigInteger("0"); - BigDecimal aNumber = new BigDecimal(a); - BigInteger result = aNumber.toBigInteger(); - assertTrue("incorrect value", result.equals(bNumber)); - } - - /** - * toBigIntegerExact() - */ - public void testToBigIntegerExact1() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E+45"; - BigDecimal aNumber = new BigDecimal(a); - String res = "-123809648392384754573567356745735635678902957849027687876782870000000000000000"; - BigInteger result = aNumber.toBigIntegerExact(); - assertEquals("incorrect value", res, result.toString()); - } - - /** - * toBigIntegerExact() - */ - public void testToBigIntegerExactException() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E-10"; - BigDecimal aNumber = new BigDecimal(a); - try { - aNumber.toBigIntegerExact(); - fail("java.lang.ArithmeticException has not been thrown"); - } catch (java.lang.ArithmeticException e) { - return; - } - } - - /** - * Convert a positive BigDecimal to an engineering string representation - */ - public void testToEngineeringStringPos() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E-501"; - BigDecimal aNumber = new BigDecimal(a); - String result = "123.80964839238475457356735674573563567890295784902768787678287E-471"; - assertEquals("incorrect value", result, aNumber.toEngineeringString()); - } - - /** - * Convert a negative BigDecimal to an engineering string representation - */ - public void testToEngineeringStringNeg() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E-501"; - BigDecimal aNumber = new BigDecimal(a); - String result = "-123.80964839238475457356735674573563567890295784902768787678287E-471"; - assertEquals("incorrect value", result, aNumber.toEngineeringString()); - } - - /** - * Convert a negative BigDecimal to an engineering string representation - */ - public void testToEngineeringStringZeroPosExponent() { - String a = "0.0E+16"; - BigDecimal aNumber = new BigDecimal(a); - String result = "0E+15"; - assertEquals("incorrect value", result, aNumber.toEngineeringString()); - } - - /** - * Convert a negative BigDecimal to an engineering string representation - */ - public void testToEngineeringStringZeroNegExponent() { - String a = "0.0E-16"; - BigDecimal aNumber = new BigDecimal(a); - String result = "0.00E-15"; - assertEquals("incorrect value", result, aNumber.toEngineeringString()); - } - - /** - * Convert a negative BigDecimal with a negative exponent to a plain string - * representation; scale == 0. - */ - public void testToPlainStringNegNegExp() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E-100"; - BigDecimal aNumber = new BigDecimal(a); - String result = "-0.000000000000000000000000000000000000000000000000000000000000000000012380964839238475457356735674573563567890295784902768787678287"; - assertTrue("incorrect value", aNumber.toPlainString().equals(result)); - } - - /** - * Convert a negative BigDecimal with a positive exponent - * to a plain string representation; - * scale == 0. - */ - public void testToPlainStringNegPosExp() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E100"; - BigDecimal aNumber = new BigDecimal(a); - String result = "-1238096483923847545735673567457356356789029578490276878767828700000000000000000000000000000000000000000000000000000000000000000000000"; - assertTrue("incorrect value", aNumber.toPlainString().equals(result)); - } - - /** - * Convert a positive BigDecimal with a negative exponent - * to a plain string representation; - * scale == 0. - */ - public void testToPlainStringPosNegExp() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E-100"; - BigDecimal aNumber = new BigDecimal(a); - String result = "0.000000000000000000000000000000000000000000000000000000000000000000012380964839238475457356735674573563567890295784902768787678287"; - assertTrue("incorrect value", aNumber.toPlainString().equals(result)); - } - - /** - * Convert a negative BigDecimal with a negative exponent - * to a plain string representation; - * scale == 0. - */ - public void testToPlainStringPosPosExp() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E+100"; - BigDecimal aNumber = new BigDecimal(a); - String result = "1238096483923847545735673567457356356789029578490276878767828700000000000000000000000000000000000000000000000000000000000000000000000"; - assertTrue("incorrect value", aNumber.toPlainString().equals(result)); - } - - /** - * Convert a BigDecimal to a string representation; - * scale == 0. - */ - public void testToStringZeroScale() { - String a = "-123809648392384754573567356745735635678902957849027687876782870"; - BigDecimal aNumber = new BigDecimal(new BigInteger(a)); - String result = "-123809648392384754573567356745735635678902957849027687876782870"; - assertTrue("incorrect value", aNumber.toString().equals(result)); - } - - /** - * Convert a positive BigDecimal to a string representation - */ - public void testToStringPos() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E-500"; - BigDecimal aNumber = new BigDecimal(a); - String result = "1.2380964839238475457356735674573563567890295784902768787678287E-468"; - assertTrue("incorrect value", aNumber.toString().equals(result)); - } - - /** - * Convert a negative BigDecimal to a string representation - */ - public void testToStringNeg() { - String a = "-123.4564563673567380964839238475457356735674573563567890295784902768787678287E-5"; - BigDecimal aNumber = new BigDecimal(a); - String result = "-0.001234564563673567380964839238475457356735674573563567890295784902768787678287"; - assertTrue("incorrect value", aNumber.toString().equals(result)); - } - - /** - * Create a BigDecimal from a positive long value; scale == 0 - */ - public void testValueOfPosZeroScale() { - long a = 98374823947823578L; - BigDecimal aNumber = BigDecimal.valueOf(a); - String result = "98374823947823578"; - assertTrue("incorrect value", aNumber.toString().equals(result)); - } - - /** - * Create a BigDecimal from a negative long value; scale is 0 - */ - public void testValueOfNegZeroScale() { - long a = -98374823947823578L; - BigDecimal aNumber = BigDecimal.valueOf(a); - String result = "-98374823947823578"; - assertTrue("incorrect value", aNumber.toString().equals(result)); - } - - /** - * Create a BigDecimal from a negative long value; scale is positive - */ - public void testValueOfNegScalePos() { - long a = -98374823947823578L; - int scale = 12; - BigDecimal aNumber = BigDecimal.valueOf(a, scale); - String result = "-98374.823947823578"; - assertTrue("incorrect value", aNumber.toString().equals(result)); - } - - /** - * Create a BigDecimal from a negative long value; scale is negative - */ - public void testValueOfNegScaleNeg() { - long a = -98374823947823578L; - int scale = -12; - BigDecimal aNumber = BigDecimal.valueOf(a, scale); - String result = "-9.8374823947823578E+28"; - assertTrue("incorrect value", aNumber.toString().equals(result)); - } - - /** - * Create a BigDecimal from a negative long value; scale is positive - */ - public void testValueOfPosScalePos() { - long a = 98374823947823578L; - int scale = 12; - BigDecimal aNumber = BigDecimal.valueOf(a, scale); - String result = "98374.823947823578"; - assertTrue("incorrect value", aNumber.toString().equals(result)); - } - - /** - * Create a BigDecimal from a negative long value; scale is negative - */ - public void testValueOfPosScaleNeg() { - long a = 98374823947823578L; - int scale = -12; - BigDecimal aNumber = BigDecimal.valueOf(a, scale); - String result = "9.8374823947823578E+28"; - assertTrue("incorrect value", aNumber.toString().equals(result)); - } - - /** - * Create a BigDecimal from a negative double value - */ - public void testValueOfDoubleNeg() { - double a = -65678765876567576.98788767; - BigDecimal result = BigDecimal.valueOf(a); - String res = "-65678765876567576"; - int resScale = 0; - assertEquals("incorrect value", res, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Create a BigDecimal from a positive double value - */ - public void testValueOfDoublePos1() { - double a = 65678765876567576.98788767; - BigDecimal result = BigDecimal.valueOf(a); - String res = "65678765876567576"; - int resScale = 0; - assertEquals("incorrect value", res, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Create a BigDecimal from a positive double value - */ - public void testValueOfDoublePos2() { - double a = 12321237576.98788767; - BigDecimal result = BigDecimal.valueOf(a); - String res = "12321237576.987888"; - int resScale = 6; - assertEquals("incorrect value", res, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Create a BigDecimal from a positive double value - */ - public void testValueOfDoublePos3() { - double a = 12321237576.9878838; - BigDecimal result = BigDecimal.valueOf(a); - String res = "12321237576.987885"; - int resScale = 6; - assertEquals("incorrect value", res, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * valueOf(Double.NaN) - */ - public void testValueOfDoubleNaN() { - double a = Double.NaN; - try { - BigDecimal.valueOf(a); - fail("NumberFormatException has not been thrown for Double.NaN"); - } catch (NumberFormatException e) { - return; - } - } - -// ANDROID ADDED - - /** - * @tests java.math.BigDecimal#intValueExact() Integer value of a negative - * BigDecimal - */ - public void test_IntValueExactNeg() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigDecimal aNumber = new BigDecimal(a); - try { - aNumber.intValueExact(); - fail("java.lang.ArithmeticException isn't thrown after calling intValueExact"); - } catch (java.lang.ArithmeticException ae) { - // expected; - } - } - - /** - * @tests java.math.BigDecimal#intValueExact() Integer value of a positive - * BigDecimal - */ - public void test_IntValueExactPos() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigDecimal aNumber = new BigDecimal(a); - try { - aNumber.intValueExact(); - fail("java.lang.ArithmeticException isn't thrown after calling intValueExact"); - } catch (java.lang.ArithmeticException ae) { - // expected; - } - } - - /** - * @tests java.math.BigDecimal#intValueExact() Integer value of a negative - * BigDecimal - */ - public void test_IntValueExactFloatNeg() { - BigDecimal aNumber = new BigDecimal("-2147483647.999"); - try { - aNumber.intValueExact(); - fail("java.lang.ArithmeticException isn't thrown after calling intValueExact"); - } catch (java.lang.ArithmeticException ae) { - // expected; - } - } - - /** - * @tests java.math.BigDecimal#intValueExact() Integer value of a positive - * BigDecimal - */ - public void test_IntValueExactFloatPos() { - float a = 2147483646.99999F; - BigDecimal aNumber = new BigDecimal(a); - try { - aNumber.intValueExact(); - fail("java.lang.ArithmeticException isn't thrown after calling intValueExact"); - } catch (java.lang.ArithmeticException ae) { - // expected; - } - } - - /** - * @tests java.math.BigDecimal#intValueExact() Integer value of a positive - * BigDecimal - */ - public void test_IntValueExactLongPos() { - long a = 2147483647L; - BigDecimal aNumber = new BigDecimal(a); - int iNumber = aNumber.intValueExact(); - assertTrue("incorrect value", iNumber == a); - } - - /** - * @tests java.math.BigDecimal#intValueExact() Integer value of a positive - * BigDecimal - */ - public void test_IntValueExactLongNeg() { - long a = -2147483648L; - BigDecimal aNumber = new BigDecimal(a); - int iNumber = aNumber.intValueExact(); - assertTrue("incorrect value", iNumber == a); - } - - /** - * @tests java.math.BigDecimal#longValueExact() Long value of a negative - * BigDecimal - */ - public void test_LongValueExactNeg() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigDecimal aNumber = new BigDecimal(a); - try { - aNumber.longValueExact(); - fail("java.lang.ArithmeticException isn't thrown after calling longValueExact"); - } catch (java.lang.ArithmeticException ae) { - // expected; - } - } - - /** - * @tests java.math.BigDecimal#longValueExact() Long value of a positive - * BigDecimal - */ - public void test_LongValueExactPos() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigDecimal aNumber = new BigDecimal(a); - try { - aNumber.longValueExact(); - fail("java.lang.ArithmeticException isn't thrown after calling longValueExact"); - } catch (java.lang.ArithmeticException ae) { - // expected; - } - } - - /** - * @tests java.math.BigDecimal#longValueExact() Long value of a negative - * BigDecimal - */ - public void test_LongValueExactFloatNeg() { - BigDecimal aNumber = new BigDecimal("-9223372036854775807.99999"); - try { - aNumber.longValueExact(); - fail("java.lang.ArithmeticException isn't thrown after calling longValueExact"); - } catch (java.lang.ArithmeticException ae) { - // expected; - } - } - - /** - * @tests java.math.BigDecimal#longValueExact() Long value of a positive - * BigDecimal - */ - public void test_LongValueExactFloatPos() { - float a = 9223372036854775806.99999F; - BigDecimal aNumber = new BigDecimal(a); - try { - aNumber.longValueExact(); - fail("java.lang.ArithmeticException isn't thrown after calling longValueExact"); - } catch (java.lang.ArithmeticException ae) { - // expected; - } - } - - /** - * @test java.math.BigDecimal#byteValueExact() Convert pisitive BigDesimal - * to byte type - */ - public void test_ByteValueExactPos() { - int i = 127; - BigDecimal bdNumber = new BigDecimal(i); - byte bNumber = bdNumber.byteValueExact(); - assertTrue("incorrect byteValueExact", i == bNumber); - } - - /** - * @test java.math.BigDecimal#byteValueExact() Convert negative BigDesimal - * to byte type - */ - public void test_ByteValueExactNeg() { - String sNumber = "-127.56789"; - int iNumber = -128; - int iPresition = 3; - MathContext mc = new MathContext(iPresition, RoundingMode.UP); - BigDecimal bdNumber = new BigDecimal(sNumber, mc); - byte bNumber = bdNumber.byteValueExact(); - assertTrue("incorrect byteValueExact", iNumber == bNumber); - } - - /** - * @test java.math.BigDecimal#byteValueExact() Convert BigDesimal created - * from char array to byte type - */ - - public void test_ByteValueExactCharZero() { - char[] cNumber = { - '-', '0', '.', '0' - }; - int iNumber = 0; - int iPresition = 5; - MathContext mc = new MathContext(iPresition, RoundingMode.HALF_DOWN); - BigDecimal bdNumber = new BigDecimal(cNumber, mc); - byte bNumber = bdNumber.byteValueExact(); - assertTrue("incorrect byteValueExact", iNumber == bNumber); - } - - /** - * @test java.math.BigDecimal#byteValueExact() Convert BigDesimal created - * from String to byte type - */ - - public void test_ByteValueExactStringZero() { - String sNumber = "00000000000000"; - int iNumber = 0; - int iPresition = 0; - MathContext mc = new MathContext(iPresition, RoundingMode.HALF_UP); - BigDecimal bdNumber = new BigDecimal(sNumber, mc); - byte bNumber = bdNumber.byteValueExact(); - assertTrue("incorrect byteValueExact", iNumber == bNumber); - } - - /** - * @test java.math.BigDecimal#byteValueExact() Convert BigDesimal created - * from double to byte type - */ - - public void test_ByteValueExactDoubleMax() { - double dNumber = Double.MAX_VALUE; - BigDecimal bdNumber = new BigDecimal(dNumber); - try { - bdNumber.byteValueExact(); - fail("java.lang.ArithmeticException isn't thrown after calling byteValueExact"); - } catch (java.lang.ArithmeticException ae) { - // expected - } - } - - /** - * @test java.math.BigDecimal#byteValueExact() Convert BigDesimal created - * from double to byte type - */ - - public void test_ByteValueExactDoubleMin() { - double dNumber = Double.MIN_VALUE; - BigDecimal bdNumber = new BigDecimal(dNumber); - try { - bdNumber.byteValueExact(); - fail("java.lang.ArithmeticException isn't thrown after calling byteValueExact"); - } catch (java.lang.ArithmeticException ae) { - // expected - } - } - - /** - * @test java.math.BigDecimal#byteValueExact() Convert BigDesimal created - * from float to byte type - */ - - public void test_ByteValueExactFloatPos() { - float fNumber = 123.5445F; - BigDecimal bdNumber = new BigDecimal(fNumber); - try { - bdNumber.byteValueExact(); - fail("java.lang.ArithmeticException isn't thrown after calling byteValueExact"); - } catch (java.lang.ArithmeticException ae) { - // expected - } - } - - /** - * @test java.math.BigDecimal#byteValueExact() Convert BigDesimal created - * from float to byte type - */ - - public void test_ByteValueExactFloatNeg() { - float fNumber = -12.987654321F; - BigDecimal bdNumber = new BigDecimal(fNumber); - try { - bdNumber.byteValueExact(); - fail("java.lang.ArithmeticException isn't thrown after calling byteValueExact"); - } catch (java.lang.ArithmeticException ae) { - // expected - } - } - - /** - * @test java.math.BigDecimal#byteValueExact() Convert BigDesimal created - * from double to byte type - */ - - public void test_ByteValueExactDouble() { - double dNumber = 123.0000D; - BigDecimal bdNumber = new BigDecimal(dNumber); - byte bNumber = bdNumber.byteValueExact(); - assertTrue("incorrect byteValueExact", dNumber == bNumber); - } - - /** - * @test java.math.BigDecimal#byteValueExact() Convert BigDesimal created - * from long to byte type - */ - - public void test_ByteValueExactLongMin() { - long lNumber = Long.MIN_VALUE; - BigDecimal bdNumber = new BigDecimal(lNumber); - try { - bdNumber.byteValueExact(); - fail("java.lang.ArithmeticException isn't thrown after calling byteValueExact"); - } catch (java.lang.ArithmeticException ae) { - // expected - } - } - - /** - * @test java.math.BigDecimal#byteValueExact() Convert BigDesimal created - * from int to byte type - */ - - public void test_ByteValueExactIntMax() { - int iNumber = Integer.MAX_VALUE; - BigDecimal bdNumber = new BigDecimal(iNumber); - try { - bdNumber.byteValueExact(); - fail("java.lang.ArithmeticException isn't thrown after calling byteValueExact"); - } catch (java.lang.ArithmeticException ae) { - // expected - } - } - - /** - * @test java.math.BigDecimal#byteValue() Convert pisitive BigDesimal to - * byte type - */ - public void test_ByteValuePos() { - int i = 127; - BigDecimal bdNumber = new BigDecimal(i); - byte bNumber = bdNumber.byteValue(); - assertTrue("incorrect byteValue", i == bNumber); - } - - /** - * @test java.math.BigDecimal#byteValue() Convert negative BigDesimal to - * byte type - */ - public void test_ByteValueNeg() { - String sNumber = "-127.56789"; - int iNumber = -128; - int iPresition = 3; - MathContext mc = new MathContext(iPresition, RoundingMode.UP); - BigDecimal bdNumber = new BigDecimal(sNumber, mc); - byte bNumber = bdNumber.byteValue(); - assertTrue("incorrect byteValueExact", iNumber == bNumber); - } - - /** - * @test java.math.BigDecimal#byteValue() Convert BigDesimal created from - * char array to byte type - */ - public void test_ByteValueCharZero() { - char[] cNumber = { - '-', '0', '.', '0' - }; - int iNumber = 0; - int iPresition = 0; - MathContext mc = new MathContext(iPresition, RoundingMode.HALF_UP); - BigDecimal bdNumber = new BigDecimal(cNumber, mc); - byte bNumber = bdNumber.byteValue(); - assertTrue("incorrect byteValue", iNumber == bNumber); - } - - /** - * @test java.math.BigDecimal#byteValue() Convert BigDesimal created from - * String to byte type - */ - public void test_ByteValueStringZero() { - String sNumber = "00000"; - int iNumber = 0; - int iPresition = 0; - MathContext mc = new MathContext(iPresition, RoundingMode.HALF_UP); - BigDecimal bdNumber = new BigDecimal(sNumber, mc); - byte bNumber = bdNumber.byteValue(); - assertTrue("incorrect byteValue", iNumber == bNumber); - } - - /** - * @test java.math.BigDecimal#byteValue() Convert BigDesimal created from - * double to byte type - */ - public void test_ByteValueDoubleMax() { - double dNumber = Double.MAX_VALUE; - BigDecimal bdNumber = new BigDecimal(dNumber); - int result = 0; - byte bNumber = bdNumber.byteValue(); - assertTrue("incorrect byteValue", bNumber == result); - } - - /** - * @test java.math.BigDecimal#byteValue() Convert BigDesimal created from - * double to byte type - */ - public void test_ByteValueDoubleMin() { - double dNumber = Double.MIN_VALUE; - BigDecimal bdNumber = new BigDecimal(dNumber); - int result = 0; - byte bNumber = bdNumber.byteValue(); - assertTrue("incorrect byteValue", bNumber == result); - } - - /** - * @test_ java.math.BigDecimal#byteValue() Convert BigDesimal created from - * float to byte type - */ - public void test_ByteValueFloatNeg() { - float fNumber = -12.987654321F; - byte bValue = -12; - BigDecimal bdNumber = new BigDecimal(fNumber); - byte bNumber = bdNumber.byteValue(); - assertTrue("incorrect byteValue", bNumber == bValue); - } - - /** - * @test java.math.BigDecimal#byteValue() Convert BigDesimal created from - * double to byte type - */ - public void test_ByteValueDouble() { - double dNumber = 123.0000D; - BigDecimal bdNumber = new BigDecimal(dNumber); - byte bNumber = bdNumber.byteValue(); - assertTrue("incorrect byteValue", dNumber == bNumber); - } - - /** - * @test java.math.BigDecimal#byteValue() Convert BigDesimal created from - * long to byte type - */ - public void test_ByteValueLongMin() { - long lNumber = Long.MIN_VALUE; - int result = 0; - BigDecimal bdNumber = new BigDecimal(lNumber); - byte bNumber = bdNumber.byteValue(); - assertTrue("incorrect byteValue", bNumber == result); - } - - /** - * @test java.math.BigDecimal#byteValue() Convert BigDesimal created from - * int to byte type - */ - public void test_ByteValueIntMin() { - int iNumber = Integer.MIN_VALUE; - int result = 0; - BigDecimal bdNumber = new BigDecimal(iNumber); - byte bNumber = bdNumber.byteValue(); - assertTrue("incorrect byteValue", bNumber == result); - } - - /** - * @test java.math.BigDecimal#byteValue() Convert BigDesimal created from - * int to byte type - */ - public void test_ByteValueIntMax() { - int iNumber = Integer.MAX_VALUE; - int result = -1; - BigDecimal bdNumber = new BigDecimal(iNumber); - byte bNumber = bdNumber.byteValue(); - assertTrue("incorrect byteValue", bNumber == result); - } - - /** - * @test java.math.BigDecimal#shortValue() Short value of a negative - * BigDecimal - */ - public void test_ShortValueNeg() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigDecimal aNumber = new BigDecimal(a); - int result = 23449; - assertTrue("incorrect value", aNumber.shortValue() == result); - } - - /** - * @test java.math.BigDecimal#shortValue() Short value of a positive - * BigDecimal - */ - public void test_ShortValuePos() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigDecimal aNumber = new BigDecimal(a); - int result = -23449; - assertTrue("incorrect value", aNumber.shortValue() == result); - } - - /** - * @test java.math.BigDecimal#shortValueExact() Short value of a negative - * BigDecimal - */ - public void test_ShortValueExactNeg() { - String a = "-123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigDecimal aNumber = new BigDecimal(a); - try { - aNumber.shortValueExact(); - fail("java.lang.ArithmeticException isn't thrown after calling intValueExact"); - } catch (java.lang.ArithmeticException ae) { - // expected; - } - } - - /** - * @test java.math.BigDecimal#shortValueExact() Short value of a positive - * BigDecimal - */ - public void test_ShortValueExactPos() { - String a = "123809648392384754573567356745735.63567890295784902768787678287E+21"; - BigDecimal aNumber = new BigDecimal(a); - try { - aNumber.shortValueExact(); - fail("java.lang.ArithmeticException isn't thrown after calling intValueExact"); - } catch (java.lang.ArithmeticException ae) { - // expected; - } - } - - /** - * @test java.math.BigDecimal#shortValueExact() Short value of a negative - * BigDecimal - */ - public void test_ShortValueExactFloatNeg() { - BigDecimal aNumber = new BigDecimal("-32766.99999"); - try { - aNumber.shortValueExact(); - fail("java.lang.ArithmeticException isn't thrown after calling intValueExact"); - } catch (java.lang.ArithmeticException ae) { - // expected; - } - } - - /** - * @test java.math.BigDecimal#shortValueExact() Short value of a positive - * BigDecimal - */ - public void test_ShortValueExactFloatPos() { - float a = 32767.99999F; - BigDecimal aNumber = new BigDecimal(a); - try { - aNumber.shortValueExact(); - fail("java.lang.ArithmeticException isn't thrown after calling intValueExact"); - } catch (java.lang.ArithmeticException ae) { - // expected; - } - } - - /** - * @test java.math.BigDecimal#shortValueExact() Short value of a positive - * BigDecimal - */ - public void test_ShortValueExactLongPos() { - long a = 12345L; - BigDecimal aNumber = new BigDecimal(a); - short shNumber = aNumber.shortValueExact(); - assertTrue("incorrect value", shNumber == a); - } - - /** - * @test java.math.BigDecimal#shortValueExact() Short value of a positive - * BigDecimal - */ - public void test_ShortValueExactLongNeg() { - long a = -12345L; - BigDecimal aNumber = new BigDecimal(a); - int iNumber = aNumber.shortValueExact(); - assertTrue("incorrect value", iNumber == a); - } - - /** - * @tests java.math.BigDecimal#stripTrailingZeros() stripTrailingZeros() for - * BigDecimal with zero - */ - public void test_stripTrailingZerosZeros() { - - BigDecimal bdNumber = new BigDecimal("0000000"); - BigDecimal result = bdNumber.stripTrailingZeros(); - assertEquals("incorrect value", result.unscaledValue(), bdNumber.unscaledValue()); - assertTrue("incorrect value", result.scale() == 0); - - bdNumber = new BigDecimal(0); - result = bdNumber.stripTrailingZeros(); - assertEquals("incorrect value", result.unscaledValue(), bdNumber.unscaledValue()); - assertTrue("incorrect value", result.scale() == 0); - - bdNumber = new BigDecimal(0.000000); - result = bdNumber.stripTrailingZeros(); - assertEquals("incorrect value", result.unscaledValue(), bdNumber.unscaledValue()); - assertTrue("incorrect value", result.scale() == 0); - } - - /** - * @tests java.math.BigDecimal#stripTrailingZeros() stripTrailingZeros() for - * positive BigDecimal - */ - public void test_stripTrailingZeros() { - - String s = "00000000100000000100000000.000000000100000000"; - int iScale = 10; - BigDecimal bdValue = new BigDecimal("1000000001000000000000000001"); - BigDecimal bdNumber = new BigDecimal(s); - BigDecimal bdResult = bdNumber.stripTrailingZeros(); - assertEquals("incorrect value", bdResult.unscaledValue(), bdValue.unscaledValue()); - assertTrue("incorrect value", bdResult.scale() == iScale); - - s = "1000.0"; - iScale = -3; - BigDecimal bd = new BigDecimal("1"); - bdNumber = new BigDecimal(s); - bdResult = bdNumber.stripTrailingZeros(); - assertEquals("incorrect value", bdResult.unscaledValue(), bd.unscaledValue()); - assertTrue("incorrect value", bdResult.scale() == iScale); - } -} diff --git a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigDecimalScaleOperationsTest.java b/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigDecimalScaleOperationsTest.java deleted file mode 100644 index cb3858c5..00000000 --- a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigDecimalScaleOperationsTest.java +++ /dev/null @@ -1,451 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @author Elena Semukhina - * @version $Revision$ - */ - -package org.apache.harmony.math.tests.java.math; - -import junit.framework.TestCase; - -import java.math.BigDecimal; -import java.math.BigInteger; -import java.math.RoundingMode; -/** - * Class: java.math.BigDecimal - * Methods: movePointLeft, movePointRight, scale, setScale, unscaledValue * - */ -public class BigDecimalScaleOperationsTest extends TestCase { - /** - * Check the default scale - */ - public void testScaleDefault() { - String a = "1231212478987482988429808779810457634781384756794987"; - int cScale = 0; - BigDecimal aNumber = new BigDecimal(new BigInteger(a)); - assertTrue("incorrect scale", aNumber.scale() == cScale); - } - - /** - * Check a negative scale - */ - public void testScaleNeg() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = -10; - int cScale = -10; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - assertTrue("incorrect scale", aNumber.scale() == cScale); - } - - /** - * Check a positive scale - */ - public void testScalePos() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 10; - int cScale = 10; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - assertTrue("incorrect scale", aNumber.scale() == cScale); - } - - /** - * Check the zero scale - */ - public void testScaleZero() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 0; - int cScale = 0; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - assertTrue("incorrect scale", aNumber.scale() == cScale); - } - - /** - * Check the unscaled value - */ - public void testUnscaledValue() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 100; - BigInteger bNumber = new BigInteger(a); - BigDecimal aNumber = new BigDecimal(bNumber, aScale); - assertTrue("incorrect unscaled value", aNumber.unscaledValue().equals(bNumber)); - } - - /** - * Set a greater new scale - */ - public void testSetScaleGreater() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 18; - int newScale = 28; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = aNumber.setScale(newScale); - assertTrue("incorrect scale", bNumber.scale() == newScale); - assertEquals("incorrect value", 0, bNumber.compareTo(aNumber)); - } - - /** - * Set a less new scale; this.scale == 8; newScale == 5. - */ - public void testSetScaleLess() { - String a = "2.345726458768760000E+10"; - int newScale = 5; - BigDecimal aNumber = new BigDecimal(a); - BigDecimal bNumber = aNumber.setScale(newScale); - assertTrue("incorrect scale", bNumber.scale() == newScale); - assertEquals("incorrect value", 0, bNumber.compareTo(aNumber)); - } - - /** - * Verify an exception when setting a new scale - */ - public void testSetScaleException() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 28; - int newScale = 18; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - try { - aNumber.setScale(newScale); - fail("ArithmeticException has not been caught"); - } catch (ArithmeticException e) { - assertEquals("Improper exception message", "Rounding necessary", e.getMessage()); - } - } - - /** - * Set the same new scale - */ - public void testSetScaleSame() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 18; - int newScale = 18; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = aNumber.setScale(newScale); - assertTrue("incorrect scale", bNumber.scale() == newScale); - assertTrue("incorrect value", bNumber.equals(aNumber)); - } - - /** - * Set a new scale - */ - public void testSetScaleRoundUp() { - String a = "1231212478987482988429808779810457634781384756794987"; - String b = "123121247898748298842980877981045763478139"; - int aScale = 28; - int newScale = 18; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = aNumber.setScale(newScale, BigDecimal.ROUND_UP); - assertTrue("incorrect scale", bNumber.scale() == newScale); - assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(b)); - } - - /** - * Set a new scale - */ - public void testSetScaleRoundDown() { - String a = "1231212478987482988429808779810457634781384756794987"; - String b = "123121247898748298842980877981045763478138"; - int aScale = 28; - int newScale = 18; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = aNumber.setScale(newScale, BigDecimal.ROUND_DOWN); - assertTrue("incorrect scale", bNumber.scale() == newScale); - assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(b)); - } - - /** - * Set a new scale - */ - public void testSetScaleRoundCeiling() { - String a = "1231212478987482988429808779810457634781384756794987"; - String b = "123121247898748298842980877981045763478139"; - int aScale = 28; - int newScale = 18; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = aNumber.setScale(newScale, BigDecimal.ROUND_CEILING); - assertTrue("incorrect scale", bNumber.scale() == newScale); - assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(b)); - } - - /** - * Set a new scale - */ - public void testSetScaleRoundFloor() { - String a = "1231212478987482988429808779810457634781384756794987"; - String b = "123121247898748298842980877981045763478138"; - int aScale = 28; - int newScale = 18; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = aNumber.setScale(newScale, BigDecimal.ROUND_FLOOR); - assertTrue("incorrect scale", bNumber.scale() == newScale); - assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(b)); - } - - /** - * Set a new scale - */ - public void testSetScaleRoundHalfUp() { - String a = "1231212478987482988429808779810457634781384756794987"; - String b = "123121247898748298842980877981045763478138"; - int aScale = 28; - int newScale = 18; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = aNumber.setScale(newScale, BigDecimal.ROUND_HALF_UP); - assertTrue("incorrect scale", bNumber.scale() == newScale); - assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(b)); - } - - /** - * Set a new scale - */ - public void testSetScaleRoundHalfDown() { - String a = "1231212478987482988429808779810457634781384756794987"; - String b = "123121247898748298842980877981045763478138"; - int aScale = 28; - int newScale = 18; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = aNumber.setScale(newScale, BigDecimal.ROUND_HALF_DOWN); - assertTrue("incorrect scale", bNumber.scale() == newScale); - assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(b)); - } - - /** - * Set a new scale - */ - public void testSetScaleRoundHalfEven() { - String a = "1231212478987482988429808779810457634781384756794987"; - String b = "123121247898748298842980877981045763478138"; - int aScale = 28; - int newScale = 18; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = aNumber.setScale(newScale, BigDecimal.ROUND_HALF_EVEN); - assertTrue("incorrect scale", bNumber.scale() == newScale); - assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(b)); - } - - /** - * SetScale(int, RoundingMode) - */ - public void testSetScaleIntRoundingMode() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 28; - int newScale = 18; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal result = aNumber.setScale(newScale, RoundingMode.HALF_EVEN); - String res = "123121247898748298842980.877981045763478138"; - int resScale = 18; - assertEquals("incorrect value", res, result.toString()); - assertEquals("incorrect scale", resScale, result.scale()); - } - - /** - * Move the decimal point to the left; the shift value is positive - */ - public void testMovePointLeftPos() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 28; - int shift = 18; - int resScale = 46; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = aNumber.movePointLeft(shift); - assertTrue("incorrect scale", bNumber.scale() == resScale); - assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(a)); - } - - /** - * Move the decimal point to the left; the shift value is positive - */ - public void testMovePointLeftNeg() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 28; - int shift = -18; - int resScale = 10; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = aNumber.movePointLeft(shift); - assertTrue("incorrect scale", bNumber.scale() == resScale); - assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(a)); - } - - public void testMovePointLeftEx() { - BigDecimal a = new BigDecimal("12345.6789012345678901234567890123456789"); - BigDecimal res = a.movePointLeft(10); - assertEquals("incorrect scale", 44, res.scale()); - assertEquals("incorrect value", "0.00000123456789012345678901234567890123456789", res.toString()); - res = a.movePointLeft(-50); - assertEquals("incorrect scale", 0, res.scale()); - assertEquals("incorrect value", "1234567890123456789012345678901234567890000000000000000", res.toString()); - try { - res = a.movePointLeft(Integer.MAX_VALUE - 2); -// assertEquals("incorrect value", "0.0938025", res[1].toString()); - fail("ArithmeticException is not thrown"); - } catch (ArithmeticException e) { - // expected - } - } - - /** - * Move the decimal point to the right; the shift value is positive - */ - public void testMovePointRightPosGreater() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 28; - int shift = 18; - int resScale = 10; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = aNumber.movePointRight(shift); - assertTrue("incorrect scale", bNumber.scale() == resScale); - assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(a)); - } - - /** - * Move the decimal point to the right; the shift value is positive - */ - public void testMovePointRightPosLess() { - String a = "1231212478987482988429808779810457634781384756794987"; - String b = "123121247898748298842980877981045763478138475679498700"; - int aScale = 28; - int shift = 30; - int resScale = 0; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = aNumber.movePointRight(shift); - assertTrue("incorrect scale", bNumber.scale() == resScale); - assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(b)); - } - - /** - * Move the decimal point to the right; the shift value is positive - */ - public void testMovePointRightNeg() { - String a = "1231212478987482988429808779810457634781384756794987"; - int aScale = 28; - int shift = -18; - int resScale = 46; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - BigDecimal bNumber = aNumber.movePointRight(shift); - assertTrue("incorrect scale", bNumber.scale() == resScale); - assertTrue("incorrect value", bNumber.unscaledValue().toString().equals(a)); - } - - /** - * Move the decimal point to the right when the scale overflows - */ - public void testMovePointRightException() { - String a = "12312124789874829887348723648726347429808779810457634781384756794987"; - int aScale = Integer.MAX_VALUE; //2147483647 - int shift = -18; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - try { - aNumber.movePointRight(shift); - fail("ArithmeticException has not been caught"); - } catch (ArithmeticException e) { - assertEquals("Improper exception message", "Underflow", e.getMessage()); - } - } - - public void testMovePointRightEx() { - BigDecimal a = new BigDecimal("12345.6789012345678901234567890123456789"); - BigDecimal res = a.movePointRight(10); - assertEquals("incorrect scale", 24, res.scale()); - assertEquals("incorrect value", "123456789012345.678901234567890123456789", res.toString()); - res = a.movePointRight(-50); - assertEquals("incorrect scale", 84, res.scale()); - assertEquals("incorrect value", "1.23456789012345678901234567890123456789E-46", res.toString()); - try { - res = a.movePointRight(Integer.MIN_VALUE + 2); - fail("ArithmeticException is not thrown"); - } catch (ArithmeticException e) { - // expected - } - } - - // Throws OutOfMemoryError instead of ArithmeticException! - public void testMovePointRightEx2() { - BigDecimal a = new BigDecimal("123456789012345678901234567890123456789E25"); - try { - BigDecimal res = a.movePointRight(Integer.MAX_VALUE - 2); - fail("ArithmeticException is not thrown"); - } catch (ArithmeticException e) { - // expected - } - } - - /** - * scaleByPowerOfTen(int n) - */ - public void testScaleByPowerOfTenEx() { - BigDecimal a = new BigDecimal("12345.6789012345678901234567890123456789"); - BigDecimal res = a.movePointRight(10); - assertEquals("incorrect scale", 24, res.scale()); - assertEquals("incorrect value", "123456789012345.678901234567890123456789", res.toString()); - res = a.scaleByPowerOfTen(-50); - assertEquals("incorrect scale", 84, res.scale()); - assertEquals("incorrect value", "1.23456789012345678901234567890123456789E-46", res.toString()); - res = a.scaleByPowerOfTen(50); - assertEquals("incorrect scale", -16, res.scale()); - assertEquals("incorrect value", "1.23456789012345678901234567890123456789E+54", res.toString()); - try { - res = a.scaleByPowerOfTen(Integer.MIN_VALUE + 2); - fail("ArithmeticException is not thrown"); - } catch (ArithmeticException e) { - // expected - } - a = new BigDecimal("123456789012345678901234567890123456789E25"); - try { - res = a.scaleByPowerOfTen(Integer.MAX_VALUE - 2); - fail("ArithmeticException is not thrown"); - } catch (ArithmeticException e) { - // expected - } - } - - /** - * precision() - */ - public void testPrecision() { - String a = "12312124789874829887348723648726347429808779810457634781384756794987"; - int aScale = 14; - BigDecimal aNumber = new BigDecimal(new BigInteger(a), aScale); - int prec = aNumber.precision(); - assertEquals(68, prec); - } - -/// ANDROID ADDED - - /** - * check that setScale with a scale greater to the existing scale does not - * change the value. - */ - public void testSetScale() { - BigDecimal x1 = new BigDecimal(1.23400); - BigDecimal x2 = x1.setScale(75); - - assertEquals(0, x1.compareTo(x2)); - assertEquals(0, x2.compareTo(x1)); - - x1.precision(); - - assertEquals(0, x1.compareTo(x2)); - assertEquals(0, x2.compareTo(x1)); - - x2.precision(); - - assertEquals(0, x1.compareTo(x2)); - assertEquals(0, x2.compareTo(x1)); - } - - -} diff --git a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerAddTest.java b/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerAddTest.java deleted file mode 100644 index f9745140..00000000 --- a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerAddTest.java +++ /dev/null @@ -1,497 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @author Elena Semukhina - * @version $Revision$ - */ - -package org.apache.harmony.math.tests.java.math; - -import java.math.BigInteger; - -import junit.framework.TestCase; - -/** - * Class: java.math.BigInteger - * Method: add - */ -public class BigIntegerAddTest extends TestCase { - /** - * Add two positive numbers of the same length - */ - public void testCase1() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {11, 22, 33, 44, 55, 66, 77, 11, 22, 33}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Add two negative numbers of the same length - */ - public void testCase2() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = -1; - int bSign = -1; - byte rBytes[] = {-12, -23, -34, -45, -56, -67, -78, -12, -23, -33}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Add two numbers of the same length. - * The first one is positive and the second is negative. - * The first one is greater in absolute value. - */ - public void testCase3() { - byte aBytes[] = {3, 4, 5, 6, 7, 8, 9}; - byte bBytes[] = {1, 2, 3, 4, 5, 6, 7}; - byte rBytes[] = {2, 2, 2, 2, 2, 2, 2}; - int aSign = 1; - int bSign = -1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Add two numbers of the same length. - * The first one is negative and the second is positive. - * The first one is greater in absolute value. - */ - public void testCase4() { - byte aBytes[] = {3, 4, 5, 6, 7, 8, 9}; - byte bBytes[] = {1, 2, 3, 4, 5, 6, 7}; - byte rBytes[] = {-3, -3, -3, -3, -3, -3, -2}; - int aSign = -1; - int bSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Add two numbers of the same length. - * The first is positive and the second is negative. - * The first is less in absolute value. - */ - public void testCase5() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; - byte bBytes[] = {3, 4, 5, 6, 7, 8, 9}; - byte rBytes[] = {-3, -3, -3, -3, -3, -3, -2}; - int aSign = 1; - int bSign = -1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Add two numbers of the same length. - * The first one is negative and the second is positive. - * The first one is less in absolute value. - */ - public void testCase6() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; - byte bBytes[] = {3, 4, 5, 6, 7, 8, 9}; - byte rBytes[] = {2, 2, 2, 2, 2, 2, 2}; - int aSign = -1; - int bSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Add two positive numbers of different length. - * The first is longer. - */ - public void testCase7() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {1, 2, 3, 4, 15, 26, 37, 41, 52, 63, 74, 15, 26, 37}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Add two positive numbers of different length. - * The second is longer. - */ - public void testCase8() { - byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - byte bBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; - byte rBytes[] = {1, 2, 3, 4, 15, 26, 37, 41, 52, 63, 74, 15, 26, 37}; - BigInteger aNumber = new BigInteger(aBytes); - BigInteger bNumber = new BigInteger(bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Add two negative numbers of different length. - * The first is longer. - */ - public void testCase9() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = -1; - int bSign = -1; - byte rBytes[] = {-2, -3, -4, -5, -16, -27, -38, -42, -53, -64, -75, -16, -27, -37}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Add two negative numbers of different length. - * The second is longer. - */ - public void testCase10() { - byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - byte bBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; - int aSign = -1; - int bSign = -1; - byte rBytes[] = {-2, -3, -4, -5, -16, -27, -38, -42, -53, -64, -75, -16, -27, -37}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Add two numbers of different length and sign. - * The first is positive. - * The first is longer. - */ - public void testCase11() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = 1; - int bSign = -1; - byte rBytes[] = {1, 2, 3, 3, -6, -15, -24, -40, -49, -58, -67, -6, -15, -23}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Add two numbers of different length and sign. - * The first is positive. - * The second is longer. - */ - public void testCase12() { - byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - byte bBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; - int aSign = 1; - int bSign = -1; - byte rBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Add two numbers of different length and sign. - * The first is negative. - * The first is longer. - */ - public void testCase13() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = -1; - int bSign = 1; - byte rBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Add two numbers of different length and sign. - * The first is negative. - * The second is longer. - */ - public void testCase14() { - byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - byte bBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; - int aSign = -1; - int bSign = 1; - byte rBytes[] = {1, 2, 3, 3, -6, -15, -24, -40, -49, -58, -67, -6, -15, -23}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Add two equal numbers of different signs - */ - public void testCase15() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; - byte bBytes[] = {1, 2, 3, 4, 5, 6, 7}; - byte rBytes[] = {0}; - int aSign = -1; - int bSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 0, result.signum()); - } - - /** - * Add zero to a number - */ - public void testCase16() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; - byte bBytes[] = {0}; - byte rBytes[] = {1, 2, 3, 4, 5, 6, 7}; - int aSign = 1; - int bSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Add a number to zero - */ - public void testCase17() { - byte aBytes[] = {0}; - byte bBytes[] = {1, 2, 3, 4, 5, 6, 7}; - byte rBytes[] = {1, 2, 3, 4, 5, 6, 7}; - int aSign = 1; - int bSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Add zero to zero - */ - public void testCase18() { - byte aBytes[] = {0}; - byte bBytes[] = {0}; - byte rBytes[] = {0}; - int aSign = 1; - int bSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 0, result.signum()); - } - - /** - * Add ZERO to a number - */ - public void testCase19() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; - byte rBytes[] = {1, 2, 3, 4, 5, 6, 7}; - int aSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = BigInteger.ZERO; - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Add a number to zero - */ - public void testCase20() { - byte bBytes[] = {1, 2, 3, 4, 5, 6, 7}; - byte rBytes[] = {1, 2, 3, 4, 5, 6, 7}; - int bSign = 1; - BigInteger aNumber = BigInteger.ZERO; - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Add ZERO to ZERO - */ - public void testCase21() { - byte rBytes[] = {0}; - BigInteger aNumber = BigInteger.ZERO; - BigInteger bNumber = BigInteger.ZERO; - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 0, result.signum()); - } - - /** - * Add ONE to ONE - */ - public void testCase22() { - byte rBytes[] = {2}; - BigInteger aNumber = BigInteger.ONE; - BigInteger bNumber = BigInteger.ONE; - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Add two numbers so that carry is 1 - */ - public void testCase23() { - byte aBytes[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; - byte bBytes[] = {-1, -1, -1, -1, -1, -1, -1, -1}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {1, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -2}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.add(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } -} diff --git a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerAndTest.java b/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerAndTest.java deleted file mode 100644 index 8ff9b6bd..00000000 --- a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerAndTest.java +++ /dev/null @@ -1,434 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @author Elena Semukhina - * @version $Revision$ - */ - -package org.apache.harmony.math.tests.java.math; - -import java.math.BigInteger; - -import junit.framework.TestCase; - -/** - * Class: java.math.BigInteger - * Method: and - */ -public class BigIntegerAndTest extends TestCase { - /** - * And for zero and a positive number - */ - public void testZeroPos() { - byte aBytes[] = {0}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = 0; - int bSign = 1; - byte rBytes[] = {0}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 0, result.signum()); - } - - /** - * And for zero and a negative number - */ - public void testZeroNeg() { - byte aBytes[] = {0}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = 0; - int bSign = -1; - byte rBytes[] = {0}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 0, result.signum()); - } - - /** - * And for a positive number and zero - */ - public void testPosZero() { - byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - byte bBytes[] = {0}; - int aSign = 1; - int bSign = 0; - byte rBytes[] = {0}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 0, result.signum()); - } - - /** - * And for a negative number and zero - */ - public void testNegPos() { - byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - byte bBytes[] = {0}; - int aSign = -1; - int bSign = 0; - byte rBytes[] = {0}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 0, result.signum()); - } - - /** - * And for zero and zero - */ - public void testZeroZero() { - byte aBytes[] = {0}; - byte bBytes[] = {0}; - int aSign = 0; - int bSign = 0; - byte rBytes[] = {0}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 0, result.signum()); - } - - /** - * And for zero and one - */ - public void testZeroOne() { - BigInteger aNumber = BigInteger.ZERO; - BigInteger bNumber = BigInteger.ONE; - BigInteger result = aNumber.and(bNumber); - assertTrue(result.equals(BigInteger.ZERO)); - assertEquals("incorrect sign", 0, result.signum()); - } - - /** - * And for one and one - */ - public void testOneOne() { - BigInteger aNumber = BigInteger.ONE; - BigInteger bNumber = BigInteger.ONE; - BigInteger result = aNumber.and(bNumber); - assertTrue(result.equals(BigInteger.ONE)); - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * And for two positive numbers of the same length - */ - public void testPosPosSameLength() { - byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {0, -128, 56, 100, 4, 4, 17, 37, 16, 1, 64, 1, 10, 3}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * And for two positive numbers; the first is longer - */ - public void testPosPosFirstLonger() { - byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {0, -2, -76, 88, 44, 1, 2, 17, 35, 16, 9, 2, 5, 6, 21}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * And for two positive numbers; the first is shorter - */ - public void testPosPosFirstShorter() { - byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {0, -2, -76, 88, 44, 1, 2, 17, 35, 16, 9, 2, 5, 6, 21}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * And for two negative numbers of the same length - */ - public void testNegNegSameLength() { - byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = -1; - int bSign = -1; - byte rBytes[] = {-1, 1, 2, 3, 3, 0, 65, -96, -48, -124, -60, 12, -40, -31, 97}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * And for two negative numbers; the first is longer - */ - public void testNegNegFirstLonger() { - byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = -1; - int bSign = -1; - byte rBytes[] = {-1, 127, -10, -57, -101, 1, 2, 2, 2, -96, -16, 8, -40, -59, 68, -88, -88, 16, 73}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * And for two negative numbers; the first is shorter - */ - public void testNegNegFirstShorter() { - byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - int aSign = -1; - int bSign = -1; - byte rBytes[] = {-1, 127, -10, -57, -101, 1, 2, 2, 2, -96, -16, 8, -40, -59, 68, -88, -88, 16, 73}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * And for two numbers of different signs and the same length - */ - public void testPosNegSameLength() { - byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = 1; - int bSign = -1; - byte rBytes[] = {0, -6, -80, 72, 8, 75, 2, -79, 34, 16, -119}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * And for two numbers of different signs and the same length - */ - public void testNegPosSameLength() { - byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = -1; - int bSign = 1; - byte rBytes[] = {0, -2, 125, -60, -104, 1, 10, 6, 2, 32, 56, 2, 4, 4, 21}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * And for a negative and a positive numbers; the first is longer - */ - public void testNegPosFirstLonger() { - byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = -1; - int bSign = 1; - byte rBytes[] = {73, -92, -48, 4, 12, 6, 4, 32, 48, 64, 0, 8, 3}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * And for a negative and a positive numbers; the first is shorter - */ - public void testNegPosFirstShorter() { - byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - int aSign = -1; - int bSign = 1; - byte rBytes[] = {0, -128, 9, 56, 100, 0, 0, 1, 1, 90, 1, -32, 0, 10, -126, 21, 82, -31, -95}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * And for a positive and a negative numbers; the first is longer - */ - public void testPosNegFirstLonger() { - byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = 1; - int bSign = -1; - byte rBytes[] = {0, -128, 9, 56, 100, 0, 0, 1, 1, 90, 1, -32, 0, 10, -126, 21, 82, -31, -95}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * And for a positive and a negative numbers; the first is shorter - */ - public void testPosNegFirstShorter() { - byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - int aSign = 1; - int bSign = -1; - byte rBytes[] = {73, -92, -48, 4, 12, 6, 4, 32, 48, 64, 0, 8, 3}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Test for a special case - */ - public void testSpecialCase1() { - byte aBytes[] = {-1, -1, -1, -1}; - byte bBytes[] = {5, -4, -3, -2}; - int aSign = -1; - int bSign = -1; - byte rBytes[] = {-1, 0, 0, 0, 0}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Test for a special case - */ - public void testSpecialCase2() { - byte aBytes[] = {-51}; - byte bBytes[] = {-52, -51, -50, -49, -48}; - int aSign = -1; - int bSign = 1; - byte rBytes[] = {0, -52, -51, -50, -49, 16}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.and(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } -} diff --git a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerCompareTest.java b/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerCompareTest.java deleted file mode 100644 index 62de6a1b..00000000 --- a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerCompareTest.java +++ /dev/null @@ -1,535 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @author Elena Semukhina - * @version $Revision$ - */ - -package org.apache.harmony.math.tests.java.math; - -import java.math.BigInteger; - -import junit.framework.TestCase; - -/** - * Class: java.math.BigInteger - * Methods: abs, compareTo, equals, max, min, negate, signum - */ -public class BigIntegerCompareTest extends TestCase { - /** - * abs() for a positive number - */ - public void testAbsPositive() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; - int aSign = 1; - byte rBytes[] = {1, 2, 3, 4, 5, 6, 7}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger result = aNumber.abs(); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * abs() for a negative number - */ - public void testAbsNegative() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; - int aSign = -1; - byte rBytes[] = {1, 2, 3, 4, 5, 6, 7}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger result = aNumber.abs(); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * compareTo(BigInteger a). - * Compare two positive numbers. - * The first is greater. - */ - public void testCompareToPosPos1() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = 1; - int bSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - assertEquals(1, aNumber.compareTo(bNumber)); - } - - /** - * compareTo(BigInteger a). - * Compare two positive numbers. - * The first is less. - */ - public void testCompareToPosPos2() { - byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = 1; - int bSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - assertEquals(-1, aNumber.compareTo(bNumber)); - } - - /** - * compareTo(BigInteger a). - * Compare two equal positive numbers. - */ - public void testCompareToEqualPos() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = 1; - int bSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - assertEquals(0, aNumber.compareTo(bNumber)); - } - - /** - * compareTo(BigInteger a). - * Compare two negative numbers. - * The first is greater in absolute value. - */ - public void testCompareToNegNeg1() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = -1; - int bSign = -1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - assertEquals(-1, aNumber.compareTo(bNumber)); - } - - /** - * compareTo(BigInteger a). - * Compare two negative numbers. - * The first is less in absolute value. - */ - public void testCompareNegNeg2() { - byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = -1; - int bSign = -1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - assertEquals(1, aNumber.compareTo(bNumber)); - } - - /** - * compareTo(BigInteger a). - * Compare two equal negative numbers. - */ - public void testCompareToEqualNeg() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = -1; - int bSign = -1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - assertEquals(0, aNumber.compareTo(bNumber)); - } - - /** - * compareTo(BigInteger a). - * Compare two numbers of different signs. - * The first is positive. - */ - public void testCompareToDiffSigns1() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = 1; - int bSign = -1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - assertEquals(1, aNumber.compareTo(bNumber)); - } - - /** - * compareTo(BigInteger a). - * Compare two numbers of different signs. - * The first is negative. - */ - public void testCompareToDiffSigns2() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = -1; - int bSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - assertEquals(-1, aNumber.compareTo(bNumber)); - } - - /** - * compareTo(BigInteger a). - * Compare a positive number to ZERO. - */ - public void testCompareToPosZero() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = BigInteger.ZERO; - assertEquals(1, aNumber.compareTo(bNumber)); - } - - /** - * compareTo(BigInteger a). - * Compare ZERO to a positive number. - */ - public void testCompareToZeroPos() { - byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - int bSign = 1; - BigInteger aNumber = BigInteger.ZERO; - BigInteger bNumber = new BigInteger(bSign, bBytes); - assertEquals(-1, aNumber.compareTo(bNumber)); - } - - /** - * compareTo(BigInteger a). - * Compare a negative number to ZERO. - */ - public void testCompareToNegZero() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = -1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = BigInteger.ZERO; - assertEquals(-1, aNumber.compareTo(bNumber)); - } - - /** - * compareTo(BigInteger a). - * Compare ZERO to a negative number. - */ - public void testCompareToZeroNeg() { - byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - int bSign = -1; - BigInteger aNumber = BigInteger.ZERO; - BigInteger bNumber = new BigInteger(bSign, bBytes); - assertEquals(1, aNumber.compareTo(bNumber)); - } - - /** - * compareTo(BigInteger a). - * Compare ZERO to ZERO. - */ - public void testCompareToZeroZero() { - BigInteger aNumber = BigInteger.ZERO; - BigInteger bNumber = BigInteger.ZERO; - assertEquals(0, aNumber.compareTo(bNumber)); - } - - /** - * equals(Object obj). - * obj is not a BigInteger - */ - public void testEqualsObject() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - Object obj = new Object(); - assertFalse(aNumber.equals(obj)); - } - - /** - * equals(null). - */ - public void testEqualsNull() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - assertFalse(aNumber.equals(null)); - } - - /** - * equals(Object obj). - * obj is a BigInteger. - * numbers are equal. - */ - public void testEqualsBigIntegerTrue() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = 1; - int bSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - Object bNumber = new BigInteger(bSign, bBytes); - assertTrue(aNumber.equals(bNumber)); - } - - /** - * equals(Object obj). - * obj is a BigInteger. - * numbers are not equal. - */ - public void testEqualsBigIntegerFalse() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - byte bBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = 1; - int bSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - Object bNumber = new BigInteger(bSign, bBytes); - assertFalse(aNumber.equals(bNumber)); - } - - /** - * max(BigInteger val). - * the first is greater. - */ - public void testMaxGreater() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - byte bBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.max(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertTrue("incorrect sign", result.signum() == 1); - } - - /** - * max(BigInteger val). - * the first is less. - */ - public void testMaxLess() { - byte aBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; - byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.max(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertTrue("incorrect sign", result.signum() == 1); - } - - /** - * max(BigInteger val). - * numbers are equal. - */ - public void testMaxEqual() { - byte aBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; - byte bBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.max(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * max(BigInteger val). - * max of negative and ZERO. - */ - public void testMaxNegZero() { - byte aBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = -1; - byte rBytes[] = {0}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = BigInteger.ZERO; - BigInteger result = aNumber.max(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertTrue("incorrect sign", result.signum() == 0); - } - - /** - * min(BigInteger val). - * the first is greater. - */ - public void testMinGreater() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - byte bBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.min(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * min(BigInteger val). - * the first is less. - */ - public void testMinLess() { - byte aBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; - byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.min(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * min(BigInteger val). - * numbers are equal. - */ - public void testMinEqual() { - byte aBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; - byte bBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.min(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertTrue("incorrect sign", result.signum() == 1); - } - - /** - * max(BigInteger val). - * min of positive and ZERO. - */ - public void testMinPosZero() { - byte aBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = 1; - byte rBytes[] = {0}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = BigInteger.ZERO; - BigInteger result = aNumber.min(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertTrue("incorrect sign", result.signum() == 0); - } - - /** - * negate() a positive number. - */ - public void testNegatePositive() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = 1; - byte rBytes[] = {-13, -57, -101, 1, 75, -90, -46, -92, -4, 14, -36, -27, -4, -91}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger result = aNumber.negate(); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertTrue("incorrect sign", result.signum() == -1); - } - - /** - * negate() a negative number. - */ - public void testNegateNegative() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = -1; - byte rBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger result = aNumber.negate(); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertTrue("incorrect sign", result.signum() == 1); - } - - /** - * negate() ZERO. - */ - public void testNegateZero() { - byte rBytes[] = {0}; - BigInteger aNumber = BigInteger.ZERO; - BigInteger result = aNumber.negate(); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 0, result.signum()); - } - - /** - * signum() of a positive number. - */ - public void testSignumPositive() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - assertEquals("incorrect sign", 1, aNumber.signum()); - } - - /** - * signum() of a negative number. - */ - public void testSignumNegative() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; - int aSign = -1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - assertEquals("incorrect sign", -1, aNumber.signum()); - } - - /** - * signum() of ZERO. - */ - public void testSignumZero() { - BigInteger aNumber = BigInteger.ZERO; - assertEquals("incorrect sign", 0, aNumber.signum()); - } -} diff --git a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerConvertTest.java b/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerConvertTest.java deleted file mode 100644 index 1a52df69..00000000 --- a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerConvertTest.java +++ /dev/null @@ -1,794 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @author Elena Semukhina - * @version $Revision$ - */ - -package org.apache.harmony.math.tests.java.math; - -import java.math.BigInteger; - -import junit.framework.TestCase; - -/** - * Class: java.math.BigInteger - * Methods: intValue, longValue, toByteArray(), valueOf(long val), - * floatValue(), doubleValue() - */ -public class BigIntegerConvertTest extends TestCase { - /** - * Return the double value of ZERO. - */ - public void testDoubleValueZero() { - String a = "0"; - double result = 0.0; - double aNumber = new BigInteger(a).doubleValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive number to a double value. - * The number's length is less than 64 bits. - */ - public void testDoubleValuePositive1() { - String a = "27467238945"; - double result = 2.7467238945E10; - double aNumber = new BigInteger(a).doubleValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive number to a double value. - * The number's bit length is inside [63, 1024]. - */ - public void testDoubleValuePositive2() { - String a = "2746723894572364578265426346273456972"; - double result = 2.7467238945723645E36; - double aNumber = new BigInteger(a).doubleValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a negative number to a double value. - * The number's bit length is less than 64 bits. - */ - public void testDoubleValueNegative1() { - String a = "-27467238945"; - double result = -2.7467238945E10; - double aNumber = new BigInteger(a).doubleValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a negative number to a double value. - * The number's bit length is inside [63, 1024]. - */ - public void testDoubleValueNegative2() { - String a = "-2746723894572364578265426346273456972"; - double result = -2.7467238945723645E36; - double aNumber = new BigInteger(a).doubleValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive number to a double value. - * Rounding is needed. - * The rounding bit is 1 and the next bit to the left is 1. - */ - public void testDoubleValuePosRounded1() { - byte[] a = {-128, 1, 2, 3, 4, 5, 60, 23, 1, -3, -5}; - int aSign = 1; - double result = 1.54747264387948E26; - double aNumber = new BigInteger(aSign, a).doubleValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive number to a double value. - * Rounding is needed. - * The rounding bit is 1 and the next bit to the left is 0 - * but some of dropped bits are 1s. - */ - public void testDoubleValuePosRounded2() { - byte[] a = {-128, 1, 2, 3, 4, 5, 36, 23, 1, -3, -5}; - int aSign = 1; - double result = 1.547472643879479E26; - double aNumber = new BigInteger(aSign, a).doubleValue(); - assertTrue(aNumber == result); - } - /** - * Convert a positive number to a double value. - * Rounding is NOT needed. - */ - public void testDoubleValuePosNotRounded() { - byte[] a = {-128, 1, 2, 3, 4, 5, -128, 23, 1, -3, -5}; - int aSign = 1; - double result = 1.5474726438794828E26; - double aNumber = new BigInteger(aSign, a).doubleValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive number to a double value. - * Rounding is needed. - */ - public void testDoubleValueNegRounded1() { - byte[] a = {-128, 1, 2, 3, 4, 5, 60, 23, 1, -3, -5}; - int aSign = -1; - double result = -1.54747264387948E26; - double aNumber = new BigInteger(aSign, a).doubleValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive number to a double value. - * Rounding is needed. - * The rounding bit is 1 and the next bit to the left is 0 - * but some of dropped bits are 1s. - */ - public void testDoubleValueNegRounded2() { - byte[] a = {-128, 1, 2, 3, 4, 5, 36, 23, 1, -3, -5}; - int aSign = -1; - double result = -1.547472643879479E26; - double aNumber = new BigInteger(aSign, a).doubleValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive number to a double value. - * Rounding is NOT needed. - */ - public void testDoubleValueNegNotRounded() { - byte[] a = {-128, 1, 2, 3, 4, 5, -128, 23, 1, -3, -5}; - int aSign = -1; - double result = -1.5474726438794828E26; - double aNumber = new BigInteger(aSign, a).doubleValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive number to a double value. - * The exponent is 1023 and the mantissa is all 1s. - * The rounding bit is 0. - * The result is Double.MAX_VALUE. - */ - public void testDoubleValuePosMaxValue() { - byte[] a = {0, -1, -1, -1, -1, -1, -1, -8, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 - }; - int aSign = 1; - double aNumber = new BigInteger(aSign, a).doubleValue(); - assertTrue(aNumber == Double.MAX_VALUE); - } - - /** - * Convert a negative number to a double value. - * The exponent is 1023 and the mantissa is all 1s. - * The result is -Double.MAX_VALUE. - */ - public void testDoubleValueNegMaxValue() { - byte[] a = {0, -1, -1, -1, -1, -1, -1, -8, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, - -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 - }; - int aSign = -1; - double aNumber = new BigInteger(aSign, a).doubleValue(); - assertTrue(aNumber == -Double.MAX_VALUE); - } - - /** - * Convert a positive number to a double value. - * The exponent is 1023 and the mantissa is all 1s. - * The rounding bit is 1. - * The result is Double.POSITIVE_INFINITY. - */ - public void testDoubleValuePositiveInfinity1() { - byte[] a = {-1, -1, -1, -1, -1, -1, -1, -8, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - }; - int aSign = 1; - double aNumber = new BigInteger(aSign, a).doubleValue(); - assertTrue(aNumber == Double.POSITIVE_INFINITY); - } - - /** - * Convert a positive number to a double value. - * The number's bit length is greater than 1024. - */ - public void testDoubleValuePositiveInfinity2() { - String a = "2746723894572364578265426346273456972283746872364768676747462342342342342342342342323423423423423423426767456345745293762384756238475634563456845634568934568347586346578648576478568456457634875673845678456786587345873645767456834756745763457863485768475678465783456702897830296720476846578634576384567845678346573465786457863"; - double aNumber = new BigInteger(a).doubleValue(); - assertTrue(aNumber == Double.POSITIVE_INFINITY); - } - - /** - * Convert a negative number to a double value. - * The number's bit length is greater than 1024. - */ - public void testDoubleValueNegativeInfinity1() { - String a = "-2746723894572364578265426346273456972283746872364768676747462342342342342342342342323423423423423423426767456345745293762384756238475634563456845634568934568347586346578648576478568456457634875673845678456786587345873645767456834756745763457863485768475678465783456702897830296720476846578634576384567845678346573465786457863"; - double aNumber = new BigInteger(a).doubleValue(); - assertTrue(aNumber == Double.NEGATIVE_INFINITY); - } - - /** - * Convert a negative number to a double value. - * The exponent is 1023 and the mantissa is all 0s. - * The rounding bit is 0. - * The result is Double.NEGATIVE_INFINITY. - */ - public void testDoubleValueNegativeInfinity2() { - byte[] a = {-1, -1, -1, -1, -1, -1, -1, -8, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - }; - int aSign = -1; - double aNumber = new BigInteger(aSign, a).doubleValue(); - assertTrue(aNumber == Double.NEGATIVE_INFINITY); - } - - /** - * Convert a positive number to a double value. - * The exponent is 1023 and the mantissa is all 0s - * but the 54th bit (implicit) is 1. - */ - public void testDoubleValuePosMantissaIsZero() { - byte[] a = {-128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - }; - int aSign = 1; - double result = 8.98846567431158E307; - double aNumber = new BigInteger(aSign, a).doubleValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive number to a double value. - * The exponent is 1023 and the mantissa is all 0s - * but the 54th bit (implicit) is 1. - */ - public void testDoubleValueNegMantissaIsZero() { - byte[] a = {-128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, - 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 - }; - int aSign = -1; - double aNumber = new BigInteger(aSign, a).doubleValue(); - assertTrue(aNumber == -8.98846567431158E307); - } - - /** - * Return the float value of ZERO. - */ - public void testFloatValueZero() { - String a = "0"; - float result = 0.0f; - float aNumber = new BigInteger(a).floatValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive number to a float value. - * The number's length is less than 32 bits. - */ - public void testFloatValuePositive1() { - String a = "27467238"; - float result = 2.7467238E7f; - float aNumber = new BigInteger(a).floatValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive number to a float value. - * The number's bit length is inside [32, 127]. - */ - public void testFloatValuePositive2() { - String a = "27467238945723645782"; - float result = 2.7467239E19f; - float aNumber = new BigInteger(a).floatValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a negative number to a float value. - * The number's bit length is less than 32 bits. - */ - public void testFloatValueNegative1() { - String a = "-27467238"; - float result = -2.7467238E7f; - float aNumber = new BigInteger(a).floatValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a negative number to a doufloatble value. - * The number's bit length is inside [63, 1024]. - */ - public void testFloatValueNegative2() { - String a = "-27467238945723645782"; - float result = -2.7467239E19f; - float aNumber = new BigInteger(a).floatValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive number to a float value. - * Rounding is needed. - * The rounding bit is 1 and the next bit to the left is 1. - */ - public void testFloatValuePosRounded1() { - byte[] a = {-128, 1, -1, -4, 4, 5, 60, 23, 1, -3, -5}; - int aSign = 1; - float result = 1.5475195E26f; - float aNumber = new BigInteger(aSign, a).floatValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive number to a float value. - * Rounding is needed. - * The rounding bit is 1 and the next bit to the left is 0 - * but some of dropped bits are 1s. - */ - public void testFloatValuePosRounded2() { - byte[] a = {-128, 1, 2, -128, 4, 5, 60, 23, 1, -3, -5}; - int aSign = 1; - float result = 1.5474728E26f; - float aNumber = new BigInteger(aSign, a).floatValue(); - assertTrue(aNumber == result); - } - /** - * Convert a positive number to a float value. - * Rounding is NOT needed. - */ - public void testFloatValuePosNotRounded() { - byte[] a = {-128, 1, 2, 3, 4, 5, 60, 23, 1, -3, -5}; - int aSign = 1; - float result = 1.5474726E26f; - float aNumber = new BigInteger(aSign, a).floatValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive number to a float value. - * Rounding is needed. - */ - public void testFloatValueNegRounded1() { - byte[] a = {-128, 1, -1, -4, 4, 5, 60, 23, 1, -3, -5}; - int aSign = -1; - float result = -1.5475195E26f; - float aNumber = new BigInteger(aSign, a).floatValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive number to a float value. - * Rounding is needed. - * The rounding bit is 1 and the next bit to the left is 0 - * but some of dropped bits are 1s. - */ - public void testFloatValueNegRounded2() { - byte[] a = {-128, 1, 2, -128, 4, 5, 60, 23, 1, -3, -5}; - int aSign = -1; - float result = -1.5474728E26f; - float aNumber = new BigInteger(aSign, a).floatValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive number to a float value. - * Rounding is NOT needed. - */ - public void testFloatValueNegNotRounded() { - byte[] a = {-128, 1, 2, 3, 4, 5, 60, 23, 1, -3, -5}; - int aSign = -1; - float result = -1.5474726E26f; - float aNumber = new BigInteger(aSign, a).floatValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive number to a float value. - * The exponent is 1023 and the mantissa is all 1s. - * The rounding bit is 0. - * The result is Float.MAX_VALUE. - */ - public void testFloatValuePosMaxValue() { - byte[] a = {0, -1, -1, -1, 0, -1, -1, -8, -1, -1, -1, -1, -1, -1, -1, -1, -1}; - int aSign = 1; - float aNumber = new BigInteger(aSign, a).floatValue(); - assertTrue(aNumber == Float.MAX_VALUE); - } - - /** - * Convert a negative number to a float value. - * The exponent is 1023 and the mantissa is all 1s. - * The rounding bit is 0. - * The result is -Float.MAX_VALUE. - */ - public void testFloatValueNegMaxValue() { - byte[] a = {0, -1, -1, -1, 0, -1, -1, -8, -1, -1, -1, -1, -1, -1, -1, -1, -1}; - int aSign = -1; - float aNumber = new BigInteger(aSign, a).floatValue(); - assertTrue(aNumber == -Float.MAX_VALUE); - } - - /** - * Convert a positive number to a float value. - * The exponent is 1023 and the mantissa is all 1s. - * The rounding bit is 1. - * The result is Float.POSITIVE_INFINITY. - */ - public void testFloatValuePositiveInfinity1() { - byte[] a = {0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; - int aSign = 1; - float aNumber = new BigInteger(aSign, a).floatValue(); - assertTrue(aNumber == Float.POSITIVE_INFINITY); - } - - /** - * Convert a positive number to a float value. - * The number's bit length is greater than 127. - */ - public void testFloatValuePositiveInfinity2() { - String a = "2746723894572364578265426346273456972283746872364768676747462342342342342342342342323423423423423423426767456345745293762384756238475634563456845634568934568347586346578648576478568456457634875673845678456786587345873645767456834756745763457863485768475678465783456702897830296720476846578634576384567845678346573465786457863"; - float aNumber = new BigInteger(a).floatValue(); - assertTrue(aNumber == Float.POSITIVE_INFINITY); - } - - /** - * Convert a negative number to a float value. - * The number's bit length is greater than 127. - */ - public void testFloatValueNegativeInfinity1() { - String a = "-2746723894572364578265426346273456972283746872364768676747462342342342342342342342323423423423423423426767456345745293762384756238475634563456845634568934568347586346578648576478568456457634875673845678456786587345873645767456834756745763457863485768475678465783456702897830296720476846578634576384567845678346573465786457863"; - float aNumber = new BigInteger(a).floatValue(); - assertTrue(aNumber == Float.NEGATIVE_INFINITY); - } - - /** - * Convert a negative number to a float value. - * The exponent is 1023 and the mantissa is all 0s. - * The rounding bit is 0. - * The result is Float.NEGATIVE_INFINITY. - */ - public void testFloatValueNegativeInfinity2() { - byte[] a = {0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; - int aSign = -1; - float aNumber = new BigInteger(aSign, a).floatValue(); - assertTrue(aNumber == Float.NEGATIVE_INFINITY); - } - - /** - * Convert a positive number to a float value. - * The exponent is 1023 and the mantissa is all 0s - * but the 54th bit (implicit) is 1. - */ - public void testFloatValuePosMantissaIsZero() { - byte[] a = {-128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - int aSign = 1; - float result = 1.7014118E38f; - float aNumber = new BigInteger(aSign, a).floatValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive number to a double value. - * The exponent is 1023 and the mantissa is all 0s - * but the 54th bit (implicit) is 1. - */ - public void testFloatValueNegMantissaIsZero() { - byte[] a = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; - int aSign = -1; - float aNumber = new BigInteger(aSign, a).floatValue(); - assertTrue(aNumber == Float.NEGATIVE_INFINITY); - } - - /** - * Convert a negative number to a float value. - * The number's bit length is less than 32 bits. - */ - public void testFloatValueBug2482() { - String a = "2147483649"; - float result = 2.14748365E9f; - float aNumber = new BigInteger(a).floatValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a positive BigInteger to an integer value. - * The low digit is positive - */ - public void testIntValuePositive1() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3}; - int resInt = 1496144643; - int aNumber = new BigInteger(aBytes).intValue(); - assertTrue(aNumber == resInt); - } - - /** - * Convert a positive BigInteger to an integer value. - * The low digit is positive - */ - public void testIntValuePositive2() { - byte aBytes[] = {12, 56, 100}; - int resInt = 800868; - int aNumber = new BigInteger(aBytes).intValue(); - assertTrue(aNumber == resInt); - } - - /** - * Convert a positive BigInteger to an integer value. - * The low digit is negative. - */ - public void testIntValuePositive3() { - byte aBytes[] = {56, 13, 78, -12, -5, 56, 100}; - int sign = 1; - int resInt = -184862620; - int aNumber = new BigInteger(sign, aBytes).intValue(); - assertTrue(aNumber == resInt); - } - - /** - * Convert a negative BigInteger to an integer value. - * The low digit is negative. - */ - public void testIntValueNegative1() { - byte aBytes[] = {12, 56, 100, -2, -76, -128, 45, 91, 3}; - int sign = -1; - int resInt = 2144511229; - int aNumber = new BigInteger(sign, aBytes).intValue(); - assertTrue(aNumber == resInt); - } - - /** - * Convert a negative BigInteger to an integer value. - * The low digit is negative. - */ - public void testIntValueNegative2() { - byte aBytes[] = {-12, 56, 100}; - int result = -771996; - int aNumber = new BigInteger(aBytes).intValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a negative BigInteger to an integer value. - * The low digit is positive. - */ - public void testIntValueNegative3() { - byte aBytes[] = {12, 56, 100, -2, -76, 127, 45, 91, 3}; - int sign = -1; - int resInt = -2133678851; - int aNumber = new BigInteger(sign, aBytes).intValue(); - assertTrue(aNumber == resInt); - } - - /** - * Convert a BigInteger to a positive long value - * The BigInteger is longer than int. - */ - public void testLongValuePositive1() { - byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, 120, -34, -12, 45, 98}; - long result = 3268209772258930018L; - long aNumber = new BigInteger(aBytes).longValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a number to a positive long value - * The number fits in a long. - */ - public void testLongValuePositive2() { - byte aBytes[] = {12, 56, 100, 18, -105, 34, -18, 45}; - long result = 880563758158769709L; - long aNumber = new BigInteger(aBytes).longValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a number to a negative long value - * The BigInteger is longer than int. - */ - public void testLongValueNegative1() { - byte aBytes[] = {12, -1, 100, -2, -76, -128, 45, 91, 3}; - long result = -43630045168837885L; - long aNumber = new BigInteger(aBytes).longValue(); - assertTrue(aNumber == result); - } - - /** - * Convert a number to a negative long value - * The number fits in a long. - */ - public void testLongValueNegative2() { - byte aBytes[] = {-12, 56, 100, 45, -101, 45, 98}; - long result = -3315696807498398L; - long aNumber = new BigInteger(aBytes).longValue(); - assertTrue(aNumber == result); - } - - /** - * valueOf (long val): convert Integer.MAX_VALUE to a BigInteger. - */ - public void testValueOfIntegerMax() { - long longVal = Integer.MAX_VALUE; - BigInteger aNumber = BigInteger.valueOf(longVal); - byte rBytes[] = {127, -1, -1, -1}; - byte resBytes[] = new byte[rBytes.length]; - resBytes = aNumber.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, aNumber.signum()); - } - - /** - * valueOf (long val): convert Integer.MIN_VALUE to a BigInteger. - */ - public void testValueOfIntegerMin() { - long longVal = Integer.MIN_VALUE; - BigInteger aNumber = BigInteger.valueOf(longVal); - byte rBytes[] = {-128, 0, 0, 0}; - byte resBytes[] = new byte[rBytes.length]; - resBytes = aNumber.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, aNumber.signum()); - } - - /** - * valueOf (long val): convert Long.MAX_VALUE to a BigInteger. - */ - public void testValueOfLongMax() { - long longVal = Long.MAX_VALUE; - BigInteger aNumber = BigInteger.valueOf(longVal); - byte rBytes[] = {127, -1, -1, -1, -1, -1, -1, -1}; - byte resBytes[] = new byte[rBytes.length]; - resBytes = aNumber.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, aNumber.signum()); - } - - /** - * valueOf (long val): convert Long.MIN_VALUE to a BigInteger. - */ - public void testValueOfLongMin() { - long longVal = Long.MIN_VALUE; - BigInteger aNumber = BigInteger.valueOf(longVal); - byte rBytes[] = {-128, 0, 0, 0, 0, 0, 0, 0}; - byte resBytes[] = new byte[rBytes.length]; - resBytes = aNumber.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, aNumber.signum()); - } - - /** - * valueOf (long val): convert a positive long value to a BigInteger. - */ - public void testValueOfLongPositive1() { - long longVal = 268209772258930018L; - BigInteger aNumber = BigInteger.valueOf(longVal); - byte rBytes[] = {3, -72, -33, 93, -24, -56, 45, 98}; - byte resBytes[] = new byte[rBytes.length]; - resBytes = aNumber.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, aNumber.signum()); - } - - /** - * valueOf (long val): convert a positive long value to a BigInteger. - * The long value fits in integer. - */ - public void testValueOfLongPositive2() { - long longVal = 58930018L; - BigInteger aNumber = BigInteger.valueOf(longVal); - byte rBytes[] = {3, -125, 51, 98}; - byte resBytes[] = new byte[rBytes.length]; - resBytes = aNumber.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, aNumber.signum()); - } - - /** - * valueOf (long val): convert a negative long value to a BigInteger. - */ - public void testValueOfLongNegative1() { - long longVal = -268209772258930018L; - BigInteger aNumber = BigInteger.valueOf(longVal); - byte rBytes[] = {-4, 71, 32, -94, 23, 55, -46, -98}; - byte resBytes[] = new byte[rBytes.length]; - resBytes = aNumber.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, aNumber.signum()); - } - - /** - * valueOf (long val): convert a negative long value to a BigInteger. - * The long value fits in integer. - */ - public void testValueOfLongNegative2() { - long longVal = -58930018L; - BigInteger aNumber = BigInteger.valueOf(longVal); - byte rBytes[] = {-4, 124, -52, -98}; - byte resBytes[] = new byte[rBytes.length]; - resBytes = aNumber.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, aNumber.signum()); - } - /** - * valueOf (long val): convert a zero long value to a BigInteger. - */ - public void testValueOfLongZero() { - long longVal = 0L; - BigInteger aNumber = BigInteger.valueOf(longVal); - byte rBytes[] = {0}; - byte resBytes[] = new byte[rBytes.length]; - resBytes = aNumber.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 0, aNumber.signum()); - } -} diff --git a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerHashCodeTest.java b/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerHashCodeTest.java deleted file mode 100644 index bdcea92d..00000000 --- a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerHashCodeTest.java +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @author Elena Semukhina - * @version $Revision$ - */ - -package org.apache.harmony.math.tests.java.math; - -import java.math.BigInteger; - -import junit.framework.TestCase; - -/** - * Class: java.math.BigInteger - * Method: hashCode() - */ -public class BigIntegerHashCodeTest extends TestCase { - /** - * Test hash codes for the same object - */ - public void testSameObject() { - String value1 = "12378246728727834290276457386374882976782849"; - String value2 = "-5634562095872038262928728727834290276457386374882976782849"; - BigInteger aNumber1 = new BigInteger(value1); - BigInteger aNumber2 = new BigInteger(value2); - int code1 = aNumber1.hashCode(); - aNumber1.add(aNumber2).shiftLeft(125); - aNumber1.subtract(aNumber2).shiftRight(125); - aNumber1.multiply(aNumber2).toByteArray(); - aNumber1.divide(aNumber2).bitLength(); - aNumber1.gcd(aNumber2).pow(7); - int code2 = aNumber1.hashCode(); - assertTrue("hash codes for the same object differ", code1 == code2); - } - - /** - * Test hash codes for equal objects. - */ - public void testEqualObjects() { - String value1 = "12378246728727834290276457386374882976782849"; - String value2 = "12378246728727834290276457386374882976782849"; - BigInteger aNumber1 = new BigInteger(value1); - BigInteger aNumber2 = new BigInteger(value2); - int code1 = aNumber1.hashCode(); - int code2 = aNumber2.hashCode(); - if (aNumber1.equals(aNumber2)) { - assertTrue("hash codes for equal objects are unequal", code1 == code2); - } - } - - /** - * Test hash codes for unequal objects. - * The codes are unequal. - */ - public void testUnequalObjectsUnequal() { - String value1 = "12378246728727834290276457386374882976782849"; - String value2 = "-5634562095872038262928728727834290276457386374882976782849"; - BigInteger aNumber1 = new BigInteger(value1); - BigInteger aNumber2 = new BigInteger(value2); - int code1 = aNumber1.hashCode(); - int code2 = aNumber2.hashCode(); - if (!aNumber1.equals(aNumber2)) { - assertTrue("hash codes for unequal objects are equal", code1 != code2); - } - } -} diff --git a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerNotTest.java b/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerNotTest.java deleted file mode 100644 index addbf98d..00000000 --- a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerNotTest.java +++ /dev/null @@ -1,194 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @author Elena Semukhina - * @version $Revision$ - */ - -package org.apache.harmony.math.tests.java.math; - -import java.math.BigInteger; - -import junit.framework.TestCase; - -/** - * Class: java.math.BigInteger - * Methods: and, andNot - */ -public class BigIntegerNotTest extends TestCase { - /** - * andNot for two positive numbers; the first is longer - */ - public void testAndNotPosPosFirstLonger() { - byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {0, -128, 9, 56, 100, 0, 0, 1, 1, 90, 1, -32, 0, 10, -126, 21, 82, -31, -96}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.andNot(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * andNot for two positive numbers; the first is shorter - */ - public void testAndNotPosPosFirstShorter() { - byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {73, -92, -48, 4, 12, 6, 4, 32, 48, 64, 0, 8, 2}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.andNot(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * andNot for two negative numbers; the first is longer - */ - public void testAndNotNegNegFirstLonger() { - byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = -1; - int bSign = -1; - byte rBytes[] = {73, -92, -48, 4, 12, 6, 4, 32, 48, 64, 0, 8, 2}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.andNot(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * andNot for a negative and a positive numbers; the first is longer - */ - public void testNegPosFirstLonger() { - byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = -1; - int bSign = 1; - byte rBytes[] = {-1, 127, -10, -57, -101, 1, 2, 2, 2, -96, -16, 8, -40, -59, 68, -88, -88, 16, 72}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.andNot(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Not for ZERO - */ - public void testNotZero() { - byte rBytes[] = {-1}; - BigInteger aNumber = BigInteger.ZERO; - BigInteger result = aNumber.not(); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Not for ONE - */ - public void testNotOne() { - byte rBytes[] = {-2}; - BigInteger aNumber = BigInteger.ONE; - BigInteger result = aNumber.not(); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Not for a positive number - */ - public void testNotPos() { - byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117}; - int aSign = 1; - byte rBytes[] = {-1, 127, -57, -101, 1, 75, -90, -46, -92, -4, 14, -36, -27, 116}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger result = aNumber.not(); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Not for a negative number - */ - public void testNotNeg() { - byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117}; - int aSign = -1; - byte rBytes[] = {0, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -118}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger result = aNumber.not(); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Not for a negative number - */ - - public void testNotSpecialCase() { - byte aBytes[] = {-1, -1, -1, -1}; - int aSign = 1; - byte rBytes[] = {-1, 0, 0, 0, 0}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger result = aNumber.not(); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } -} diff --git a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerOrTest.java b/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerOrTest.java deleted file mode 100644 index c0f0e7d9..00000000 --- a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerOrTest.java +++ /dev/null @@ -1,421 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @author Elena Semukhina - * @version $Revision$ - */ - -package org.apache.harmony.math.tests.java.math; - -import java.math.BigInteger; - -import junit.framework.TestCase; - -/** - * Class: java.math.BigInteger - * Method: or - */ -public class BigIntegerOrTest extends TestCase { - /** - * Or for zero and a positive number - */ - public void testZeroPos() { - byte aBytes[] = {0}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = 0; - int bSign = 1; - byte rBytes[] = {0, -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Or for zero and a negative number - */ - public void testZeroNeg() { - byte aBytes[] = {0}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = 0; - int bSign = -1; - byte rBytes[] = {-1, 1, 2, 3, 3, -6, -15, -24, -40, -49, -58, -67, -6, -15, -23}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Or for a positive number and zero - */ - public void testPosZero() { - byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - byte bBytes[] = {0}; - int aSign = 1; - int bSign = 0; - byte rBytes[] = {0, -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Or for a negative number and zero - */ - public void testNegPos() { - byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - byte bBytes[] = {0}; - int aSign = -1; - int bSign = 0; - byte rBytes[] = {-1, 1, 2, 3, 3, -6, -15, -24, -40, -49, -58, -67, -6, -15, -23}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Or for zero and zero - */ - public void testZeroZero() { - byte aBytes[] = {0}; - byte bBytes[] = {0}; - int aSign = 0; - int bSign = 0; - byte rBytes[] = {0}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 0, result.signum()); - } - - /** - * Or for zero and one - */ - public void testZeroOne() { - byte aBytes[] = {0}; - byte bBytes[] = {1}; - int aSign = 0; - int bSign = 1; - byte rBytes[] = {1}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Or for one and one - */ - public void testOneOne() { - byte aBytes[] = {1}; - byte bBytes[] = {1}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {1}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Or for two positive numbers of the same length - */ - public void testPosPosSameLength() { - byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {0, -2, -3, -4, -4, -1, -66, 95, 47, 123, 59, -13, 39, 30, -97}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Or for two positive numbers; the first is longer - */ - public void testPosPosFirstLonger() { - byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {0, -128, 9, 56, 100, -2, -3, -3, -3, 95, 15, -9, 39, 58, -69, 87, 87, -17, -73}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Or for two positive numbers; the first is shorter - */ - public void testPosPosFirstShorter() { - byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {0, -128, 9, 56, 100, -2, -3, -3, -3, 95, 15, -9, 39, 58, -69, 87, 87, -17, -73}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", 1, result.signum()); - } - - /** - * Or for two negative numbers of the same length - */ - public void testNegNegSameLength() { - byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = -1; - int bSign = -1; - byte rBytes[] = {-1, 127, -57, -101, -5, -5, -18, -38, -17, -2, -65, -2, -11, -3}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Or for two negative numbers; the first is longer - */ - public void testNegNegFirstLonger() { - byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = -1; - int bSign = -1; - byte rBytes[] = {-1, 1, 75, -89, -45, -2, -3, -18, -36, -17, -10, -3, -6, -7, -21}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Or for two negative numbers; the first is shorter - */ - public void testNegNegFirstShorter() { - byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - int aSign = -1; - int bSign = -1; - byte rBytes[] = {-1, 1, 75, -89, -45, -2, -3, -18, -36, -17, -10, -3, -6, -7, -21}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Or for two numbers of different signs and the same length - */ - public void testPosNegSameLength() { - byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = 1; - int bSign = -1; - byte rBytes[] = {-1, 1, -126, 59, 103, -2, -11, -7, -3, -33, -57, -3, -5, -5, -21}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Or for two numbers of different signs and the same length - */ - public void testNegPosSameLength() { - byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = -1; - int bSign = 1; - byte rBytes[] = {-1, 5, 79, -73, -9, -76, -3, 78, -35, -17, 119}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Or for a negative and a positive numbers; the first is longer - */ - public void testNegPosFirstLonger() { - byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = -1; - int bSign = 1; - byte rBytes[] = {-1, 127, -10, -57, -101, -1, -1, -2, -2, -91, -2, 31, -1, -11, 125, -22, -83, 30, 95}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Or for two negative numbers; the first is shorter - */ - public void testNegPosFirstShorter() { - byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - int aSign = -1; - int bSign = 1; - byte rBytes[] = {-74, 91, 47, -5, -13, -7, -5, -33, -49, -65, -1, -9, -3}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Or for a positive and a negative numbers; the first is longer - */ - public void testPosNegFirstLonger() { - byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - int aSign = 1; - int bSign = -1; - byte rBytes[] = {-74, 91, 47, -5, -13, -7, -5, -33, -49, -65, -1, -9, -3}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - /** - * Or for a positive and a negative number; the first is shorter - */ - public void testPosNegFirstShorter() { - byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75}; - int aSign = 1; - int bSign = -1; - byte rBytes[] = {-1, 127, -10, -57, -101, -1, -1, -2, -2, -91, -2, 31, -1, -11, 125, -22, -83, 30, 95}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.or(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals("incorrect sign", -1, result.signum()); - } - - public void testRegression() { - // Regression test for HARMONY-1996 - BigInteger x = new BigInteger("-1023"); - BigInteger r1 = x.and((BigInteger.ZERO.not()).shiftLeft(32)); - BigInteger r3 = x.and((BigInteger.ZERO.not().shiftLeft(32) ).not()); - BigInteger result = r1.or(r3); - assertEquals(x, result); - } -} diff --git a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerSubtractTest.java b/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerSubtractTest.java deleted file mode 100644 index 019bf309..00000000 --- a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerSubtractTest.java +++ /dev/null @@ -1,548 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @author Elena Semukhina - * @version $Revision$ - */ - -package org.apache.harmony.math.tests.java.math; - -import java.math.BigInteger; - -import junit.framework.TestCase; - -/** - * Class: java.math.BigInteger - * Method: subtract - */ -public class BigIntegerSubtractTest extends TestCase { - /** - * Subtract two positive numbers of the same length. - * The first is greater. - */ - public void testCase1() { - byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - byte bBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {9, 18, 27, 36, 45, 54, 63, 9, 18, 27}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(1, result.signum()); - } - - /** - * Subtract two positive numbers of the same length. - * The second is greater. - */ - public void testCase2() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {-10, -19, -28, -37, -46, -55, -64, -10, -19, -27}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(-1, result.signum()); - } - - /** - * Subtract two numbers of the same length and different signs. - * The first is positive. - * The first is greater in absolute value. - */ - public void testCase3() { - byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - byte bBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3}; - int aSign = 1; - int bSign = -1; - byte rBytes[] = {11, 22, 33, 44, 55, 66, 77, 11, 22, 33}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(1, result.signum()); - } - - /** - * Subtract two numbers of the same length and different signs. - * The first is positive. - * The second is greater in absolute value. - */ - public void testCase4() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = 1; - int bSign = -1; - byte rBytes[] = {11, 22, 33, 44, 55, 66, 77, 11, 22, 33}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(1, result.signum()); - } - - /** - * Subtract two negative numbers of the same length. - * The first is greater in absolute value. - */ - public void testCase5() { - byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - byte bBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3}; - int aSign = -1; - int bSign = -1; - byte rBytes[] = {-10, -19, -28, -37, -46, -55, -64, -10, -19, -27}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(-1, result.signum()); - } - - /** - * Subtract two negative numbers of the same length. - * The second is greater in absolute value. - */ - public void testCase6() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = -1; - int bSign = -1; - byte rBytes[] = {9, 18, 27, 36, 45, 54, 63, 9, 18, 27}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(1, result.signum()); - } - - /** - * Subtract two numbers of the same length and different signs. - * The first is negative. - * The first is greater in absolute value. - */ - public void testCase7() { - byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - byte bBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3}; - int aSign = -1; - int bSign = 1; - byte rBytes[] = {-12, -23, -34, -45, -56, -67, -78, -12, -23, -33}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(-1, result.signum()); - } - - /** - * Subtract two numbers of the same length and different signs. - * The first is negative. - * The second is greater in absolute value. - */ - public void testCase8() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = -1; - int bSign = 1; - byte rBytes[] = {-12, -23, -34, -45, -56, -67, -78, -12, -23, -33}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(-1, result.signum()); - } - - /** - * Subtract two positive numbers of different length. - * The first is longer. - */ - public void testCase9() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {1, 2, 3, 3, -6, -15, -24, -40, -49, -58, -67, -6, -15, -23}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(1, result.signum()); - } - - /** - * Subtract two positive numbers of different length. - * The second is longer. - */ - public void testCase10() { - byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - byte bBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(-1, result.signum()); - } - - /** - * Subtract two numbers of different length and different signs. - * The first is positive. - * The first is greater in absolute value. - */ - public void testCase11() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = 1; - int bSign = -1; - byte rBytes[] = {1, 2, 3, 4, 15, 26, 37, 41, 52, 63, 74, 15, 26, 37}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(1, result.signum()); - } - - /** - * Subtract two numbers of the same length and different signs. - * The first is positive. - * The second is greater in absolute value. - */ - public void testCase12() { - byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - byte bBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; - int aSign = 1; - int bSign = -1; - byte rBytes[] = {1, 2, 3, 4, 15, 26, 37, 41, 52, 63, 74, 15, 26, 37}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(1, result.signum()); - } - - /** - * Subtract two numbers of different length and different signs. - * The first is negative. - * The first is longer. - */ - public void testCase13() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = -1; - int bSign = 1; - byte rBytes[] = {-2, -3, -4, -5, -16, -27, -38, -42, -53, -64, -75, -16, -27, -37}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(-1, result.signum()); - } - - /** - * Subtract two numbers of the same length and different signs. - * The first is negative. - * The second is longer. - */ - public void testCase14() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = -1; - int bSign = 1; - byte rBytes[] = {-2, -3, -4, -5, -16, -27, -38, -42, -53, -64, -75, -16, -27, -37}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(-1, result.signum()); - } - - /** - * Subtract two negative numbers of different length. - * The first is longer. - */ - public void testCase15() { - byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; - byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - int aSign = -1; - int bSign = -1; - byte rBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(-1, result.signum()); -} - - /** - * Subtract two negative numbers of different length. - * The second is longer. - */ - public void testCase16() { - byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; - byte bBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; - int aSign = -1; - int bSign = -1; - byte rBytes[] = {1, 2, 3, 3, -6, -15, -24, -40, -49, -58, -67, -6, -15, -23}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(1, result.signum()); - } - - /** - * Subtract two positive equal in absolute value numbers. - */ - public void testCase17() { - byte aBytes[] = {-120, 34, 78, -23, -111, 45, 127, 23, 45, -3}; - byte bBytes[] = {-120, 34, 78, -23, -111, 45, 127, 23, 45, -3}; - byte rBytes[] = {0}; - int aSign = 1; - int bSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(0, result.signum()); - } - - /** - * Subtract zero from a number. - * The number is positive. - */ - public void testCase18() { - byte aBytes[] = {120, 34, 78, -23, -111, 45, 127, 23, 45, -3}; - byte bBytes[] = {0}; - byte rBytes[] = {120, 34, 78, -23, -111, 45, 127, 23, 45, -3}; - int aSign = 1; - int bSign = 0; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(1, result.signum()); - } - - /** - * Subtract a number from zero. - * The number is negative. - */ - public void testCase19() { - byte aBytes[] = {0}; - byte bBytes[] = {120, 34, 78, -23, -111, 45, 127, 23, 45, -3}; - byte rBytes[] = {120, 34, 78, -23, -111, 45, 127, 23, 45, -3}; - int aSign = 0; - int bSign = -1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(1, result.signum()); - } - - /** - * Subtract zero from zero. - */ - public void testCase20() { - byte aBytes[] = {0}; - byte bBytes[] = {0}; - byte rBytes[] = {0}; - int aSign = 0; - int bSign = 0; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(0, result.signum()); - } - - /** - * Subtract ZERO from a number. - * The number is positive. - */ - public void testCase21() { - byte aBytes[] = {120, 34, 78, -23, -111, 45, 127, 23, 45, -3}; - byte rBytes[] = {120, 34, 78, -23, -111, 45, 127, 23, 45, -3}; - int aSign = 1; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = BigInteger.ZERO; - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(1, result.signum()); - } - - /** - * Subtract a number from ZERO. - * The number is negative. - */ - public void testCase22() { - byte bBytes[] = {120, 34, 78, -23, -111, 45, 127, 23, 45, -3}; - byte rBytes[] = {120, 34, 78, -23, -111, 45, 127, 23, 45, -3}; - int bSign = -1; - BigInteger aNumber = BigInteger.ZERO; - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(1, result.signum()); - } - - /** - * Subtract ZERO from ZERO. - */ - public void testCase23() { - byte rBytes[] = {0}; - BigInteger aNumber = BigInteger.ZERO; - BigInteger bNumber = BigInteger.ZERO; - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(0, result.signum()); - } - - /** - * Subtract ONE from ONE. - */ - public void testCase24() { - byte rBytes[] = {0}; - BigInteger aNumber = BigInteger.ONE; - BigInteger bNumber = BigInteger.ONE; - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(0, result.signum()); - } - - /** - * Subtract two numbers so that borrow is 1. - */ - public void testCase25() { - byte aBytes[] = {-1, -1, -1, -1, -1, -1, -1, -1}; - byte bBytes[] = {-128, -128, -128, -128, -128, -128, -128, -128, -128}; - int aSign = 1; - int bSign = 1; - byte rBytes[] = {-128, 127, 127, 127, 127, 127, 127, 127, 127}; - BigInteger aNumber = new BigInteger(aSign, aBytes); - BigInteger bNumber = new BigInteger(bSign, bBytes); - BigInteger result = aNumber.subtract(bNumber); - byte resBytes[] = new byte[rBytes.length]; - resBytes = result.toByteArray(); - for(int i = 0; i < resBytes.length; i++) { - assertTrue(resBytes[i] == rBytes[i]); - } - assertEquals(-1, result.signum()); - } -} diff --git a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerToStringTest.java b/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerToStringTest.java deleted file mode 100644 index 34b4b194..00000000 --- a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerToStringTest.java +++ /dev/null @@ -1,228 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @author Elena Semukhina - * @version $Revision$ - */ - -package org.apache.harmony.math.tests.java.math; - -import java.math.BigInteger; - -import junit.framework.TestCase; - -/** - * Class: java.math.BigInteger - * Method: toString(int radix) - */ -public class BigIntegerToStringTest extends TestCase { - /** - * If 36 < radix < 2 it should be set to 10 - */ - public void testRadixOutOfRange() { - String value = "442429234853876401"; - int radix = 10; - BigInteger aNumber = new BigInteger(value, radix); - String result = aNumber.toString(45); - assertTrue(result.equals(value)); - } - - /** - * test negative number of radix 2 - */ - public void testRadix2Neg() { - String value = "-101001100010010001001010101110000101010110001010010101010101010101010101010101010101010101010010101"; - int radix = 2; - BigInteger aNumber = new BigInteger(value, radix); - String result = aNumber.toString(radix); - assertTrue(result.equals(value)); - } - - /** - * test positive number of radix 2 - */ - public void testRadix2Pos() { - String value = "101000011111000000110101010101010101010001001010101010101010010101010101010000100010010"; - int radix = 2; - BigInteger aNumber = new BigInteger(value, radix); - String result = aNumber.toString(radix); - assertTrue(result.equals(value)); - } - - /** - * test negative number of radix 10 - */ - public void testRadix10Neg() { - String value = "-2489756308572364789878394872984"; - int radix = 16; - BigInteger aNumber = new BigInteger(value, radix); - String result = aNumber.toString(radix); - assertTrue(result.equals(value)); - } - - /** - * test positive number of radix 10 - */ - public void testRadix10Pos() { - String value = "2387627892347567398736473476"; - int radix = 16; - BigInteger aNumber = new BigInteger(value, radix); - String result = aNumber.toString(radix); - assertTrue(result.equals(value)); - } - - /** - * test negative number of radix 16 - */ - public void testRadix16Neg() { - String value = "-287628a883451b800865c67e8d7ff20"; - int radix = 16; - BigInteger aNumber = new BigInteger(value, radix); - String result = aNumber.toString(radix); - assertTrue(result.equals(value)); - } - - /** - * test positive number of radix 16 - */ - public void testRadix16Pos() { - String value = "287628a883451b800865c67e8d7ff20"; - int radix = 16; - BigInteger aNumber = new BigInteger(value, radix); - String result = aNumber.toString(radix); - assertTrue(result.equals(value)); - } - - /** - * test negative number of radix 24 - */ - public void testRadix24Neg() { - String value = "-287628a88gmn3451b8ijk00865c67e8d7ff20"; - int radix = 24; - BigInteger aNumber = new BigInteger(value, radix); - String result = aNumber.toString(radix); - assertTrue(result.equals(value)); - } - - /** - * test positive number of radix 24 - */ - public void testRadix24Pos() { - String value = "287628a883451bg80ijhk0865c67e8d7ff20"; - int radix = 24; - BigInteger aNumber = new BigInteger(value, radix); - String result = aNumber.toString(radix); - assertTrue(result.equals(value)); - } - - /** - * test negative number of radix 24 - */ - public void testRadix36Neg() { - String value = "-uhguweut98iu4h3478tq3985pq98yeiuth33485yq4aiuhalai485yiaehasdkr8tywi5uhslei8"; - int radix = 36; - BigInteger aNumber = new BigInteger(value, radix); - String result = aNumber.toString(radix); - assertTrue(result.equals(value)); - } - - /** - * test positive number of radix 24 - */ - public void testRadix36Pos() { - String value = "23895lt45y6vhgliuwhgi45y845htsuerhsi4586ysuerhtsio5y68peruhgsil4568ypeorihtse48y6"; - int radix = 36; - BigInteger aNumber = new BigInteger(value, radix); - String result = aNumber.toString(radix); - assertTrue(result.equals(value)); - } - -// ANDROID ADDED - - /** - * @tests java.math.BigInteger#toString() - */ - public void test_toString1() { - - String s = "0000000000"; - BigInteger bi = new BigInteger(s); - String sBI = bi.toString(); - assertEquals("toString method returns incorrect value instead of " + s, sBI, "0"); - } - - /** - * @tests java.math.BigInteger#toString() - */ - public void test_toString2() { - String s = "1234567890987654321"; - BigInteger bi = new BigInteger(s); - String sBI = bi.toString(); - assertEquals("toString method returns incorrect value instead of " + s, sBI, s); - } - - /** - * @tests java.math.BigInteger#toString() - */ - public void test_toString3() { - String s = "-1234567890987654321"; - BigInteger bi = new BigInteger(s); - String sBI = bi.toString(); - assertEquals("toString method returns incorrect value instead of " + s, sBI, s); - } - - /** - * @tests java.math.BigInteger#toString() - */ - public void test_toString4() { - String s = "12345678901234"; - long l = 12345678901234L; - BigInteger bi = BigInteger.valueOf(l); - String sBI = bi.toString(); - assertEquals("toString method returns incorrect value instead of " + s, sBI, s); - } - - /** - * @tests java.math.BigInteger#toString() - */ - public void test_toString5() { - String s = "-12345678901234"; - long l = -12345678901234L; - BigInteger bi = BigInteger.valueOf(l); - String sBI = bi.toString(); - assertEquals("toString method returns incorrect value instead of " + s, sBI, s); - } - - /** - * @tests java.math.BigInteger#toString() - */ - public void test_toString() { - byte aBytes[] = { - 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91 - }; - String s = "247856948764430159964673417020251"; - BigInteger bi = new BigInteger(aBytes); - String sBI = bi.toString(); - assertEquals("toString method returns incorrect value instead of " + s, sBI, s); - byte aBytes_[] = { - -12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91 - }; - s = "-238920881723209930210060613844133"; - bi = new BigInteger(aBytes_); - sBI = bi.toString(); - assertEquals("toString method returns incorrect value instead of " + s, sBI, s); - } -} diff --git a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerXorTest.java b/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerXorTest.java deleted file mode 100644 index f1922178..00000000 --- a/luni/src/test/java/org/apache/harmony/math/tests/java/math/BigIntegerXorTest.java +++ /dev/null @@ -1,279 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -/** - * @author Elena Semukhina - * @version $Revision$ - */ - -package org.apache.harmony.math.tests.java.math; - -import java.math.BigInteger; - -import junit.framework.TestCase; - -/** - * Class: java.math.BigInteger - * Method: xor - */ -public class BigIntegerXorTest extends TestCase { - /** - * Xor for zero and a positive number - */ - public void testZeroPos() { - String numA = "0"; - String numB = "27384627835298756289327365"; - String res = "27384627835298756289327365"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } - - /** - * Xor for zero and a negative number - */ - public void testZeroNeg() { - String numA = "0"; - String numB = "-27384627835298756289327365"; - String res = "-27384627835298756289327365"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } - - /** - * Xor for a positive number and zero - */ - public void testPosZero() { - String numA = "27384627835298756289327365"; - String numB = "0"; - String res = "27384627835298756289327365"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } - - /** - * Xor for a negative number and zero - */ - public void testNegPos() { - String numA = "-27384627835298756289327365"; - String numB = "0"; - String res = "-27384627835298756289327365"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } - - /** - * Xor for zero and zero - */ - public void testZeroZero() { - String numA = "0"; - String numB = "0"; - String res = "0"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } - - /** - * Xor for zero and one - */ - public void testZeroOne() { - String numA = "0"; - String numB = "1"; - String res = "1"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } - - /** - * Xor for one and one - */ - public void testOneOne() { - String numA = "1"; - String numB = "1"; - String res = "0"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } - - /** - * Xor for two positive numbers of the same length - */ - public void testPosPosSameLength() { - String numA = "283746278342837476784564875684767"; - String numB = "293478573489347658763745839457637"; - String res = "71412358434940908477702819237626"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } - - /** - * Xor for two positive numbers; the first is longer - */ - public void testPosPosFirstLonger() { - String numA = "2837462783428374767845648748973847593874837948575684767"; - String numB = "293478573489347658763745839457637"; - String res = "2837462783428374767845615168483972194300564226167553530"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } - - /** - * Xor for two positive numbers; the first is shorter - */ - public void testPosPosFirstShorter() { - String numA = "293478573489347658763745839457637"; - String numB = "2837462783428374767845648748973847593874837948575684767"; - String res = "2837462783428374767845615168483972194300564226167553530"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } - - /** - * Xor for two negative numbers of the same length - */ - public void testNegNegSameLength() { - String numA = "-283746278342837476784564875684767"; - String numB = "-293478573489347658763745839457637"; - String res = "71412358434940908477702819237626"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } - - /** - * Xor for two negative numbers; the first is longer - */ - public void testNegNegFirstLonger() { - String numA = "-2837462783428374767845648748973847593874837948575684767"; - String numB = "-293478573489347658763745839457637"; - String res = "2837462783428374767845615168483972194300564226167553530"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } - - /** - * Xor for two negative numbers; the first is shorter - */ - public void testNegNegFirstShorter() { - String numA = "293478573489347658763745839457637"; - String numB = "2837462783428374767845648748973847593874837948575684767"; - String res = "2837462783428374767845615168483972194300564226167553530"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } - - /** - * Xor for two numbers of different signs and the same length - */ - public void testPosNegSameLength() { - String numA = "283746278342837476784564875684767"; - String numB = "-293478573489347658763745839457637"; - String res = "-71412358434940908477702819237628"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } - - /** - * Xor for two numbers of different signs and the same length - */ - public void testNegPosSameLength() { - String numA = "-283746278342837476784564875684767"; - String numB = "293478573489347658763745839457637"; - String res = "-71412358434940908477702819237628"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } - - /** - * Xor for a negative and a positive numbers; the first is longer - */ - public void testNegPosFirstLonger() { - String numA = "-2837462783428374767845648748973847593874837948575684767"; - String numB = "293478573489347658763745839457637"; - String res = "-2837462783428374767845615168483972194300564226167553532"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } - - /** - * Xor for two negative numbers; the first is shorter - */ - public void testNegPosFirstShorter() { - String numA = "-293478573489347658763745839457637"; - String numB = "2837462783428374767845648748973847593874837948575684767"; - String res = "-2837462783428374767845615168483972194300564226167553532"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } - - /** - * Xor for a positive and a negative numbers; the first is longer - */ - public void testPosNegFirstLonger() { - String numA = "2837462783428374767845648748973847593874837948575684767"; - String numB = "-293478573489347658763745839457637"; - String res = "-2837462783428374767845615168483972194300564226167553532"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } - - /** - * Xor for a positive and a negative number; the first is shorter - */ - public void testPosNegFirstShorter() { - String numA = "293478573489347658763745839457637"; - String numB = "-2837462783428374767845648748973847593874837948575684767"; - String res = "-2837462783428374767845615168483972194300564226167553532"; - BigInteger aNumber = new BigInteger(numA); - BigInteger bNumber = new BigInteger(numB); - BigInteger result = aNumber.xor(bNumber); - assertTrue(res.equals(result.toString())); - } -} -- 2.11.0