From ac0c7bbe3c1baf3031a1d255d40db17e4eee42f7 Mon Sep 17 00:00:00 2001 From: Victor Chang Date: Fri, 24 Aug 2018 15:53:15 +0100 Subject: [PATCH] ICU-13808 Document ArithmeticException thrown by DecimalFormat - Document the exception in the following methods setMultiplier, setMathContext, setMathContextICU - Add test to check the documented behavior --- .../src/com/ibm/icu/text/DecimalFormat.java | 6 +++ .../icu/dev/test/format/NumberFormatTest.java | 47 +++++++++++++++++-- 2 files changed, 50 insertions(+), 3 deletions(-) diff --git a/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java b/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java index 1185b5ba12..ea2a1ca16e 100644 --- a/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java +++ b/icu4j/main/classes/core/src/com/ibm/icu/text/DecimalFormat.java @@ -1123,6 +1123,8 @@ public class DecimalFormat extends NumberFormat { * * @param multiplier The number by which all numbers passed to {@link #format} will be multiplied. * @throws IllegalArgumentException If the given multiplier is zero. + * @throws ArithmeticException when inverting multiplier produces a non-terminating decimal result + * in conjunction with MathContext of unlimited precision. * @category Multipliers * @stable ICU 2.0 */ @@ -1296,6 +1298,8 @@ public class DecimalFormat extends NumberFormat { * method. * * @param mathContext The MathContext to use when rounding numbers. + * @throws ArithmeticException when inverting multiplier produces a non-terminating decimal result + * in conjunction with MathContext of unlimited precision. * @see java.math.MathContext * @category Rounding * @stable ICU 4.2 @@ -1330,6 +1334,8 @@ public class DecimalFormat extends NumberFormat { * {@link com.ibm.icu.math.MathContext}. * * @param mathContextICU The MathContext to use when rounding numbers. + * @throws ArithmeticException when inverting multiplier produces a non-terminating decimal result + * in conjunction with MathContext of unlimited precision. * @see #setMathContext(java.math.MathContext) * @category Rounding * @stable ICU 4.2 diff --git a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java index 246c8aa975..008bfedb34 100644 --- a/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java +++ b/icu4j/main/tests/core/src/com/ibm/icu/dev/test/format/NumberFormatTest.java @@ -4962,10 +4962,51 @@ public class NumberFormatTest extends TestFmwk { df.setMathContext(fourDigits); BigInteger actual4Digits = ((BigDecimal) df.parse(hugeNumberString)).toBigIntegerExact(); assertEquals("Extreme division with fourDigits", huge4Digits, actual4Digits); + } + + /** + * ArithmeticException is thrown when inverting multiplier produces a non-terminating + * decimal result in conjunction with MathContext of unlimited precision. + */ + @Test + public void testSetMathContextArithmeticException() { + DecimalFormat df = new DecimalFormat(); + df.setMultiplier(7); try { - df.setMathContext(unlimitedCeiling); - df.parse(hugeNumberString); - fail("Extreme division with unlimitedCeiling should throw ArithmeticException"); + df.setMathContext(java.math.MathContext.UNLIMITED); + fail("Extreme division with unlimited precision should throw ArithmeticException"); + } catch (ArithmeticException e) { + // expected + } + } + + /** + * ArithmeticException is thrown when inverting multiplier produces a non-terminating + * decimal result in conjunction with MathContext of unlimited precision. + */ + @Test + public void testSetMathContextICUArithmeticException() { + DecimalFormat df = new DecimalFormat(); + df.setMultiplier(7); + try { + df.setMathContextICU(new MathContext(0)); + fail("Extreme division with unlimited precision should throw ArithmeticException"); + } catch (ArithmeticException e) { + // expected + } + } + + /** + * ArithmeticException is thrown when inverting multiplier produces a non-terminating + * decimal result in conjunction with MathContext of unlimited precision. + */ + @Test + public void testSetMultiplierArithmeticException() { + DecimalFormat df = new DecimalFormat(); + df.setMathContext(java.math.MathContext.UNLIMITED); + try { + df.setMultiplier(7); + fail("Extreme division with unlimited precision should throw ArithmeticException"); } catch (ArithmeticException e) { // expected }