-//##header\r
-/*\r
- *******************************************************************************\r
- * Copyright (C) 2001-2009, International Business Machines Corporation and *\r
- * others. All Rights Reserved. *\r
- *******************************************************************************\r
- */\r
-\r
-/** \r
- * Port From: ICU4C v1.8.1 : format : IntlTestDecimalFormatAPI\r
- * Source File: $ICU4CRoot/source/test/intltest/dcfmapts.cpp\r
- **/\r
-\r
-package com.ibm.icu.dev.test.format;\r
-\r
-import java.text.AttributedCharacterIterator;\r
-import java.text.FieldPosition;\r
-import java.text.Format;\r
-import java.text.ParsePosition;\r
-import java.util.Iterator;\r
-import java.util.Locale;\r
-import java.util.Vector;\r
-\r
-import com.ibm.icu.text.DecimalFormat;\r
-import com.ibm.icu.text.DecimalFormatSymbols;\r
-import com.ibm.icu.text.NumberFormat;\r
-\r
-// This is an API test, not a unit test. It doesn't test very many cases, and doesn't\r
-// try to test the full functionality. It just calls each function in the class and\r
-// verifies that it works on a basic level.\r
-public class IntlTestDecimalFormatAPIC extends com.ibm.icu.dev.test.TestFmwk {\r
- \r
- public static void main(String[] args) throws Exception {\r
- new IntlTestDecimalFormatAPIC().run(args);\r
- }\r
-\r
- // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.\r
- public void TestAPI() {\r
-\r
- logln("DecimalFormat API test---");\r
- logln("");\r
- Locale.setDefault(Locale.ENGLISH);\r
-\r
- // ======= Test constructors\r
-\r
- logln("Testing DecimalFormat constructors");\r
-\r
- DecimalFormat def = new DecimalFormat();\r
-\r
- final String pattern = new String("#,##0.# FF");\r
- DecimalFormat pat = null;\r
- try {\r
- pat = new DecimalFormat(pattern);\r
- } catch (IllegalArgumentException e) {\r
- errln("ERROR: Could not create DecimalFormat (pattern)");\r
- }\r
-\r
- DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.FRENCH);\r
-\r
- DecimalFormat cust1 = new DecimalFormat(pattern, symbols);\r
-\r
- // ======= Test clone(), assignment, and equality\r
-\r
- logln("Testing clone() and equality operators");\r
-\r
- Format clone = (Format) def.clone();\r
- if (!def.equals(clone)) {\r
- errln("ERROR: Clone() failed");\r
- }\r
-\r
- // ======= Test various format() methods\r
-\r
- logln("Testing various format() methods");\r
-\r
- // final double d = -10456.0037; // this appears as -10456.003700000001 on NT\r
- // final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT\r
- final double d = -10456.00370000000000; // this works!\r
- final long l = 100000000;\r
- logln("" + Double.toString(d) + " is the double value");\r
-\r
- StringBuffer res1 = new StringBuffer();\r
- StringBuffer res2 = new StringBuffer();\r
- StringBuffer res3 = new StringBuffer();\r
- StringBuffer res4 = new StringBuffer();\r
- FieldPosition pos1 = new FieldPosition(0);\r
- FieldPosition pos2 = new FieldPosition(0);\r
- FieldPosition pos3 = new FieldPosition(0);\r
- FieldPosition pos4 = new FieldPosition(0);\r
-\r
- res1 = def.format(d, res1, pos1);\r
- logln("" + Double.toString(d) + " formatted to " + res1);\r
-\r
- res2 = pat.format(l, res2, pos2);\r
- logln("" + l + " formatted to " + res2);\r
-\r
- res3 = cust1.format(d, res3, pos3);\r
- logln("" + Double.toString(d) + " formatted to " + res3);\r
-\r
- res4 = cust1.format(l, res4, pos4);\r
- logln("" + l + " formatted to " + res4);\r
-\r
- // ======= Test parse()\r
-\r
- logln("Testing parse()");\r
-\r
- String text = new String("-10,456.0037");\r
- ParsePosition pos = new ParsePosition(0);\r
- String patt = new String("#,##0.#");\r
- pat.applyPattern(patt);\r
- double d2 = pat.parse(text, pos).doubleValue();\r
- if (d2 != d) {\r
- errln(\r
- "ERROR: Roundtrip failed (via parse(" + Double.toString(d2) + " != " + Double.toString(d) + ")) for " + text); \r
- }\r
- logln(text + " parsed into " + (long) d2);\r
-\r
- // ======= Test getters and setters\r
-\r
- logln("Testing getters and setters");\r
-\r
- final DecimalFormatSymbols syms = pat.getDecimalFormatSymbols();\r
- def.setDecimalFormatSymbols(syms);\r
- if (!pat.getDecimalFormatSymbols().equals(def.getDecimalFormatSymbols())) {\r
- errln("ERROR: set DecimalFormatSymbols() failed");\r
- }\r
-\r
- String posPrefix;\r
- pat.setPositivePrefix("+");\r
- posPrefix = pat.getPositivePrefix();\r
- logln("Positive prefix (should be +): " + posPrefix);\r
- if (posPrefix != "+") {\r
- errln("ERROR: setPositivePrefix() failed");\r
- }\r
-\r
- String negPrefix;\r
- pat.setNegativePrefix("-");\r
- negPrefix = pat.getNegativePrefix();\r
- logln("Negative prefix (should be -): " + negPrefix);\r
- if (negPrefix != "-") {\r
- errln("ERROR: setNegativePrefix() failed");\r
- }\r
-\r
- String posSuffix;\r
- pat.setPositiveSuffix("_");\r
- posSuffix = pat.getPositiveSuffix();\r
- logln("Positive suffix (should be _): " + posSuffix);\r
- if (posSuffix != "_") {\r
- errln("ERROR: setPositiveSuffix() failed");\r
- }\r
-\r
- String negSuffix;\r
- pat.setNegativeSuffix("~");\r
- negSuffix = pat.getNegativeSuffix();\r
- logln("Negative suffix (should be ~): " + negSuffix);\r
- if (negSuffix != "~") {\r
- errln("ERROR: setNegativeSuffix() failed");\r
- }\r
-\r
- long multiplier = 0;\r
- pat.setMultiplier(8);\r
- multiplier = pat.getMultiplier();\r
- logln("Multiplier (should be 8): " + multiplier);\r
- if (multiplier != 8) {\r
- errln("ERROR: setMultiplier() failed");\r
- }\r
-\r
- int groupingSize = 0;\r
- pat.setGroupingSize(2);\r
- groupingSize = pat.getGroupingSize();\r
- logln("Grouping size (should be 2): " + (long) groupingSize);\r
- if (groupingSize != 2) {\r
- errln("ERROR: setGroupingSize() failed");\r
- }\r
-\r
- pat.setDecimalSeparatorAlwaysShown(true);\r
- boolean tf = pat.isDecimalSeparatorAlwaysShown();\r
- logln(\r
- "DecimalSeparatorIsAlwaysShown (should be true) is " + (tf ? "true" : "false")); \r
- if (tf != true) {\r
- errln("ERROR: setDecimalSeparatorAlwaysShown() failed");\r
- }\r
-\r
- String funkyPat;\r
- funkyPat = pat.toPattern();\r
- logln("Pattern is " + funkyPat);\r
-\r
- String locPat;\r
- locPat = pat.toLocalizedPattern();\r
- logln("Localized pattern is " + locPat);\r
-\r
- // ======= Test applyPattern()\r
-\r
- logln("Testing applyPattern()");\r
-\r
- String p1 = new String("#,##0.0#;(#,##0.0#)");\r
- logln("Applying pattern " + p1);\r
- pat.applyPattern(p1);\r
- String s2;\r
- s2 = pat.toPattern();\r
- logln("Extracted pattern is " + s2);\r
- if (!s2.equals(p1)) {\r
- errln("ERROR: toPattern() result did not match pattern applied");\r
- }\r
-\r
- String p2 = new String("#,##0.0# FF;(#,##0.0# FF)");\r
- logln("Applying pattern " + p2);\r
- pat.applyLocalizedPattern(p2);\r
- String s3;\r
- s3 = pat.toLocalizedPattern();\r
- logln("Extracted pattern is " + s3);\r
- if (!s3.equals(p2)) {\r
- errln("ERROR: toLocalizedPattern() result did not match pattern applied");\r
- }\r
-\r
- // ======= Test getStaticClassID()\r
-\r
- // logln("Testing instanceof()");\r
-\r
- // try {\r
- // NumberFormat test = new DecimalFormat();\r
-\r
- // if (! (test instanceof DecimalFormat)) {\r
- // errln("ERROR: instanceof failed");\r
- // }\r
- // }\r
- // catch (Exception e) {\r
- // errln("ERROR: Couldn't create a DecimalFormat");\r
- // }\r
- \r
- }\r
-\r
- public void TestRounding() {\r
- double Roundingnumber = 2.55;\r
- double Roundingnumber1 = -2.55;\r
- //+2.55 results -2.55 results\r
- double result[] = {\r
- 3, -3, \r
- 2, -2, \r
- 3, -2, \r
- 2, -3, \r
- 3, -3, \r
- 3, -3, \r
- 3, -3 \r
- };\r
- DecimalFormat pat = new DecimalFormat();\r
- String s = "";\r
- s = pat.toPattern();\r
- logln("pattern = " + s);\r
- int mode;\r
- int i = 0;\r
- String message;\r
- String resultStr;\r
- for (mode = 0; mode < 7; mode++) {\r
- pat.setRoundingMode(mode);\r
- if (pat.getRoundingMode() != mode) {\r
- errln(\r
- "SetRoundingMode or GetRoundingMode failed for mode=" + mode); \r
- }\r
-\r
- //for +2.55 with RoundingIncrement=1.0\r
- pat.setRoundingIncrement(1.0);\r
- resultStr = pat.format(Roundingnumber);\r
- message = "round(" + (double) Roundingnumber\r
- + "," + mode + ",FALSE) with RoundingIncrement=1.0==>"; \r
- verify(message, resultStr, result[i++]);\r
- message = "";\r
- resultStr = "";\r
-\r
- //for -2.55 with RoundingIncrement=1.0\r
- resultStr = pat.format(Roundingnumber1);\r
- message = "round(" + (double) Roundingnumber1\r
- + "," + mode + ",FALSE) with RoundingIncrement=1.0==>"; \r
- verify(message, resultStr, result[i++]);\r
- message = "";\r
- resultStr = "";\r
- }\r
- }\r
-\r
-//#if defined(FOUNDATION10) || defined(J2SE13)\r
-//#else\r
- public void testFormatToCharacterIterator() {\r
-\r
- Number number = new Double(350.76);\r
- Number negativeNumber = new Double(-350.76);\r
-\r
- Locale us = Locale.US;\r
-\r
- // test number instance\r
- t_Format(1, number, NumberFormat.getNumberInstance(us),\r
- getNumberVectorUS());\r
-\r
- // test percent instance\r
- t_Format(3, number, NumberFormat.getPercentInstance(us),\r
- getPercentVectorUS());\r
-\r
- // test permille pattern\r
- DecimalFormat format = new DecimalFormat("###0.##\u2030");\r
- t_Format(4, number, format, getPermilleVector());\r
-\r
- // test exponential pattern with positive exponent\r
- format = new DecimalFormat("00.0#E0");\r
- t_Format(5, number, format, getPositiveExponentVector());\r
-\r
- // test exponential pattern with negative exponent\r
- format = new DecimalFormat("0000.0#E0");\r
- t_Format(6, number, format, getNegativeExponentVector());\r
-\r
- // test currency instance with US Locale\r
- t_Format(7, number, NumberFormat.getCurrencyInstance(us),\r
- getPositiveCurrencyVectorUS());\r
-\r
- // test negative currency instance with US Locale\r
- t_Format(8, negativeNumber, NumberFormat.getCurrencyInstance(us),\r
- getNegativeCurrencyVectorUS());\r
-\r
- // test multiple grouping seperators\r
- number = new Long(100300400);\r
- t_Format(11, number, NumberFormat.getNumberInstance(us),\r
- getNumberVector2US());\r
-\r
- // test 0\r
- number = new Long(0);\r
- t_Format(12, number, NumberFormat.getNumberInstance(us),\r
- getZeroVector());\r
- }\r
-\r
- private static Vector getNumberVectorUS() {\r
- Vector v = new Vector();\r
- v.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER));\r
- v.add(new FieldContainer(3, 4, NumberFormat.Field.DECIMAL_SEPARATOR));\r
- v.add(new FieldContainer(4, 6, NumberFormat.Field.FRACTION));\r
- return v;\r
- }\r
- \r
-// private static Vector getPositiveCurrencyVectorTR() {\r
-// Vector v = new Vector();\r
-// v.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER));\r
-// v.add(new FieldContainer(4, 6, NumberFormat.Field.CURRENCY));\r
-// return v;\r
-// }\r
-//\r
-// private static Vector getNegativeCurrencyVectorTR() {\r
-// Vector v = new Vector();\r
-// v.add(new FieldContainer(0, 1, NumberFormat.Field.SIGN));\r
-// v.add(new FieldContainer(1, 4, NumberFormat.Field.INTEGER));\r
-// v.add(new FieldContainer(5, 7, NumberFormat.Field.CURRENCY));\r
-// return v;\r
-// }\r
-\r
- private static Vector getPositiveCurrencyVectorUS() {\r
- Vector v = new Vector();\r
- v.add(new FieldContainer(0, 1, NumberFormat.Field.CURRENCY));\r
- v.add(new FieldContainer(1, 4, NumberFormat.Field.INTEGER));\r
- v.add(new FieldContainer(4, 5, NumberFormat.Field.DECIMAL_SEPARATOR));\r
- v.add(new FieldContainer(5, 7, NumberFormat.Field.FRACTION));\r
- return v;\r
- }\r
-\r
- private static Vector getNegativeCurrencyVectorUS() {\r
- Vector v = new Vector();\r
- v.add(new FieldContainer(1, 2, NumberFormat.Field.CURRENCY));\r
- v.add(new FieldContainer(2, 5, NumberFormat.Field.INTEGER));\r
- v.add(new FieldContainer(5, 6, NumberFormat.Field.DECIMAL_SEPARATOR));\r
- v.add(new FieldContainer(6, 8, NumberFormat.Field.FRACTION));\r
- return v;\r
- }\r
-\r
- private static Vector getPercentVectorUS() {\r
- Vector v = new Vector();\r
- v.add(new FieldContainer(0, 2, NumberFormat.Field.INTEGER));\r
- v.add(new FieldContainer(2, 3, NumberFormat.Field.INTEGER));\r
- v.add(new FieldContainer(2, 3, NumberFormat.Field.GROUPING_SEPARATOR));\r
- v.add(new FieldContainer(3, 6, NumberFormat.Field.INTEGER));\r
- v.add(new FieldContainer(6, 7, NumberFormat.Field.PERCENT));\r
- return v;\r
- }\r
-\r
- private static Vector getPermilleVector() {\r
- Vector v = new Vector();\r
- v.add(new FieldContainer(0, 6, NumberFormat.Field.INTEGER));\r
- v.add(new FieldContainer(6, 7, NumberFormat.Field.PERMILLE));\r
- return v;\r
- }\r
-\r
- private static Vector getNegativeExponentVector() {\r
- Vector v = new Vector();\r
- v.add(new FieldContainer(0, 4, NumberFormat.Field.INTEGER));\r
- v.add(new FieldContainer(4, 5, NumberFormat.Field.DECIMAL_SEPARATOR));\r
- v.add(new FieldContainer(5, 6, NumberFormat.Field.FRACTION));\r
- v.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT_SYMBOL));\r
- v.add(new FieldContainer(7, 8, NumberFormat.Field.EXPONENT_SIGN));\r
- v.add(new FieldContainer(8, 9, NumberFormat.Field.EXPONENT));\r
- return v;\r
- }\r
-\r
- private static Vector getPositiveExponentVector() {\r
- Vector v = new Vector();\r
- v.add(new FieldContainer(0, 2, NumberFormat.Field.INTEGER));\r
- v.add(new FieldContainer(2, 3, NumberFormat.Field.DECIMAL_SEPARATOR));\r
- v.add(new FieldContainer(3, 5, NumberFormat.Field.FRACTION));\r
- v.add(new FieldContainer(5, 6, NumberFormat.Field.EXPONENT_SYMBOL));\r
- v.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT));\r
- return v;\r
- }\r
-\r
- private static Vector getNumberVector2US() {\r
- Vector v = new Vector();\r
- v.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER));\r
- v.add(new FieldContainer(3, 4, NumberFormat.Field.GROUPING_SEPARATOR));\r
- v.add(new FieldContainer(3, 4, NumberFormat.Field.INTEGER));\r
- v.add(new FieldContainer(4, 7, NumberFormat.Field.INTEGER));\r
- v.add(new FieldContainer(7, 8, NumberFormat.Field.GROUPING_SEPARATOR));\r
- v.add(new FieldContainer(7, 8, NumberFormat.Field.INTEGER));\r
- v.add(new FieldContainer(8, 11, NumberFormat.Field.INTEGER));\r
- return v;\r
- }\r
-\r
- private static Vector getZeroVector() {\r
- Vector v = new Vector();\r
- v.add(new FieldContainer(0, 1, NumberFormat.Field.INTEGER));\r
- return v;\r
- } \r
- \r
- private void t_Format(int count, Object object, Format format,\r
- Vector expectedResults) {\r
- Vector results = findFields(format.formatToCharacterIterator(object));\r
- assertTrue("Test " + count\r
- + ": Format returned incorrect CharacterIterator for "\r
- + format.format(object), compare(results, expectedResults));\r
- }\r
-\r
- /**\r
- * compares two vectors regardless of the order of their elements\r
- */\r
- private static boolean compare(Vector vector1, Vector vector2) {\r
- return vector1.size() == vector2.size() && vector1.containsAll(vector2);\r
- }\r
- \r
- /**\r
- * finds attributes with regards to char index in this\r
- * AttributedCharacterIterator, and puts them in a vector\r
- * \r
- * @param iterator\r
- * @return a vector, each entry in this vector are of type FieldContainer ,\r
- * which stores start and end indexes and an attribute this range\r
- * has\r
- */\r
- private static Vector findFields(AttributedCharacterIterator iterator) {\r
- Vector result = new Vector();\r
- while (iterator.getIndex() != iterator.getEndIndex()) {\r
- int start = iterator.getRunStart();\r
- int end = iterator.getRunLimit();\r
-\r
- Iterator it = iterator.getAttributes().keySet().iterator();\r
- while (it.hasNext()) {\r
- AttributedCharacterIterator.Attribute attribute = (AttributedCharacterIterator.Attribute) it\r
- .next();\r
- Object value = iterator.getAttribute(attribute);\r
- result.add(new FieldContainer(start, end, attribute, value));\r
- // System.out.println(start + " " + end + ": " + attribute + ",\r
- // " + value );\r
- // System.out.println("v.add(new FieldContainer(" + start +"," +\r
- // end +"," + attribute+ "," + value+ "));");\r
- }\r
- iterator.setIndex(end);\r
- }\r
- return result;\r
- }\r
- protected static class FieldContainer {\r
- int start, end;\r
-\r
- AttributedCharacterIterator.Attribute attribute;\r
-\r
- Object value;\r
-\r
-// called from support_decimalformat and support_simpledateformat tests\r
- public FieldContainer(int start, int end,\r
- AttributedCharacterIterator.Attribute attribute) {\r
- this(start, end, attribute, attribute);\r
- }\r
-\r
-// called from support_messageformat tests\r
- public FieldContainer(int start, int end, AttributedCharacterIterator.Attribute attribute, int value) {\r
- this(start, end, attribute, new Integer(value));\r
- }\r
-\r
-// called from support_messageformat tests\r
- public FieldContainer(int start, int end, AttributedCharacterIterator.Attribute attribute,\r
- Object value) {\r
- this.start = start;\r
- this.end = end;\r
- this.attribute = attribute;\r
- this.value = value;\r
- }\r
-\r
- public boolean equals(Object obj) {\r
- if (!(obj instanceof FieldContainer))\r
- return false;\r
-\r
- FieldContainer fc = (FieldContainer) obj;\r
- return (start == fc.start && end == fc.end\r
- && attribute == fc.attribute && value.equals(fc.value));\r
- }\r
- } \r
-//#endif\r
-\r
- /*Helper functions */\r
- public void verify(String message, String got, double expected) {\r
- logln(message + got + " Expected : " + (long)expected);\r
- String expectedStr = "";\r
- expectedStr=expectedStr + (long)expected;\r
- if(!got.equals(expectedStr) ) {\r
- errln("ERROR: Round() failed: " + message + got + " Expected : " + expectedStr);\r
- }\r
- }\r
-}\r
-//eof\r
+//##header J2SE15
+/*
+ *******************************************************************************
+ * Copyright (C) 2001-2009, International Business Machines Corporation and *
+ * others. All Rights Reserved. *
+ *******************************************************************************
+ */
+
+/**
+ * Port From: ICU4C v1.8.1 : format : IntlTestDecimalFormatAPI
+ * Source File: $ICU4CRoot/source/test/intltest/dcfmapts.cpp
+ **/
+
+package com.ibm.icu.dev.test.format;
+
+import java.text.AttributedCharacterIterator;
+import java.text.FieldPosition;
+import java.text.Format;
+import java.text.ParsePosition;
+import java.util.Iterator;
+import java.util.Locale;
+import java.util.Vector;
+
+import com.ibm.icu.text.DecimalFormat;
+import com.ibm.icu.text.DecimalFormatSymbols;
+import com.ibm.icu.text.NumberFormat;
+
+// This is an API test, not a unit test. It doesn't test very many cases, and doesn't
+// try to test the full functionality. It just calls each function in the class and
+// verifies that it works on a basic level.
+public class IntlTestDecimalFormatAPIC extends com.ibm.icu.dev.test.TestFmwk {
+
+ public static void main(String[] args) throws Exception {
+ new IntlTestDecimalFormatAPIC().run(args);
+ }
+
+ // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
+ public void TestAPI() {
+
+ logln("DecimalFormat API test---");
+ logln("");
+ Locale.setDefault(Locale.ENGLISH);
+
+ // ======= Test constructors
+
+ logln("Testing DecimalFormat constructors");
+
+ DecimalFormat def = new DecimalFormat();
+
+ final String pattern = new String("#,##0.# FF");
+ DecimalFormat pat = null;
+ try {
+ pat = new DecimalFormat(pattern);
+ } catch (IllegalArgumentException e) {
+ errln("ERROR: Could not create DecimalFormat (pattern)");
+ }
+
+ DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.FRENCH);
+
+ DecimalFormat cust1 = new DecimalFormat(pattern, symbols);
+
+ // ======= Test clone(), assignment, and equality
+
+ logln("Testing clone() and equality operators");
+
+ Format clone = (Format) def.clone();
+ if (!def.equals(clone)) {
+ errln("ERROR: Clone() failed");
+ }
+
+ // ======= Test various format() methods
+
+ logln("Testing various format() methods");
+
+ // final double d = -10456.0037; // this appears as -10456.003700000001 on NT
+ // final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT
+ final double d = -10456.00370000000000; // this works!
+ final long l = 100000000;
+ logln("" + Double.toString(d) + " is the double value");
+
+ StringBuffer res1 = new StringBuffer();
+ StringBuffer res2 = new StringBuffer();
+ StringBuffer res3 = new StringBuffer();
+ StringBuffer res4 = new StringBuffer();
+ FieldPosition pos1 = new FieldPosition(0);
+ FieldPosition pos2 = new FieldPosition(0);
+ FieldPosition pos3 = new FieldPosition(0);
+ FieldPosition pos4 = new FieldPosition(0);
+
+ res1 = def.format(d, res1, pos1);
+ logln("" + Double.toString(d) + " formatted to " + res1);
+
+ res2 = pat.format(l, res2, pos2);
+ logln("" + l + " formatted to " + res2);
+
+ res3 = cust1.format(d, res3, pos3);
+ logln("" + Double.toString(d) + " formatted to " + res3);
+
+ res4 = cust1.format(l, res4, pos4);
+ logln("" + l + " formatted to " + res4);
+
+ // ======= Test parse()
+
+ logln("Testing parse()");
+
+ String text = new String("-10,456.0037");
+ ParsePosition pos = new ParsePosition(0);
+ String patt = new String("#,##0.#");
+ pat.applyPattern(patt);
+ double d2 = pat.parse(text, pos).doubleValue();
+ if (d2 != d) {
+ errln(
+ "ERROR: Roundtrip failed (via parse(" + Double.toString(d2) + " != " + Double.toString(d) + ")) for " + text);
+ }
+ logln(text + " parsed into " + (long) d2);
+
+ // ======= Test getters and setters
+
+ logln("Testing getters and setters");
+
+ final DecimalFormatSymbols syms = pat.getDecimalFormatSymbols();
+ def.setDecimalFormatSymbols(syms);
+ if (!pat.getDecimalFormatSymbols().equals(def.getDecimalFormatSymbols())) {
+ errln("ERROR: set DecimalFormatSymbols() failed");
+ }
+
+ String posPrefix;
+ pat.setPositivePrefix("+");
+ posPrefix = pat.getPositivePrefix();
+ logln("Positive prefix (should be +): " + posPrefix);
+ if (posPrefix != "+") {
+ errln("ERROR: setPositivePrefix() failed");
+ }
+
+ String negPrefix;
+ pat.setNegativePrefix("-");
+ negPrefix = pat.getNegativePrefix();
+ logln("Negative prefix (should be -): " + negPrefix);
+ if (negPrefix != "-") {
+ errln("ERROR: setNegativePrefix() failed");
+ }
+
+ String posSuffix;
+ pat.setPositiveSuffix("_");
+ posSuffix = pat.getPositiveSuffix();
+ logln("Positive suffix (should be _): " + posSuffix);
+ if (posSuffix != "_") {
+ errln("ERROR: setPositiveSuffix() failed");
+ }
+
+ String negSuffix;
+ pat.setNegativeSuffix("~");
+ negSuffix = pat.getNegativeSuffix();
+ logln("Negative suffix (should be ~): " + negSuffix);
+ if (negSuffix != "~") {
+ errln("ERROR: setNegativeSuffix() failed");
+ }
+
+ long multiplier = 0;
+ pat.setMultiplier(8);
+ multiplier = pat.getMultiplier();
+ logln("Multiplier (should be 8): " + multiplier);
+ if (multiplier != 8) {
+ errln("ERROR: setMultiplier() failed");
+ }
+
+ int groupingSize = 0;
+ pat.setGroupingSize(2);
+ groupingSize = pat.getGroupingSize();
+ logln("Grouping size (should be 2): " + (long) groupingSize);
+ if (groupingSize != 2) {
+ errln("ERROR: setGroupingSize() failed");
+ }
+
+ pat.setDecimalSeparatorAlwaysShown(true);
+ boolean tf = pat.isDecimalSeparatorAlwaysShown();
+ logln(
+ "DecimalSeparatorIsAlwaysShown (should be true) is " + (tf ? "true" : "false"));
+ if (tf != true) {
+ errln("ERROR: setDecimalSeparatorAlwaysShown() failed");
+ }
+
+ String funkyPat;
+ funkyPat = pat.toPattern();
+ logln("Pattern is " + funkyPat);
+
+ String locPat;
+ locPat = pat.toLocalizedPattern();
+ logln("Localized pattern is " + locPat);
+
+ // ======= Test applyPattern()
+
+ logln("Testing applyPattern()");
+
+ String p1 = new String("#,##0.0#;(#,##0.0#)");
+ logln("Applying pattern " + p1);
+ pat.applyPattern(p1);
+ String s2;
+ s2 = pat.toPattern();
+ logln("Extracted pattern is " + s2);
+ if (!s2.equals(p1)) {
+ errln("ERROR: toPattern() result did not match pattern applied");
+ }
+
+ String p2 = new String("#,##0.0# FF;(#,##0.0# FF)");
+ logln("Applying pattern " + p2);
+ pat.applyLocalizedPattern(p2);
+ String s3;
+ s3 = pat.toLocalizedPattern();
+ logln("Extracted pattern is " + s3);
+ if (!s3.equals(p2)) {
+ errln("ERROR: toLocalizedPattern() result did not match pattern applied");
+ }
+
+ // ======= Test getStaticClassID()
+
+ // logln("Testing instanceof()");
+
+ // try {
+ // NumberFormat test = new DecimalFormat();
+
+ // if (! (test instanceof DecimalFormat)) {
+ // errln("ERROR: instanceof failed");
+ // }
+ // }
+ // catch (Exception e) {
+ // errln("ERROR: Couldn't create a DecimalFormat");
+ // }
+
+ }
+
+ public void TestRounding() {
+ double Roundingnumber = 2.55;
+ double Roundingnumber1 = -2.55;
+ //+2.55 results -2.55 results
+ double result[] = {
+ 3, -3,
+ 2, -2,
+ 3, -2,
+ 2, -3,
+ 3, -3,
+ 3, -3,
+ 3, -3
+ };
+ DecimalFormat pat = new DecimalFormat();
+ String s = "";
+ s = pat.toPattern();
+ logln("pattern = " + s);
+ int mode;
+ int i = 0;
+ String message;
+ String resultStr;
+ for (mode = 0; mode < 7; mode++) {
+ pat.setRoundingMode(mode);
+ if (pat.getRoundingMode() != mode) {
+ errln(
+ "SetRoundingMode or GetRoundingMode failed for mode=" + mode);
+ }
+
+ //for +2.55 with RoundingIncrement=1.0
+ pat.setRoundingIncrement(1.0);
+ resultStr = pat.format(Roundingnumber);
+ message = "round(" + (double) Roundingnumber
+ + "," + mode + ",FALSE) with RoundingIncrement=1.0==>";
+ verify(message, resultStr, result[i++]);
+ message = "";
+ resultStr = "";
+
+ //for -2.55 with RoundingIncrement=1.0
+ resultStr = pat.format(Roundingnumber1);
+ message = "round(" + (double) Roundingnumber1
+ + "," + mode + ",FALSE) with RoundingIncrement=1.0==>";
+ verify(message, resultStr, result[i++]);
+ message = "";
+ resultStr = "";
+ }
+ }
+
+//#if defined(FOUNDATION10) || defined(J2SE13)
+//#else
+ public void testFormatToCharacterIterator() {
+
+ Number number = new Double(350.76);
+ Number negativeNumber = new Double(-350.76);
+
+ Locale us = Locale.US;
+
+ // test number instance
+ t_Format(1, number, NumberFormat.getNumberInstance(us),
+ getNumberVectorUS());
+
+ // test percent instance
+ t_Format(3, number, NumberFormat.getPercentInstance(us),
+ getPercentVectorUS());
+
+ // test permille pattern
+ DecimalFormat format = new DecimalFormat("###0.##\u2030");
+ t_Format(4, number, format, getPermilleVector());
+
+ // test exponential pattern with positive exponent
+ format = new DecimalFormat("00.0#E0");
+ t_Format(5, number, format, getPositiveExponentVector());
+
+ // test exponential pattern with negative exponent
+ format = new DecimalFormat("0000.0#E0");
+ t_Format(6, number, format, getNegativeExponentVector());
+
+ // test currency instance with US Locale
+ t_Format(7, number, NumberFormat.getCurrencyInstance(us),
+ getPositiveCurrencyVectorUS());
+
+ // test negative currency instance with US Locale
+ t_Format(8, negativeNumber, NumberFormat.getCurrencyInstance(us),
+ getNegativeCurrencyVectorUS());
+
+ // test multiple grouping seperators
+ number = new Long(100300400);
+ t_Format(11, number, NumberFormat.getNumberInstance(us),
+ getNumberVector2US());
+
+ // test 0
+ number = new Long(0);
+ t_Format(12, number, NumberFormat.getNumberInstance(us),
+ getZeroVector());
+ }
+
+ private static Vector getNumberVectorUS() {
+ Vector v = new Vector();
+ v.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER));
+ v.add(new FieldContainer(3, 4, NumberFormat.Field.DECIMAL_SEPARATOR));
+ v.add(new FieldContainer(4, 6, NumberFormat.Field.FRACTION));
+ return v;
+ }
+
+// private static Vector getPositiveCurrencyVectorTR() {
+// Vector v = new Vector();
+// v.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER));
+// v.add(new FieldContainer(4, 6, NumberFormat.Field.CURRENCY));
+// return v;
+// }
+//
+// private static Vector getNegativeCurrencyVectorTR() {
+// Vector v = new Vector();
+// v.add(new FieldContainer(0, 1, NumberFormat.Field.SIGN));
+// v.add(new FieldContainer(1, 4, NumberFormat.Field.INTEGER));
+// v.add(new FieldContainer(5, 7, NumberFormat.Field.CURRENCY));
+// return v;
+// }
+
+ private static Vector getPositiveCurrencyVectorUS() {
+ Vector v = new Vector();
+ v.add(new FieldContainer(0, 1, NumberFormat.Field.CURRENCY));
+ v.add(new FieldContainer(1, 4, NumberFormat.Field.INTEGER));
+ v.add(new FieldContainer(4, 5, NumberFormat.Field.DECIMAL_SEPARATOR));
+ v.add(new FieldContainer(5, 7, NumberFormat.Field.FRACTION));
+ return v;
+ }
+
+ private static Vector getNegativeCurrencyVectorUS() {
+ Vector v = new Vector();
+ v.add(new FieldContainer(1, 2, NumberFormat.Field.CURRENCY));
+ v.add(new FieldContainer(2, 5, NumberFormat.Field.INTEGER));
+ v.add(new FieldContainer(5, 6, NumberFormat.Field.DECIMAL_SEPARATOR));
+ v.add(new FieldContainer(6, 8, NumberFormat.Field.FRACTION));
+ return v;
+ }
+
+ private static Vector getPercentVectorUS() {
+ Vector v = new Vector();
+ v.add(new FieldContainer(0, 2, NumberFormat.Field.INTEGER));
+ v.add(new FieldContainer(2, 3, NumberFormat.Field.INTEGER));
+ v.add(new FieldContainer(2, 3, NumberFormat.Field.GROUPING_SEPARATOR));
+ v.add(new FieldContainer(3, 6, NumberFormat.Field.INTEGER));
+ v.add(new FieldContainer(6, 7, NumberFormat.Field.PERCENT));
+ return v;
+ }
+
+ private static Vector getPermilleVector() {
+ Vector v = new Vector();
+ v.add(new FieldContainer(0, 6, NumberFormat.Field.INTEGER));
+ v.add(new FieldContainer(6, 7, NumberFormat.Field.PERMILLE));
+ return v;
+ }
+
+ private static Vector getNegativeExponentVector() {
+ Vector v = new Vector();
+ v.add(new FieldContainer(0, 4, NumberFormat.Field.INTEGER));
+ v.add(new FieldContainer(4, 5, NumberFormat.Field.DECIMAL_SEPARATOR));
+ v.add(new FieldContainer(5, 6, NumberFormat.Field.FRACTION));
+ v.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT_SYMBOL));
+ v.add(new FieldContainer(7, 8, NumberFormat.Field.EXPONENT_SIGN));
+ v.add(new FieldContainer(8, 9, NumberFormat.Field.EXPONENT));
+ return v;
+ }
+
+ private static Vector getPositiveExponentVector() {
+ Vector v = new Vector();
+ v.add(new FieldContainer(0, 2, NumberFormat.Field.INTEGER));
+ v.add(new FieldContainer(2, 3, NumberFormat.Field.DECIMAL_SEPARATOR));
+ v.add(new FieldContainer(3, 5, NumberFormat.Field.FRACTION));
+ v.add(new FieldContainer(5, 6, NumberFormat.Field.EXPONENT_SYMBOL));
+ v.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT));
+ return v;
+ }
+
+ private static Vector getNumberVector2US() {
+ Vector v = new Vector();
+ v.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER));
+ v.add(new FieldContainer(3, 4, NumberFormat.Field.GROUPING_SEPARATOR));
+ v.add(new FieldContainer(3, 4, NumberFormat.Field.INTEGER));
+ v.add(new FieldContainer(4, 7, NumberFormat.Field.INTEGER));
+ v.add(new FieldContainer(7, 8, NumberFormat.Field.GROUPING_SEPARATOR));
+ v.add(new FieldContainer(7, 8, NumberFormat.Field.INTEGER));
+ v.add(new FieldContainer(8, 11, NumberFormat.Field.INTEGER));
+ return v;
+ }
+
+ private static Vector getZeroVector() {
+ Vector v = new Vector();
+ v.add(new FieldContainer(0, 1, NumberFormat.Field.INTEGER));
+ return v;
+ }
+
+ private void t_Format(int count, Object object, Format format,
+ Vector expectedResults) {
+ Vector results = findFields(format.formatToCharacterIterator(object));
+ assertTrue("Test " + count
+ + ": Format returned incorrect CharacterIterator for "
+ + format.format(object), compare(results, expectedResults));
+ }
+
+ /**
+ * compares two vectors regardless of the order of their elements
+ */
+ private static boolean compare(Vector vector1, Vector vector2) {
+ return vector1.size() == vector2.size() && vector1.containsAll(vector2);
+ }
+
+ /**
+ * finds attributes with regards to char index in this
+ * AttributedCharacterIterator, and puts them in a vector
+ *
+ * @param iterator
+ * @return a vector, each entry in this vector are of type FieldContainer ,
+ * which stores start and end indexes and an attribute this range
+ * has
+ */
+ private static Vector findFields(AttributedCharacterIterator iterator) {
+ Vector result = new Vector();
+ while (iterator.getIndex() != iterator.getEndIndex()) {
+ int start = iterator.getRunStart();
+ int end = iterator.getRunLimit();
+
+ Iterator it = iterator.getAttributes().keySet().iterator();
+ while (it.hasNext()) {
+ AttributedCharacterIterator.Attribute attribute = (AttributedCharacterIterator.Attribute) it
+ .next();
+ Object value = iterator.getAttribute(attribute);
+ result.add(new FieldContainer(start, end, attribute, value));
+ // System.out.println(start + " " + end + ": " + attribute + ",
+ // " + value );
+ // System.out.println("v.add(new FieldContainer(" + start +"," +
+ // end +"," + attribute+ "," + value+ "));");
+ }
+ iterator.setIndex(end);
+ }
+ return result;
+ }
+ protected static class FieldContainer {
+ int start, end;
+
+ AttributedCharacterIterator.Attribute attribute;
+
+ Object value;
+
+// called from support_decimalformat and support_simpledateformat tests
+ public FieldContainer(int start, int end,
+ AttributedCharacterIterator.Attribute attribute) {
+ this(start, end, attribute, attribute);
+ }
+
+// called from support_messageformat tests
+ public FieldContainer(int start, int end, AttributedCharacterIterator.Attribute attribute, int value) {
+ this(start, end, attribute, new Integer(value));
+ }
+
+// called from support_messageformat tests
+ public FieldContainer(int start, int end, AttributedCharacterIterator.Attribute attribute,
+ Object value) {
+ this.start = start;
+ this.end = end;
+ this.attribute = attribute;
+ this.value = value;
+ }
+
+ public boolean equals(Object obj) {
+ if (!(obj instanceof FieldContainer))
+ return false;
+
+ FieldContainer fc = (FieldContainer) obj;
+ return (start == fc.start && end == fc.end
+ && attribute == fc.attribute && value.equals(fc.value));
+ }
+ }
+//#endif
+
+ /*Helper functions */
+ public void verify(String message, String got, double expected) {
+ logln(message + got + " Expected : " + (long)expected);
+ String expectedStr = "";
+ expectedStr=expectedStr + (long)expected;
+ if(!got.equals(expectedStr) ) {
+ errln("ERROR: Round() failed: " + message + got + " Expected : " + expectedStr);
+ }
+ }
+}
+//eof