001: /*****************************************************************************************
002: *
003: * Copyright (C) 1996-2004, International Business Machines
004: * Corporation and others. All Rights Reserved.
005: **/
006:
007: /**
008: * Port From: JDK 1.4b1 : java.text.Format.IntlTestDecimalFormatAPI
009: * Source File: java/text/format/IntlTestDecimalFormatAPI.java
010: **/package com.ibm.icu.dev.test.format;
011:
012: import com.ibm.icu.text.*;
013: import com.ibm.icu.math.BigDecimal;
014: import java.util.Locale;
015: import java.text.ParsePosition;
016: import java.text.Format;
017: import java.text.FieldPosition;
018:
019: public class IntlTestDecimalFormatAPI extends
020: com.ibm.icu.dev.test.TestFmwk {
021: public static void main(String[] args) throws Exception {
022: new IntlTestDecimalFormatAPI().run(args);
023: }
024:
025: /**
026: * Problem 1: simply running
027: * decF4.setRoundingMode(java.math.BigDecimal.ROUND_HALF_UP) does not work
028: * as decF4.setRoundingIncrement(.0001) must also be run.
029: * Problem 2: decF4.format(8.88885) does not return 8.8889 as expected.
030: * You must run decF4.format(new BigDecimal(Double.valueOf(8.88885))) in
031: * order for this to work as expected.
032: * Problem 3: There seems to be no way to set half up to be the default
033: * rounding mode.
034: * We solved the problem with the code at the bottom of this page however
035: * this is not quite general purpose enough to include in icu4j. A static
036: * setDefaultRoundingMode function would solve the problem nicely. Also
037: * decimal places past 20 are not handled properly. A small ammount of work
038: * would make bring this up to snuff.
039: */
040: public void testJB1871() {
041: // problem 2
042: double number = 8.88885;
043: String expected = "8.8889";
044:
045: String pat = ",##0.0000";
046: DecimalFormat dec = new DecimalFormat(pat);
047: dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);
048: double roundinginc = 0.0001;
049: dec.setRoundingIncrement(roundinginc);
050: String str = dec.format(number);
051: if (!str.equals(expected)) {
052: errln("Fail: " + number + " x \"" + pat + "\" = \"" + str
053: + "\", expected \"" + expected + "\"");
054: }
055:
056: pat = ",##0.0001";
057: dec = new DecimalFormat(pat);
058: dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);
059: str = dec.format(number);
060: if (!str.equals(expected)) {
061: errln("Fail: " + number + " x \"" + pat + "\" = \"" + str
062: + "\", expected \"" + expected + "\"");
063: }
064:
065: // testing 20 decimal places
066: pat = ",##0.00000000000000000001";
067: dec = new DecimalFormat(pat);
068: BigDecimal bignumber = new BigDecimal("8.888888888888888888885");
069: expected = "8.88888888888888888889";
070:
071: dec.setRoundingMode(BigDecimal.ROUND_HALF_UP);
072: str = dec.format(bignumber);
073: if (!str.equals(expected)) {
074: errln("Fail: " + bignumber + " x \"" + pat + "\" = \""
075: + str + "\", expected \"" + expected + "\"");
076: }
077:
078: }
079:
080: /**
081: * This test checks various generic API methods in DecimalFormat to achieve
082: * 100% API coverage.
083: */
084: public void TestAPI() {
085: logln("DecimalFormat API test---");
086: logln("");
087: Locale.setDefault(Locale.ENGLISH);
088:
089: // ======= Test constructors
090:
091: logln("Testing DecimalFormat constructors");
092:
093: DecimalFormat def = new DecimalFormat();
094:
095: final String pattern = new String("#,##0.# FF");
096: DecimalFormat pat = null;
097: try {
098: pat = new DecimalFormat(pattern);
099: } catch (IllegalArgumentException e) {
100: errln("ERROR: Could not create DecimalFormat (pattern)");
101: }
102:
103: DecimalFormatSymbols symbols = new DecimalFormatSymbols(
104: Locale.FRENCH);
105:
106: DecimalFormat cust1 = new DecimalFormat(pattern, symbols);
107:
108: // ======= Test clone(), assignment, and equality
109:
110: logln("Testing clone() and equality operators");
111:
112: Format clone = (Format) def.clone();
113: if (!def.equals(clone)) {
114: errln("ERROR: Clone() failed");
115: }
116:
117: // ======= Test various format() methods
118:
119: logln("Testing various format() methods");
120:
121: // final double d = -10456.0037; // this appears as -10456.003700000001 on NT
122: // final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT
123: final double d = -10456.00370000000000; // this works!
124: final long l = 100000000;
125: logln("" + d + " is the double value");
126:
127: StringBuffer res1 = new StringBuffer();
128: StringBuffer res2 = new StringBuffer();
129: StringBuffer res3 = new StringBuffer();
130: StringBuffer res4 = new StringBuffer();
131: FieldPosition pos1 = new FieldPosition(0);
132: FieldPosition pos2 = new FieldPosition(0);
133: FieldPosition pos3 = new FieldPosition(0);
134: FieldPosition pos4 = new FieldPosition(0);
135:
136: res1 = def.format(d, res1, pos1);
137: logln("" + d + " formatted to " + res1);
138:
139: res2 = pat.format(l, res2, pos2);
140: logln("" + l + " formatted to " + res2);
141:
142: res3 = cust1.format(d, res3, pos3);
143: logln("" + d + " formatted to " + res3);
144:
145: res4 = cust1.format(l, res4, pos4);
146: logln("" + l + " formatted to " + res4);
147:
148: // ======= Test parse()
149:
150: logln("Testing parse()");
151:
152: String text = new String("-10,456.0037");
153: ParsePosition pos = new ParsePosition(0);
154: String patt = new String("#,##0.#");
155: pat.applyPattern(patt);
156: double d2 = pat.parse(text, pos).doubleValue();
157: if (d2 != d) {
158: errln("ERROR: Roundtrip failed (via parse(" + d2 + " != "
159: + d + ")) for " + text);
160: }
161: logln(text + " parsed into " + (long) d2);
162:
163: // ======= Test getters and setters
164:
165: logln("Testing getters and setters");
166:
167: final DecimalFormatSymbols syms = pat.getDecimalFormatSymbols();
168: def.setDecimalFormatSymbols(syms);
169: if (!pat.getDecimalFormatSymbols().equals(
170: def.getDecimalFormatSymbols())) {
171: errln("ERROR: set DecimalFormatSymbols() failed");
172: }
173:
174: String posPrefix;
175: pat.setPositivePrefix("+");
176: posPrefix = pat.getPositivePrefix();
177: logln("Positive prefix (should be +): " + posPrefix);
178: if (posPrefix != "+") {
179: errln("ERROR: setPositivePrefix() failed");
180: }
181:
182: String negPrefix;
183: pat.setNegativePrefix("-");
184: negPrefix = pat.getNegativePrefix();
185: logln("Negative prefix (should be -): " + negPrefix);
186: if (negPrefix != "-") {
187: errln("ERROR: setNegativePrefix() failed");
188: }
189:
190: String posSuffix;
191: pat.setPositiveSuffix("_");
192: posSuffix = pat.getPositiveSuffix();
193: logln("Positive suffix (should be _): " + posSuffix);
194: if (posSuffix != "_") {
195: errln("ERROR: setPositiveSuffix() failed");
196: }
197:
198: String negSuffix;
199: pat.setNegativeSuffix("~");
200: negSuffix = pat.getNegativeSuffix();
201: logln("Negative suffix (should be ~): " + negSuffix);
202: if (negSuffix != "~") {
203: errln("ERROR: setNegativeSuffix() failed");
204: }
205:
206: long multiplier = 0;
207: pat.setMultiplier(8);
208: multiplier = pat.getMultiplier();
209: logln("Multiplier (should be 8): " + multiplier);
210: if (multiplier != 8) {
211: errln("ERROR: setMultiplier() failed");
212: }
213:
214: int groupingSize = 0;
215: pat.setGroupingSize(2);
216: groupingSize = pat.getGroupingSize();
217: logln("Grouping size (should be 2): " + (long) groupingSize);
218: if (groupingSize != 2) {
219: errln("ERROR: setGroupingSize() failed");
220: }
221:
222: pat.setDecimalSeparatorAlwaysShown(true);
223: boolean tf = pat.isDecimalSeparatorAlwaysShown();
224: logln("DecimalSeparatorIsAlwaysShown (should be true) is "
225: + (tf ? "true" : "false"));
226: if (tf != true) {
227: errln("ERROR: setDecimalSeparatorAlwaysShown() failed");
228: }
229:
230: String funkyPat;
231: funkyPat = pat.toPattern();
232: logln("Pattern is " + funkyPat);
233:
234: String locPat;
235: locPat = pat.toLocalizedPattern();
236: logln("Localized pattern is " + locPat);
237:
238: // ======= Test applyPattern()
239:
240: logln("Testing applyPattern()");
241:
242: String p1 = new String("#,##0.0#;(#,##0.0#)");
243: logln("Applying pattern " + p1);
244: pat.applyPattern(p1);
245: String s2;
246: s2 = pat.toPattern();
247: logln("Extracted pattern is " + s2);
248: if (!s2.equals(p1)) {
249: errln("ERROR: toPattern() result did not match pattern applied");
250: }
251:
252: String p2 = new String("#,##0.0# FF;(#,##0.0# FF)");
253: logln("Applying pattern " + p2);
254: pat.applyLocalizedPattern(p2);
255: String s3;
256: s3 = pat.toLocalizedPattern();
257: logln("Extracted pattern is " + s3);
258: if (!s3.equals(p2)) {
259: errln("ERROR: toLocalizedPattern() result did not match pattern applied");
260: }
261: }
262: }
|