2 *******************************************************************************
\r
3 * Copyright (C) 2001-2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
9 * Port From: ICU4C v1.8.1 : format : IntlTestDecimalFormatAPI
\r
10 * Source File: $ICU4CRoot/source/test/intltest/dcfmapts.cpp
\r
13 package com.ibm.icu.dev.test.format;
\r
15 import java.text.AttributedCharacterIterator;
\r
16 import java.text.FieldPosition;
\r
17 import java.text.Format;
\r
18 import java.text.ParsePosition;
\r
19 import java.util.Iterator;
\r
20 import java.util.Locale;
\r
21 import java.util.Vector;
\r
23 import com.ibm.icu.text.DecimalFormat;
\r
24 import com.ibm.icu.text.DecimalFormatSymbols;
\r
25 import com.ibm.icu.text.NumberFormat;
\r
27 // This is an API test, not a unit test. It doesn't test very many cases, and doesn't
\r
28 // try to test the full functionality. It just calls each function in the class and
\r
29 // verifies that it works on a basic level.
\r
30 public class IntlTestDecimalFormatAPIC extends com.ibm.icu.dev.test.TestFmwk {
\r
32 public static void main(String[] args) throws Exception {
\r
33 new IntlTestDecimalFormatAPIC().run(args);
\r
36 // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
\r
37 public void TestAPI() {
\r
39 logln("DecimalFormat API test---");
\r
41 Locale.setDefault(Locale.ENGLISH);
\r
43 // ======= Test constructors
\r
45 logln("Testing DecimalFormat constructors");
\r
47 DecimalFormat def = new DecimalFormat();
\r
49 final String pattern = new String("#,##0.# FF");
\r
50 DecimalFormat pat = null;
\r
52 pat = new DecimalFormat(pattern);
\r
53 } catch (IllegalArgumentException e) {
\r
54 errln("ERROR: Could not create DecimalFormat (pattern)");
\r
57 DecimalFormatSymbols symbols = new DecimalFormatSymbols(Locale.FRENCH);
\r
59 DecimalFormat cust1 = new DecimalFormat(pattern, symbols);
\r
61 // ======= Test clone(), assignment, and equality
\r
63 logln("Testing clone() and equality operators");
\r
65 Format clone = (Format) def.clone();
\r
66 if (!def.equals(clone)) {
\r
67 errln("ERROR: Clone() failed");
\r
70 // ======= Test various format() methods
\r
72 logln("Testing various format() methods");
\r
74 // final double d = -10456.0037; // this appears as -10456.003700000001 on NT
\r
75 // final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT
\r
76 final double d = -10456.00370000000000; // this works!
\r
77 final long l = 100000000;
\r
78 logln("" + Double.toString(d) + " is the double value");
\r
80 StringBuffer res1 = new StringBuffer();
\r
81 StringBuffer res2 = new StringBuffer();
\r
82 StringBuffer res3 = new StringBuffer();
\r
83 StringBuffer res4 = new StringBuffer();
\r
84 FieldPosition pos1 = new FieldPosition(0);
\r
85 FieldPosition pos2 = new FieldPosition(0);
\r
86 FieldPosition pos3 = new FieldPosition(0);
\r
87 FieldPosition pos4 = new FieldPosition(0);
\r
89 res1 = def.format(d, res1, pos1);
\r
90 logln("" + Double.toString(d) + " formatted to " + res1);
\r
92 res2 = pat.format(l, res2, pos2);
\r
93 logln("" + l + " formatted to " + res2);
\r
95 res3 = cust1.format(d, res3, pos3);
\r
96 logln("" + Double.toString(d) + " formatted to " + res3);
\r
98 res4 = cust1.format(l, res4, pos4);
\r
99 logln("" + l + " formatted to " + res4);
\r
101 // ======= Test parse()
\r
103 logln("Testing parse()");
\r
105 String text = new String("-10,456.0037");
\r
106 ParsePosition pos = new ParsePosition(0);
\r
107 String patt = new String("#,##0.#");
\r
108 pat.applyPattern(patt);
\r
109 double d2 = pat.parse(text, pos).doubleValue();
\r
112 "ERROR: Roundtrip failed (via parse(" + Double.toString(d2) + " != " + Double.toString(d) + ")) for " + text);
\r
114 logln(text + " parsed into " + (long) d2);
\r
116 // ======= Test getters and setters
\r
118 logln("Testing getters and setters");
\r
120 final DecimalFormatSymbols syms = pat.getDecimalFormatSymbols();
\r
121 def.setDecimalFormatSymbols(syms);
\r
122 if (!pat.getDecimalFormatSymbols().equals(def.getDecimalFormatSymbols())) {
\r
123 errln("ERROR: set DecimalFormatSymbols() failed");
\r
127 pat.setPositivePrefix("+");
\r
128 posPrefix = pat.getPositivePrefix();
\r
129 logln("Positive prefix (should be +): " + posPrefix);
\r
130 if (posPrefix != "+") {
\r
131 errln("ERROR: setPositivePrefix() failed");
\r
135 pat.setNegativePrefix("-");
\r
136 negPrefix = pat.getNegativePrefix();
\r
137 logln("Negative prefix (should be -): " + negPrefix);
\r
138 if (negPrefix != "-") {
\r
139 errln("ERROR: setNegativePrefix() failed");
\r
143 pat.setPositiveSuffix("_");
\r
144 posSuffix = pat.getPositiveSuffix();
\r
145 logln("Positive suffix (should be _): " + posSuffix);
\r
146 if (posSuffix != "_") {
\r
147 errln("ERROR: setPositiveSuffix() failed");
\r
151 pat.setNegativeSuffix("~");
\r
152 negSuffix = pat.getNegativeSuffix();
\r
153 logln("Negative suffix (should be ~): " + negSuffix);
\r
154 if (negSuffix != "~") {
\r
155 errln("ERROR: setNegativeSuffix() failed");
\r
158 long multiplier = 0;
\r
159 pat.setMultiplier(8);
\r
160 multiplier = pat.getMultiplier();
\r
161 logln("Multiplier (should be 8): " + multiplier);
\r
162 if (multiplier != 8) {
\r
163 errln("ERROR: setMultiplier() failed");
\r
166 int groupingSize = 0;
\r
167 pat.setGroupingSize(2);
\r
168 groupingSize = pat.getGroupingSize();
\r
169 logln("Grouping size (should be 2): " + (long) groupingSize);
\r
170 if (groupingSize != 2) {
\r
171 errln("ERROR: setGroupingSize() failed");
\r
174 pat.setDecimalSeparatorAlwaysShown(true);
\r
175 boolean tf = pat.isDecimalSeparatorAlwaysShown();
\r
177 "DecimalSeparatorIsAlwaysShown (should be true) is " + (tf ? "true" : "false"));
\r
179 errln("ERROR: setDecimalSeparatorAlwaysShown() failed");
\r
183 funkyPat = pat.toPattern();
\r
184 logln("Pattern is " + funkyPat);
\r
187 locPat = pat.toLocalizedPattern();
\r
188 logln("Localized pattern is " + locPat);
\r
190 // ======= Test applyPattern()
\r
192 logln("Testing applyPattern()");
\r
194 String p1 = new String("#,##0.0#;(#,##0.0#)");
\r
195 logln("Applying pattern " + p1);
\r
196 pat.applyPattern(p1);
\r
198 s2 = pat.toPattern();
\r
199 logln("Extracted pattern is " + s2);
\r
200 if (!s2.equals(p1)) {
\r
201 errln("ERROR: toPattern() result did not match pattern applied");
\r
204 String p2 = new String("#,##0.0# FF;(#,##0.0# FF)");
\r
205 logln("Applying pattern " + p2);
\r
206 pat.applyLocalizedPattern(p2);
\r
208 s3 = pat.toLocalizedPattern();
\r
209 logln("Extracted pattern is " + s3);
\r
210 if (!s3.equals(p2)) {
\r
211 errln("ERROR: toLocalizedPattern() result did not match pattern applied");
\r
214 // ======= Test getStaticClassID()
\r
216 // logln("Testing instanceof()");
\r
219 // NumberFormat test = new DecimalFormat();
\r
221 // if (! (test instanceof DecimalFormat)) {
\r
222 // errln("ERROR: instanceof failed");
\r
225 // catch (Exception e) {
\r
226 // errln("ERROR: Couldn't create a DecimalFormat");
\r
231 public void TestRounding() {
\r
232 double Roundingnumber = 2.55;
\r
233 double Roundingnumber1 = -2.55;
\r
234 //+2.55 results -2.55 results
\r
235 double result[] = {
\r
244 DecimalFormat pat = new DecimalFormat();
\r
246 s = pat.toPattern();
\r
247 logln("pattern = " + s);
\r
252 for (mode = 0; mode < 7; mode++) {
\r
253 pat.setRoundingMode(mode);
\r
254 if (pat.getRoundingMode() != mode) {
\r
256 "SetRoundingMode or GetRoundingMode failed for mode=" + mode);
\r
259 //for +2.55 with RoundingIncrement=1.0
\r
260 pat.setRoundingIncrement(1.0);
\r
261 resultStr = pat.format(Roundingnumber);
\r
262 message = "round(" + (double) Roundingnumber
\r
263 + "," + mode + ",FALSE) with RoundingIncrement=1.0==>";
\r
264 verify(message, resultStr, result[i++]);
\r
268 //for -2.55 with RoundingIncrement=1.0
\r
269 resultStr = pat.format(Roundingnumber1);
\r
270 message = "round(" + (double) Roundingnumber1
\r
271 + "," + mode + ",FALSE) with RoundingIncrement=1.0==>";
\r
272 verify(message, resultStr, result[i++]);
\r
278 public void testFormatToCharacterIterator() {
\r
280 Number number = new Double(350.76);
\r
281 Number negativeNumber = new Double(-350.76);
\r
283 Locale us = Locale.US;
\r
285 // test number instance
\r
286 t_Format(1, number, NumberFormat.getNumberInstance(us),
\r
287 getNumberVectorUS());
\r
289 // test percent instance
\r
290 t_Format(3, number, NumberFormat.getPercentInstance(us),
\r
291 getPercentVectorUS());
\r
293 // test permille pattern
\r
294 DecimalFormat format = new DecimalFormat("###0.##\u2030");
\r
295 t_Format(4, number, format, getPermilleVector());
\r
297 // test exponential pattern with positive exponent
\r
298 format = new DecimalFormat("00.0#E0");
\r
299 t_Format(5, number, format, getPositiveExponentVector());
\r
301 // test exponential pattern with negative exponent
\r
302 format = new DecimalFormat("0000.0#E0");
\r
303 t_Format(6, number, format, getNegativeExponentVector());
\r
305 // test currency instance with US Locale
\r
306 t_Format(7, number, NumberFormat.getCurrencyInstance(us),
\r
307 getPositiveCurrencyVectorUS());
\r
309 // test negative currency instance with US Locale
\r
310 t_Format(8, negativeNumber, NumberFormat.getCurrencyInstance(us),
\r
311 getNegativeCurrencyVectorUS());
\r
313 // test multiple grouping seperators
\r
314 number = new Long(100300400);
\r
315 t_Format(11, number, NumberFormat.getNumberInstance(us),
\r
316 getNumberVector2US());
\r
319 number = new Long(0);
\r
320 t_Format(12, number, NumberFormat.getNumberInstance(us),
\r
324 private static Vector getNumberVectorUS() {
\r
325 Vector v = new Vector();
\r
326 v.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER));
\r
327 v.add(new FieldContainer(3, 4, NumberFormat.Field.DECIMAL_SEPARATOR));
\r
328 v.add(new FieldContainer(4, 6, NumberFormat.Field.FRACTION));
\r
332 // private static Vector getPositiveCurrencyVectorTR() {
\r
333 // Vector v = new Vector();
\r
334 // v.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER));
\r
335 // v.add(new FieldContainer(4, 6, NumberFormat.Field.CURRENCY));
\r
339 // private static Vector getNegativeCurrencyVectorTR() {
\r
340 // Vector v = new Vector();
\r
341 // v.add(new FieldContainer(0, 1, NumberFormat.Field.SIGN));
\r
342 // v.add(new FieldContainer(1, 4, NumberFormat.Field.INTEGER));
\r
343 // v.add(new FieldContainer(5, 7, NumberFormat.Field.CURRENCY));
\r
347 private static Vector getPositiveCurrencyVectorUS() {
\r
348 Vector v = new Vector();
\r
349 v.add(new FieldContainer(0, 1, NumberFormat.Field.CURRENCY));
\r
350 v.add(new FieldContainer(1, 4, NumberFormat.Field.INTEGER));
\r
351 v.add(new FieldContainer(4, 5, NumberFormat.Field.DECIMAL_SEPARATOR));
\r
352 v.add(new FieldContainer(5, 7, NumberFormat.Field.FRACTION));
\r
356 private static Vector getNegativeCurrencyVectorUS() {
\r
357 Vector v = new Vector();
\r
358 v.add(new FieldContainer(1, 2, NumberFormat.Field.CURRENCY));
\r
359 v.add(new FieldContainer(2, 5, NumberFormat.Field.INTEGER));
\r
360 v.add(new FieldContainer(5, 6, NumberFormat.Field.DECIMAL_SEPARATOR));
\r
361 v.add(new FieldContainer(6, 8, NumberFormat.Field.FRACTION));
\r
365 private static Vector getPercentVectorUS() {
\r
366 Vector v = new Vector();
\r
367 v.add(new FieldContainer(0, 2, NumberFormat.Field.INTEGER));
\r
368 v.add(new FieldContainer(2, 3, NumberFormat.Field.INTEGER));
\r
369 v.add(new FieldContainer(2, 3, NumberFormat.Field.GROUPING_SEPARATOR));
\r
370 v.add(new FieldContainer(3, 6, NumberFormat.Field.INTEGER));
\r
371 v.add(new FieldContainer(6, 7, NumberFormat.Field.PERCENT));
\r
375 private static Vector getPermilleVector() {
\r
376 Vector v = new Vector();
\r
377 v.add(new FieldContainer(0, 6, NumberFormat.Field.INTEGER));
\r
378 v.add(new FieldContainer(6, 7, NumberFormat.Field.PERMILLE));
\r
382 private static Vector getNegativeExponentVector() {
\r
383 Vector v = new Vector();
\r
384 v.add(new FieldContainer(0, 4, NumberFormat.Field.INTEGER));
\r
385 v.add(new FieldContainer(4, 5, NumberFormat.Field.DECIMAL_SEPARATOR));
\r
386 v.add(new FieldContainer(5, 6, NumberFormat.Field.FRACTION));
\r
387 v.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT_SYMBOL));
\r
388 v.add(new FieldContainer(7, 8, NumberFormat.Field.EXPONENT_SIGN));
\r
389 v.add(new FieldContainer(8, 9, NumberFormat.Field.EXPONENT));
\r
393 private static Vector getPositiveExponentVector() {
\r
394 Vector v = new Vector();
\r
395 v.add(new FieldContainer(0, 2, NumberFormat.Field.INTEGER));
\r
396 v.add(new FieldContainer(2, 3, NumberFormat.Field.DECIMAL_SEPARATOR));
\r
397 v.add(new FieldContainer(3, 5, NumberFormat.Field.FRACTION));
\r
398 v.add(new FieldContainer(5, 6, NumberFormat.Field.EXPONENT_SYMBOL));
\r
399 v.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT));
\r
403 private static Vector getNumberVector2US() {
\r
404 Vector v = new Vector();
\r
405 v.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER));
\r
406 v.add(new FieldContainer(3, 4, NumberFormat.Field.GROUPING_SEPARATOR));
\r
407 v.add(new FieldContainer(3, 4, NumberFormat.Field.INTEGER));
\r
408 v.add(new FieldContainer(4, 7, NumberFormat.Field.INTEGER));
\r
409 v.add(new FieldContainer(7, 8, NumberFormat.Field.GROUPING_SEPARATOR));
\r
410 v.add(new FieldContainer(7, 8, NumberFormat.Field.INTEGER));
\r
411 v.add(new FieldContainer(8, 11, NumberFormat.Field.INTEGER));
\r
415 private static Vector getZeroVector() {
\r
416 Vector v = new Vector();
\r
417 v.add(new FieldContainer(0, 1, NumberFormat.Field.INTEGER));
\r
421 private void t_Format(int count, Object object, Format format,
\r
422 Vector expectedResults) {
\r
423 Vector results = findFields(format.formatToCharacterIterator(object));
\r
424 assertTrue("Test " + count
\r
425 + ": Format returned incorrect CharacterIterator for "
\r
426 + format.format(object), compare(results, expectedResults));
\r
430 * compares two vectors regardless of the order of their elements
\r
432 private static boolean compare(Vector vector1, Vector vector2) {
\r
433 return vector1.size() == vector2.size() && vector1.containsAll(vector2);
\r
437 * finds attributes with regards to char index in this
\r
438 * AttributedCharacterIterator, and puts them in a vector
\r
441 * @return a vector, each entry in this vector are of type FieldContainer ,
\r
442 * which stores start and end indexes and an attribute this range
\r
445 private static Vector findFields(AttributedCharacterIterator iterator) {
\r
446 Vector result = new Vector();
\r
447 while (iterator.getIndex() != iterator.getEndIndex()) {
\r
448 int start = iterator.getRunStart();
\r
449 int end = iterator.getRunLimit();
\r
451 Iterator it = iterator.getAttributes().keySet().iterator();
\r
452 while (it.hasNext()) {
\r
453 AttributedCharacterIterator.Attribute attribute = (AttributedCharacterIterator.Attribute) it
\r
455 Object value = iterator.getAttribute(attribute);
\r
456 result.add(new FieldContainer(start, end, attribute, value));
\r
457 // System.out.println(start + " " + end + ": " + attribute + ",
\r
459 // System.out.println("v.add(new FieldContainer(" + start +"," +
\r
460 // end +"," + attribute+ "," + value+ "));");
\r
462 iterator.setIndex(end);
\r
466 protected static class FieldContainer {
\r
469 AttributedCharacterIterator.Attribute attribute;
\r
473 // called from support_decimalformat and support_simpledateformat tests
\r
474 public FieldContainer(int start, int end,
\r
475 AttributedCharacterIterator.Attribute attribute) {
\r
476 this(start, end, attribute, attribute);
\r
479 // called from support_messageformat tests
\r
480 public FieldContainer(int start, int end, AttributedCharacterIterator.Attribute attribute, int value) {
\r
481 this(start, end, attribute, new Integer(value));
\r
484 // called from support_messageformat tests
\r
485 public FieldContainer(int start, int end, AttributedCharacterIterator.Attribute attribute,
\r
487 this.start = start;
\r
489 this.attribute = attribute;
\r
490 this.value = value;
\r
493 public boolean equals(Object obj) {
\r
494 if (!(obj instanceof FieldContainer))
\r
497 FieldContainer fc = (FieldContainer) obj;
\r
498 return (start == fc.start && end == fc.end
\r
499 && attribute == fc.attribute && value.equals(fc.value));
\r
503 /*Helper functions */
\r
504 public void verify(String message, String got, double expected) {
\r
505 logln(message + got + " Expected : " + (long)expected);
\r
506 String expectedStr = "";
\r
507 expectedStr=expectedStr + (long)expected;
\r
508 if(!got.equals(expectedStr) ) {
\r
509 errln("ERROR: Round() failed: " + message + got + " Expected : " + expectedStr);
\r