From bf164d9eda14761f4b86d52e637d9f3287b9fa96 Mon Sep 17 00:00:00 2001 From: Neil Fuller <nfuller@google.com> Date: Wed, 1 Oct 2014 15:38:35 +0100 Subject: Applying skipped merges This contains previously-skipped merges for: bd1ed0e0 93ad3c2a Change-Id: Ied3a7d9b0b8dc93de4a5c93ed962fb73324c3891 --- .../harmony/tests/java/text/DecimalFormatTest.java | 3034 ++++++++++---------- 1 file changed, 1508 insertions(+), 1526 deletions(-) (limited to 'harmony-tests') diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/text/DecimalFormatTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/text/DecimalFormatTest.java index bfb846e..ad96256 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/text/DecimalFormatTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/text/DecimalFormatTest.java @@ -27,10 +27,13 @@ import java.text.DecimalFormatSymbols; import java.text.FieldPosition; import java.text.NumberFormat; import java.text.ParsePosition; -import java.util.BitSet; +import java.util.ArrayList; import java.util.Currency; +import java.util.List; import java.util.Locale; +import junit.framework.Assert; +import junit.framework.AssertionFailedError; import junit.framework.TestCase; import org.apache.harmony.testframework.serialization.SerializationTest; @@ -39,7 +42,7 @@ import org.apache.harmony.testframework.serialization.SerializationTest; public class DecimalFormatTest extends TestCase { // https://code.google.com/p/android/issues/detail?id=59600 - public void test_empty_NaN() throws Exception { + public void test_setNan_emptyString() throws Exception { DecimalFormatSymbols dfs = new DecimalFormatSymbols(); dfs.setNaN(""); DecimalFormat df = new DecimalFormat(); @@ -49,53 +52,54 @@ public class DecimalFormatTest extends TestCase { public void testAttributedCharacterIterator() throws Exception { // Regression for http://issues.apache.org/jira/browse/HARMONY-333 - AttributedCharacterIterator iterator = new DecimalFormat().formatToCharacterIterator(new Integer(1)); + AttributedCharacterIterator iterator = new DecimalFormat().formatToCharacterIterator( + new Integer(1)); assertNotNull(iterator); assertFalse("attributes should exist", iterator.getAttributes().isEmpty()); } - public void test_isParseBigDecimalLjava_lang_Boolean_isParseIntegerOnlyLjava_lang_Boolean() throws Exception { - // parseBigDecimal default to false - DecimalFormat form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); - assertFalse(form.isParseBigDecimal()); - form.setParseBigDecimal(true); - assertTrue(form.isParseBigDecimal()); + public void test_parse_bigDecimal() throws Exception { + // parseBigDecimal default to false + DecimalFormat form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + assertFalse(form.isParseBigDecimal()); + form.setParseBigDecimal(true); + assertTrue(form.isParseBigDecimal()); - Number result = form.parse("123.123"); - assertEquals(new BigDecimal("123.123"), result); + Number result = form.parse("123.123"); + assertEquals(new BigDecimal("123.123"), result); - form.setParseBigDecimal(false); - assertFalse(form.isParseBigDecimal()); + form.setParseBigDecimal(false); + assertFalse(form.isParseBigDecimal()); - result = form.parse("123.123"); - assertFalse(result instanceof BigDecimal); + result = form.parse("123.123"); + assertFalse(result instanceof BigDecimal); } - public void test_isParseIntegerOnly() throws Exception { - DecimalFormat format = new DecimalFormat(); - assertFalse("Default value of isParseIntegerOnly is true", format.isParseIntegerOnly()); + public void test_parse_integerOnly() throws Exception { + DecimalFormat format = new DecimalFormat(); + assertFalse("Default value of isParseIntegerOnly is true", format.isParseIntegerOnly()); - format.setParseIntegerOnly(true); - assertTrue(format.isParseIntegerOnly()); - Number result = format.parse("123.123"); - assertEquals(new Long("123"), result); + format.setParseIntegerOnly(true); + assertTrue(format.isParseIntegerOnly()); + Number result = format.parse("123.123"); + assertEquals(new Long("123"), result); - format.setParseIntegerOnly(false); - assertFalse(format.isParseIntegerOnly()); - result = format.parse("123.123"); - assertEquals(new Double("123.123"), result); + format.setParseIntegerOnly(false); + assertFalse(format.isParseIntegerOnly()); + result = format.parse("123.123"); + assertEquals(new Double("123.123"), result); } // Test the type of the returned object - public void test_parseLjava_lang_String_Ljava_text_ParsePosition() { - DecimalFormat form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + public void test_parse_returnType() { + DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US); Number number = form.parse("23.1", new ParsePosition(0)); assertTrue(number instanceof Double); // Test parsed object of type double when // parseBigDecimal is set to true - form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + form = (DecimalFormat) NumberFormat.getInstance(Locale.US); number = form.parse("23.1", new ParsePosition(0)); assertTrue(number instanceof Double); @@ -109,7 +113,7 @@ public class DecimalFormatTest extends TestCase { // into Long unless the value is out of the bound of Long or // some special values such as NaN or Infinity. - form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + form = (DecimalFormat) NumberFormat.getInstance(Locale.US); form.setParseIntegerOnly(true); number = form.parse("23.1f", new ParsePosition(0)); @@ -135,7 +139,7 @@ public class DecimalFormatTest extends TestCase { // Even if parseIntegerOnly is set to true, NaN will be parsed to Double - form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + form = (DecimalFormat) NumberFormat.getInstance(Locale.US); form.setParseIntegerOnly(true); DecimalFormatSymbols symbols = new DecimalFormatSymbols(); number = form.parse(symbols.getNaN(), new ParsePosition(0)); @@ -144,7 +148,7 @@ public class DecimalFormatTest extends TestCase { // Even if parseIntegerOnly is set to true, Infinity will still be // parsed to Double - form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + form = (DecimalFormat) NumberFormat.getInstance(Locale.US); form.setParseIntegerOnly(true); symbols = new DecimalFormatSymbols(); number = form.parse(symbols.getInfinity(), new ParsePosition(0)); @@ -152,7 +156,7 @@ public class DecimalFormatTest extends TestCase { // ParseBigDecimal take precedence of parseBigInteger - form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + form = (DecimalFormat) NumberFormat.getInstance(Locale.US); form.setParseIntegerOnly(true); form.setParseBigDecimal(true); @@ -170,7 +174,7 @@ public class DecimalFormatTest extends TestCase { // Test whether the parsed object is of type float. (To be specific, // they are of type Double) - form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + form = (DecimalFormat) NumberFormat.getInstance(Locale.US); number = form.parse("23.1f", new ParsePosition(0)); assertTrue(number instanceof Double); @@ -182,7 +186,7 @@ public class DecimalFormatTest extends TestCase { // Integer will be parsed to Long, unless parseBigDecimal is set to true - form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + form = (DecimalFormat) NumberFormat.getInstance(Locale.US); number = form.parse("123", new ParsePosition(0)); assertTrue(number instanceof Long); @@ -194,7 +198,7 @@ public class DecimalFormatTest extends TestCase { // NaN will be parsed to Double, no matter parseBigDecimal set or not. - form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + form = (DecimalFormat) NumberFormat.getInstance(Locale.US); symbols = new DecimalFormatSymbols(); number = form.parse(symbols.getNaN() + "", new ParsePosition(0)); assertTrue(number instanceof Double); @@ -206,7 +210,7 @@ public class DecimalFormatTest extends TestCase { // Infinity will be parsed to Double, no matter parseBigDecimal set or // not. - form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + form = (DecimalFormat) NumberFormat.getInstance(Locale.US); symbols = new DecimalFormatSymbols(); number = form.parse(symbols.getInfinity(), new ParsePosition(0)); @@ -215,7 +219,7 @@ public class DecimalFormatTest extends TestCase { assertEquals("Infinity", number.toString()); // When set bigDecimal to true, the result of parsing infinity - form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + form = (DecimalFormat) NumberFormat.getInstance(Locale.US); symbols = new DecimalFormatSymbols(); form.setParseBigDecimal(true); @@ -226,7 +230,7 @@ public class DecimalFormatTest extends TestCase { // Negative infinity will be parsed to double no matter parseBigDecimal // set or not - form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + form = (DecimalFormat) NumberFormat.getInstance(Locale.US); symbols = new DecimalFormatSymbols(); number = form.parse("-" + symbols.getInfinity(), new ParsePosition(0)); @@ -236,7 +240,7 @@ public class DecimalFormatTest extends TestCase { // When set bigDecimal to true, the result of parsing minus infinity - form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + form = (DecimalFormat) NumberFormat.getInstance(Locale.US); symbols = new DecimalFormatSymbols(); form.setParseBigDecimal(true); @@ -248,7 +252,7 @@ public class DecimalFormatTest extends TestCase { // -0.0 will be parsed to different type according to the combination of // parseBigDecimal and parseIntegerOnly - form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + form = (DecimalFormat) NumberFormat.getInstance(Locale.US); // parseBigDecimal == true; // parseIntegerOnly == false; @@ -297,7 +301,7 @@ public class DecimalFormatTest extends TestCase { // When parseBigDecimal is set to false, no matter how massive the // mantissa part of a number is, the number will be parsed into Double - form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + form = (DecimalFormat) NumberFormat.getInstance(Locale.US); number = form.parse("9,223,372,036,854,775,808.00", new ParsePosition(0)); @@ -313,7 +317,7 @@ public class DecimalFormatTest extends TestCase { // When parseBigDecimal is set to true, if mantissa part of number // exceeds Long.MAX_VALUE, the number will be parsed into BigDecimal - form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + form = (DecimalFormat) NumberFormat.getInstance(Locale.US); form.setParseBigDecimal(true); number = form.parse("9,223,372,036,854,775,808.00", @@ -358,142 +362,160 @@ public class DecimalFormatTest extends TestCase { } } - public void test_parseLjava_lang_String_Ljava_text_ParsePosition_2() { - DecimalFormat form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); - form.setParseIntegerOnly(true); - form.setParseBigDecimal(true); + public void test_parse_largeBigDecimal() { + DecimalFormat form = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + form.setParseIntegerOnly(true); + form.setParseBigDecimal(true); - final String doubleMax2 = "359,538,626,972,463,141,629,054,847,463,408," - + "713,596,141,135,051,689,993,197,834,953,606,314,521,560,057,077," - + "521,179,117,265,533,756,343,080,917,907,028,764,928,468,642,653," - + "778,928,365,536,935,093,407,075,033,972,099,821,153,102,564,152," - + "490,980,180,778,657,888,151,737,016,910,267,884,609,166,473,806," - + "445,896,331,617,118,664,246,696,549,595,652,408,289,446,337,476," - + "354,361,838,599,762,500,808,052,368,249,716,736"; - Number number = form.parse(doubleMax2, new ParsePosition(0)); - assertTrue(number instanceof BigDecimal); - BigDecimal result = (BigDecimal)number; - assertEquals(new BigDecimal(Double.MAX_VALUE).add(new BigDecimal(Double.MAX_VALUE)), result); + final String doubleMax2 = "359,538,626,972,463,141,629,054,847,463,408," + + "713,596,141,135,051,689,993,197,834,953,606,314,521,560,057,077," + + "521,179,117,265,533,756,343,080,917,907,028,764,928,468,642,653," + + "778,928,365,536,935,093,407,075,033,972,099,821,153,102,564,152," + + "490,980,180,778,657,888,151,737,016,910,267,884,609,166,473,806," + + "445,896,331,617,118,664,246,696,549,595,652,408,289,446,337,476," + + "354,361,838,599,762,500,808,052,368,249,716,736"; + Number number = form.parse(doubleMax2, new ParsePosition(0)); + assertTrue(number instanceof BigDecimal); + BigDecimal result = (BigDecimal) number; + assertEquals(new BigDecimal(Double.MAX_VALUE).add(new BigDecimal(Double.MAX_VALUE)), + result); } - public void test_getMaximumFractionDigits() { - NumberFormat nform = DecimalFormat.getInstance(Locale.US); - DecimalFormat form = (DecimalFormat) nform; - - // getMaximumFractionDigits of NumberFormat default to 3 - // getMaximumFractionDigits of DecimalFormat default to 3 - assertEquals(3, nform.getMaximumFractionDigits()); + public void testMaximumFractionDigits_getAndSet() { + DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US); + // getMaximumFractionDigits of DecimalFormat defaults to 3 assertEquals(3, form.getMaximumFractionDigits()); - // Greater than 340 (critical number used to distinguish - // BigInteger and BigDecimal) - nform.setMaximumFractionDigits(500); - assertEquals(500, nform.getMaximumFractionDigits()); - assertEquals(500, form.getMaximumFractionDigits()); + form.setMaximumFractionDigits(310); + assertEquals(310, form.getMaximumFractionDigits()); + // Deliberately > 340. The API docs mention 340 and suggest that you can set the value + // higher but it will use 340 as a ceiling. form.setMaximumFractionDigits(500); - assertEquals(500, nform.getMaximumFractionDigits()); assertEquals(500, form.getMaximumFractionDigits()); + form.setMaximumFractionDigits(500); + assertEquals(500, form.getMaximumFractionDigits()); form.format(12.3); - assertEquals(500, nform.getMaximumFractionDigits()); assertEquals(500, form.getMaximumFractionDigits()); + + form.setMaximumFractionDigits(-2); + assertEquals(0, form.getMaximumFractionDigits()); } - public void test_getMinimumFractionDigits() { - NumberFormat nform = DecimalFormat.getInstance(Locale.US); - DecimalFormat form = (DecimalFormat) nform; + public void testMinimumFractionDigits_getAndSet() { + DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US); // getMinimumFractionDigits from NumberFormat (default to 0) // getMinimumFractionDigits from DecimalFormat (default to 0) - assertEquals(0, nform.getMinimumFractionDigits()); assertEquals(0, form.getMinimumFractionDigits()); - // Greater than 340 (critical number used to distinguish - // BigInteger and BigDecimal) - nform.setMinimumFractionDigits(500); - assertEquals(500, nform.getMinimumFractionDigits()); + form.setMinimumFractionDigits(310); + assertEquals(310, form.getMinimumFractionDigits()); + + // Deliberately > 340. The API docs mention 340 and suggest that you can set the value + // higher but it will use 340 as a ceiling. + form.setMinimumFractionDigits(500); assertEquals(500, form.getMinimumFractionDigits()); form.setMaximumFractionDigits(400); - assertEquals(400, nform.getMinimumFractionDigits()); assertEquals(400, form.getMinimumFractionDigits()); - } - //FIXME This test fails on Harmony ClassLibrary - public void test_getMaximumIntegerDigits() { - final int maxIntDigit = 309; + form.setMinimumFractionDigits(-3); + assertEquals(0, form.getMinimumFractionDigits()); + } + public void testMaximumIntegerDigits_getAndSet() { // When use default locale, in this case zh_CN // the returned instance of NumberFormat is a DecimalFormat DecimalFormat form = new DecimalFormat("00.###E0"); assertEquals(2, form.getMaximumIntegerDigits()); - NumberFormat nform = DecimalFormat.getInstance(Locale.US); - form = null; - if (nform instanceof DecimalFormat) { - form = (DecimalFormat) nform; - } + form = (DecimalFormat) NumberFormat.getInstance(Locale.US); + + form.setMaximumIntegerDigits(300); + assertEquals(300, form.getMaximumIntegerDigits()); - // Greater than 309 (critical number used to distinguish - // BigInteger and BigDecimal) - nform.setMaximumIntegerDigits(500); - assertEquals(500, nform.getMaximumIntegerDigits()); + // Deliberately > 309. The API docs mention 309 and suggest that you can set the value + // higher but it will use 309 as a ceiling. + form.setMaximumIntegerDigits(500); assertEquals(500, form.getMaximumIntegerDigits()); form = new DecimalFormat("00.###E0"); assertEquals(2, form.getMaximumIntegerDigits()); form.setMaximumIntegerDigits(500); - assertEquals(500, nform.getMaximumIntegerDigits()); assertEquals(500, form.getMaximumIntegerDigits()); form.format(12.3); - assertEquals(500, nform.getMaximumIntegerDigits()); assertEquals(500, form.getMaximumIntegerDigits()); - nform = DecimalFormat.getInstance(Locale.US); - form = null; - if (nform instanceof DecimalFormat) { - form = (DecimalFormat) nform; - } - // getMaximumIntegerDigits from NumberFormat default to 309 - // getMaximumIntegerDigits from DecimalFormat default to 309 - // the following 2 assertions will fail on RI implementation, since the - // implementation of ICU and RI are not identical. RI does not give - // DecimalFormat an initial bound about its maximumIntegerDigits - // (default to Integer.MAX_VALUE: 2147483647 ) - assertEquals(maxIntDigit, nform.getMaximumIntegerDigits()); - assertEquals(maxIntDigit, form.getMaximumIntegerDigits()); + form.setMaximumIntegerDigits(-3); + assertEquals(0, form.getMaximumIntegerDigits()); // regression test for HARMONY-878 assertTrue(new DecimalFormat("0\t'0'").getMaximumIntegerDigits() > 0); } - public void test_getMinimumIntegerDigits() { + public void testMinimumIntegerDigits_getAndSet() { final int minIntDigit = 1; - NumberFormat nform = DecimalFormat.getInstance(Locale.US); - DecimalFormat form = (DecimalFormat) nform; + DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US); - // getMaximumIntegerDigits from NumberFormat (default to 1) // getMaximumIntegerDigits from DecimalFormat (default to 1) - assertEquals(minIntDigit, nform.getMinimumIntegerDigits()); assertEquals(minIntDigit, form.getMinimumIntegerDigits()); - // Greater than 309 (critical number used to distinguish - // BigInteger and BigDecimal) - nform.setMinimumIntegerDigits(500); - assertEquals(500, nform.getMinimumIntegerDigits()); + form.setMinimumIntegerDigits(300); + assertEquals(300, form.getMinimumIntegerDigits()); + + // Deliberately > 309. The API docs mention 309 and suggest that you can set the value + // higher but it will use 309 as a ceiling. + form.setMinimumIntegerDigits(500); assertEquals(500, form.getMinimumIntegerDigits()); form.setMaximumIntegerDigits(400); - assertEquals(400, nform.getMinimumIntegerDigits()); assertEquals(400, form.getMinimumIntegerDigits()); + form.setMinimumIntegerDigits(-3); + assertEquals(0, form.getMinimumIntegerDigits()); } - public void test_formatLjava_lang_Obj_Ljava_StringBuffer_Ljava_text_FieldPosition() { - NumberFormat nform = DecimalFormat.getInstance(Locale.US); - DecimalFormat form = (DecimalFormat) nform; + // When MaxFractionDigits is set first and less than MinFractionDigits, max + // will be changed to min value + public void testMinimumFactionDigits_minChangesMax() { + DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US); + + form.setMaximumFractionDigits(100); + form.setMinimumFractionDigits(200); + + assertEquals(200, form.getMaximumFractionDigits()); + assertEquals(200, form.getMinimumFractionDigits()); + + form.setMaximumIntegerDigits(100); + form.setMinimumIntegerDigits(200); + + assertEquals(200, form.getMaximumIntegerDigits()); + assertEquals(200, form.getMinimumIntegerDigits()); + } + + // When MinFractionDigits is set first and less than MaxFractionDigits, min + // will be changed to max value + public void testMaximumFactionDigits_maxChangesMin() { + DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US); + + form.setMinimumFractionDigits(200); + form.setMaximumFractionDigits(100); + + assertEquals(100, form.getMaximumFractionDigits()); + assertEquals(100, form.getMinimumFractionDigits()); + + form.setMinimumIntegerDigits(200); + form.setMaximumIntegerDigits(100); + + assertEquals(100, form.getMaximumIntegerDigits()); + assertEquals(100, form.getMinimumIntegerDigits()); + } + + public void test_formatObject_errorCases() { + DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US); // If Object(including null) is not of type Number, // IllegalArgumentException will be thrown out @@ -531,37 +553,32 @@ public class DecimalFormatTest extends TestCase { fail(); } catch (IllegalArgumentException expected) { } + } - FieldPosition pos; - StringBuffer out; - DecimalFormat format = (DecimalFormat) NumberFormat - .getInstance(Locale.US); + public void test_formatObject() { + DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US); // format maxLong - pos = new FieldPosition(0); - out = format.format(new Long(Long.MAX_VALUE), new StringBuffer(), pos); - assertTrue("Wrong result L1: " + out, out.toString().equals( - "9,223,372,036,854,775,807")); + FieldPosition pos = new FieldPosition(0); + StringBuffer out = format.format(new Long(Long.MAX_VALUE), new StringBuffer(), pos); + assertTrue("Wrong result L1: " + out, out.toString().equals("9,223,372,036,854,775,807")); // format minLong pos = new FieldPosition(0); out = format.format(new Long(Long.MIN_VALUE), new StringBuffer(), pos); - assertTrue("Wrong result L2: " + out, out.toString().equals( - "-9,223,372,036,854,775,808")); + assertTrue("Wrong result L2: " + out, out.toString().equals("-9,223,372,036,854,775,808")); // format maxLong of type BigInteger pos = new FieldPosition(0); - out = format.format(new java.math.BigInteger(String - .valueOf(Long.MAX_VALUE)), new StringBuffer(), pos); - assertTrue("Wrong result BI1: " + out, out.toString().equals( - "9,223,372,036,854,775,807")); + out = format.format(new java.math.BigInteger(String.valueOf(Long.MAX_VALUE)), + new StringBuffer(), pos); + assertTrue("Wrong result BI1: " + out, out.toString().equals("9,223,372,036,854,775,807")); // format minLong of type BigInteger pos = new FieldPosition(0); - out = format.format(new java.math.BigInteger(String - .valueOf(Long.MIN_VALUE)), new StringBuffer(), pos); - assertTrue("Wrong result BI2: " + out, out.toString().equals( - "-9,223,372,036,854,775,808")); + out = format.format(new java.math.BigInteger(String.valueOf(Long.MIN_VALUE)), + new StringBuffer(), pos); + assertTrue("Wrong result BI2: " + out, out.toString().equals("-9,223,372,036,854,775,808")); // format maxLong + 1 java.math.BigInteger big; @@ -569,27 +586,23 @@ public class DecimalFormatTest extends TestCase { big = new java.math.BigInteger(String.valueOf(Long.MAX_VALUE)) .add(new java.math.BigInteger("1")); out = format.format(big, new StringBuffer(), pos); - assertTrue("Wrong result BI3: " + out, out.toString().equals( - "9,223,372,036,854,775,808")); + assertTrue("Wrong result BI3: " + out, out.toString().equals("9,223,372,036,854,775,808")); // format minLong - 1 pos = new FieldPosition(0); big = new java.math.BigInteger(String.valueOf(Long.MIN_VALUE)) .add(new java.math.BigInteger("-1")); out = format.format(big, new StringBuffer(), pos); - assertTrue("Wrong result BI4: " + out, out.toString().equals( - "-9,223,372,036,854,775,809")); + assertTrue("Wrong result BI4: " + out, out.toString().equals("-9,223,372,036,854,775,809")); // format big decimal pos = new FieldPosition(0); - out = format.format(new java.math.BigDecimal("51.348"), - new StringBuffer(), pos); + out = format.format(new java.math.BigDecimal("51.348"), new StringBuffer(), pos); assertTrue("Wrong result BD1: " + out, out.toString().equals("51.348")); // format big decimal pos = new FieldPosition(0); - out = format.format(new java.math.BigDecimal("51"), new StringBuffer(), - pos); + out = format.format(new java.math.BigDecimal("51"), new StringBuffer(), pos); assertTrue("Wrong result BD2: " + out, out.toString().equals("51")); // format big decimal Double.MAX_VALUE * 2 @@ -602,126 +615,35 @@ public class DecimalFormatTest extends TestCase { + "490,980,180,778,657,888,151,737,016,910,267,884,609,166,473,806," + "445,896,331,617,118,664,246,696,549,595,652,408,289,446,337,476," + "354,361,838,599,762,500,808,052,368,249,716,736"; - bigDecimal = new BigDecimal(Double.MAX_VALUE).add(new BigDecimal( - Double.MAX_VALUE)); + bigDecimal = new BigDecimal(Double.MAX_VALUE).add(new BigDecimal(Double.MAX_VALUE)); out = format.format(bigDecimal, new StringBuffer(), pos); - assertTrue("Wrong result BDmax2: " + out, out.toString().equals( - doubleMax2)); + assertTrue("Wrong result BDmax2: " + out, out.toString().equals(doubleMax2)); // format big decimal Double.MIN_VALUE + Double.MIN_VALUE // and Double.MIN_VALUE - Double.MIN_VALUE pos = new FieldPosition(0); - bigDecimal = new BigDecimal(Double.MIN_VALUE).add(new BigDecimal( - Double.MIN_VALUE)); + bigDecimal = new BigDecimal(Double.MIN_VALUE).add(new BigDecimal(Double.MIN_VALUE)); out = format.format(bigDecimal, new StringBuffer(), pos); - bigDecimal = new BigDecimal(Float.MAX_VALUE).add(new BigDecimal( - Float.MAX_VALUE)); + bigDecimal = new BigDecimal(Float.MAX_VALUE).add(new BigDecimal(Float.MAX_VALUE)); out = format.format(bigDecimal, new StringBuffer(), pos); final String BDFloatMax2 = "680,564,693,277,057,719,623,408,366,969,033,850,880"; - assertTrue("Wrong result BDFloatMax2: " + out, out.toString().equals( - BDFloatMax2)); + assertTrue("Wrong result BDFloatMax2: " + out, out.toString().equals(BDFloatMax2)); // format big decimal Float.MIN_VALUE + Float.MIN_VALUE // and Float.MIN_VALUE - Float.MIN_VALUE - bigDecimal = new BigDecimal(Float.MIN_VALUE).add(new BigDecimal( - Float.MIN_VALUE)); + bigDecimal = new BigDecimal(Float.MIN_VALUE).add(new BigDecimal(Float.MIN_VALUE)); out = format.format(bigDecimal, new StringBuffer(), pos); final String BDFloatMin2 = "0"; - bigDecimal = new BigDecimal(Float.MIN_VALUE).subtract(new BigDecimal( - Float.MIN_VALUE)); + bigDecimal = new BigDecimal(Float.MIN_VALUE).subtract(new BigDecimal(Float.MIN_VALUE)); out = format.format(bigDecimal, new StringBuffer(), pos); - assertTrue("Wrong result BDFloatMax2: " + out, out.toString().equals( - BDFloatMin2)); - - } - - public void test_setMaximumFractionDigitsLjava_lang_Integer() { - NumberFormat nform = DecimalFormat.getInstance(Locale.US); - DecimalFormat form = (DecimalFormat) nform; - - form.setMaximumFractionDigits(-2); - assertEquals(0, form.getMaximumFractionDigits()); - - form.setMaximumFractionDigits(341); - assertEquals(341, form.getMaximumFractionDigits()); - } - - public void test_setMinimumFractionDigitsLjava_lang_Integer() { - NumberFormat nform = DecimalFormat.getInstance(Locale.US); - DecimalFormat form = (DecimalFormat) nform; - - form.setMinimumFractionDigits(-3); - assertEquals(0, form.getMinimumFractionDigits()); - - form.setMinimumFractionDigits(310); - assertEquals(310, form.getMinimumFractionDigits()); - } - - public void test_setMaximumIntegerDigitsLjava_lang_Integer() { - NumberFormat nform = DecimalFormat.getInstance(Locale.US); - DecimalFormat form = (DecimalFormat) nform; - - form.setMaximumIntegerDigits(-3); - assertEquals(0, form.getMaximumIntegerDigits()); - - form.setMaximumIntegerDigits(310); - assertEquals(310, form.getMaximumIntegerDigits()); - } - - public void test_setMinimumIntegerDigitsLjava_lang_Integer() { - NumberFormat nform = DecimalFormat.getInstance(Locale.US); - DecimalFormat form = (DecimalFormat) nform; - - form.setMinimumIntegerDigits(-3); - assertEquals(0, form.getMinimumIntegerDigits()); - - form.setMinimumIntegerDigits(310); - assertEquals(310, form.getMinimumIntegerDigits()); + assertTrue("Wrong result BDFloatMax2: " + out, out.toString().equals(BDFloatMin2)); } - // When MaxFractionDigits is set first and less than MinFractionDigits, max - // will be changed to min value - public void test_setMinimumFactionDigitsLjava_lang_Integer_setMaximumFractionDigitsLjava_lang_Integer() { - NumberFormat nform = DecimalFormat.getInstance(Locale.US); - DecimalFormat form = (DecimalFormat) nform; - - form.setMaximumFractionDigits(100); - form.setMinimumFractionDigits(200); - - assertEquals(200, form.getMaximumFractionDigits()); - assertEquals(200, form.getMinimumFractionDigits()); - - form.setMaximumIntegerDigits(100); - form.setMinimumIntegerDigits(200); - - assertEquals(200, form.getMaximumIntegerDigits()); - assertEquals(200, form.getMinimumIntegerDigits()); - } - - // When MinFractionDigits is set first and less than MaxFractionDigits, min - // will be changed to max value - public void test_setMaximumFactionDigitsLjava_lang_Integer_setMinimumFractionDigitsLjava_lang_Integer() { - NumberFormat nform = DecimalFormat.getInstance(Locale.US); - DecimalFormat form = (DecimalFormat) nform; - - form.setMinimumFractionDigits(200); - form.setMaximumFractionDigits(100); - - assertEquals(100, form.getMaximumFractionDigits()); - assertEquals(100, form.getMinimumFractionDigits()); - - form.setMinimumIntegerDigits(200); - form.setMaximumIntegerDigits(100); - - assertEquals(100, form.getMaximumIntegerDigits()); - assertEquals(100, form.getMinimumIntegerDigits()); - } - - public void test_equalsLjava_lang_Object() { - DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + public void test_equals() { + DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US); DecimalFormat cloned = (DecimalFormat) format.clone(); cloned.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US)); assertEquals(format, cloned); @@ -733,136 +655,139 @@ public class DecimalFormatTest extends TestCase { } public void test_getNegativePrefix() { - DecimalFormat df = new DecimalFormat(); - df.setNegativePrefix("--"); - assertTrue("Incorrect negative prefix", df.getNegativePrefix().equals("--")); + DecimalFormat df = new DecimalFormat(); + df.setNegativePrefix("--"); + assertTrue("Incorrect negative prefix", df.getNegativePrefix().equals("--")); } public void test_getNegativeSuffix() { - DecimalFormat df = new DecimalFormat(); - df.setNegativeSuffix("&"); - assertTrue("Incorrect negative suffix", df.getNegativeSuffix().equals("&")); + DecimalFormat df = new DecimalFormat(); + df.setNegativeSuffix("&"); + assertTrue("Incorrect negative suffix", df.getNegativeSuffix().equals("&")); } public void test_getPositivePrefix() { - DecimalFormat df = new DecimalFormat(); - df.setPositivePrefix("++"); - assertTrue("Incorrect positive prefix", df.getPositivePrefix().equals("++")); + DecimalFormat df = new DecimalFormat(); + df.setPositivePrefix("++"); + assertTrue("Incorrect positive prefix", df.getPositivePrefix().equals("++")); } public void test_getPositiveSuffix() { - DecimalFormat df = new DecimalFormat(); - df.setPositiveSuffix("%"); - assertTrue("Incorrect positive prefix", df.getPositiveSuffix().equals("%")); + DecimalFormat df = new DecimalFormat(); + df.setPositiveSuffix("%"); + assertTrue("Incorrect positive prefix", df.getPositiveSuffix().equals("%")); } - public void test_setPositivePrefixLjava_lang_String() throws Exception { - DecimalFormat format = new DecimalFormat(); - assertEquals("", format.getPositivePrefix()); + public void test_setPositivePrefix() throws Exception { + DecimalFormat format = new DecimalFormat(); + assertEquals("", format.getPositivePrefix()); - format.setPositivePrefix("PosPrf"); - assertEquals("PosPrf", format.getPositivePrefix()); - assertTrue(format.parse("PosPrf123.45").doubleValue() == 123.45); + format.setPositivePrefix("PosPrf"); + assertEquals("PosPrf", format.getPositivePrefix()); + assertTrue(format.parse("PosPrf123.45").doubleValue() == 123.45); - format.setPositivePrefix(""); - assertEquals("", format.getPositivePrefix()); + format.setPositivePrefix(""); + assertEquals("", format.getPositivePrefix()); - format.setPositivePrefix(null); - assertNull(format.getPositivePrefix()); + format.setPositivePrefix(null); + assertNull(format.getPositivePrefix()); } - public void test_setPositiveSuffixLjava_lang_String() throws Exception { - DecimalFormat format = new DecimalFormat(); - assertEquals("", format.getPositiveSuffix()); + public void test_setPositiveSuffix() throws Exception { + DecimalFormat format = new DecimalFormat(); + assertEquals("", format.getPositiveSuffix()); - format.setPositiveSuffix("PosSfx"); - assertEquals("PosSfx", format.getPositiveSuffix()); - assertTrue(format.parse("123.45PosSfx").doubleValue() == 123.45); + format.setPositiveSuffix("PosSfx"); + assertEquals("PosSfx", format.getPositiveSuffix()); + assertTrue(format.parse("123.45PosSfx").doubleValue() == 123.45); - format.setPositiveSuffix(""); - assertEquals("", format.getPositiveSuffix()); + format.setPositiveSuffix(""); + assertEquals("", format.getPositiveSuffix()); - format.setPositiveSuffix(null); - assertNull(format.getPositiveSuffix()); + format.setPositiveSuffix(null); + assertNull(format.getPositiveSuffix()); } - public void test_setNegativePrefixLjava_lang_String() throws Exception { - DecimalFormat format = new DecimalFormat(); - assertEquals("-", format.getNegativePrefix()); + public void test_setNegativePrefix() throws Exception { + DecimalFormat format = new DecimalFormat(); + assertEquals("-", format.getNegativePrefix()); - format.setNegativePrefix("NegPrf"); - assertEquals("NegPrf", format.getNegativePrefix()); - assertTrue(format.parse("NegPrf123.45").doubleValue() == -123.45); - format.setNegativePrefix(""); - assertEquals("", format.getNegativePrefix()); + format.setNegativePrefix("NegPrf"); + assertEquals("NegPrf", format.getNegativePrefix()); + assertTrue(format.parse("NegPrf123.45").doubleValue() == -123.45); + format.setNegativePrefix(""); + assertEquals("", format.getNegativePrefix()); - format.setNegativePrefix(null); - assertNull(format.getNegativePrefix()); + format.setNegativePrefix(null); + assertNull(format.getNegativePrefix()); } - public void test_setNegativeSuffixLjava_lang_String() throws Exception { - DecimalFormat format = new DecimalFormat(); - assertEquals("", format.getNegativeSuffix()); + public void test_setNegativeSuffix() throws Exception { + DecimalFormat format = new DecimalFormat(); + assertEquals("", format.getNegativeSuffix()); - format.setNegativeSuffix("NegSfx"); - assertEquals("NegSfx", format.getNegativeSuffix()); - assertTrue(format.parse("123.45NegPfx").doubleValue() == 123.45); + format.setNegativeSuffix("NegSfx"); + assertEquals("NegSfx", format.getNegativeSuffix()); + assertTrue(format.parse("123.45NegPfx").doubleValue() == 123.45); - format.setNegativeSuffix(""); - assertEquals("", format.getNegativeSuffix()); + format.setNegativeSuffix(""); + assertEquals("", format.getNegativeSuffix()); - format.setNegativeSuffix(null); - assertNull(format.getNegativeSuffix()); + format.setNegativeSuffix(null); + assertNull(format.getNegativeSuffix()); } - public void test_setGroupingUse() { - DecimalFormat format = new DecimalFormat(); + public void test_setGroupingUsed() { + DecimalFormat format = new DecimalFormat(); - StringBuffer buf = new StringBuffer(); - format.setGroupingUsed(false); - format.format(new Long(1970), buf, new FieldPosition(0)); - assertEquals("1970", buf.toString()); - assertFalse(format.isGroupingUsed()); - format.format(new Long(1970), buf, new FieldPosition(0)); - assertEquals("19701970", buf.toString()); - assertFalse(format.isGroupingUsed()); + StringBuffer buf = new StringBuffer(); + format.setGroupingUsed(false); + format.format(new Long(1970), buf, new FieldPosition(0)); + assertEquals("1970", buf.toString()); + assertFalse(format.isGroupingUsed()); + format.format(new Long(1970), buf, new FieldPosition(0)); + assertEquals("19701970", buf.toString()); + assertFalse(format.isGroupingUsed()); - format.setGroupingUsed(true); - format.format(new Long(1970), buf, new FieldPosition(0)); - assertEquals("197019701,970", buf.toString()); - assertTrue(format.isGroupingUsed()); + format.setGroupingUsed(true); + format.format(new Long(1970), buf, new FieldPosition(0)); + assertEquals("197019701,970", buf.toString()); + assertTrue(format.isGroupingUsed()); } public void test_isGroupingUsed() { - assertFalse(new DecimalFormat("####.##").isGroupingUsed()); - assertFalse(new DecimalFormat("######.######").isGroupingUsed()); - assertFalse(new DecimalFormat("000000.000000").isGroupingUsed()); - assertFalse(new DecimalFormat("######.000000").isGroupingUsed()); - assertFalse(new DecimalFormat("000000.######").isGroupingUsed()); - assertFalse(new DecimalFormat(" ###.###").isGroupingUsed()); - assertFalse(new DecimalFormat("$#####.######").isGroupingUsed()); - assertFalse(new DecimalFormat("$$####.######").isGroupingUsed()); - - assertTrue(new DecimalFormat("###,####").isGroupingUsed()); - } - - public void test_Constructor() { - // Test for method java.text.DecimalFormat() - // the constructor form that specifies a pattern is equal to the form - // constructed with no pattern and applying that pattern using the - // applyPattern call - DecimalFormat format1 = new DecimalFormat(); - format1.applyPattern("'$'1000.0000"); - DecimalFormat format2 = new DecimalFormat(); - format2.applyPattern("'$'1000.0000"); - assertTrue("Constructed format did not match applied format object", format2.equals(format1)); - DecimalFormat format3 = new DecimalFormat("'$'1000.0000"); - assertTrue("Constructed format did not match applied format object", format3.equals(format1)); - DecimalFormat format4 = new DecimalFormat("'$'8000.0000"); - assertTrue("Constructed format did not match applied format object", !format4.equals(format1)); - } - - public void test_ConstructorLjava_lang_String() { + assertFalse(new DecimalFormat("####.##").isGroupingUsed()); + assertFalse(new DecimalFormat("######.######").isGroupingUsed()); + assertFalse(new DecimalFormat("000000.000000").isGroupingUsed()); + assertFalse(new DecimalFormat("######.000000").isGroupingUsed()); + assertFalse(new DecimalFormat("000000.######").isGroupingUsed()); + assertFalse(new DecimalFormat(" ###.###").isGroupingUsed()); + assertFalse(new DecimalFormat("$#####.######").isGroupingUsed()); + assertFalse(new DecimalFormat("$$####.######").isGroupingUsed()); + + assertTrue(new DecimalFormat("###,####").isGroupingUsed()); + } + + public void testConstructor_noArg() { + // Test for method java.text.DecimalFormat() + // the constructor form that specifies a pattern is equal to the form + // constructed with no pattern and applying that pattern using the + // applyPattern call + DecimalFormat format1 = new DecimalFormat(); + format1.applyPattern("'$'1000.0000"); + DecimalFormat format2 = new DecimalFormat(); + format2.applyPattern("'$'1000.0000"); + assertTrue("Constructed format did not match applied format object", + format2.equals(format1)); + DecimalFormat format3 = new DecimalFormat("'$'1000.0000"); + assertTrue("Constructed format did not match applied format object", + format3.equals(format1)); + DecimalFormat format4 = new DecimalFormat("'$'8000.0000"); + assertTrue("Constructed format did not match applied format object", + !format4.equals(format1)); + } + + public void testConstructor_string() { // Test for method java.text.DecimalFormat(java.lang.String) // the constructor form that specifies a pattern is equal to the form // constructed with no pattern and applying that pattern using the @@ -870,7 +795,8 @@ public class DecimalFormatTest extends TestCase { DecimalFormat format = new DecimalFormat("'$'0000.0000"); DecimalFormat format1 = new DecimalFormat(); format1.applyPattern("'$'0000.0000"); - assertTrue("Constructed format did not match applied format object", format.equals(format1)); + assertTrue("Constructed format did not match applied format object", + format.equals(format1)); new DecimalFormat("####.##"); new DecimalFormat("######.######"); @@ -902,44 +828,46 @@ public class DecimalFormatTest extends TestCase { } } - public void test_ConstructorLjava_lang_StringLjava_text_DecimalFormatSymbols() { - // case 1: Try to construct object using correct pattern and format - // symbols. - DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.CANADA); - DecimalFormat format1 = new DecimalFormat("'$'1000.0000", dfs); - DecimalFormat format2 = new DecimalFormat(); - format2.applyPattern("'$'1000.0000"); - format2.setDecimalFormatSymbols(dfs); - assertTrue("Constructed format did not match applied format object", format2.equals(format1)); - assertTrue("Constructed format did not match applied format object", - !format1.equals(new DecimalFormat("'$'1000.0000", new DecimalFormatSymbols(Locale.CHINA)))); - - // case 2: Try to construct object using null arguments. - try { - new DecimalFormat("'$'1000.0000", (DecimalFormatSymbols) null); - fail(); - } catch (NullPointerException expected) { - } - try { - new DecimalFormat(null, new DecimalFormatSymbols()); - fail(); - } catch (NullPointerException expected) { - } - try { - new DecimalFormat(null, (DecimalFormatSymbols) null); - fail(); - } catch (NullPointerException expected) { - } - - // case 3: Try to construct object using incorrect pattern. - try { - new DecimalFormat("$'", new DecimalFormatSymbols()); - fail(); - } catch (IllegalArgumentException expected) { - } - } - - public void test_applyPatternLjava_lang_String() { + public void testConstructor_stringAndSymbols() { + // case 1: Try to construct object using correct pattern and format + // symbols. + DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.CANADA); + DecimalFormat format1 = new DecimalFormat("'$'1000.0000", dfs); + DecimalFormat format2 = new DecimalFormat(); + format2.applyPattern("'$'1000.0000"); + format2.setDecimalFormatSymbols(dfs); + assertTrue("Constructed format did not match applied format object", + format2.equals(format1)); + assertTrue("Constructed format did not match applied format object", + !format1.equals( + new DecimalFormat("'$'1000.0000", new DecimalFormatSymbols(Locale.CHINA)))); + + // case 2: Try to construct object using null arguments. + try { + new DecimalFormat("'$'1000.0000", (DecimalFormatSymbols) null); + fail(); + } catch (NullPointerException expected) { + } + try { + new DecimalFormat(null, new DecimalFormatSymbols()); + fail(); + } catch (NullPointerException expected) { + } + try { + new DecimalFormat(null, (DecimalFormatSymbols) null); + fail(); + } catch (NullPointerException expected) { + } + + // case 3: Try to construct object using incorrect pattern. + try { + new DecimalFormat("$'", new DecimalFormatSymbols()); + fail(); + } catch (IllegalArgumentException expected) { + } + } + + public void test_applyPattern() { DecimalFormat format = new DecimalFormat("#.#"); assertEquals("Wrong pattern 1", "#0.#", format.toPattern()); format = new DecimalFormat("#."); @@ -979,34 +907,35 @@ public class DecimalFormatTest extends TestCase { } // AndroidOnly: icu supports 2 grouping sizes - public void test_applyPatternLjava_lang_String2() { + public void test_applyPattern_icu2GroupingSizes() { DecimalFormat decFormat = new DecimalFormat("#.#"); String[] patterns = { - "####.##", "######.######", "000000.000000", - "######.000000", "000000.######", " ###.###", "$#####.######", - "$$####.######", "%#,##,###,####", "#,##0.00;(#,##0.00)", - "##.##-E" + "####.##", "######.######", "000000.000000", + "######.000000", "000000.######", " ###.###", "$#####.######", + "$$####.######", "%#,##,###,####", "#,##0.00;(#,##0.00)", + "##.##-E" }; String[] expResult = { - "#0.##", "#0.######", "#000000.000000", - "#.000000", "#000000.######", " #0.###", "$#0.######", - "$$#0.######", - "%#,###,####", // icu only. icu supports two grouping sizes - "#,##0.00;(#,##0.00)", - "#0.##-'E'" // icu only. E in the suffix does not need to be quoted. This is done automatically. + "#0.##", "#0.######", "#000000.000000", + "#.000000", "#000000.######", " #0.###", "$#0.######", + "$$#0.######", + "%#,###,####", // icu only. icu supports two grouping sizes + "#,##0.00;(#,##0.00)", + "#0.##-'E'" + // icu only. E in the suffix does not need to be quoted. This is done automatically. }; for (int i = 0; i < patterns.length; i++) { decFormat.applyPattern(patterns[i]); String result = decFormat.toPattern(); assertEquals("Failed to apply following pattern: " + patterns[i] + - "\n expected: " + expResult[i] + - "\n returned: " + result, expResult[i], result); + "\n expected: " + expResult[i] + + "\n returned: " + result, expResult[i], result); } } - public void test_applyLocalizedPatternLjava_lang_String() throws Exception { + public void test_applyLocalizedPattern() throws Exception { DecimalFormat format = new DecimalFormat(); // case 1: Try to apply correct variants of pattern. @@ -1035,28 +964,28 @@ public class DecimalFormatTest extends TestCase { } public void test_toPattern() { - DecimalFormat format = new DecimalFormat(); - format.applyPattern("#.#"); - assertEquals("Wrong pattern 1", "#0.#", format.toPattern()); - format.applyPattern("#."); - assertEquals("Wrong pattern 2", "#0.", format.toPattern()); - format.applyPattern("#"); - assertEquals("Wrong pattern 3", "#", format.toPattern()); - format.applyPattern(".#"); - assertEquals("Wrong pattern 4", "#.0", format.toPattern()); + DecimalFormat format = new DecimalFormat(); + format.applyPattern("#.#"); + assertEquals("Wrong pattern 1", "#0.#", format.toPattern()); + format.applyPattern("#."); + assertEquals("Wrong pattern 2", "#0.", format.toPattern()); + format.applyPattern("#"); + assertEquals("Wrong pattern 3", "#", format.toPattern()); + format.applyPattern(".#"); + assertEquals("Wrong pattern 4", "#.0", format.toPattern()); } public void test_toLocalizedPattern() { - DecimalFormat format = new DecimalFormat(); - format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US)); - format.applyLocalizedPattern("#.#"); - assertEquals("Wrong pattern 1", "#0.#", format.toLocalizedPattern()); - format.applyLocalizedPattern("#."); - assertEquals("Wrong pattern 2", "#0.", format.toLocalizedPattern()); - format.applyLocalizedPattern("#"); - assertEquals("Wrong pattern 3", "#", format.toLocalizedPattern()); - format.applyLocalizedPattern(".#"); - assertEquals("Wrong pattern 4", "#.0", format.toLocalizedPattern()); + DecimalFormat format = new DecimalFormat(); + format.setDecimalFormatSymbols(new DecimalFormatSymbols(Locale.US)); + format.applyLocalizedPattern("#.#"); + assertEquals("Wrong pattern 1", "#0.#", format.toLocalizedPattern()); + format.applyLocalizedPattern("#."); + assertEquals("Wrong pattern 2", "#0.", format.toLocalizedPattern()); + format.applyLocalizedPattern("#"); + assertEquals("Wrong pattern 3", "#", format.toLocalizedPattern()); + format.applyLocalizedPattern(".#"); + assertEquals("Wrong pattern 4", "#.0", format.toLocalizedPattern()); } public void test_hashCode() { @@ -1066,7 +995,7 @@ public class DecimalFormatTest extends TestCase { } public void test_clone() { - DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.US); DecimalFormat cloned = (DecimalFormat) format.clone(); assertEquals(cloned.getDecimalFormatSymbols(), format.getDecimalFormatSymbols()); @@ -1080,302 +1009,482 @@ public class DecimalFormatTest extends TestCase { assertTrue("Object's changed clone should not be equal!", !format.equals(format1)); } - private void compare(String testName, String format, String expected) { - assertTrue(testName + " got: " + format + " expected: " + expected, - format.equals(expected)); - } - - // icu4c and the RI disagree about these patterns, and I'm not yet sure which is correct. - public void test_formatDLjava_lang_StringBufferLjava_text_FieldPosition_problem_cases() { - final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); - DecimalFormat df; - - df = new DecimalFormat("##0.0E0", dfs); - compare("##0.0E0: 123.0", df.format(123.0), "123E0"); - compare("##0.0E0: 1234.0", df.format(1234.0), "1.234E3"); - compare("##0.0E0: 12346.0", df.format(12346.0), "12.35E3"); - - df = new DecimalFormat("#00.0##E0", dfs); - compare("#00.0##E0: 0.1", df.format(0.1), ".100E0"); - compare("#00.0##E0: 0.12", df.format(0.12), ".120E0"); - compare("#00.0##E0: 0.123", df.format(0.123), ".123E0"); - compare("#00.0##E0: 0.1234", df.format(0.1234), ".1234E0"); - compare("#00.0##E0: 0.1234567", df.format(0.1234567), ".123457E0"); - compare("#00.0##E0: 0.01", df.format(0.01), "10.0E-3"); - compare("#00.0##E0: 0.012", df.format(0.012), "12.0E-3"); - compare("#00.0##E0: 0.0123", df.format(0.0123), "12.3E-3"); - compare("#00.0##E0: 0.01234", df.format(0.01234), "12.34E-3"); - compare("#00.0##E0: 0.01234567", df.format(0.01234567), "12.3457E-3"); - compare("#00.0##E0: 0.001", df.format(0.001), "1.00E-3"); - compare("#00.0##E0: 0.0012", df.format(0.0012), "1.20E-3"); - compare("#00.0##E0: 0.00123", df.format(0.00123), "1.23E-3"); - compare("#00.0##E0: 0.001234", df.format(0.001234), "1.234E-3"); - compare("#00.0##E0: 0.001234567", df.format(0.001234567), "1.23457E-3"); - compare("#00.0##E0: 0.0001", df.format(0.0001), "100E-6"); - compare("#00.0##E0: 0.00012", df.format(0.00012), "120E-6"); - compare("#00.0##E0: 0.000123", df.format(0.000123), "123E-6"); - compare("#00.0##E0: 0.0001234", df.format(0.0001234), "123.4E-6"); - compare("#00.0##E0: 0.0001234567", df.format(0.0001234567), "123.457E-6"); - - compare("#00.0##E0: 0.0", df.format(0.0), "0.00E0"); - compare("#00.0##E0: 1.0", df.format(1.0), "1.00E0"); - compare("#00.0##E0: 12.0", df.format(12.0), "12.0E0"); - compare("#00.0##E0: 123.0", df.format(123.0), "123E0"); - compare("#00.0##E0: 1234.0", df.format(1234.0), "1.234E3"); - compare("#00.0##E0: 12345.0", df.format(12345.0), "12.345E3"); - compare("#00.0##E0: 123456.0", df.format(123456.0), "123.456E3"); - compare("#00.0##E0: 1234567.0", df.format(1234567.0), "1.23457E6"); - compare("#00.0##E0: 12345678.0", df.format(12345678.0), "12.3457E6"); - compare("#00.0##E0: 99999999.0", df.format(99999999.0), "100E6"); - - df = new DecimalFormat("#.0E0", dfs); - compare("#.0E0: 0.0", df.format(0.0), ".0E0"); - compare("#.0E0: 1.0", df.format(1.0), ".1E1"); - compare("#.0E0: 12.0", df.format(12.0), ".12E2"); - compare("#.0E0: 123.0", df.format(123.0), ".12E3"); - compare("#.0E0: 1234.0", df.format(1234.0), ".12E4"); - compare("#.0E0: 9999.0", df.format(9999.0), ".1E5"); - - df = new DecimalFormat("0.E0", dfs); - compare("0.E0: 0.0", df.format(0.0), "0.E0"); - compare("0.E0: 1.0", df.format(1.0), "1.E0"); - compare("0.E0: 12.0", df.format(12.0), "1.E1"); - compare("0.E0: 123.0", df.format(123.0), "1.E2"); - compare("0.E0: 1234.0", df.format(1234.0), "1.E3"); - compare("0.E0: 9999.0", df.format(9999.0), "1.E4"); - - df = new DecimalFormat("##0.00#E0", dfs); - compare("##0.00#E0: 0.1", df.format(0.1), ".100E0"); - compare("##0.00#E0: 0.1234567", df.format(0.1234567), ".123457E0"); - compare("##0.00#E0: 0.9999999", df.format(0.9999999), "1.00E0"); - compare("##0.00#E0: 0.01", df.format(0.01), "10.0E-3"); - compare("##0.00#E0: 0.01234567", df.format(0.01234567), "12.3457E-3"); - compare("##0.00#E0: 0.09999999", df.format(0.09999999), ".100E0"); - compare("##0.00#E0: 0.001", df.format(0.001), "1.00E-3"); - compare("##0.00#E0: 0.001234567", df.format(0.001234567), "1.23457E-3"); - compare("##0.00#E0: 0.009999999", df.format(0.009999999), "10.0E-3"); - compare("##0.00#E0: 0.0001", df.format(0.0001), "100E-6"); - compare("##0.00#E0: 0.0001234567", df.format(0.0001234567), "123.457E-6"); - compare("##0.00#E0: 0.0009999999", df.format(0.0009999999), "1.00E-3"); - - df = new DecimalFormat("###0.00#E0", dfs); - compare("###0.00#E0: 0.1", df.format(0.1), ".100E0"); - compare("###0.00#E0: 0.12345678", df.format(0.12345678), ".1234568E0"); - compare("###0.00#E0: 0.99999999", df.format(0.99999999), "1.00E0"); - compare("###0.00#E0: 0.01", df.format(0.01), "100E-4"); - compare("###0.00#E0: 0.012345678", df.format(0.012345678), "123.4568E-4"); - compare("###0.00#E0: 0.099999999", df.format(0.099999999), ".100E0"); - compare("###0.00#E0: 0.001", df.format(0.001), "10.0E-4"); - compare("###0.00#E0: 0.0012345678", df.format(0.0012345678), "12.34568E-4"); - compare("###0.00#E0: 0.0099999999", df.format(0.0099999999), "100E-4"); - compare("###0.00#E0: 0.0001", df.format(0.0001), "1.00E-4"); - compare("###0.00#E0: 0.00012345678", df.format(0.00012345678), "1.234568E-4"); - compare("###0.00#E0: 0.00099999999", df.format(0.00099999999), "10.0E-4"); - compare("###0.00#E0: 0.00001", df.format(0.00001), "1000E-8"); - compare("###0.00#E0: 0.000012345678", df.format(0.000012345678), "1234.568E-8"); - compare("###0.00#E0: 0.000099999999", df.format(0.000099999999), "1.00E-4"); - - df = new DecimalFormat("###0.0#E0", dfs); - compare("###0.0#E0: 0.1", df.format(0.1), ".10E0"); - compare("###0.0#E0: 0.1234567", df.format(0.1234567), ".123457E0"); - compare("###0.0#E0: 0.9999999", df.format(0.9999999), "1.0E0"); - compare("###0.0#E0: 0.01", df.format(0.01), "100E-4"); - compare("###0.0#E0: 0.01234567", df.format(0.01234567), "123.457E-4"); - compare("###0.0#E0: 0.09999999", df.format(0.09999999), ".10E0"); - compare("###0.0#E0: 0.001", df.format(0.001), "10E-4"); - compare("###0.0#E0: 0.001234567", df.format(0.001234567), "12.3457E-4"); - compare("###0.0#E0: 0.009999999", df.format(0.009999999), "100E-4"); - compare("###0.0#E0: 0.0001", df.format(0.0001), "1.0E-4"); - compare("###0.0#E0: 0.0001234567", df.format(0.0001234567), "1.23457E-4"); - compare("###0.0#E0: 0.0009999999", df.format(0.0009999999), "10E-4"); - compare("###0.0#E0: 0.00001", df.format(0.00001), "1000E-8"); - compare("###0.0#E0: 0.00001234567", df.format(0.00001234567), "1234.57E-8"); - compare("###0.0#E0: 0.00009999999", df.format(0.00009999999), "1.0E-4"); - } - - public void test_formatDLjava_lang_StringBufferLjava_text_FieldPosition() { + public void test_formatDouble_maximumFractionDigits() { + DecimalFormat df = new DecimalFormat("###0.##", new DecimalFormatSymbols(Locale.US)); + df.setMaximumFractionDigits(3); + assertEquals(3, df.getMaximumFractionDigits()); + assertEquals("1.235", df.format(1.23456)); + df.setMinimumFractionDigits(4); + assertEquals(4, df.getMaximumFractionDigits()); + assertEquals("456.0000", df.format(456)); + + df = new DecimalFormat("##0.#"); + df.setMaximumFractionDigits(30); + assertEquals("0", df.format(0.0)); + assertEquals("-0", df.format(-0.0)); + assertEquals("1", df.format(1.0)); + assertEquals("-1", df.format(-1.0)); + } + + public void test_formatDouble_minimumFractionDigits() { + DecimalFormat df = new DecimalFormat("###0.##", new DecimalFormatSymbols(Locale.US)); + df.setMinimumFractionDigits(4); + assertEquals(4, df.getMinimumFractionDigits()); + assertEquals("1.2300", df.format(1.23)); + df.setMaximumFractionDigits(2); + assertEquals(2, df.getMinimumFractionDigits()); + assertEquals("456.00", df.format(456)); + + df = new DecimalFormat("##0.#", new DecimalFormatSymbols(Locale.US)); + df.setMinimumFractionDigits(30); + assertEquals("0.000000000000000000000000000000", df.format(0.0)); + assertEquals("-0.000000000000000000000000000000", df.format(-0.0)); + assertEquals("1.000000000000000000000000000000", df.format(1.0)); + assertEquals("-1.000000000000000000000000000000", df.format(-1.0)); + } + + public void test_formatDouble_withFieldPosition() { new Support_DecimalFormat( "test_formatDLjava_lang_StringBufferLjava_text_FieldPosition") .t_format_with_FieldPosition(); + } + // This test serves as a regression test for Android's behavior. + // There are many patterns that produce different output from the RI but are sometimes the + // consequence of Android following the ICU DecimalFormat rules. + public void test_formatDouble_scientificNotation() { + FormatTester formatTester = new FormatTester(); final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); DecimalFormat df = new DecimalFormat("00.0#E0", dfs); - compare("00.0#E0: 0.0", df.format(0.0), "00.0E0"); - compare("00.0#E0: 1.0", df.format(1.0), "10.0E-1"); - compare("00.0#E0: 12.0", df.format(12.0), "12.0E0"); - compare("00.0#E0: 123.0", df.format(123.0), "12.3E1"); - compare("00.0#E0: 1234.0", df.format(1234.0), "12.34E2"); - compare("00.0#E0: 12346.0", df.format(12346.0), "12.35E3"); - compare("00.0#E0: 99999.0", df.format(99999.0), "10.0E4"); - compare("00.0#E0: 1.2", df.format(1.2), "12.0E-1"); - compare("00.0#E0: 12.3", df.format(12.3), "12.3E0"); - compare("00.0#E0: 123.4", df.format(123.4), "12.34E1"); - compare("00.0#E0: 1234.6", df.format(1234.6), "12.35E2"); - compare("00.0#E0: 9999.9", df.format(9999.9), "10.0E3"); - compare("00.0#E0: 0.1", df.format(0.1), "10.0E-2"); - compare("00.0#E0: 0.12", df.format(0.12), "12.0E-2"); - compare("00.0#E0: 0.123", df.format(0.123), "12.3E-2"); - compare("00.0#E0: 0.1234", df.format(0.1234), "12.34E-2"); - compare("00.0#E0: 0.12346", df.format(0.12346), "12.35E-2"); - compare("00.0#E0: 0.99999", df.format(0.99999), "10.0E-1"); - compare("00.0#E0: -1.0", df.format(-1.0), "-10.0E-1"); - compare("00.0#E0: -12.0", df.format(-12.0), "-12.0E0"); - compare("00.0#E0: -123.0", df.format(-123.0), "-12.3E1"); - compare("00.0#E0: -1234.0", df.format(-1234.0), "-12.34E2"); - compare("00.0#E0: -12346.0", df.format(-12346.0), "-12.35E3"); - compare("00.0#E0: -99999.0", df.format(-99999.0), "-10.0E4"); + // ["00.0#E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, + // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=2, + // maxFractionDigits=2,minIntegerDigits=2,minFractionDigits=1,grouping=false] + // Because maximum integer digit was not explicitly set: The exponent can be any integer. + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (2) + "max fractional digits (2) == 4 + formatTester.format(df, "00.0E0", 0.0); + formatTester.format(df, "10.0E-1", 1.0); + formatTester.format(df, "12.0E0", 12.0); + formatTester.format(df, "12.3E1", 123.0); + formatTester.format(df, "12.34E2", 1234.0); + formatTester.format(df, "12.35E3", 12346.0); + formatTester.format(df, "10.0E4", 99999.0); + formatTester.format(df, "12.0E-1", 1.2); + formatTester.format(df, "12.3E0", 12.3); + formatTester.format(df, "12.34E1", 123.4); + formatTester.format(df, "12.35E2", 1234.6); + formatTester.format(df, "10.0E3", 9999.9); + formatTester.format(df, "10.0E-2", 0.1); + formatTester.format(df, "12.0E-2", 0.12); + formatTester.format(df, "12.3E-2", 0.123); + formatTester.format(df, "12.34E-2", 0.1234); + formatTester.format(df, "12.35E-2", 0.12346); + formatTester.format(df, "10.0E-1", 0.99999); + formatTester.format(df, "-10.0E-1", -1.0); + formatTester.format(df, "-12.0E0", -12.0); + formatTester.format(df, "-12.3E1", -123.0); + formatTester.format(df, "-12.34E2", -1234.0); + formatTester.format(df, "-12.35E3", -12346.0); + formatTester.format(df, "-10.0E4", -99999.0); + + df = new DecimalFormat("#00.0##E0", dfs); + // ["#00.0##E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, + // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=3, + // maxFractionDigits=3,minIntegerDigits=2,minFractionDigits=1,grouping=false] + // Because maximum integer digit count is set: The exponent must be a multiple of it (3). + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (2) + "max fractional digits (3) == 5 + formatTester.format(df, "100E-3", 0.1); + formatTester.format(df, "120E-3", 0.12); + formatTester.format(df, "123E-3", 0.123); + formatTester.format(df, "123.4E-3", 0.1234); + formatTester.format(df, "123.46E-3", 0.1234567); + formatTester.format(df, "10E-3", 0.01); + formatTester.format(df, "12E-3", 0.012); + formatTester.format(df, "12.3E-3", 0.0123); + formatTester.format(df, "12.34E-3", 0.01234); + formatTester.format(df, "12.346E-3", 0.01234567); + formatTester.format(df, "1.0E-3", 0.001); + formatTester.format(df, "1.2E-3", 0.0012); + formatTester.format(df, "1.23E-3", 0.00123); + formatTester.format(df, "1.234E-3", 0.001234); + formatTester.format(df, "1.2346E-3", 0.001234567); + formatTester.format(df, "100E-6", 0.0001); + formatTester.format(df, "120E-6", 0.00012); + formatTester.format(df, "123E-6", 0.000123); + formatTester.format(df, "123.4E-6", 0.0001234); + formatTester.format(df, "123.46E-6", 0.0001234567); + formatTester.format(df, "0.0E0", 0.0); + formatTester.format(df, "1.0E0", 1.0); + formatTester.format(df, "12E0", 12.0); + formatTester.format(df, "123E0", 123.0); + formatTester.format(df, "1.234E3", 1234.0); + formatTester.format(df, "12.345E3", 12345.0); + formatTester.format(df, "123.46E3", 123456.0); + formatTester.format(df, "1.2346E6", 1234567.0); + formatTester.format(df, "12.346E6", 12345678.0); + formatTester.format(df, "100E6", 99999999.0); + + df = new DecimalFormat("#.0E0", dfs); + // ["#.0E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, + // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=1, + // maxFractionDigits=1,minIntegerDigits=0,minFractionDigits=1,grouping=false] + // Because maximum integer digit count is set: The exponent must be a multiple of it (1). + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (0) + "max fractional digits (1) == 1 + formatTester.format(df, "0.0E0", 0.0); + formatTester.format(df, "1.0E0", 1.0); + formatTester.format(df, "1.0E1", 12.0); + formatTester.format(df, "1.0E2", 123.0); + formatTester.format(df, "1.0E3", 1234.0); + formatTester.format(df, "1.0E4", 9999.0); + + df = new DecimalFormat("0.E0", dfs); + // ["0.E0",isDecimalSeparatorAlwaysShown=true,groupingSize=0,multiplier=1,negativePrefix=-, + // negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=1,maxFractionDigits=0, + // minIntegerDigits=1,minFractionDigits=0,grouping=false] + // Because maximum integer digit was not explicitly set: The exponent can be any integer. + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (1) + "max fractional digits (0) == 1 + formatTester.format(df, "0E0", 0.0); + formatTester.format(df, "1E0", 1.0); + formatTester.format(df, "1E1", 12.0); + formatTester.format(df, "1E2", 123.0); + formatTester.format(df, "1E3", 1234.0); + formatTester.format(df, "1E4", 9999.0); + + df = new DecimalFormat("##0.00#E0", dfs); + // ["##0.00#E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, + // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=3, + // maxFractionDigits=3,minIntegerDigits=1,minFractionDigits=2,grouping=false] + // Because maximum integer digit count is set: The exponent must be a multiple of it (3). + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (1) + "max fractional digits (3) == 4 + formatTester.format(df, "100E-3", 0.1); + formatTester.format(df, "123.5E-3", 0.1234567); + formatTester.format(df, "1.00E0", 0.9999999); + formatTester.format(df, "10.0E-3", 0.01); + formatTester.format(df, "12.35E-3", 0.01234567); + formatTester.format(df, "100E-3", 0.09999999); + formatTester.format(df, "1.00E-3", 0.001); + formatTester.format(df, "1.235E-3", 0.001234567); + formatTester.format(df, "10.0E-3", 0.009999999); + formatTester.format(df, "100E-6", 0.0001); + formatTester.format(df, "123.5E-6", 0.0001234567); + formatTester.format(df, "1.00E-3", 0.0009999999); + + df = new DecimalFormat("###0.00#E0", dfs); + // ["###0.00#E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, + // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=4, + // maxFractionDigits=3,minIntegerDigits=1,minFractionDigits=2,grouping=false] + // Because maximum integer digit count is set: The exponent must be a multiple of it (4). + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (1) + "max fractional digits (3) == 4 + formatTester.format(df, "1000E-4", 0.1); + formatTester.format(df, "1235E-4", 0.12345678); + formatTester.format(df, "1.00E0", 0.99999999); + formatTester.format(df, "100E-4", 0.01); + formatTester.format(df, "123.5E-4", 0.012345678); + formatTester.format(df, "1000E-4", 0.099999999); + formatTester.format(df, "10.0E-4", 0.001); + formatTester.format(df, "12.35E-4", 0.0012345678); + formatTester.format(df, "100E-4", 0.0099999999); + formatTester.format(df, "1.00E-4", 0.0001); + formatTester.format(df, "1.235E-4", 0.00012345678); + formatTester.format(df, "10.0E-4", 0.00099999999); + formatTester.format(df, "1000E-8", 0.00001); + formatTester.format(df, "1235E-8", 0.000012345678); + formatTester.format(df, "1.00E-4", 0.000099999999); + + df = new DecimalFormat("###0.0#E0", dfs); + // ["###0.0#E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, + // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=4, + // maxFractionDigits=2,minIntegerDigits=1,minFractionDigits=1,grouping=false] + // Because maximum integer digit count is set: The exponent must be a multiple of it (4). + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (1) + "max fractional digits (2) == 3 + formatTester.format(df, "1000E-4", 0.1); + formatTester.format(df, "1230E-4", 0.1234567); + formatTester.format(df, "1.0E0", 0.9999999); + formatTester.format(df, "100E-4", 0.01); + formatTester.format(df, "123E-4", 0.01234567); + formatTester.format(df, "1000E-4", 0.09999999); + formatTester.format(df, "10E-4", 0.001); + formatTester.format(df, "12.3E-4", 0.001234567); + formatTester.format(df, "100E-4", 0.009999999); + formatTester.format(df, "1.0E-4", 0.0001); + formatTester.format(df, "1.23E-4", 0.0001234567); + formatTester.format(df, "10E-4", 0.0009999999); + formatTester.format(df, "1000E-8", 0.00001); + formatTester.format(df, "1230E-8", 0.00001234567); + formatTester.format(df, "1.0E-4", 0.00009999999); df = new DecimalFormat("##0.0E0", dfs); - compare("##0.0E0: 0.0", df.format(0.0), "0.0E0"); - compare("##0.0E0: 1.0", df.format(1.0), "1.0E0"); - compare("##0.0E0: 12.0", df.format(12.0), "12E0"); - compare("##0.0E0: 99999.0", df.format(99999.0), "100E3"); - compare("##0.0E0: 999999.0", df.format(999999.0), "1.0E6"); + // ["##0.0E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, + // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=3, + // maxFractionDigits=1,minIntegerDigits=1,minFractionDigits=1,grouping=false] + // Because maximum integer digit count is set: The exponent must be a multiple of it (3). + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (1) + "max fractional digits (1) == 2 + formatTester.format(df, "0.0E0", 0.0); + formatTester.format(df, "1.0E0", 1.0); + formatTester.format(df, "12E0", 12.0); + formatTester.format(df, "120E0", 123.0); + formatTester.format(df, "1.2E3", 1234.0); + formatTester.format(df, "12E3", 12346.0); + formatTester.format(df, "100E3", 99999.0); + formatTester.format(df, "1.0E6", 999999.0); df = new DecimalFormat("0.#E0", dfs); - compare("0.#E0: 0.0", df.format(0.0), "0E0"); - compare("0.#E0: 1.0", df.format(1.0), "1E0"); - compare("0.#E0: 12.0", df.format(12.0), "1.2E1"); - compare("0.#E0: 123.0", df.format(123.0), "1.2E2"); - compare("0.#E0: 1234.0", df.format(1234.0), "1.2E3"); - compare("0.#E0: 9999.0", df.format(9999.0), "1E4"); + // ["0.#E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, + // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=1, + // maxFractionDigits=1,minIntegerDigits=1,minFractionDigits=0,grouping=false] + // Because maximum integer digit was not explicitly set: The exponent can be any integer. + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (1) + "max fractional digits (1) == 2 + formatTester.format(df, "0E0", 0.0); + formatTester.format(df, "1E0", 1.0); + formatTester.format(df, "1.2E1", 12.0); + formatTester.format(df, "1.2E2", 123.0); + formatTester.format(df, "1.2E3", 1234.0); + formatTester.format(df, "1E4", 9999.0); df = new DecimalFormat(".0E0", dfs); - compare(".0E0: 0.0", df.format(0.0), ".0E0"); - compare(".0E0: 1.0", df.format(1.0), ".1E1"); - compare(".0E0: 12.0", df.format(12.0), ".1E2"); - compare(".0E0: 123.0", df.format(123.0), ".1E3"); - compare(".0E0: 1234.0", df.format(1234.0), ".1E4"); - compare(".0E0: 9999.0", df.format(9999.0), ".1E5"); - - String formatString = "##0.#"; - df = new DecimalFormat(formatString, dfs); - df.setMinimumFractionDigits(30); - compare(formatString + ": 0.000000000000000000000000000000", df - .format(0.0), "0.000000000000000000000000000000"); - compare(formatString + ": -0.000000000000000000000000000000", df - .format(-0.0), "-0.000000000000000000000000000000"); - compare(formatString + ": 1.000000000000000000000000000000", df - .format(1.0), "1.000000000000000000000000000000"); - compare(formatString + ": -1.000000000000000000000000000000", df - .format(-1.0), "-1.000000000000000000000000000000"); - - df = new DecimalFormat(formatString); - df.setMaximumFractionDigits(30); - compare(formatString + ": 0", df.format(0.0), "0"); - compare(formatString + ": -0", df.format(-0.0), "-0"); - compare(formatString + ": 1", df.format(1.0), "1"); - compare(formatString + ": -1", df.format(-1.0), "-1"); - } + // [".0E0",isDecimalSeparatorAlwaysShown=true,groupingSize=0,multiplier=1,negativePrefix=-, + // negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=0,maxFractionDigits=1, + // minIntegerDigits=0,minFractionDigits=1,grouping=false] + // Because maximum integer digit was not explicitly set: The exponent can be any integer. + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (0) + "max fractional digits (1) == 2 + formatTester.format(df, ".0E0", 0.0); + formatTester.format(df, ".1E1", 1.0); + formatTester.format(df, ".1E2", 12.0); + formatTester.format(df, ".1E3", 123.0); + formatTester.format(df, ".1E4", 1234.0); + formatTester.format(df, ".1E5", 9999.0); + + formatTester.throwFailures(); + } + + public void test_formatDouble_scientificNotationMinusZero() throws Exception { + FormatTester formatTester = new FormatTester(); + final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); - public void test_format_minus_zero() throws Exception { - final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); + DecimalFormat df = new DecimalFormat("00.0#E0", dfs); + // ["00.0#E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, + // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=2, + // maxFractionDigits=2,minIntegerDigits=2,minFractionDigits=1,grouping=false] + // Because maximum integer digit was not explicitly set: The exponent can be any integer. + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (2) + "max fractional digits (2) == 4 + formatTester.format(df, "-00.0E0", -0.0); - DecimalFormat df = new DecimalFormat("00.0#E0", dfs); - compare("00.0#E0: -0.0", df.format(-0.0), "-00.0E0"); + df = new DecimalFormat("##0.0E0", dfs); + // ["##0.0E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, + // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=3, + // maxFractionDigits=1,minIntegerDigits=1,minFractionDigits=1,grouping=false] + // Because maximum integer digit count is set: The exponent must be a multiple of it (3). + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (1) + "max fractional digits (1) == 2 + formatTester.format(df, "-0.0E0", -0.0); - df = new DecimalFormat("##0.0E0", dfs); - compare("##0.0E0: -0.0", df.format(-0.0), "-0.0E0"); + df = new DecimalFormat("#.0E0", dfs); + // ["#.0E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, + // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=1, + // maxFractionDigits=1,minIntegerDigits=0,minFractionDigits=1,grouping=false] + // Because maximum integer digit count is set: The exponent must be a multiple of it (1). + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (0) + "max fractional digits (1) == 2 + formatTester.format(df, "-0.0E0", -0.0); - df = new DecimalFormat("#.0E0", dfs); - compare("#.0E0: -0.0", df.format(-0.0), "-.0E0"); + df = new DecimalFormat("0.#E0", dfs); + // ["0.#E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, + // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=1, + // maxFractionDigits=1,minIntegerDigits=1,minFractionDigits=0,grouping=false] + // Because maximum integer digit was not explicitly set: The exponent can be any integer. + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (1) + "max fractional digits (1) == 2 + formatTester.format(df, "-0E0", -0.0); + + df = new DecimalFormat(".0E0", dfs); + // [".0E0",isDecimalSeparatorAlwaysShown=true,groupingSize=0,multiplier=1,negativePrefix=-, + // negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=0,maxFractionDigits=1, + // minIntegerDigits=0,minFractionDigits=1,grouping=false] + // Because maximum integer digit was not explicitly set: The exponent can be any integer. + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (0) + "max fractional digits (1) == 1 + formatTester.format(df, "-.0E0", -0.0); - df = new DecimalFormat("0.#E0", dfs); - compare("0.#E0: -0.0", df.format(-0.0), "-0E0"); + formatTester.throwFailures(); + } + + public void test_formatLong_maximumIntegerDigits() { + DecimalFormat df = new DecimalFormat("###0.##"); + df.setMaximumIntegerDigits(2); + assertEquals(2, df.getMaximumIntegerDigits()); + assertEquals("34", df.format(1234)); + df.setMinimumIntegerDigits(4); + assertEquals(4, df.getMaximumIntegerDigits()); + assertEquals("0026", df.format(26)); + } - df = new DecimalFormat(".0E0", dfs); - compare(".0E0: -0.0", df.format(-0.0), "-.0E0"); + public void test_formatLong_minimumIntegerDigits() { + DecimalFormat df = new DecimalFormat("###0.##", new DecimalFormatSymbols(Locale.US)); + df.setMinimumIntegerDigits(3); + assertEquals(3, df.getMinimumIntegerDigits()); + assertEquals("012", df.format(12)); + df.setMaximumIntegerDigits(2); + assertEquals(2, df.getMinimumIntegerDigits()); + assertEquals("00.7", df.format(0.7)); } - public void test_formatJLjava_lang_StringBufferLjava_text_FieldPosition() { + // See also the _formatDouble tests. This tests a subset of patterns / values. + public void test_formatLong_scientificNotation() { + FormatTester formatTester = new FormatTester(); final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); DecimalFormat df = new DecimalFormat("00.0#E0", dfs); - assertEquals("00.0#E0: 0", "00.0E0", df.format(0)); - assertEquals("00.0#E0: 1", "10.0E-1", df.format(1)); - assertEquals("00.0#E0: 12", "12.0E0", df.format(12)); - assertEquals("00.0#E0: 123", "12.3E1", df.format(123)); - assertEquals("00.0#E0: 1234", "12.34E2", df.format(1234)); - assertEquals("00.0#E0: 12346", "12.35E3", df.format(12346)); - assertEquals("00.0#E0: 99999", "10.0E4", df.format(99999)); - assertEquals("00.0#E0: -1", "-10.0E-1", df.format(-1)); - assertEquals("00.0#E0: -12", "-12.0E0", df.format(-12)); - assertEquals("00.0#E0: -123", "-12.3E1", df.format(-123)); - assertEquals("00.0#E0: -1234", "-12.34E2", df.format(-1234)); - assertEquals("00.0#E0: -12346", "-12.35E3", df.format(-12346)); - assertEquals("00.0#E0: -99999", "-10.0E4", df.format(-99999)); + // ["00.0#E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, + // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=2, + // maxFractionDigits=2,minIntegerDigits=2,minFractionDigits=1,grouping=false] + // Because maximum integer digit was not explicitly set: The exponent can be any integer. + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (2) + "max fractional digits (2) == 4 + formatTester.format(df, "00.0E0", 0); + formatTester.format(df, "10.0E-1", 1); + formatTester.format(df, "12.0E0", 12); + formatTester.format(df, "12.3E1", 123); + formatTester.format(df, "12.34E2", 1234); + formatTester.format(df, "12.35E3", 12346); + formatTester.format(df, "10.0E4", 99999); + formatTester.format(df, "-10.0E-1", -1); + formatTester.format(df, "-12.0E0", -12); + formatTester.format(df, "-12.3E1", -123); + formatTester.format(df, "-12.34E2", -1234); + formatTester.format(df, "-12.35E3", -12346); + formatTester.format(df, "-10.0E4", -99999); df = new DecimalFormat("##0.0E0", dfs); - assertEquals("##0.0E0: 0", "0.0E0", df.format(0)); - assertEquals("##0.0E0: 1", "1.0E0", df.format(1)); - assertEquals("##0.0E0: 12", "12E0", df.format(12)); - assertEquals("##0.0E0: 123", "123E0", df.format(123)); - assertEquals("##0.0E0: 1234", "1.234E3", df.format(1234)); - assertEquals("##0.0E0: 12346", "12.35E3", df.format(12346)); - assertEquals("##0.0E0: 99999", "100E3", df.format(99999)); - assertEquals("##0.0E0: 999999", "1.0E6", df.format(999999)); + // ["##0.0E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, + // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=3, + // maxFractionDigits=1,minIntegerDigits=1,minFractionDigits=1,grouping=false] + // Because maximum integer digit count is set: The exponent must be a multiple of it (3). + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (1) + "max fractional digits (1) == 2 + formatTester.format(df, "0.0E0", 0); + formatTester.format(df, "1.0E0", 1); + formatTester.format(df, "12E0", 12); + formatTester.format(df, "120E0", 123); + formatTester.format(df, "1.2E3", 1234); + formatTester.format(df, "12E3", 12346); + formatTester.format(df, "100E3", 99999); + formatTester.format(df, "1.0E6", 999999); df = new DecimalFormat("#00.0##E0", dfs); - assertEquals("#00.0##E0: 0", "0.00E0", df.format(0)); - assertEquals("#00.0##E0: 1", "1.00E0", df.format(1)); - assertEquals("#00.0##E0: 12", "12.0E0", df.format(12)); - assertEquals("#00.0##E0: 123", "123E0", df.format(123)); - assertEquals("#00.0##E0: 1234", "1.234E3", df.format(1234)); - assertEquals("#00.0##E0: 12345", "12.345E3", df.format(12345)); - assertEquals("#00.0##E0: 123456", "123.456E3", df.format(123456)); - assertEquals("#00.0##E0: 1234567", "1.23457E6", df.format(1234567)); - assertEquals("#00.0##E0: 12345678", "12.3457E6", df.format(12345678)); - assertEquals("#00.0##E0: 99999999", "100E6", df.format(99999999)); + // ["##0.0E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, + // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=3, + // maxFractionDigits=1,minIntegerDigits=1,minFractionDigits=1,grouping=false] + // Because maximum integer digit count is set: The exponent must be a multiple of it (3). + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (2) + "max fractional digits (3) == 5 + formatTester.format(df, "0.0E0", 0); + formatTester.format(df, "1.0E0", 1); + formatTester.format(df, "12E0", 12); + formatTester.format(df, "123E0", 123); + formatTester.format(df, "1.234E3", 1234); + formatTester.format(df, "12.345E3", 12345); + formatTester.format(df, "123.46E3", 123456); + formatTester.format(df, "1.2346E6", 1234567); + formatTester.format(df, "12.346E6", 12345678); + formatTester.format(df, "100E6", 99999999); df = new DecimalFormat("#.0E0", dfs); - assertEquals("#.0E0: 0", ".0E0", df.format(0)); - assertEquals("#.0E0: 1", ".1E1", df.format(1)); - assertEquals("#.0E0: 12", ".12E2", df.format(12)); - assertEquals("#.0E0: 123", ".12E3", df.format(123)); - assertEquals("#.0E0: 1234", ".12E4", df.format(1234)); - assertEquals("#.0E0: 9999", ".1E5", df.format(9999)); + // ["#.0E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, + // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=1, + // maxFractionDigits=1,minIntegerDigits=0,minFractionDigits=1,grouping=false] + // Because maximum integer digit count is set: The exponent must be a multiple of it (1). + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (0) + "max fractional digits (1) == 1 + formatTester.format(df, "0.0E0", 0); + formatTester.format(df, "1.0E0", 1); + formatTester.format(df, "1.0E1", 12); + formatTester.format(df, "1.0E2", 123); + formatTester.format(df, "1.0E3", 1234); + formatTester.format(df, "1.0E4", 9999); df = new DecimalFormat("0.#E0", dfs); - assertEquals("0.#E0: 0", "0E0", df.format(0)); - assertEquals("0.#E0: 1", "1E0", df.format(1)); - assertEquals("0.#E0: 12", "1.2E1", df.format(12)); - assertEquals("0.#E0: 123", "1.2E2", df.format(123)); - assertEquals("0.#E0: 1234", "1.2E3", df.format(1234)); - assertEquals("0.#E0: 9999", "1E4", df.format(9999)); - } - - public void test_formatToCharacterIteratorLjava_lang_Object() { - - try { - // Regression for HARMONY-466 - new DecimalFormat().formatToCharacterIterator(null); - fail("NullPointerException expected"); - } catch (NullPointerException e) { - // expected - } - - new Support_DecimalFormat( - "test_formatToCharacterIteratorLjava_lang_Object") - .t_formatToCharacterIterator(); - } + // ["0.#E0",isDecimalSeparatorAlwaysShown=false,groupingSize=0,multiplier=1, + // negativePrefix=-,negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=1, + // maxFractionDigits=1,minIntegerDigits=1,minFractionDigits=0,grouping=false] + // Because maximum integer digit was not explicitly set: The exponent can be any integer. + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (1) + "max fractional digits (1) == 2 + formatTester.format(df, "0E0", 0); + formatTester.format(df, "1E0", 1); + formatTester.format(df, "1.2E1", 12); + formatTester.format(df, "1.2E2", 123); + formatTester.format(df, "1.2E3", 1234); + formatTester.format(df, "1E4", 9999); - public void test_formatD() { - DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.ENGLISH); - format.setGroupingUsed(false); + df = new DecimalFormat(".0E0", dfs); + // [".0E0",isDecimalSeparatorAlwaysShown=true,groupingSize=0,multiplier=1,negativePrefix=-, + // negativeSuffix=,positivePrefix=,positiveSuffix=,maxIntegerDigits=0,maxFractionDigits=1, + // minIntegerDigits=0,minFractionDigits=1,grouping=false] + // Because maximum integer digit was not explicitly set: The exponent can be any integer. + // Scientific notation => use significant digit logic + // '@' not present: Significant digits: Min: 1, + // Max: "min integer digits" (0) + "max fractional digits (1) == 1 + formatTester.format(df, ".0E0", 0); + formatTester.format(df, ".1E1", 1); + formatTester.format(df, ".1E2", 12); + formatTester.format(df, ".1E3", 123); + formatTester.format(df, ".1E4", 1234); + formatTester.format(df, ".1E5", 9999); + + formatTester.throwFailures(); + } + + public void test_formatDouble_wideRange() { + final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); + DecimalFormat format = new DecimalFormat("#0.#", dfs); + format.setMaximumIntegerDigits(400); format.setMaximumFractionDigits(400); + for (int i = 0; i < 309; i++) { String tval = "1"; - for (int j = 0; j < i; j++) + for (int j = 0; j < i; j++) { tval += "0"; + } double d = Double.parseDouble(tval); String result = format.format(d); assertEquals(i + ") e:" + tval + " r:" + result, tval, result); } for (int i = 0; i < 322; i++) { String tval = "0."; - for (int j = 0; j < i; j++) + for (int j = 0; j < i; j++) { tval += "0"; + } tval += "1"; double d = Double.parseDouble(tval); String result = format.format(d); @@ -1383,87 +1492,79 @@ public class DecimalFormatTest extends TestCase { } } - public void test_formatD_2() { - DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.ENGLISH); - format.setGroupingUsed(false); - format.setMaximumFractionDigits(400); - - assertEquals("999999999999999", format.format(999999999999999.)); - assertEquals("999999999999999.9", format.format(999999999999999.9)); - assertEquals("99999999999999.98", format.format(99999999999999.99)); - assertEquals("9999999999999.998", format.format(9999999999999.999)); - assertEquals("999999999999.9999", format.format(999999999999.9999)); - assertEquals("99999999999.99998", format.format(99999999999.99999)); - assertEquals("9999999999.999998", format.format(9999999999.999999)); - assertEquals("999999999.9999999", format.format(999999999.9999999)); - assertEquals("99999999.99999999", format.format(99999999.99999999)); - assertEquals("9999999.999999998", format.format(9999999.999999999)); - assertEquals("99999.99999999999", format.format(99999.99999999999)); - assertEquals("9999.999999999998", format.format(9999.999999999999)); - assertEquals("999.9999999999999", format.format(999.9999999999999)); - assertEquals("99.99999999999999", format.format(99.99999999999999)); - assertEquals("9.999999999999998", format.format(9.999999999999999)); - assertEquals("0.9999999999999999", format.format(.9999999999999999)); - } - - public void test_formatD_3() { - DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(Locale.ENGLISH); - format.setGroupingUsed(false); - format.setMaximumFractionDigits(400); - - assertEquals("123456789012345", format.format(123456789012345.)); - assertEquals("1", "12345678901234.5", format.format(12345678901234.5)); - assertEquals("2", "1234567890123.25", format.format(1234567890123.25)); - assertEquals("3", "999999999999.375", format.format(999999999999.375)); - assertEquals("4", "99999999999.0625", format.format(99999999999.0625)); - assertEquals("5", "9999999999.03125", format.format(9999999999.03125)); - assertEquals("6", "999999999.015625", format.format(999999999.015625)); - assertEquals("7", "99999999.0078125", format.format(99999999.0078125)); - assertEquals("8", "9999999.00390625", format.format(9999999.00390625)); - assertEquals("9", "999999.001953125", format.format(999999.001953125)); - assertEquals("10", "9999.00048828125", format.format(9999.00048828125)); - assertEquals("11", "999.000244140625", format.format(999.000244140625)); - assertEquals("12", "99.0001220703125", format.format(99.0001220703125)); - assertEquals("13", "9.00006103515625", format.format(9.00006103515625)); - assertEquals("14", "0.000030517578125", format.format(0.000030517578125)); + public void test_formatDouble_varyingScaleProblemCases() throws Exception { + final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); + DecimalFormat format = new DecimalFormat("#0.#", dfs); + format.setMaximumIntegerDigits(400); + format.setMaximumFractionDigits(400); + + assertEquals("999999999999999", format.format(999999999999999.)); + assertEquals("999999999999999.9", format.format(999999999999999.9)); + assertEquals("99999999999999.98", format.format(99999999999999.99)); + assertEquals("9999999999999.998", format.format(9999999999999.999)); + assertEquals("999999999999.9999", format.format(999999999999.9999)); + assertEquals("99999999999.99998", format.format(99999999999.99999)); + assertEquals("9999999999.999998", format.format(9999999999.999999)); + assertEquals("999999999.9999999", format.format(999999999.9999999)); + assertEquals("99999999.99999999", format.format(99999999.99999999)); + assertEquals("9999999.999999998", format.format(9999999.999999999)); + assertEquals("99999.99999999999", format.format(99999.99999999999)); + assertEquals("9999.999999999998", format.format(9999.999999999999)); + assertEquals("999.9999999999999", format.format(999.9999999999999)); + assertEquals("99.99999999999999", format.format(99.99999999999999)); + assertEquals("9.999999999999998", format.format(9.999999999999999)); + assertEquals("0.9999999999999999", format.format(.9999999999999999)); + } + + public void test_formatDouble_varyingScale() throws Exception { + final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); + DecimalFormat format = new DecimalFormat("#0.#", dfs); + format.setMaximumIntegerDigits(400); + format.setMaximumFractionDigits(400); + + assertEquals("123456789012345", format.format(123456789012345.)); + assertEquals("12345678901234.5", format.format(12345678901234.5)); + assertEquals("1234567890123.25", format.format(1234567890123.25)); + assertEquals("999999999999.375", format.format(999999999999.375)); + assertEquals("99999999999.0625", format.format(99999999999.0625)); + assertEquals("9999999999.03125", format.format(9999999999.03125)); + assertEquals("999999999.015625", format.format(999999999.015625)); + assertEquals("99999999.0078125", format.format(99999999.0078125)); + assertEquals("9999999.00390625", format.format(9999999.00390625)); + assertEquals("999999.001953125", format.format(999999.001953125)); + assertEquals("9999.00048828125", format.format(9999.00048828125)); + assertEquals("999.000244140625", format.format(999.000244140625)); + assertEquals("99.0001220703125", format.format(99.0001220703125)); + assertEquals("9.00006103515625", format.format(9.00006103515625)); + assertEquals("0.000030517578125", format.format(0.000030517578125)); } public void test_getDecimalFormatSymbols() { DecimalFormat df = (DecimalFormat) NumberFormat.getInstance(Locale.ENGLISH); DecimalFormatSymbols dfs = df.getDecimalFormatSymbols(); - assertTrue("Identical symbols", dfs != df.getDecimalFormatSymbols()); + assertNotSame(dfs, df.getDecimalFormatSymbols()); } public void test_getCurrency() { Currency currK = Currency.getInstance("KRW"); Currency currX = Currency.getInstance("XXX"); Currency currE = Currency.getInstance("EUR"); - Currency curr01; - DecimalFormat df = (DecimalFormat) NumberFormat - .getCurrencyInstance(new Locale("ko", "KR")); - assertTrue("Test1: Returned incorrect currency", - df.getCurrency() == currK); + DecimalFormat df = (DecimalFormat) NumberFormat.getCurrencyInstance(new Locale("ko", "KR")); + assertTrue("Test1: Returned incorrect currency", df.getCurrency() == currK); - df = (DecimalFormat) NumberFormat.getCurrencyInstance(new Locale("", - "KR")); - assertTrue("Test2: Returned incorrect currency", - df.getCurrency() == currK); + df = (DecimalFormat) NumberFormat.getCurrencyInstance(new Locale("", "KR")); + assertTrue("Test2: Returned incorrect currency", df.getCurrency() == currK); - df = (DecimalFormat) NumberFormat.getCurrencyInstance(new Locale("ko", - "")); - assertTrue("Test3: Returned incorrect currency", - df.getCurrency() == currX); + df = (DecimalFormat) NumberFormat.getCurrencyInstance(new Locale("ko", "")); + assertTrue("Test3: Returned incorrect currency", df.getCurrency() == currX); - df = (DecimalFormat) NumberFormat.getCurrencyInstance(new Locale("fr", - "FR")); - assertTrue("Test4: Returned incorrect currency", - df.getCurrency() == currE); + df = (DecimalFormat) NumberFormat.getCurrencyInstance(new Locale("fr", "FR")); + assertTrue("Test4: Returned incorrect currency", df.getCurrency() == currE); // Regression for HARMONY-1351 df = (DecimalFormat) NumberFormat.getCurrencyInstance(new Locale("QWERTY")); - assertTrue("Test5: Returned incorrect currency", - df.getCurrency() == currX); + assertTrue("Test5: Returned incorrect currency", df.getCurrency() == currX); // JDK fails these tests since it doesn't have the PREEURO variant // df = (DecimalFormat)NumberFormat.getCurrencyInstance(new Locale("fr", @@ -1483,8 +1584,7 @@ public class DecimalFormatTest extends TestCase { public void test_getMultiplier() { final int defaultMultiplier = 1; - NumberFormat nform = DecimalFormat.getInstance(Locale.US); - DecimalFormat form = (DecimalFormat) nform; + DecimalFormat form = (DecimalFormat) NumberFormat.getInstance(Locale.US); assertEquals(defaultMultiplier, form.getMultiplier()); DecimalFormat df = new DecimalFormat("###0.##"); @@ -1504,94 +1604,92 @@ public class DecimalFormatTest extends TestCase { assertTrue("Wrong set value", df.isDecimalSeparatorAlwaysShown()); } - public void test_parseLjava_lang_StringLjava_text_ParsePosition() { - DecimalFormat format = (DecimalFormat) NumberFormat - .getNumberInstance(Locale.ENGLISH); + public void test_parse_withParsePosition() { + DecimalFormat format = (DecimalFormat) NumberFormat.getNumberInstance(Locale.ENGLISH); ParsePosition pos = new ParsePosition(0); Number result = format.parse("9223372036854775807", pos); - assertTrue("Wrong result type for Long.MAX_VALUE", - result.getClass() == Long.class); - assertTrue("Wrong result Long.MAX_VALUE", - result.longValue() == Long.MAX_VALUE); + assertTrue("Wrong result type for Long.MAX_VALUE", result.getClass() == Long.class); + assertTrue("Wrong result Long.MAX_VALUE", result.longValue() == Long.MAX_VALUE); pos = new ParsePosition(0); result = format.parse("-9223372036854775808", pos); - assertTrue("Wrong result type for Long.MIN_VALUE", - result.getClass() == Long.class); - assertTrue("Wrong result Long.MIN_VALUE: " + result.longValue(), result - .longValue() == Long.MIN_VALUE); + assertTrue("Wrong result type for Long.MIN_VALUE", result.getClass() == Long.class); + assertTrue("Wrong result Long.MIN_VALUE: " + result.longValue(), + result.longValue() == Long.MIN_VALUE); pos = new ParsePosition(0); result = format.parse("9223372036854775808", pos); - assertTrue("Wrong result type for Long.MAX_VALUE+1", - result.getClass() == Double.class); + assertTrue("Wrong result type for Long.MAX_VALUE+1", result.getClass() == Double.class); assertTrue("Wrong result Long.MAX_VALUE + 1", result.doubleValue() == (double) Long.MAX_VALUE + 1); pos = new ParsePosition(0); result = format.parse("-9223372036854775809", pos); - assertTrue("Wrong result type for Long.MIN_VALUE+1", - result.getClass() == Double.class); + assertTrue("Wrong result type for Long.MIN_VALUE+1", result.getClass() == Double.class); assertTrue("Wrong result Long.MIN_VALUE - 1", result.doubleValue() == (double) Long.MIN_VALUE - 1); pos = new ParsePosition(0); result = format.parse("18446744073709551629", pos); - assertTrue("Wrong result type for overflow", - result.getClass() == Double.class); - assertTrue("Wrong result for overflow", - result.doubleValue() == 18446744073709551629d); + assertTrue("Wrong result type for overflow", result.getClass() == Double.class); + assertTrue("Wrong result for overflow", result.doubleValue() == 18446744073709551629d); pos = new ParsePosition(0); result = format.parse("42325917317067571199", pos); - assertTrue("Wrong result type for overflow a: " + result, result - .getClass() == Double.class); - assertTrue("Wrong result for overflow a: " + result, result - .doubleValue() == 42325917317067571199d); + assertTrue("Wrong result type for overflow a: " + result, + result.getClass() == Double.class); + assertTrue("Wrong result for overflow a: " + result, + result.doubleValue() == 42325917317067571199d); pos = new ParsePosition(0); result = format.parse("4232591731706757119E1", pos); - assertTrue("Wrong result type for overflow b: " + result, result - .getClass() == Double.class); - assertTrue("Wrong result for overflow b: " + result, result - .doubleValue() == 42325917317067571190d); + assertTrue("Wrong result type for overflow b: " + result, + result.getClass() == Double.class); + assertTrue("Wrong result for overflow b: " + result, + result.doubleValue() == 42325917317067571190d); pos = new ParsePosition(0); result = format.parse(".42325917317067571199E20", pos); - assertTrue("Wrong result type for overflow c: " + result, result - .getClass() == Double.class); - assertTrue("Wrong result for overflow c: " + result, result - .doubleValue() == 42325917317067571199d); + assertTrue("Wrong result type for overflow c: " + result, + result.getClass() == Double.class); + assertTrue("Wrong result for overflow c: " + result, + result.doubleValue() == 42325917317067571199d); pos = new ParsePosition(0); result = format.parse("922337203685477580.9E1", pos); - assertTrue("Wrong result type for overflow d: " + result, result - .getClass() == Double.class); - assertTrue("Wrong result for overflow d: " + result, result - .doubleValue() == 9223372036854775809d); + assertTrue("Wrong result type for overflow d: " + result, + result.getClass() == Double.class); + assertTrue("Wrong result for overflow d: " + result, + result.doubleValue() == 9223372036854775809d); pos = new ParsePosition(0); result = format.parse("9.223372036854775809E18", pos); - assertTrue("Wrong result type for overflow e: " + result, result - .getClass() == Double.class); - assertTrue("Wrong result for overflow e: " + result, result - .doubleValue() == 9223372036854775809d); + assertTrue("Wrong result type for overflow e: " + result, + result.getClass() == Double.class); + assertTrue("Wrong result for overflow e: " + result, + result.doubleValue() == 9223372036854775809d); } - public void test_parse_with_multiplier() { - DecimalFormat format = (DecimalFormat) NumberFormat.getNumberInstance(Locale.ENGLISH); - Number result; + public void test_parse_withMultiplier() { + DecimalFormat format = (DecimalFormat) NumberFormat.getNumberInstance(Locale.ENGLISH); + Number result; - format.setMultiplier(100); - result = format.parse("9223372036854775807", new ParsePosition(0)); - assertEquals("Wrong result type multiplier 100: " + result, Double.class, result.getClass()); - assertEquals("Wrong result for multiplier 100: " + result, 92233720368547758.07d, result.doubleValue()); + format.setMultiplier(100); + result = format.parse("9223372036854775807", new ParsePosition(0)); + assertEquals("Wrong result type multiplier 100: " + result, Double.class, + result.getClass()); + assertEquals("Wrong result for multiplier 100: " + result, + 92233720368547758.07d, result.doubleValue()); - format.setMultiplier(1000); - result = format.parse("9223372036854775807", new ParsePosition(0)); - assertEquals("Wrong result type multiplier 1000: " + result, Double.class, result.getClass()); - assertEquals("Wrong result for multiplier 1000: " + result, 9223372036854775.807d, result.doubleValue()); + format.setMultiplier(1000); + result = format.parse("9223372036854775807", new ParsePosition(0)); + assertEquals("Wrong result type multiplier 1000: " + result, Double.class, + result.getClass()); + assertEquals("Wrong result for multiplier 1000: " + result, + 9223372036854775.807d, result.doubleValue()); - format.setMultiplier(10000); - result = format.parse("9223372036854775807", new ParsePosition(0)); - assertEquals("Wrong result type multiplier 10000: " + result, Double.class, result.getClass()); - assertEquals("Wrong result for multiplier 10000: " + result, 922337203685477.5807d, result.doubleValue()); + format.setMultiplier(10000); + result = format.parse("9223372036854775807", new ParsePosition(0)); + assertEquals("Wrong result type multiplier 10000: " + result, + Double.class, result.getClass()); + assertEquals("Wrong result for multiplier 10000: " + result, + 922337203685477.5807d, result.doubleValue()); } - public void test_setDecimalFormatSymbolsLjava_text_DecimalFormatSymbols() { + public void test_setDecimalFormatSymbols() { DecimalFormat df = new DecimalFormat("###0.##"); DecimalFormatSymbols dfs = new DecimalFormatSymbols(); dfs.setDecimalSeparator('@'); @@ -1609,19 +1707,17 @@ public class DecimalFormatTest extends TestCase { assertNotSame(symbols, symbolsOut); } - public void test_setDecimalSeparatorAlwaysShownZ() { - DecimalFormat df = new DecimalFormat("###0.##", - new DecimalFormatSymbols(Locale.US)); + public void test_setDecimalSeparatorAlwaysShown() { + DecimalFormat df = new DecimalFormat("###0.##", new DecimalFormatSymbols(Locale.US)); assertEquals("Wrong default result", "5", df.format(5)); df.setDecimalSeparatorAlwaysShown(true); assertTrue("Not set", df.isDecimalSeparatorAlwaysShown()); assertEquals("Wrong set result", "7.", df.format(7)); } - public void test_setCurrencyLjava_util_Currency() { + public void test_setCurrency() { Locale locale = Locale.CANADA; - DecimalFormat df = ((DecimalFormat) NumberFormat - .getCurrencyInstance(locale)); + DecimalFormat df = ((DecimalFormat) NumberFormat.getCurrencyInstance(locale)); try { df.setCurrency(null); @@ -1632,18 +1728,14 @@ public class DecimalFormatTest extends TestCase { Currency currency = Currency.getInstance("AED"); df.setCurrency(currency); assertTrue("Returned incorrect currency", currency == df.getCurrency()); - assertTrue("Returned incorrect currency symbol", currency.getSymbol( - locale) - .equals(df.getDecimalFormatSymbols().getCurrencySymbol())); - assertTrue("Returned incorrect international currency symbol", currency - .getCurrencyCode().equals( - df.getDecimalFormatSymbols() - .getInternationalCurrencySymbol())); - } - - public void test_setGroupingSizeI() { - DecimalFormat df = new DecimalFormat("###0.##", - new DecimalFormatSymbols(Locale.ENGLISH)); + assertEquals("Returned incorrect currency symbol", currency.getSymbol(locale), + df.getDecimalFormatSymbols().getCurrencySymbol()); + assertEquals("Returned incorrect international currency symbol", currency.getCurrencyCode(), + df.getDecimalFormatSymbols().getInternationalCurrencySymbol()); + } + + public void test_setGroupingSize() { + DecimalFormat df = new DecimalFormat("###0.##", new DecimalFormatSymbols(Locale.ENGLISH)); df.setGroupingUsed(true); df.setGroupingSize(2); assertEquals("Value not set", 2, df.getGroupingSize()); @@ -1651,66 +1743,11 @@ public class DecimalFormatTest extends TestCase { assertTrue("Invalid format:" + result, result.equals("1,23")); } - public void test_setMaximumFractionDigitsI() { - DecimalFormat df = new DecimalFormat("###0.##", - new DecimalFormatSymbols(Locale.US)); - df.setMaximumFractionDigits(3); - assertEquals("Not set", 3, df.getMaximumFractionDigits()); - assertEquals("Wrong maximum", "1.235", df.format(1.23456)); - df.setMinimumFractionDigits(4); - assertEquals("Not changed", 4, df.getMaximumFractionDigits()); - assertEquals("Incorrect fraction", "456.0000", df.format(456)); - } - - public void test_setMaximumIntegerDigitsI() { - DecimalFormat df = new DecimalFormat("###0.##"); - df.setMaximumIntegerDigits(2); - assertEquals("Not set", 2, df.getMaximumIntegerDigits()); - assertEquals("Wrong maximum", "34", df.format(1234)); - df.setMinimumIntegerDigits(4); - assertEquals("Not changed", 4, df.getMaximumIntegerDigits()); - assertEquals("Incorrect integer", "0026", df.format(26)); - } - - public void test_setMinimumFractionDigitsI() { - DecimalFormat df = new DecimalFormat("###0.##", - new DecimalFormatSymbols(Locale.US)); - df.setMinimumFractionDigits(4); - assertEquals("Not set", 4, df.getMinimumFractionDigits()); - assertEquals("Wrong minimum", "1.2300", df.format(1.23)); - df.setMaximumFractionDigits(2); - assertEquals("Not changed", 2, df.getMinimumFractionDigits()); - assertEquals("Incorrect fraction", "456.00", df.format(456)); - } - - public void test_setMinimumIntegerDigitsI() { - DecimalFormat df = new DecimalFormat("###0.##", - new DecimalFormatSymbols(Locale.US)); - df.setMinimumIntegerDigits(3); - assertEquals("Not set", 3, df.getMinimumIntegerDigits()); - assertEquals("Wrong minimum", "012", df.format(12)); - df.setMaximumIntegerDigits(2); - assertEquals("Not changed", 2, df.getMinimumIntegerDigits()); - assertEquals("Incorrect integer", "00.7", df.format(0.7)); - } - - public void test_setMultiplierI() { - DecimalFormat df = new DecimalFormat("###0.##"); - df.setMultiplier(10); - assertEquals("Wrong multiplier", 10, df.getMultiplier()); - assertEquals("Wrong format", "50", df.format(5)); - assertEquals("Wrong parse", 5, df.parse("50", new ParsePosition(0)).intValue()); - - // regression test for HARMONY-879 - df.setMultiplier(-1); - assertEquals("Wrong multiplier for negative value", -1, df.getMultiplier()); - } - public void testSerializationSelf() throws Exception { SerializationTest.verifySelf(new DecimalFormat()); } - public void test_serializationHarmonyRICompatible() throws Exception { + public void testSerializationHarmonyRICompatible() throws Exception { NumberFormat nf = NumberFormat.getInstance(Locale.FRANCE); DecimalFormat df = null; @@ -1725,8 +1762,9 @@ public class DecimalFormatTest extends TestCase { DecimalFormat deserializedDF = null; try { - oinput = new ObjectInputStream(this.getClass().getResource( - "/serialization/org/apache/harmony/tests/java/text/DecimalFormat.ser").openStream()); + oinput = new ObjectInputStream(getClass().getResource( + "/serialization/org/apache/harmony/tests/java/text/DecimalFormat.ser") + .openStream()); deserializedDF = (DecimalFormat) oinput.readObject(); } finally { try { @@ -1744,7 +1782,8 @@ public class DecimalFormatTest extends TestCase { assertEquals(df.getPositiveSuffix(), deserializedDF.getPositiveSuffix()); assertEquals(df.getCurrency(), deserializedDF.getCurrency()); - DecimalFormatSymbolsTest.assertDecimalFormatSymbolsRIFrance(deserializedDF.getDecimalFormatSymbols()); + DecimalFormatSymbolsTest.assertDecimalFormatSymbolsRIFrance( + deserializedDF.getDecimalFormatSymbols()); assertEquals(df.getGroupingSize(), df.getGroupingSize()); assertEquals(df.getMaximumFractionDigits(), deserializedDF @@ -1765,9 +1804,9 @@ public class DecimalFormatTest extends TestCase { } - public void testParseInfinityBigDecimalFalse() { + public void test_parse_infinityBigDecimalFalse() { // Regression test for HARMONY-106 - DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(); + DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(); DecimalFormatSymbols symbols = new DecimalFormatSymbols(); Number number = format.parse(symbols.getInfinity(), new ParsePosition(0)); @@ -1775,9 +1814,9 @@ public class DecimalFormatTest extends TestCase { assertTrue(Double.isInfinite(number.doubleValue())); } - public void testParseMinusInfinityBigDecimalFalse() { + public void test_parse_minusInfinityBigDecimalFalse() { // Regression test for HARMONY-106 - DecimalFormat format = (DecimalFormat) DecimalFormat.getInstance(); + DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(); DecimalFormatSymbols symbols = new DecimalFormatSymbols(); Number number = format.parse("-" + symbols.getInfinity(), new ParsePosition(0)); @@ -1785,665 +1824,608 @@ public class DecimalFormatTest extends TestCase { assertTrue(Double.isInfinite(number.doubleValue())); } - public void testSetDecimalFormatSymbolsAsNull(){ + public void test_setDecimalFormatSymbolsAsNull() { // Regression for HARMONY-1070 - DecimalFormat format = (DecimalFormat)DecimalFormat.getInstance(); + DecimalFormat format = (DecimalFormat) NumberFormat.getInstance(); format.setDecimalFormatSymbols(null); } + public void test_formatToCharacterIterator_null() { + try { + // Regression for HARMONY-466 + new DecimalFormat().formatToCharacterIterator(null); + fail("NullPointerException expected"); + } catch (NullPointerException e) { + // expected + } + } + + public void test_formatToCharacterIterator_original() { + new Support_DecimalFormat( + "test_formatToCharacterIteratorLjava_lang_Object") + .t_formatToCharacterIterator(); + } + public void test_formatToCharacterIterator() throws Exception { - AttributedCharacterIterator iterator; - int[] runStarts; - int[] runLimits; - String result; - char current; - - // BigInteger. - iterator = new DecimalFormat().formatToCharacterIterator(new BigInteger("123456789")); - runStarts = new int[] {0, 0, 0, 3, 4, 4, 4, 7, 8, 8, 8}; - runLimits = new int[] {3, 3, 3, 4, 7, 7, 7, 8, 11, 11, 11}; - result = "123,456,789"; - current = iterator.current(); - for (int i = 0; i < runStarts.length; i++) { - assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart()); - assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit()); - assertEquals("wrong char @" + i, result.charAt(i), current); - current = iterator.next(); - } - assertEquals(0, iterator.getBeginIndex()); - assertEquals(11, iterator.getEndIndex()); - - // For BigDecimal with multiplier test. - DecimalFormat df = new DecimalFormat(); - df.setMultiplier(10); - iterator = df.formatToCharacterIterator(new BigDecimal("12345678901234567890")); - result = "123,456,789,012,345,678,900"; - current = iterator.current(); - for (int i = 0; i < result.length(); i++) { - assertEquals("wrong char @" + i, result.charAt(i), current); - current = iterator.next(); - } - - // For BigDecimal with multiplier test. - df = new DecimalFormat(); - df.setMultiplier(-1); - df.setMaximumFractionDigits(20); - iterator = df.formatToCharacterIterator(new BigDecimal("1.23456789012345678901")); - result = "-1.23456789012345678901"; - current = iterator.current(); - for (int i = 0; i < result.length(); i++) { - assertEquals("wrong char @" + i, result.charAt(i), current); - current = iterator.next(); - } - - iterator = new DecimalFormat().formatToCharacterIterator(new BigDecimal("1.23456789E301")); - runStarts = new int[] {0, 0, 2, 3, 3, 3, 6, 7, 7, 7, 10, 11, 11, 11, 14}; - runLimits = new int[] {2, 2, 3, 6, 6, 6, 7, 10, 10, 10, 11, 14, 14, 14, 15}; - result = "12,345,678,900,"; // 000,000,000,000.... - current = iterator.current(); - for (int i = 0; i < runStarts.length; i++) { - assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart()); - assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit()); - assertEquals("wrong char @" + i, result.charAt(i), current); - current = iterator.next(); - } - assertEquals(0, iterator.getBeginIndex()); - assertEquals(402, iterator.getEndIndex()); - - iterator = new DecimalFormat().formatToCharacterIterator(new BigDecimal("1.2345678E4")); - runStarts = new int[] {0, 0, 2, 3, 3, 3, 6, 7, 7, 7}; - runLimits = new int[] {2, 2, 3, 6, 6, 6, 7, 10, 10, 10}; - result = "12,345.678"; - current = iterator.current(); - for (int i = 0; i < runStarts.length; i++) { - assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart()); - assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit()); - assertEquals("wrong char @" + i, result.charAt(i), current); - current = iterator.next(); - } - assertEquals(0, iterator.getBeginIndex()); - assertEquals(10, iterator.getEndIndex()); - } - - public void test_formatToCharacterIterator_very_large() throws Exception { - AttributedCharacterIterator iterator; - int[] runStarts; - int[] runLimits; - String result; - char current; - - Number number = new BigDecimal("1.23456789E1234"); - assertEquals("1.23456789E+1234", number.toString()); - iterator = new DecimalFormat().formatToCharacterIterator(number); - runStarts = new int[] {0, 0, 2, 3, 3, 3, 6, 7, 7, 7, 10, 11, 11, 11, 14}; - runLimits = new int[] {2, 2, 3, 6, 6, 6, 7, 10, 10, 10, 11, 14, 14, 14, 15}; - result = "12,345,678,900,"; // 000,000,000,000.... - current = iterator.current(); - for (int i = 0; i < runStarts.length; i++) { - assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart()); - assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit()); - assertEquals("wrong char @" + i, result.charAt(i), current); - current = iterator.next(); - } - assertEquals(0, iterator.getBeginIndex()); - assertEquals(1646, iterator.getEndIndex()); - } - - public void test_formatToCharacterIteratorLjava_lang_Object__ArithmeticException() { - DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat - .getInstance(Locale.US); - decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); - decimalFormat.setMaximumFractionDigits(0); - try { - // when rounding is needed, but RoundingMode is set to RoundingMode.UNNECESSARY, throw ArithmeticException - decimalFormat.formatToCharacterIterator(new Double(1.5)); - fail("ArithmeticException expected"); - } catch (ArithmeticException e) { - // expected - } - } - - public void test_formatDLjava_lang_StringBufferLjava_text_FieldPosition_ArithmeticException() { - DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat - .getInstance(Locale.US); - decimalFormat.setMaximumFractionDigits(0); - decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); - - try { - // when rounding is needed, but RoundingMode is set to RoundingMode.UNNECESSARY, throw ArithmeticException - decimalFormat - .format(11.5, new StringBuffer(), new FieldPosition(0)); - fail("ArithmeticException expected"); - } catch (ArithmeticException e) { - // expected - } - } - - public void test_formatJLjava_lang_StringBufferLjava_text_FieldPosition_ArithmeticException() { - - final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); - DecimalFormat decimalFormat = new DecimalFormat("00.0#E0", dfs); - - decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); - try { - // when rounding is needed, but RoundingMode is set to RoundingMode.UNNECESSARY, throw ArithmeticException - decimalFormat.format(99999, new StringBuffer(), - new FieldPosition(0)); - fail("ArithmeticException expected"); - } catch (ArithmeticException e) { - // expected - } - } - - public void test_GetRoundingMode() { - - // get the default RoundingMode of this DecimalFormat - DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat - .getInstance(Locale.US); - - // the default RoundingMode is HALF_EVEN - assertEquals("Incorrect default RoundingMode", decimalFormat.getRoundingMode(), RoundingMode.HALF_EVEN); - - // set RoundingMode.HALF_DOWN of this DecimalFormat - decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN); - assertEquals("Returned incorrect RoundingMode", decimalFormat - .getRoundingMode(), RoundingMode.HALF_DOWN); - - } - - public void test_SetRoundingMode_Ljava_math_RoundingMode() { - DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat - .getInstance(Locale.US); - // ignore the fraction part of a given value - decimalFormat.setMaximumFractionDigits(0); - - // set RoundingMode.HALF_DOWN of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN); - String result = decimalFormat.format(11.3); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", - "11", result); - - result = decimalFormat.format(11.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", - "11", result); - - result = decimalFormat.format(11.6); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", - "12", result); - - // set RoundingMode.CEILING of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.CEILING); - result = decimalFormat.format(11.3); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING", - "12", result); - - result = decimalFormat.format(-11.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING", - "-11", result); - - // set RoundingMode.DOWN of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.DOWN); - result = decimalFormat.format(11.3); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", - "11", result); - - result = decimalFormat.format(-11.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", - "-11", result); - - result = decimalFormat.format(0); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", - "0", result); - - // set RoundingMode.FLOOR of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.FLOOR); - result = decimalFormat.format(11.3); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", - "11", result); - - result = decimalFormat.format(-11.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", - "-12", result); - - result = decimalFormat.format(0); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", - "0", result); - - // set RoundingMode.HALF_EVEN of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.HALF_EVEN); - result = decimalFormat.format(5.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", - "6", result); - - result = decimalFormat.format(-5.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", - "-6", result); - - result = decimalFormat.format(0.2); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", - "0", result); - - // set RoundingMode.HALF_UP of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.HALF_UP); - result = decimalFormat.format(5.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", - "6", result); - - result = decimalFormat.format(-5.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", - "-6", result); - - result = decimalFormat.format(0.2); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", - "0", result); - - result = decimalFormat.format(-0.2); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", - "-0", result); - - // set RoundingMode.UP of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.UP); - result = decimalFormat.format(5.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", - "6", result); - - result = decimalFormat.format(-5.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", - "-6", result); - - result = decimalFormat.format(0.2); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", - "1", result); - - result = decimalFormat.format(-0.2); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", - "-1", result); - - // set RoundingMode.UNNECESSARY of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); - - try { - // when rounding is needed but RoundingMode is set to RoundingMode.UNNECESSARY, throw ArithmeticException - result = decimalFormat.format(5.5); - fail("ArithmeticException expected: RoundingMode.UNNECESSARY"); - } catch (ArithmeticException e) { - // expected - } - - result = decimalFormat.format(1.0); - assertEquals( - "Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY", - "1", result); - - result = decimalFormat.format(-1.0); - assertEquals( - "Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY", - "-1", result); - - try { - // when the given RoundingMode is null, throw NullPointerException - decimalFormat.setRoundingMode(null); - fail("NullPointerException expected"); - } catch (NullPointerException e) { - // expected - } - - // set MaxFractionDigits to 3, test different DecimalFormat format - // function with differnt RoundingMode - decimalFormat.setMaximumFractionDigits(3); - - // set RoundingMode.HALF_DOWN of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN); - result = decimalFormat.format(11.5653); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", - "11.565", result); - - result = decimalFormat.format(11.5655); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", - "11.565", result); - - result = decimalFormat.format(11.5656); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", - "11.566", result); - - // set RoundingMode.CEILING of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.CEILING); - result = decimalFormat.format(11.5653); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING", - "11.566", result); - - result = decimalFormat.format(-11.5653); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING", - "-11.565", result); - - // set RoundingMode.DOWN of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.DOWN); - result = decimalFormat.format(11.5653); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", - "11.565", result); - - result = decimalFormat.format(-11.5653); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", - "-11.565", result); - - result = decimalFormat.format(0); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", - "0", result); - - // set RoundingMode.FLOOR of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.FLOOR); - result = decimalFormat.format(11.5653); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", - "11.565", result); - - result = decimalFormat.format(-11.5655); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", - "-11.566", result); - - result = decimalFormat.format(0); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", - "0", result); - - // set RoundingMode.HALF_EVEN of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.HALF_EVEN); - result = decimalFormat.format(11.5653); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", - "11.565", result); - - result = decimalFormat.format(-11.5655); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", - "-11.566", result); - - result = decimalFormat.format(11.5656); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", - "11.566", result); - - // set RoundingMode.HALF_UP of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.HALF_UP); - result = decimalFormat.format(11.5653); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", - "11.565", result); - - result = decimalFormat.format(-11.5655); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", - "-11.566", result); - - result = decimalFormat.format(11.5656); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", - "11.566", result); - - // set RoundingMode.UP of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.UP); - result = decimalFormat.format(11.5653); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", - "11.566", result); - - result = decimalFormat.format(-11.5655); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", - "-11.566", result); - - // set RoundingMode.UNNECESSARY of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); - result = decimalFormat.format(-11.565); - assertEquals( - "Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY", - "-11.565", result); - - result = decimalFormat.format(11.565); - assertEquals( - "Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY", - "11.565", result); - - // when setting MaxFractionDigits to negative value -2, default it as - // zero, test different DecimalFormat format - // function with differnt RoundingMode - decimalFormat.setMaximumFractionDigits(-2); - - // set RoundingMode.HALF_DOWN of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN); - result = decimalFormat.format(11.3); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", - "11", result); - - result = decimalFormat.format(11.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", - "11", result); - - result = decimalFormat.format(11.6); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", - "12", result); - - // set RoundingMode.CEILING of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.CEILING); - result = decimalFormat.format(11.3); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING", - "12", result); - - result = decimalFormat.format(-11.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING", - "-11", result); - - // set RoundingMode.DOWN of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.DOWN); - result = decimalFormat.format(11.3); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", - "11", result); - - result = decimalFormat.format(-11.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", - "-11", result); - - result = decimalFormat.format(0); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", - "0", result); - - // set RoundingMode.FLOOR of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.FLOOR); - result = decimalFormat.format(11.3); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", - "11", result); - - result = decimalFormat.format(-11.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", - "-12", result); - - result = decimalFormat.format(0); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", - "0", result); - - // set RoundingMode.HALF_EVEN of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.HALF_EVEN); - result = decimalFormat.format(5.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", - "6", result); - - result = decimalFormat.format(-5.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", - "-6", result); - - result = decimalFormat.format(0.2); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", - "0", result); - - // set RoundingMode.HALF_UP of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.HALF_UP); - result = decimalFormat.format(5.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", - "6", result); - - result = decimalFormat.format(-5.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", - "-6", result); - - result = decimalFormat.format(0.2); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", - "0", result); - - result = decimalFormat.format(-0.2); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", - "-0", result); - - // set RoundingMode.UP of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.UP); - result = decimalFormat.format(5.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", - "6", result); - - result = decimalFormat.format(-5.5); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", - "-6", result); - - result = decimalFormat.format(0.2); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", - "1", result); - - result = decimalFormat.format(-0.2); - assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", - "-1", result); - - // set RoundingMode.UNNECESSARY of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); - - result = decimalFormat.format(1.0); - assertEquals( - "Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY", - "1", result); - - result = decimalFormat.format(-1.0); - assertEquals( - "Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY", - "-1", result); - - // Regression for HARMONY-6485 - // Test with applyPattern call after setRoundingMode - - // set RoundingMode.HALF_UP of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.HALF_UP); - decimalFormat.applyPattern(".##"); - result = decimalFormat.format(0.125); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".13", result); - result = decimalFormat.format(0.255); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".26", result); - result = decimalFormat.format(0.732); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".73", result); - result = decimalFormat.format(0.467); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".47", result); - - // set RoundingMode.HALF_DOWN of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN); - decimalFormat.applyPattern(".##"); - result = decimalFormat.format(0.125); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".12", result); - result = decimalFormat.format(0.255); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".25", result); - result = decimalFormat.format(0.732); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".73", result); - result = decimalFormat.format(0.467); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".47", result); - - // set RoundingMode.UP of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.UP); - decimalFormat.applyPattern(".##"); - result = decimalFormat.format(0.125); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".13", result); - result = decimalFormat.format(0.255); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".26", result); - result = decimalFormat.format(0.732); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".74", result); - result = decimalFormat.format(0.467); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".47", result); - - // set RoundingMode.DOWN of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.DOWN); - decimalFormat.applyPattern(".##"); - result = decimalFormat.format(0.125); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".12", result); - result = decimalFormat.format(0.255); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".25", result); - result = decimalFormat.format(0.732); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".73", result); - result = decimalFormat.format(0.467); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".46", result); - - // set RoundingMode.HALF_EVEN of this DecimalFormat and test its - // behavior - decimalFormat.setRoundingMode(RoundingMode.HALF_EVEN); - decimalFormat.applyPattern(".##"); - result = decimalFormat.format(0.125); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".12", result); - result = decimalFormat.format(0.255); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".26", result); - result = decimalFormat.format(0.732); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".73", result); - result = decimalFormat.format(0.467); - assertEquals( - "Incorrect RoundingMode behavior after applyPattern", - ".47", result); - } + AttributedCharacterIterator iterator; + int[] runStarts; + int[] runLimits; + String result; + char current; + + // BigInteger. + iterator = new DecimalFormat().formatToCharacterIterator(new BigInteger("123456789")); + runStarts = new int[] { 0, 0, 0, 3, 4, 4, 4, 7, 8, 8, 8 }; + runLimits = new int[] { 3, 3, 3, 4, 7, 7, 7, 8, 11, 11, 11 }; + result = "123,456,789"; + current = iterator.current(); + for (int i = 0; i < runStarts.length; i++) { + assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart()); + assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit()); + assertEquals("wrong char @" + i, result.charAt(i), current); + current = iterator.next(); + } + assertEquals(0, iterator.getBeginIndex()); + assertEquals(11, iterator.getEndIndex()); + + // For BigDecimal with multiplier test. + DecimalFormat df = new DecimalFormat(); + df.setMultiplier(10); + iterator = df.formatToCharacterIterator(new BigDecimal("12345678901234567890")); + result = "123,456,789,012,345,678,900"; + current = iterator.current(); + for (int i = 0; i < result.length(); i++) { + assertEquals("wrong char @" + i, result.charAt(i), current); + current = iterator.next(); + } + + // For BigDecimal with multiplier test. + df = new DecimalFormat(); + df.setMultiplier(-1); + df.setMaximumFractionDigits(20); + iterator = df.formatToCharacterIterator(new BigDecimal("1.23456789012345678901")); + result = "-1.23456789012345678901"; + current = iterator.current(); + for (int i = 0; i < result.length(); i++) { + assertEquals("wrong char @" + i, result.charAt(i), current); + current = iterator.next(); + } + + iterator = new DecimalFormat().formatToCharacterIterator(new BigDecimal("1.23456789E301")); + runStarts = new int[] { 0, 0, 2, 3, 3, 3, 6, 7, 7, 7, 10, 11, 11, 11, 14 }; + runLimits = new int[] { 2, 2, 3, 6, 6, 6, 7, 10, 10, 10, 11, 14, 14, 14, 15 }; + result = "12,345,678,900,"; // 000,000,000,000.... + current = iterator.current(); + for (int i = 0; i < runStarts.length; i++) { + assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart()); + assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit()); + assertEquals("wrong char @" + i, result.charAt(i), current); + current = iterator.next(); + } + assertEquals(0, iterator.getBeginIndex()); + assertEquals(402, iterator.getEndIndex()); + + iterator = new DecimalFormat().formatToCharacterIterator(new BigDecimal("1.2345678E4")); + runStarts = new int[] { 0, 0, 2, 3, 3, 3, 6, 7, 7, 7 }; + runLimits = new int[] { 2, 2, 3, 6, 6, 6, 7, 10, 10, 10 }; + result = "12,345.678"; + current = iterator.current(); + for (int i = 0; i < runStarts.length; i++) { + assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart()); + assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit()); + assertEquals("wrong char @" + i, result.charAt(i), current); + current = iterator.next(); + } + assertEquals(0, iterator.getBeginIndex()); + assertEquals(10, iterator.getEndIndex()); + } + + public void test_formatToCharacterIterator_veryLarge() throws Exception { + AttributedCharacterIterator iterator; + int[] runStarts; + int[] runLimits; + String result; + char current; + + Number number = new BigDecimal("1.23456789E1234"); + assertEquals("1.23456789E+1234", number.toString()); + iterator = new DecimalFormat().formatToCharacterIterator(number); + runStarts = new int[] { 0, 0, 2, 3, 3, 3, 6, 7, 7, 7, 10, 11, 11, 11, 14 }; + runLimits = new int[] { 2, 2, 3, 6, 6, 6, 7, 10, 10, 10, 11, 14, 14, 14, 15 }; + result = "12,345,678,900,"; // 000,000,000,000.... + current = iterator.current(); + for (int i = 0; i < runStarts.length; i++) { + assertEquals("wrong start @" + i, runStarts[i], iterator.getRunStart()); + assertEquals("wrong limit @" + i, runLimits[i], iterator.getRunLimit()); + assertEquals("wrong char @" + i, result.charAt(i), current); + current = iterator.next(); + } + assertEquals(0, iterator.getBeginIndex()); + assertEquals(1646, iterator.getEndIndex()); + } + + public void test_formatToCharacterIterator_roundingUnnecessaryArithmeticException() { + DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); + decimalFormat.setMaximumFractionDigits(0); + try { + // when rounding is needed, but RoundingMode is set to RoundingMode.UNNECESSARY, + // throw ArithmeticException + decimalFormat.formatToCharacterIterator(new Double(1.5)); + fail("ArithmeticException expected"); + } catch (ArithmeticException e) { + // expected + } + } + + public void test_formatDouble_roundingUnnecessaryArithmeticException() { + DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + decimalFormat.setMaximumFractionDigits(0); + decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); + + try { + // when rounding is needed, but RoundingMode is set to RoundingMode.UNNECESSARY, + // throw ArithmeticException + decimalFormat.format(11.5, new StringBuffer(), new FieldPosition(0)); + fail("ArithmeticException expected"); + } catch (ArithmeticException e) { + // expected + } + } + + public void test_format_roundingUnnecessaryArithmeticException() { + final DecimalFormatSymbols dfs = new DecimalFormatSymbols(Locale.US); + DecimalFormat decimalFormat = new DecimalFormat("00.0#E0", dfs); + + decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); + try { + // when rounding is needed, but RoundingMode is set to RoundingMode.UNNECESSARY, + // throw ArithmeticException + decimalFormat.format(99999, new StringBuffer(), new FieldPosition(0)); + fail("ArithmeticException expected"); + } catch (ArithmeticException e) { + // expected + } + } + + public void test_getRoundingMode() { + // get the default RoundingMode of this DecimalFormat + DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + + // the default RoundingMode is HALF_EVEN + assertEquals("Incorrect default RoundingMode", + decimalFormat.getRoundingMode(), RoundingMode.HALF_EVEN); + + // set RoundingMode.HALF_DOWN of this DecimalFormat + decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN); + assertEquals("Returned incorrect RoundingMode", + decimalFormat.getRoundingMode(), RoundingMode.HALF_DOWN); + + } + + public void test_setRoundingMode_null() { + DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + try { + // when the given RoundingMode is null, throw NullPointerException + decimalFormat.setRoundingMode(null); + fail("NullPointerException expected"); + } catch (NullPointerException e) { + // expected + } + } + + public void test_format_withRoundingMode() { + DecimalFormat decimalFormat = (DecimalFormat) DecimalFormat.getInstance(Locale.US); + // ignore the fraction part of a given value + decimalFormat.setMaximumFractionDigits(0); + + // set RoundingMode.HALF_DOWN of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN); + String result = decimalFormat.format(11.3); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", "11", result); + + result = decimalFormat.format(11.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", "11", result); + + result = decimalFormat.format(11.6); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", "12", result); + + // set RoundingMode.CEILING of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.CEILING); + result = decimalFormat.format(11.3); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING", "12", result); + + result = decimalFormat.format(-11.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING", "-11", result); + + // set RoundingMode.DOWN of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.DOWN); + result = decimalFormat.format(11.3); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", "11", result); + + result = decimalFormat.format(-11.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", "-11", result); + + result = decimalFormat.format(0); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", "0", result); + + // set RoundingMode.FLOOR of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.FLOOR); + result = decimalFormat.format(11.3); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", "11", result); + + result = decimalFormat.format(-11.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", "-12", result); + + result = decimalFormat.format(0); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", "0", result); + + // set RoundingMode.HALF_EVEN of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.HALF_EVEN); + result = decimalFormat.format(5.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", "6", result); + + result = decimalFormat.format(-5.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", "-6", result); + + result = decimalFormat.format(0.2); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", "0", result); + + // set RoundingMode.HALF_UP of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.HALF_UP); + result = decimalFormat.format(5.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "6", result); + + result = decimalFormat.format(-5.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "-6", result); + + result = decimalFormat.format(0.2); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "0", result); + + result = decimalFormat.format(-0.2); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "-0", result); + + // set RoundingMode.UP of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.UP); + result = decimalFormat.format(5.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "6", result); + + result = decimalFormat.format(-5.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "-6", result); + + result = decimalFormat.format(0.2); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "1", result); + + result = decimalFormat.format(-0.2); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "-1", result); + + // set RoundingMode.UNNECESSARY of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); + + try { + // when rounding is needed but RoundingMode is set to RoundingMode.UNNECESSARY, + // throw ArithmeticException + result = decimalFormat.format(5.5); + fail("ArithmeticException expected: RoundingMode.UNNECESSARY"); + } catch (ArithmeticException e) { + // expected + } + + result = decimalFormat.format(1.0); + assertEquals( + "Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY", "1", result); + + result = decimalFormat.format(-1.0); + assertEquals( + "Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY", "-1", result); + + // set MaxFractionDigits to 3, test different DecimalFormat format + // function with differnt RoundingMode + decimalFormat.setMaximumFractionDigits(3); + + // set RoundingMode.HALF_DOWN of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN); + result = decimalFormat.format(11.5653); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", "11.565", result); + + result = decimalFormat.format(11.5655); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", "11.565", result); + + result = decimalFormat.format(11.5656); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", "11.566", result); + + // set RoundingMode.CEILING of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.CEILING); + result = decimalFormat.format(11.5653); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING", "11.566", result); + + result = decimalFormat.format(-11.5653); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING", "-11.565", result); + + // set RoundingMode.DOWN of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.DOWN); + result = decimalFormat.format(11.5653); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", "11.565", result); + + result = decimalFormat.format(-11.5653); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", "-11.565", result); + + result = decimalFormat.format(0); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", "0", result); + + // set RoundingMode.FLOOR of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.FLOOR); + result = decimalFormat.format(11.5653); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", "11.565", result); + + result = decimalFormat.format(-11.5655); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", "-11.566", result); + + result = decimalFormat.format(0); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", "0", result); + + // set RoundingMode.HALF_EVEN of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.HALF_EVEN); + result = decimalFormat.format(11.5653); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", "11.565", result); + + result = decimalFormat.format(-11.5655); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", "-11.566", result); + + result = decimalFormat.format(11.5656); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", "11.566", result); + + // set RoundingMode.HALF_UP of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.HALF_UP); + result = decimalFormat.format(11.5653); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "11.565", result); + + result = decimalFormat.format(-11.5655); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "-11.566", result); + + result = decimalFormat.format(11.5656); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "11.566", result); + + // set RoundingMode.UP of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.UP); + result = decimalFormat.format(11.5653); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "11.566", result); + + result = decimalFormat.format(-11.5655); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "-11.566", result); + + // set RoundingMode.UNNECESSARY of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); + result = decimalFormat.format(-11.565); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY", + "-11.565", result); + + result = decimalFormat.format(11.565); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY", + "11.565", result); + + // when setting MaxFractionDigits to negative value -2, default it as + // zero, test different DecimalFormat format + // function with differnt RoundingMode + decimalFormat.setMaximumFractionDigits(-2); + + // set RoundingMode.HALF_DOWN of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN); + result = decimalFormat.format(11.3); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", "11", result); + + result = decimalFormat.format(11.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", "11", result); + + result = decimalFormat.format(11.6); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_DOWN", "12", result); + + // set RoundingMode.CEILING of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.CEILING); + result = decimalFormat.format(11.3); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING", "12", result); + + result = decimalFormat.format(-11.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.CEILING", "-11", result); + + // set RoundingMode.DOWN of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.DOWN); + result = decimalFormat.format(11.3); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", "11", result); + + result = decimalFormat.format(-11.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", "-11", result); + + result = decimalFormat.format(0); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.DOWN", "0", result); + + // set RoundingMode.FLOOR of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.FLOOR); + result = decimalFormat.format(11.3); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", "11", result); + + result = decimalFormat.format(-11.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", "-12", result); + + result = decimalFormat.format(0); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.FLOOR", "0", result); + + // set RoundingMode.HALF_EVEN of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.HALF_EVEN); + result = decimalFormat.format(5.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", "6", result); + + result = decimalFormat.format(-5.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", "-6", result); + + result = decimalFormat.format(0.2); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_EVEN", "0", result); + + // set RoundingMode.HALF_UP of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.HALF_UP); + result = decimalFormat.format(5.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "6", result); + + result = decimalFormat.format(-5.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "-6", result); + + result = decimalFormat.format(0.2); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "0", result); + + result = decimalFormat.format(-0.2); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.HALF_UP", "-0", result); + + // set RoundingMode.UP of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.UP); + result = decimalFormat.format(5.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "6", result); + + result = decimalFormat.format(-5.5); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "-6", result); + + result = decimalFormat.format(0.2); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "1", result); + + result = decimalFormat.format(-0.2); + assertEquals("Incorrect RoundingMode behavior: RoundingMode.UP", "-1", result); + + // set RoundingMode.UNNECESSARY of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.UNNECESSARY); + + result = decimalFormat.format(1.0); + assertEquals( + "Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY", "1", result); + + result = decimalFormat.format(-1.0); + assertEquals( + "Incorrect RoundingMode behavior: RoundingMode.UNNECESSARY", "-1", result); + + // Regression for HARMONY-6485 + // Test with applyPattern call after setRoundingMode + + // set RoundingMode.HALF_UP of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.HALF_UP); + decimalFormat.applyPattern(".##"); + result = decimalFormat.format(0.125); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".13", result); + result = decimalFormat.format(0.255); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".26", result); + result = decimalFormat.format(0.732); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".73", result); + result = decimalFormat.format(0.467); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".47", result); + + // set RoundingMode.HALF_DOWN of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.HALF_DOWN); + decimalFormat.applyPattern(".##"); + result = decimalFormat.format(0.125); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".12", result); + result = decimalFormat.format(0.255); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".25", result); + result = decimalFormat.format(0.732); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".73", result); + result = decimalFormat.format(0.467); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".47", result); + + // set RoundingMode.UP of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.UP); + decimalFormat.applyPattern(".##"); + result = decimalFormat.format(0.125); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".13", result); + result = decimalFormat.format(0.255); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".26", result); + result = decimalFormat.format(0.732); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".74", result); + result = decimalFormat.format(0.467); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".47", result); + + // set RoundingMode.DOWN of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.DOWN); + decimalFormat.applyPattern(".##"); + result = decimalFormat.format(0.125); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".12", result); + result = decimalFormat.format(0.255); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".25", result); + result = decimalFormat.format(0.732); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".73", result); + result = decimalFormat.format(0.467); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".46", result); + + // set RoundingMode.HALF_EVEN of this DecimalFormat and test its + // behavior + decimalFormat.setRoundingMode(RoundingMode.HALF_EVEN); + decimalFormat.applyPattern(".##"); + result = decimalFormat.format(0.125); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".12", result); + result = decimalFormat.format(0.255); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".26", result); + result = decimalFormat.format(0.732); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".73", result); + result = decimalFormat.format(0.467); + assertEquals("Incorrect RoundingMode behavior after applyPattern", ".47", result); + } + + private static class FormatTester { + private List<AssertionFailedError> failures = new ArrayList<AssertionFailedError>(); + + public void format(DecimalFormat format, String expected, double value) { + try { + Assert.assertEquals(format.toPattern() + ": " + value, expected, + format.format(value)); + } catch (AssertionFailedError e) { + failures.add(e); + } + } + + public void format(DecimalFormat format, String expected, int value) { + try { + Assert.assertEquals(format.toPattern() + ": " + value, expected, + format.format(value)); + } catch (AssertionFailedError e) { + failures.add(e); + } + } + + public void throwFailures() throws AssertionFailedError { + if (failures.isEmpty()) { + return; + } + if (failures.size() == 1) { + throw failures.get(0); + } + AssertionFailedError combined = new AssertionFailedError("Multiple format failures"); + for (AssertionFailedError failure : failures) { + combined.addSuppressed(failure); + } + throw combined; + } + } } -- cgit v1.1 From 8afb381342e073b3bfcc8700b370e381ac23b2e0 Mon Sep 17 00:00:00 2001 From: Elliott Hughes <enh@google.com> Date: Sat, 25 Oct 2014 23:32:07 -0700 Subject: Allow 1 ulp difference in test_cbrt_D and test_sinh_D. Bug: 18016320 (cherry picked from commit cf5f86467f5be5f1c3ae2a5518c65f496ac93e33) Change-Id: I05c3810320d035323e5fec07712b47586eb100bb --- .../apache/harmony/tests/java/lang/MathTest.java | 84 ++++++++-------------- 1 file changed, 30 insertions(+), 54 deletions(-) (limited to 'harmony-tests') diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/MathTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/MathTest.java index cd38545..ed8e2b5 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/MathTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/MathTest.java @@ -120,34 +120,22 @@ public class MathTest extends junit.framework.TestCase { */ public void test_cbrt_D() { //Test for special situations - assertTrue("Should return Double.NaN", Double.isNaN(Math - .cbrt(Double.NaN))); - assertEquals("Should return Double.POSITIVE_INFINITY", - Double.POSITIVE_INFINITY, Math - .cbrt(Double.POSITIVE_INFINITY), 0D); - assertEquals("Should return Double.NEGATIVE_INFINITY", - Double.NEGATIVE_INFINITY, Math - .cbrt(Double.NEGATIVE_INFINITY), 0D); - assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math - .cbrt(0.0))); - assertEquals(Double.doubleToLongBits(+0.0), Double.doubleToLongBits(Math - .cbrt(+0.0))); - assertEquals(Double.doubleToLongBits(-0.0), Double.doubleToLongBits(Math - .cbrt(-0.0))); - - assertEquals("Should return 3.0", 3.0, Math.cbrt(27.0), 0D); - assertEquals("Should return 23.111993172558684", 23.111993172558684, - Math.cbrt(12345.6), 0D); - assertEquals("Should return 5.643803094122362E102", - 5.643803094122362E102, Math.cbrt(Double.MAX_VALUE), 0D); - assertEquals("Should return 0.01", 0.01, Math.cbrt(0.000001), 0D); - - assertEquals("Should return -3.0", -3.0, Math.cbrt(-27.0), 0D); - assertEquals("Should return -23.111993172558684", -23.111993172558684, - Math.cbrt(-12345.6), 0D); - assertEquals("Should return 1.7031839360032603E-108", - 1.7031839360032603E-108, Math.cbrt(Double.MIN_VALUE), 0D); - assertEquals("Should return -0.01", -0.01, Math.cbrt(-0.000001), 0D); + assertTrue(Double.isNaN(Math.cbrt(Double.NaN))); + assertEquals(Double.POSITIVE_INFINITY, Math.cbrt(Double.POSITIVE_INFINITY), 0D); + assertEquals(Double.NEGATIVE_INFINITY, Math.cbrt(Double.NEGATIVE_INFINITY), 0D); + assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math.cbrt(0.0))); + assertEquals(Double.doubleToLongBits(+0.0), Double.doubleToLongBits(Math.cbrt(+0.0))); + assertEquals(Double.doubleToLongBits(-0.0), Double.doubleToLongBits(Math.cbrt(-0.0))); + + assertEquals(3.0, Math.cbrt(27.0), 0D); + assertEquals(23.111993172558684, Math.cbrt(12345.6), Math.ulp(23.111993172558684)); + assertEquals(5.643803094122362E102, Math.cbrt(Double.MAX_VALUE), 0D); + assertEquals(0.01, Math.cbrt(0.000001), 0D); + + assertEquals(-3.0, Math.cbrt(-27.0), 0D); + assertEquals(-23.111993172558684, Math.cbrt(-12345.6), Math.ulp(-23.111993172558684)); + assertEquals(1.7031839360032603E-108, Math.cbrt(Double.MIN_VALUE), 0D); + assertEquals(-0.01, Math.cbrt(-0.000001), 0D); } /** @@ -1703,32 +1691,20 @@ public class MathTest extends junit.framework.TestCase { */ public void test_sinh_D() { // Test for special situations - assertTrue("Should return NaN", Double.isNaN(Math.sinh(Double.NaN))); - assertEquals("Should return POSITIVE_INFINITY", - Double.POSITIVE_INFINITY, Math.sinh(Double.POSITIVE_INFINITY), 0D); - assertEquals("Should return NEGATIVE_INFINITY", - Double.NEGATIVE_INFINITY, Math.sinh(Double.NEGATIVE_INFINITY), 0D); - assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math - .sinh(0.0))); - assertEquals(Double.doubleToLongBits(+0.0), Double - .doubleToLongBits(Math.sinh(+0.0))); - assertEquals(Double.doubleToLongBits(-0.0), Double - .doubleToLongBits(Math.sinh(-0.0))); - - assertEquals("Should return POSITIVE_INFINITY", - Double.POSITIVE_INFINITY, Math.sinh(1234.56), 0D); - assertEquals("Should return NEGATIVE_INFINITY", - Double.NEGATIVE_INFINITY, Math.sinh(-1234.56), 0D); - assertEquals("Should return 1.0000000000001666E-6", - 1.0000000000001666E-6, Math.sinh(0.000001), 0D); - assertEquals("Should return -1.0000000000001666E-6", - -1.0000000000001666E-6, Math.sinh(-0.000001), 0D); - assertEquals("Should return 5.115386441963859", 5.115386441963859, Math - .sinh(2.33482), 0D); - assertEquals("Should return POSITIVE_INFINITY", - Double.POSITIVE_INFINITY, Math.sinh(Double.MAX_VALUE), 0D); - assertEquals("Should return 4.9E-324", 4.9E-324, Math - .sinh(Double.MIN_VALUE), 0D); + assertTrue(Double.isNaN(Math.sinh(Double.NaN))); + assertEquals(Double.POSITIVE_INFINITY, Math.sinh(Double.POSITIVE_INFINITY), 0D); + assertEquals(Double.NEGATIVE_INFINITY, Math.sinh(Double.NEGATIVE_INFINITY), 0D); + assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math.sinh(0.0))); + assertEquals(Double.doubleToLongBits(+0.0), Double.doubleToLongBits(Math.sinh(+0.0))); + assertEquals(Double.doubleToLongBits(-0.0), Double.doubleToLongBits(Math.sinh(-0.0))); + + assertEquals(Double.POSITIVE_INFINITY, Math.sinh(1234.56), 0D); + assertEquals(Double.NEGATIVE_INFINITY, Math.sinh(-1234.56), 0D); + assertEquals(1.0000000000001666E-6, Math.sinh(0.000001), 0D); + assertEquals(-1.0000000000001666E-6, Math.sinh(-0.000001), 0D); + assertEquals(5.115386441963859, Math.sinh(2.33482), Math.ulp(5.115386441963859)); + assertEquals(Double.POSITIVE_INFINITY, Math.sinh(Double.MAX_VALUE), 0D); + assertEquals(4.9E-324, Math.sinh(Double.MIN_VALUE), 0D); } /** -- cgit v1.1 From 9ed62cd1e87a433130a089f8cf27de560c643c8a Mon Sep 17 00:00:00 2001 From: Elliott Hughes <enh@google.com> Date: Sat, 25 Oct 2014 23:32:07 -0700 Subject: Allow 1 ulp difference in test_cbrt_D and test_sinh_D. Bug: 18016320 (cherry picked from commit cf5f86467f5be5f1c3ae2a5518c65f496ac93e33) Change-Id: I05c3810320d035323e5fec07712b47586eb100bb (cherry picked from commit 8afb381342e073b3bfcc8700b370e381ac23b2e0) --- .../apache/harmony/tests/java/lang/MathTest.java | 84 ++++++++-------------- 1 file changed, 30 insertions(+), 54 deletions(-) (limited to 'harmony-tests') diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/MathTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/MathTest.java index cd38545..ed8e2b5 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/MathTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/lang/MathTest.java @@ -120,34 +120,22 @@ public class MathTest extends junit.framework.TestCase { */ public void test_cbrt_D() { //Test for special situations - assertTrue("Should return Double.NaN", Double.isNaN(Math - .cbrt(Double.NaN))); - assertEquals("Should return Double.POSITIVE_INFINITY", - Double.POSITIVE_INFINITY, Math - .cbrt(Double.POSITIVE_INFINITY), 0D); - assertEquals("Should return Double.NEGATIVE_INFINITY", - Double.NEGATIVE_INFINITY, Math - .cbrt(Double.NEGATIVE_INFINITY), 0D); - assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math - .cbrt(0.0))); - assertEquals(Double.doubleToLongBits(+0.0), Double.doubleToLongBits(Math - .cbrt(+0.0))); - assertEquals(Double.doubleToLongBits(-0.0), Double.doubleToLongBits(Math - .cbrt(-0.0))); - - assertEquals("Should return 3.0", 3.0, Math.cbrt(27.0), 0D); - assertEquals("Should return 23.111993172558684", 23.111993172558684, - Math.cbrt(12345.6), 0D); - assertEquals("Should return 5.643803094122362E102", - 5.643803094122362E102, Math.cbrt(Double.MAX_VALUE), 0D); - assertEquals("Should return 0.01", 0.01, Math.cbrt(0.000001), 0D); - - assertEquals("Should return -3.0", -3.0, Math.cbrt(-27.0), 0D); - assertEquals("Should return -23.111993172558684", -23.111993172558684, - Math.cbrt(-12345.6), 0D); - assertEquals("Should return 1.7031839360032603E-108", - 1.7031839360032603E-108, Math.cbrt(Double.MIN_VALUE), 0D); - assertEquals("Should return -0.01", -0.01, Math.cbrt(-0.000001), 0D); + assertTrue(Double.isNaN(Math.cbrt(Double.NaN))); + assertEquals(Double.POSITIVE_INFINITY, Math.cbrt(Double.POSITIVE_INFINITY), 0D); + assertEquals(Double.NEGATIVE_INFINITY, Math.cbrt(Double.NEGATIVE_INFINITY), 0D); + assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math.cbrt(0.0))); + assertEquals(Double.doubleToLongBits(+0.0), Double.doubleToLongBits(Math.cbrt(+0.0))); + assertEquals(Double.doubleToLongBits(-0.0), Double.doubleToLongBits(Math.cbrt(-0.0))); + + assertEquals(3.0, Math.cbrt(27.0), 0D); + assertEquals(23.111993172558684, Math.cbrt(12345.6), Math.ulp(23.111993172558684)); + assertEquals(5.643803094122362E102, Math.cbrt(Double.MAX_VALUE), 0D); + assertEquals(0.01, Math.cbrt(0.000001), 0D); + + assertEquals(-3.0, Math.cbrt(-27.0), 0D); + assertEquals(-23.111993172558684, Math.cbrt(-12345.6), Math.ulp(-23.111993172558684)); + assertEquals(1.7031839360032603E-108, Math.cbrt(Double.MIN_VALUE), 0D); + assertEquals(-0.01, Math.cbrt(-0.000001), 0D); } /** @@ -1703,32 +1691,20 @@ public class MathTest extends junit.framework.TestCase { */ public void test_sinh_D() { // Test for special situations - assertTrue("Should return NaN", Double.isNaN(Math.sinh(Double.NaN))); - assertEquals("Should return POSITIVE_INFINITY", - Double.POSITIVE_INFINITY, Math.sinh(Double.POSITIVE_INFINITY), 0D); - assertEquals("Should return NEGATIVE_INFINITY", - Double.NEGATIVE_INFINITY, Math.sinh(Double.NEGATIVE_INFINITY), 0D); - assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math - .sinh(0.0))); - assertEquals(Double.doubleToLongBits(+0.0), Double - .doubleToLongBits(Math.sinh(+0.0))); - assertEquals(Double.doubleToLongBits(-0.0), Double - .doubleToLongBits(Math.sinh(-0.0))); - - assertEquals("Should return POSITIVE_INFINITY", - Double.POSITIVE_INFINITY, Math.sinh(1234.56), 0D); - assertEquals("Should return NEGATIVE_INFINITY", - Double.NEGATIVE_INFINITY, Math.sinh(-1234.56), 0D); - assertEquals("Should return 1.0000000000001666E-6", - 1.0000000000001666E-6, Math.sinh(0.000001), 0D); - assertEquals("Should return -1.0000000000001666E-6", - -1.0000000000001666E-6, Math.sinh(-0.000001), 0D); - assertEquals("Should return 5.115386441963859", 5.115386441963859, Math - .sinh(2.33482), 0D); - assertEquals("Should return POSITIVE_INFINITY", - Double.POSITIVE_INFINITY, Math.sinh(Double.MAX_VALUE), 0D); - assertEquals("Should return 4.9E-324", 4.9E-324, Math - .sinh(Double.MIN_VALUE), 0D); + assertTrue(Double.isNaN(Math.sinh(Double.NaN))); + assertEquals(Double.POSITIVE_INFINITY, Math.sinh(Double.POSITIVE_INFINITY), 0D); + assertEquals(Double.NEGATIVE_INFINITY, Math.sinh(Double.NEGATIVE_INFINITY), 0D); + assertEquals(Double.doubleToLongBits(0.0), Double.doubleToLongBits(Math.sinh(0.0))); + assertEquals(Double.doubleToLongBits(+0.0), Double.doubleToLongBits(Math.sinh(+0.0))); + assertEquals(Double.doubleToLongBits(-0.0), Double.doubleToLongBits(Math.sinh(-0.0))); + + assertEquals(Double.POSITIVE_INFINITY, Math.sinh(1234.56), 0D); + assertEquals(Double.NEGATIVE_INFINITY, Math.sinh(-1234.56), 0D); + assertEquals(1.0000000000001666E-6, Math.sinh(0.000001), 0D); + assertEquals(-1.0000000000001666E-6, Math.sinh(-0.000001), 0D); + assertEquals(5.115386441963859, Math.sinh(2.33482), Math.ulp(5.115386441963859)); + assertEquals(Double.POSITIVE_INFINITY, Math.sinh(Double.MAX_VALUE), 0D); + assertEquals(4.9E-324, Math.sinh(Double.MIN_VALUE), 0D); } /** -- cgit v1.1 From 00bf89dd858de6c7eaca555210ba429a89193722 Mon Sep 17 00:00:00 2001 From: Kenny Root <kroot@google.com> Date: Fri, 7 Nov 2014 14:35:33 -0800 Subject: Switch order of digest and digest encryption algorithm Sometimes "digest encryption algorithm" would be "RSA" which would match a Signature provider, but its default setup would be whatever the provider chose. This works fine with newer algorithms that have a specific OID for their signature format (e.g., ECDSA and SHA256), but not with algorithms that just have a generic OID for all possible uses (e.g., RSA). Stock Android never hits this problem, because nothing registers a "Signature.RSA" provider, but Spongycastle does so using JarURLClassLoader after inserting Spongycastle causes a problem. Flip the order of tries to make this work more uniformly with more JAR and provider combinations. (cherry picked from commit b1da6d3df5f9cce6e6d77c63599eba62edb465d6) Bug: 17790692 Bug: https://code.google.com/p/android/issues/detail?id=68562 Change-Id: I3bb07ea25d7bf1d55fa2466b204594179ac38932 --- .../harmony/tests/java/util/jar/JarFileTest.java | 77 ++++++++++++++++++++++ 1 file changed, 77 insertions(+) (limited to 'harmony-tests') diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/jar/JarFileTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/jar/JarFileTest.java index d5d8191..f55829d 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/jar/JarFileTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/jar/JarFileTest.java @@ -24,7 +24,15 @@ import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.security.CodeSigner; +import java.security.InvalidKeyException; +import java.security.InvalidParameterException; import java.security.Permission; +import java.security.PrivateKey; +import java.security.Provider; +import java.security.PublicKey; +import java.security.Security; +import java.security.SignatureException; +import java.security.SignatureSpi; import java.security.cert.Certificate; import java.security.cert.X509Certificate; import java.util.Arrays; @@ -1014,4 +1022,73 @@ public class JarFileTest extends TestCase { res = jarFile.getInputStream(zipEntry).read(); assertEquals("Wrong length of empty jar entry", -1, res); } + + public void testJarFile_BadSignatureProvider_Success() throws Exception { + Security.insertProviderAt(new JarFileBadProvider(), 1); + try { + // Needs a JAR with "RSA" as digest encryption algorithm + checkSignedJar(jarName6); + } finally { + Security.removeProvider(JarFileBadProvider.NAME); + } + } + + public static class JarFileBadProvider extends Provider { + public static final String NAME = "JarFileBadProvider"; + + public JarFileBadProvider() { + super(NAME, 1.0, "Bad provider for JarFileTest"); + + put("Signature.RSA", NotReallyASignature.class.getName()); + } + + /** + * This should never be instantiated, so everything throws an exception. + */ + public static class NotReallyASignature extends SignatureSpi { + @Override + protected void engineInitVerify(PublicKey publicKey) throws InvalidKeyException { + fail("Should not call this provider"); + } + + @Override + protected void engineInitSign(PrivateKey privateKey) throws InvalidKeyException { + fail("Should not call this provider"); + } + + @Override + protected void engineUpdate(byte b) throws SignatureException { + fail("Should not call this provider"); + } + + @Override + protected void engineUpdate(byte[] b, int off, int len) throws SignatureException { + fail("Should not call this provider"); + } + + @Override + protected byte[] engineSign() throws SignatureException { + fail("Should not call this provider"); + return null; + } + + @Override + protected boolean engineVerify(byte[] sigBytes) throws SignatureException { + fail("Should not call this provider"); + return false; + } + + @Override + protected void engineSetParameter(String param, Object value) + throws InvalidParameterException { + fail("Should not call this provider"); + } + + @Override + protected Object engineGetParameter(String param) throws InvalidParameterException { + fail("Should not call this provider"); + return null; + } + } + } } -- cgit v1.1 From ef7f5a16547089a7cdba9e48d780720f606ff54a Mon Sep 17 00:00:00 2001 From: Kenny Root <kroot@google.com> Date: Thu, 13 Nov 2014 10:14:46 -0800 Subject: JarUtils: stop trying to build chain past candidates length If the certs in the PKCS#7 bag are in a loop, it will go on forever trying to build a chain. Instead just stop trying to build the chain when our chain exceeds the length of the candidates. Bug: 17972577 Change-Id: If4f92e3eeabe893612a618bab0068a0f8cf75ea9 --- .../harmony/tests/java/util/jar/JarFileTest.java | 88 +++++++++++++++++----- 1 file changed, 71 insertions(+), 17 deletions(-) (limited to 'harmony-tests') diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/jar/JarFileTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/jar/JarFileTest.java index f55829d..0bc8920 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/jar/JarFileTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/util/jar/JarFileTest.java @@ -37,7 +37,14 @@ import java.security.cert.Certificate; import java.security.cert.X509Certificate; import java.util.Arrays; import java.util.Enumeration; +import java.util.List; import java.util.Vector; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import java.util.jar.Attributes; import java.util.jar.JarEntry; import java.util.jar.JarFile; @@ -97,6 +104,27 @@ public class JarFileTest extends TestCase { private final String emptyEntryJar = "EmptyEntries_signed.jar"; + /* + * /usr/bin/openssl genrsa 2048 > root1.pem + * /usr/bin/openssl req -new -key root1.pem -out root1.csr -subj '/CN=root1' + * /usr/bin/openssl x509 -req -days 3650 -in root1.csr -signkey root1.pem -out root1.crt + * /usr/bin/openssl genrsa 2048 > root2.pem + * /usr/bin/openssl req -new -key root2.pem -out root2.csr -subj '/CN=root2' + * echo 4000 > root1.srl + * echo 8000 > root2.srl + * /usr/bin/openssl x509 -req -days 3650 -in root2.csr -CA root1.crt -CAkey root1.pem -out root2.crt + * /usr/bin/openssl x509 -req -days 3650 -in root1.csr -CA root2.crt -CAkey root2.pem -out root1.crt + * /usr/bin/openssl genrsa 2048 > signer.pem + * /usr/bin/openssl req -new -key signer.pem -out signer.csr -subj '/CN=signer' + * /usr/bin/openssl x509 -req -days 3650 -in signer.csr -CA root1.crt -CAkey root1.pem -out signer.crt + * /usr/bin/openssl pkcs12 -inkey signer.pem -in signer.crt -export -out signer.p12 -name signer -passout pass:certloop + * keytool -importkeystore -srckeystore signer.p12 -srcstoretype PKCS12 -destkeystore signer.jks -srcstorepass certloop -deststorepass certloop + * cat signer.crt root1.crt root2.crt > chain.crt + * zip -d hyts_certLoop.jar 'META-INF/*' + * jarsigner -keystore signer.jks -certchain chain.crt -storepass certloop hyts_certLoop.jar signer + */ + private final String certLoopJar = "hyts_certLoop.jar"; + private final String emptyEntry1 = "subfolder/internalSubset01.js"; private final String emptyEntry2 = "svgtest.js"; @@ -616,6 +644,9 @@ public class JarFileTest extends TestCase { // JAR with a signature that has PKCS#7 Authenticated Attributes checkSignedJar(authAttrsJar); + + // JAR with certificates that loop + checkSignedJar(certLoopJar, 3); } /** @@ -628,29 +659,52 @@ public class JarFileTest extends TestCase { checkSignedJar(jarName9); } + /** + * Checks that a JAR is signed correctly with a signature length of 1. + */ private void checkSignedJar(String jarName) throws Exception { - Support_Resources.copyFile(resources, null, jarName); + checkSignedJar(jarName, 1); + } - File file = new File(resources, jarName); - boolean foundCerts = false; + /** + * Checks that a JAR is signed correctly with a signature length of sigLength. + */ + private void checkSignedJar(String jarName, final int sigLength) throws Exception { + Support_Resources.copyFile(resources, null, jarName); - JarFile jarFile = new JarFile(file, true); - try { + final File file = new File(resources, jarName); - Enumeration<JarEntry> e = jarFile.entries(); - while (e.hasMoreElements()) { - JarEntry entry = e.nextElement(); - InputStream is = jarFile.getInputStream(entry); - is.skip(100000); - is.close(); - Certificate[] certs = entry.getCertificates(); - if (certs != null && certs.length > 0) { - foundCerts = true; - break; + ExecutorService executor = Executors.newSingleThreadExecutor(); + Future<Boolean> future = executor.submit(new Callable<Boolean>() { + @Override + public Boolean call() throws Exception { + JarFile jarFile = new JarFile(file, true); + try { + Enumeration<JarEntry> e = jarFile.entries(); + while (e.hasMoreElements()) { + JarEntry entry = e.nextElement(); + InputStream is = jarFile.getInputStream(entry); + is.skip(100000); + is.close(); + Certificate[] certs = entry.getCertificates(); + if (certs != null && certs.length > 0) { + assertEquals(sigLength, certs.length); + return true; + } + } + return false; + } finally { + jarFile.close(); } } - } finally { - jarFile.close(); + }); + executor.shutdown(); + final boolean foundCerts; + try { + foundCerts = future.get(10, TimeUnit.SECONDS); + } catch (TimeoutException e) { + fail("Could not finish building chain; possibly confused by loops"); + return; // Not actually reached. } assertTrue( -- cgit v1.1 From 944632aa6018d04d5acfa12879fb7f01ba2959ff Mon Sep 17 00:00:00 2001 From: Jason Parks <jparks@google.com> Date: Thu, 20 Nov 2014 18:44:11 -0600 Subject: =?UTF-8?q?Do=20not=20run=20the=20multicast=20tests=20if=20the=20d?= =?UTF-8?q?evice=20doesn=E2=80=99t=20support=20multicast.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: 18449007 Change-Id: Ia7511083cc36dd7d6835191bf4b79d23ed039b5b --- .../tests/java/net/MulticastSocketTest.java | 130 ++++++++++++++++++++- 1 file changed, 129 insertions(+), 1 deletion(-) (limited to 'harmony-tests') diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/MulticastSocketTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/MulticastSocketTest.java index e3e1207..d531301 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/MulticastSocketTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/MulticastSocketTest.java @@ -56,6 +56,7 @@ public class MulticastSocketTest extends junit.framework.TestCase { private NetworkInterface loopbackInterface; private NetworkInterface ipv4NetworkInterface; private NetworkInterface ipv6NetworkInterface; + private boolean supportsMulticast; @Override protected void setUp() throws Exception { @@ -69,6 +70,14 @@ public class MulticastSocketTest extends junit.framework.TestCase { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); assertNotNull(interfaces); + // Determine if the device is marked to support multicast or not. If this propery is not + // set we assume the device has an interface capable of supporting multicast. + supportsMulticast = Boolean.valueOf( + System.getProperty("android.cts.device.multicast", "true")); + if (!supportsMulticast) { + return; + } + while (interfaces.hasMoreElements() && (ipv4NetworkInterface == null || ipv6NetworkInterface == null)) { NetworkInterface nextInterface = interfaces.nextElement(); @@ -90,6 +99,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_Constructor() throws IOException { + if (!supportsMulticast) { + return; + } // Regression test for 497. MulticastSocket s = new MulticastSocket(); // Regression test for Harmony-1162. @@ -99,6 +111,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_ConstructorI() throws IOException { + if (!supportsMulticast) { + return; + } MulticastSocket orig = new MulticastSocket(); int port = orig.getLocalPort(); orig.close(); @@ -110,6 +125,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_getInterface() throws Exception { + if (!supportsMulticast) { + return; + } // Validate that we get the expected response when one was not set. MulticastSocket mss = new MulticastSocket(0); // We expect an ANY address in this case. @@ -134,6 +152,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_getNetworkInterface() throws IOException { + if (!supportsMulticast) { + return; + } // Validate that we get the expected response when one was not set. MulticastSocket mss = new MulticastSocket(0); NetworkInterface theInterface = mss.getNetworkInterface(); @@ -176,6 +197,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_getTimeToLive() throws Exception { + if (!supportsMulticast) { + return; + } MulticastSocket mss = new MulticastSocket(); mss.setTimeToLive(120); assertEquals("Returned incorrect 1st TTL", 120, mss.getTimeToLive()); @@ -185,6 +209,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_getTTL() throws Exception { + if (!supportsMulticast) { + return; + } MulticastSocket mss = new MulticastSocket(); mss.setTTL((byte) 120); assertEquals("Returned incorrect TTL", 120, mss.getTTL()); @@ -192,10 +219,16 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_joinGroupLjava_net_InetAddress_IPv4() throws Exception { + if (!supportsMulticast) { + return; + } test_joinGroupLjava_net_InetAddress(GOOD_IPv4); } public void test_joinGroupLjava_net_InetAddress_IPv6() throws Exception { + if (!supportsMulticast) { + return; + } test_joinGroupLjava_net_InetAddress(GOOD_IPv6); } @@ -220,6 +253,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_joinGroup_null_null() throws Exception { + if (!supportsMulticast) { + return; + } MulticastSocket mss = new MulticastSocket(0); try { mss.joinGroup(null, null); @@ -230,6 +266,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_joinGroup_non_multicast_address_IPv4() throws Exception { + if (!supportsMulticast) { + return; + } MulticastSocket mss = new MulticastSocket(0); try { mss.joinGroup(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 0), null); @@ -240,6 +279,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_joinGroup_non_multicast_address_IPv6() throws Exception { + if (!supportsMulticast) { + return; + } MulticastSocket mss = new MulticastSocket(0); try { mss.joinGroup(new InetSocketAddress(InetAddress.getByName("::1"), 0), null); @@ -251,12 +293,18 @@ public class MulticastSocketTest extends junit.framework.TestCase { public void test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface_IPv4() throws Exception { + if (!supportsMulticast) { + return; + } test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface( ipv4NetworkInterface, GOOD_IPv4, BAD_IPv4); } public void test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface_IPv6() throws Exception { + if (!supportsMulticast) { + return; + } test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface( ipv6NetworkInterface, GOOD_IPv6, BAD_IPv6); } @@ -310,6 +358,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface() throws Exception { + if (!supportsMulticast) { + return; + } // Check that we can join on specific interfaces and that we only receive if data is // received on that interface. This test is only really useful on devices with multiple // non-loopback interfaces. @@ -378,12 +429,18 @@ public class MulticastSocketTest extends junit.framework.TestCase { public void test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface_multiple_joins_IPv4() throws Exception { + if (!supportsMulticast) { + return; + } test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface_multiple_joins( ipv4NetworkInterface, GOOD_IPv4); } public void test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface_multiple_joins_IPv6() throws Exception { + if (!supportsMulticast) { + return; + } test_joinGroupLjava_net_SocketAddressLjava_net_NetworkInterface_multiple_joins( ipv6NetworkInterface, GOOD_IPv6); } @@ -405,10 +462,16 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_leaveGroupLjava_net_InetAddress_IPv4() throws Exception { + if (!supportsMulticast) { + return; + } test_leaveGroupLjava_net_InetAddress(GOOD_IPv4); } public void test_leaveGroupLjava_net_InetAddress_IPv6() throws Exception { + if (!supportsMulticast) { + return; + } test_leaveGroupLjava_net_InetAddress(GOOD_IPv6); } @@ -428,6 +491,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_leaveGroup_null_null() throws Exception { + if (!supportsMulticast) { + return; + } MulticastSocket mss = new MulticastSocket(0); try { mss.leaveGroup(null, null); @@ -438,6 +504,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_leaveGroup_non_multicast_address_IPv4() throws Exception { + if (!supportsMulticast) { + return; + } MulticastSocket mss = new MulticastSocket(0); try { mss.leaveGroup(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), 0), null); @@ -448,6 +517,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_leaveGroup_non_multicast_address_IPv6() throws Exception { + if (!supportsMulticast) { + return; + } MulticastSocket mss = new MulticastSocket(0); try { mss.leaveGroup(new InetSocketAddress(InetAddress.getByName("::1"), 0), null); @@ -459,12 +531,18 @@ public class MulticastSocketTest extends junit.framework.TestCase { public void test_leaveGroupLjava_net_SocketAddressLjava_net_NetworkInterface_IPv4() throws Exception { + if (!supportsMulticast) { + return; + } test_leaveGroupLjava_net_SocketAddressLjava_net_NetworkInterface( ipv4NetworkInterface, GOOD_IPv4, BAD_IPv4); } public void test_leaveGroupLjava_net_SocketAddressLjava_net_NetworkInterface_IPv6() throws Exception { + if (!supportsMulticast) { + return; + } test_leaveGroupLjava_net_SocketAddressLjava_net_NetworkInterface( ipv6NetworkInterface, GOOD_IPv6, BAD_IPv6); } @@ -505,10 +583,16 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_sendLjava_net_DatagramPacketB_IPv4() throws Exception { + if (!supportsMulticast) { + return; + } test_sendLjava_net_DatagramPacketB(GOOD_IPv4); } public void test_sendLjava_net_DatagramPacketB_IPv6() throws Exception { + if (!supportsMulticast) { + return; + } test_sendLjava_net_DatagramPacketB(GOOD_IPv6); } @@ -531,6 +615,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_setInterfaceLjava_net_InetAddress() throws Exception { + if (!supportsMulticast) { + return; + } MulticastSocket mss = new MulticastSocket(); mss.setInterface(InetAddress.getLocalHost()); InetAddress theInterface = mss.getInterface(); @@ -549,10 +636,16 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_setInterface_unbound_address_IPv4() throws Exception { + if (!supportsMulticast) { + return; + } test_setInterface_unbound_address(GOOD_IPv4); } public void test_setInterface_unbound_address_IPv6() throws Exception { + if (!supportsMulticast) { + return; + } test_setInterface_unbound_address(GOOD_IPv6); } @@ -568,6 +661,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_setNetworkInterfaceLjava_net_NetworkInterface_null() throws Exception { + if (!supportsMulticast) { + return; + } // Validate that null interface is handled ok. MulticastSocket mss = new MulticastSocket(); try { @@ -579,6 +675,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_setNetworkInterfaceLjava_net_NetworkInterface_round_trip() throws Exception { + if (!supportsMulticast) { + return; + } // Validate that we can get and set the interface. MulticastSocket mss = new MulticastSocket(); mss.setNetworkInterface(ipv4NetworkInterface); @@ -588,10 +687,16 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_setNetworkInterfaceLjava_net_NetworkInterface_IPv4() throws Exception { + if (!supportsMulticast) { + return; + } test_setNetworkInterfaceLjava_net_NetworkInterface(GOOD_IPv4); } public void test_setNetworkInterfaceLjava_net_NetworkInterface_IPv6() throws Exception { + if (!supportsMulticast) { + return; + } test_setNetworkInterfaceLjava_net_NetworkInterface(GOOD_IPv6); } @@ -630,6 +735,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_setTimeToLiveI() throws Exception { + if (!supportsMulticast) { + return; + } MulticastSocket mss = new MulticastSocket(); mss.setTimeToLive(120); assertEquals("Returned incorrect 1st TTL", 120, mss.getTimeToLive()); @@ -639,6 +747,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_setTTLB() throws Exception { + if (!supportsMulticast) { + return; + } MulticastSocket mss = new MulticastSocket(); mss.setTTL((byte) 120); assertEquals("Failed to set TTL", 120, mss.getTTL()); @@ -646,6 +757,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_ConstructorLjava_net_SocketAddress() throws Exception { + if (!supportsMulticast) { + return; + } MulticastSocket ms = new MulticastSocket((SocketAddress) null); assertTrue("should not be bound", !ms.isBound() && !ms.isClosed() && !ms.isConnected()); ms.bind(null); @@ -677,6 +791,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_getLoopbackMode() throws Exception { + if (!supportsMulticast) { + return; + } MulticastSocket ms = new MulticastSocket(null); assertTrue("should not be bound", !ms.isBound() && !ms.isClosed() && !ms.isConnected()); ms.getLoopbackMode(); @@ -686,6 +803,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_setLoopbackModeZ() throws Exception { + if (!supportsMulticast) { + return; + } MulticastSocket ms = new MulticastSocket(); ms.setLoopbackMode(true); assertTrue("loopback should be true", ms.getLoopbackMode()); @@ -696,10 +816,16 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_setLoopbackModeSendReceive_IPv4() throws Exception { + if (!supportsMulticast) { + return; + } test_setLoopbackModeSendReceive(GOOD_IPv4); } public void test_setLoopbackModeSendReceive_IPv6() throws Exception { + if (!supportsMulticast) { + return; + } test_setLoopbackModeSendReceive(GOOD_IPv6); } @@ -726,6 +852,9 @@ public class MulticastSocketTest extends junit.framework.TestCase { } public void test_setReuseAddressZ() throws Exception { + if (!supportsMulticast) { + return; + } // Test case were we to set ReuseAddress to false. MulticastSocket theSocket1 = new MulticastSocket(null); theSocket1.setReuseAddress(false); @@ -797,5 +926,4 @@ public class MulticastSocketTest extends junit.framework.TestCase { private static String extractMessage(DatagramPacket rdp) { return new String(rdp.getData(), 0, rdp.getLength()); } - } -- cgit v1.1 From a951e74897193848ee88ed1c5b0a1d536f403d70 Mon Sep 17 00:00:00 2001 From: Narayan Kamath <narayan@google.com> Date: Thu, 4 Dec 2014 13:05:21 +0000 Subject: Fix failing socket tests. Use bogus numeric addresses instead of invalid hostnames to make InetAddress.getAllByName fail before we ask a DNS server. This guards against flakiness caused by evil DNS servers. Note that from an API perspective, Socket* and SSLSocket* don't care about *why* getAllByName failed, just that it did. This change also fixes a test that was asserting that the exception we throw contains a useful error message. Again, this test was changed to use a broken numeric address. bug: 18575971 (cherry picked from commit 83b5554108e839b0c78178a029b65fddce8983f0) Change-Id: Ia1c521c05634ce798a61b060f4d3fbbc164bb9fc --- .../harmony/tests/java/net/InetAddressTest.java | 18 +++----- .../apache/harmony/tests/java/net/SocketTest.java | 2 +- .../harmony/tests/javax/net/SocketFactoryTest.java | 4 +- .../tests/javax/net/ssl/SSLSocketFactoryTest.java | 54 +++++++++------------- .../harmony/tests/javax/net/ssl/SSLSocketTest.java | 4 +- 5 files changed, 33 insertions(+), 49 deletions(-) (limited to 'harmony-tests') diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/InetAddressTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/InetAddressTest.java index 959f83c..42f88c1 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/InetAddressTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/InetAddressTest.java @@ -24,31 +24,25 @@ import java.net.Inet4Address; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.UnknownHostException; -import java.security.Permission; import java.util.ArrayList; import java.util.Enumeration; import org.apache.harmony.testframework.serialization.SerializationTest; import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert; -import tests.support.Support_Configuration; public class InetAddressTest extends junit.framework.TestCase { /** * java.net.InetAddress#getByName(String) */ - public void test_getByNameUnknownHostException() { + public void test_getByName_exceptionContainsUsefulMessage() { // Related to HARMONY-5784 - - // loop a few times to flex the negative cache paths - for (int i = 0; i < 5; i++) { - try { - InetAddress.getByName("unknown.unknown.bad"); - fail("An UnknownHostException should have been thrown"); - } catch (UnknownHostException e) { - assertTrue(e.getMessage().contains("unknown.unknown.bad")); - } + try { + InetAddress.getByName("1.2.3.4hello"); + fail(); + } catch (UnknownHostException e) { + assertTrue(e.getMessage().contains("1.2.3.4hello")); } } diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/SocketTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/SocketTest.java index 18d2cb2..4df92e2 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/SocketTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/java/net/SocketTest.java @@ -274,7 +274,7 @@ public class SocketTest extends junit.framework.TestCase { public void test_connect_unknownhost() throws Exception { Socket socket = new Socket(); try { - socket.connect(new InetSocketAddress("unknownhost.invalid", 12345)); + socket.connect(new InetSocketAddress("1.2.3.4hello", 12345)); fail(); } catch (UnknownHostException expected) { } diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/javax/net/SocketFactoryTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/javax/net/SocketFactoryTest.java index e939a9b..d566ee3 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/javax/net/SocketFactoryTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/javax/net/SocketFactoryTest.java @@ -65,7 +65,7 @@ public class SocketFactoryTest extends TestCase { assertTrue("Failed to create socket", s.getPort() == sport); try { - sf.createSocket("bla-bla", sport); + sf.createSocket("1.2.3.4hello", sport); fail("UnknownHostException wasn't thrown"); } catch (UnknownHostException expected) { } @@ -180,7 +180,7 @@ public class SocketFactoryTest extends TestCase { assertTrue("1: Failed to create socket", s.getPort() == sport); try { - sf.createSocket("bla-bla", sport, InetAddress.getLocalHost(), 0); + sf.createSocket("1.2.3.4hello", sport, InetAddress.getLocalHost(), 0); fail("UnknownHostException wasn't thrown"); } catch (UnknownHostException expected) { } diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/javax/net/ssl/SSLSocketFactoryTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/javax/net/ssl/SSLSocketFactoryTest.java index 0d91116..e890032 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/javax/net/ssl/SSLSocketFactoryTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/javax/net/ssl/SSLSocketFactoryTest.java @@ -61,52 +61,42 @@ public class SSLSocketFactoryTest extends TestCase { /** * javax.net.ssl.SSLSocketFactory#createSocket(Socket s, String host, int port, boolean autoClose) */ - public void test_createSocket() { + public void test_createSocket() throws Exception { SSLSocketFactory sf = (SSLSocketFactory)SSLSocketFactory.getDefault(); int sport = startServer("test_createSocket()"); int[] invalid = { Integer.MIN_VALUE, -1, 65536, Integer.MAX_VALUE }; - try { - Socket st = new Socket("localhost", sport); - Socket s = sf.createSocket(st, "localhost", sport, false); - assertFalse(s.isClosed()); - } catch (Exception ex) { - fail("Unexpected exception " + ex); - } - try { - Socket st = new Socket("localhost", sport); - Socket s = sf.createSocket(st, "localhost", sport, true); - s.close(); - assertTrue(st.isClosed()); - } catch (Exception ex) { - fail("Unexpected exception " + ex); - } + + + Socket st = new Socket("localhost", sport); + Socket s = sf.createSocket(st, "localhost", sport, false); + assertFalse(s.isClosed()); + + st = new Socket("localhost", sport); + s = sf.createSocket(st, "localhost", sport, true); + s.close(); + assertTrue(st.isClosed()); + try { sf.createSocket(null, "localhost", sport, true); - fail("IOException wasn't thrown"); - } catch (IOException ioe) { - // expected - } catch (NullPointerException e) { - // expected + fail(); + } catch (NullPointerException expected) { } + for (int i = 0; i < invalid.length; i++) { try { - Socket s = sf.createSocket(new Socket(), "localhost", 1080, false); - fail("IOException wasn't thrown"); - } catch (IOException ioe) { - // expected + s = sf.createSocket(new Socket(), "localhost", 1080, false); + fail(); + } catch (IOException expected) { } } try { - Socket st = new Socket("bla-bla", sport); - Socket s = sf.createSocket(st, "bla-bla", sport, false); - fail("UnknownHostException wasn't thrown: " + "bla-bla"); - } catch (UnknownHostException uhe) { - // expected - } catch (Exception e) { - fail(e + " was thrown instead of UnknownHostException"); + st = new Socket("1.2.3.4hello", sport); + s = sf.createSocket(st, "1.2.3.4hello", sport, false); + fail(); + } catch (UnknownHostException expected) { } } diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/javax/net/ssl/SSLSocketTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/javax/net/ssl/SSLSocketTest.java index b4cbde2..950d534 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/javax/net/ssl/SSLSocketTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/javax/net/ssl/SSLSocketTest.java @@ -158,7 +158,7 @@ public class SSLSocketTest extends TestCase { } try { - getSSLSocket("bla-bla", sport); + getSSLSocket("1.2.3.4hello", sport); fail(); } catch (UnknownHostException expected) { } @@ -199,7 +199,7 @@ public class SSLSocketTest extends TestCase { } try { - getSSLSocket("bla-bla", sport, InetAddress.getLocalHost(), 0); + getSSLSocket("1.2.3.4hello", sport, InetAddress.getLocalHost(), 0); fail(); } catch (UnknownHostException expected) { } -- cgit v1.1 From 0311cd2be856e4c5872ebbe5ac7e12eac334ad73 Mon Sep 17 00:00:00 2001 From: Mathieu Chartier <mathieuc@google.com> Date: Tue, 13 Jan 2015 10:45:52 -0800 Subject: Increase ThreadTest tolerance Aims to fix flaky test. Decreased minimum from 90% to 80%. Increased maximum from 110% to 120%. Increased maximum waitMillis from 10ms to 30ms. Bug: 18788389 Change-Id: I92477225492712448b0a78354730a037e2dda1a1 --- .../harmony/tests/org/apache/harmony/kernel/dalvik/ThreadsTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'harmony-tests') diff --git a/harmony-tests/src/test/java/org/apache/harmony/tests/org/apache/harmony/kernel/dalvik/ThreadsTest.java b/harmony-tests/src/test/java/org/apache/harmony/tests/org/apache/harmony/kernel/dalvik/ThreadsTest.java index 19c6229..c971e99 100644 --- a/harmony-tests/src/test/java/org/apache/harmony/tests/org/apache/harmony/kernel/dalvik/ThreadsTest.java +++ b/harmony-tests/src/test/java/org/apache/harmony/tests/org/apache/harmony/kernel/dalvik/ThreadsTest.java @@ -234,10 +234,10 @@ public class ThreadsTest extends TestCase { * Allow a bit more slop for the maximum on "expected * instantaneous" results. */ - long minimum = (long) ((double) expectedMillis * 0.90); + long minimum = (long) ((double) expectedMillis * 0.80); long maximum = - Math.max((long) ((double) expectedMillis * 1.10), 10); - long waitMillis = Math.max(expectedMillis * 10, 10); + Math.max((long) ((double) expectedMillis * 1.20), 10); + long waitMillis = Math.max(expectedMillis * 10, 30); long duration = getDurationMillis(waitMillis); if (duration < minimum) { -- cgit v1.1