001: //##header
002: /*
003: *******************************************************************************
004: * Copyright (C) 2001-2006, International Business Machines Corporation and *
005: * others. All Rights Reserved. *
006: *******************************************************************************
007: */
008:
009: /**
010: * Port From: ICU4C v1.8.1 : format : IntlTestDecimalFormatAPI
011: * Source File: $ICU4CRoot/source/test/intltest/dcfmapts.cpp
012: **/package com.ibm.icu.dev.test.format;
013:
014: import java.text.AttributedCharacterIterator;
015: import java.text.FieldPosition;
016: import java.text.Format;
017: import java.text.ParsePosition;
018: import java.util.Iterator;
019: import java.util.Locale;
020: import java.util.Vector;
021:
022: import com.ibm.icu.text.DecimalFormat;
023: import com.ibm.icu.text.DecimalFormatSymbols;
024: import com.ibm.icu.text.NumberFormat;
025:
026: // This is an API test, not a unit test. It doesn't test very many cases, and doesn't
027: // try to test the full functionality. It just calls each function in the class and
028: // verifies that it works on a basic level.
029: public class IntlTestDecimalFormatAPIC extends
030: com.ibm.icu.dev.test.TestFmwk {
031:
032: public static void main(String[] args) throws Exception {
033: new IntlTestDecimalFormatAPIC().run(args);
034: }
035:
036: // This test checks various generic API methods in DecimalFormat to achieve 100% API coverage.
037: public void TestAPI() {
038:
039: logln("DecimalFormat API test---");
040: logln("");
041: Locale.setDefault(Locale.ENGLISH);
042:
043: // ======= Test constructors
044:
045: logln("Testing DecimalFormat constructors");
046:
047: DecimalFormat def = new DecimalFormat();
048:
049: final String pattern = new String("#,##0.# FF");
050: DecimalFormat pat = null;
051: try {
052: pat = new DecimalFormat(pattern);
053: } catch (IllegalArgumentException e) {
054: errln("ERROR: Could not create DecimalFormat (pattern)");
055: }
056:
057: DecimalFormatSymbols symbols = new DecimalFormatSymbols(
058: Locale.FRENCH);
059:
060: DecimalFormat cust1 = new DecimalFormat(pattern, symbols);
061:
062: // ======= Test clone(), assignment, and equality
063:
064: logln("Testing clone() and equality operators");
065:
066: Format clone = (Format) def.clone();
067: if (!def.equals(clone)) {
068: errln("ERROR: Clone() failed");
069: }
070:
071: // ======= Test various format() methods
072:
073: logln("Testing various format() methods");
074:
075: // final double d = -10456.0037; // this appears as -10456.003700000001 on NT
076: // final double d = -1.04560037e-4; // this appears as -1.0456003700000002E-4 on NT
077: final double d = -10456.00370000000000; // this works!
078: final long l = 100000000;
079: logln("" + Double.toString(d) + " is the double value");
080:
081: StringBuffer res1 = new StringBuffer();
082: StringBuffer res2 = new StringBuffer();
083: StringBuffer res3 = new StringBuffer();
084: StringBuffer res4 = new StringBuffer();
085: FieldPosition pos1 = new FieldPosition(0);
086: FieldPosition pos2 = new FieldPosition(0);
087: FieldPosition pos3 = new FieldPosition(0);
088: FieldPosition pos4 = new FieldPosition(0);
089:
090: res1 = def.format(d, res1, pos1);
091: logln("" + Double.toString(d) + " formatted to " + res1);
092:
093: res2 = pat.format(l, res2, pos2);
094: logln("" + l + " formatted to " + res2);
095:
096: res3 = cust1.format(d, res3, pos3);
097: logln("" + Double.toString(d) + " formatted to " + res3);
098:
099: res4 = cust1.format(l, res4, pos4);
100: logln("" + l + " formatted to " + res4);
101:
102: // ======= Test parse()
103:
104: logln("Testing parse()");
105:
106: String text = new String("-10,456.0037");
107: ParsePosition pos = new ParsePosition(0);
108: String patt = new String("#,##0.#");
109: pat.applyPattern(patt);
110: double d2 = pat.parse(text, pos).doubleValue();
111: if (d2 != d) {
112: errln("ERROR: Roundtrip failed (via parse("
113: + Double.toString(d2) + " != " + Double.toString(d)
114: + ")) for " + text);
115: }
116: logln(text + " parsed into " + (long) d2);
117:
118: // ======= Test getters and setters
119:
120: logln("Testing getters and setters");
121:
122: final DecimalFormatSymbols syms = pat.getDecimalFormatSymbols();
123: def.setDecimalFormatSymbols(syms);
124: if (!pat.getDecimalFormatSymbols().equals(
125: def.getDecimalFormatSymbols())) {
126: errln("ERROR: set DecimalFormatSymbols() failed");
127: }
128:
129: String posPrefix;
130: pat.setPositivePrefix("+");
131: posPrefix = pat.getPositivePrefix();
132: logln("Positive prefix (should be +): " + posPrefix);
133: if (posPrefix != "+") {
134: errln("ERROR: setPositivePrefix() failed");
135: }
136:
137: String negPrefix;
138: pat.setNegativePrefix("-");
139: negPrefix = pat.getNegativePrefix();
140: logln("Negative prefix (should be -): " + negPrefix);
141: if (negPrefix != "-") {
142: errln("ERROR: setNegativePrefix() failed");
143: }
144:
145: String posSuffix;
146: pat.setPositiveSuffix("_");
147: posSuffix = pat.getPositiveSuffix();
148: logln("Positive suffix (should be _): " + posSuffix);
149: if (posSuffix != "_") {
150: errln("ERROR: setPositiveSuffix() failed");
151: }
152:
153: String negSuffix;
154: pat.setNegativeSuffix("~");
155: negSuffix = pat.getNegativeSuffix();
156: logln("Negative suffix (should be ~): " + negSuffix);
157: if (negSuffix != "~") {
158: errln("ERROR: setNegativeSuffix() failed");
159: }
160:
161: long multiplier = 0;
162: pat.setMultiplier(8);
163: multiplier = pat.getMultiplier();
164: logln("Multiplier (should be 8): " + multiplier);
165: if (multiplier != 8) {
166: errln("ERROR: setMultiplier() failed");
167: }
168:
169: int groupingSize = 0;
170: pat.setGroupingSize(2);
171: groupingSize = pat.getGroupingSize();
172: logln("Grouping size (should be 2): " + (long) groupingSize);
173: if (groupingSize != 2) {
174: errln("ERROR: setGroupingSize() failed");
175: }
176:
177: pat.setDecimalSeparatorAlwaysShown(true);
178: boolean tf = pat.isDecimalSeparatorAlwaysShown();
179: logln("DecimalSeparatorIsAlwaysShown (should be true) is "
180: + (tf ? "true" : "false"));
181: if (tf != true) {
182: errln("ERROR: setDecimalSeparatorAlwaysShown() failed");
183: }
184:
185: String funkyPat;
186: funkyPat = pat.toPattern();
187: logln("Pattern is " + funkyPat);
188:
189: String locPat;
190: locPat = pat.toLocalizedPattern();
191: logln("Localized pattern is " + locPat);
192:
193: // ======= Test applyPattern()
194:
195: logln("Testing applyPattern()");
196:
197: String p1 = new String("#,##0.0#;(#,##0.0#)");
198: logln("Applying pattern " + p1);
199: pat.applyPattern(p1);
200: String s2;
201: s2 = pat.toPattern();
202: logln("Extracted pattern is " + s2);
203: if (!s2.equals(p1)) {
204: errln("ERROR: toPattern() result did not match pattern applied");
205: }
206:
207: String p2 = new String("#,##0.0# FF;(#,##0.0# FF)");
208: logln("Applying pattern " + p2);
209: pat.applyLocalizedPattern(p2);
210: String s3;
211: s3 = pat.toLocalizedPattern();
212: logln("Extracted pattern is " + s3);
213: if (!s3.equals(p2)) {
214: errln("ERROR: toLocalizedPattern() result did not match pattern applied");
215: }
216:
217: // ======= Test getStaticClassID()
218:
219: // logln("Testing instanceof()");
220:
221: // try {
222: // NumberFormat test = new DecimalFormat();
223:
224: // if (! (test instanceof DecimalFormat)) {
225: // errln("ERROR: instanceof failed");
226: // }
227: // }
228: // catch (Exception e) {
229: // errln("ERROR: Couldn't create a DecimalFormat");
230: // }
231:
232: }
233:
234: public void TestRounding() {
235: double Roundingnumber = 2.55;
236: double Roundingnumber1 = -2.55;
237: //+2.55 results -2.55 results
238: double result[] = { 3, -3, 2, -2, 3, -2, 2, -3, 3, -3, 3, -3,
239: 3, -3 };
240: DecimalFormat pat = new DecimalFormat();
241: String s = "";
242: s = pat.toPattern();
243: logln("pattern = " + s);
244: int mode;
245: int i = 0;
246: String message;
247: String resultStr;
248: for (mode = 0; mode < 7; mode++) {
249: pat.setRoundingMode(mode);
250: if (pat.getRoundingMode() != mode) {
251: errln("SetRoundingMode or GetRoundingMode failed for mode="
252: + mode);
253: }
254:
255: //for +2.55 with RoundingIncrement=1.0
256: pat.setRoundingIncrement(1.0);
257: resultStr = pat.format(Roundingnumber);
258: message = "round(" + (double) Roundingnumber + "," + mode
259: + ",FALSE) with RoundingIncrement=1.0==>";
260: verify(message, resultStr, result[i++]);
261: message = "";
262: resultStr = "";
263:
264: //for -2.55 with RoundingIncrement=1.0
265: resultStr = pat.format(Roundingnumber1);
266: message = "round(" + (double) Roundingnumber1 + "," + mode
267: + ",FALSE) with RoundingIncrement=1.0==>";
268: verify(message, resultStr, result[i++]);
269: message = "";
270: resultStr = "";
271: }
272: }
273:
274: //#ifndef FOUNDATION
275: public void testFormatToCharacterIterator() {
276:
277: Number number = new Double(350.76);
278: Number negativeNumber = new Double(-350.76);
279:
280: Locale us = Locale.US;
281: Locale tr = new Locale("tr", "TR");
282:
283: // test number instance
284: t_Format(1, number, NumberFormat.getNumberInstance(us),
285: getNumberVectorUS());
286:
287: // // test percent instance
288: t_Format(3, number, NumberFormat.getPercentInstance(us),
289: getPercentVectorUS());
290: //
291: // // test permille pattern
292: DecimalFormat format = new DecimalFormat("###0.##\u2030");
293: // t_Format(4, number, format, getPermilleVector());
294:
295: // test exponential pattern with positive exponent
296: format = new DecimalFormat("00.0#E0");
297: t_Format(5, number, format, getPositiveExponentVector());
298:
299: // // test exponential pattern with negative exponent
300: format = new DecimalFormat("0000.0#E0");
301: t_Format(6, number, format, getNegativeExponentVector());
302:
303: // test currency instance with US Locale
304: t_Format(7, number, NumberFormat.getCurrencyInstance(us),
305: getPositiveCurrencyVectorUS());
306:
307: // // test negative currency instance with US Locale
308: t_Format(8, negativeNumber, NumberFormat
309: .getCurrencyInstance(us), getNegativeCurrencyVectorUS());
310:
311: // // test multiple grouping seperators
312: number = new Long(100300400);
313: t_Format(11, number, NumberFormat.getNumberInstance(us),
314: getNumberVector2US());
315:
316: // // test 0
317: number = new Long(0);
318: t_Format(12, number, NumberFormat.getNumberInstance(us),
319: getZeroVector());
320: }
321:
322: private static Vector getNumberVectorUS() {
323: Vector v = new Vector();
324: v.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER));
325: v.add(new FieldContainer(3, 4,
326: NumberFormat.Field.DECIMAL_SEPARATOR));
327: v.add(new FieldContainer(4, 6, NumberFormat.Field.FRACTION));
328: return v;
329: }
330:
331: private static Vector getPositiveCurrencyVectorTR() {
332: Vector v = new Vector();
333: v.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER));
334: v.add(new FieldContainer(4, 6, NumberFormat.Field.CURRENCY));
335: return v;
336: }
337:
338: private static Vector getNegativeCurrencyVectorTR() {
339: Vector v = new Vector();
340: v.add(new FieldContainer(0, 1, NumberFormat.Field.SIGN));
341: v.add(new FieldContainer(1, 4, NumberFormat.Field.INTEGER));
342: v.add(new FieldContainer(5, 7, NumberFormat.Field.CURRENCY));
343: return v;
344: }
345:
346: private static Vector getPositiveCurrencyVectorUS() {
347: Vector v = new Vector();
348: v.add(new FieldContainer(0, 1, NumberFormat.Field.CURRENCY));
349: v.add(new FieldContainer(1, 4, NumberFormat.Field.INTEGER));
350: v.add(new FieldContainer(4, 5,
351: NumberFormat.Field.DECIMAL_SEPARATOR));
352: v.add(new FieldContainer(5, 7, NumberFormat.Field.FRACTION));
353: return v;
354: }
355:
356: private static Vector getNegativeCurrencyVectorUS() {
357: Vector v = new Vector();
358: v.add(new FieldContainer(1, 2, NumberFormat.Field.CURRENCY));
359: v.add(new FieldContainer(2, 5, NumberFormat.Field.INTEGER));
360: v.add(new FieldContainer(5, 6,
361: NumberFormat.Field.DECIMAL_SEPARATOR));
362: v.add(new FieldContainer(6, 8, NumberFormat.Field.FRACTION));
363: return v;
364: }
365:
366: private static Vector getPercentVectorUS() {
367: Vector v = new Vector();
368: v.add(new FieldContainer(0, 2, NumberFormat.Field.INTEGER));
369: v.add(new FieldContainer(2, 3, NumberFormat.Field.INTEGER));
370: v.add(new FieldContainer(2, 3,
371: NumberFormat.Field.GROUPING_SEPARATOR));
372: v.add(new FieldContainer(3, 6, NumberFormat.Field.INTEGER));
373: v.add(new FieldContainer(6, 7, NumberFormat.Field.PERCENT));
374: return v;
375: }
376:
377: private static Vector getPermilleVector() {
378: Vector v = new Vector();
379: v.add(new FieldContainer(0, 6, NumberFormat.Field.INTEGER));
380: v.add(new FieldContainer(6, 7, NumberFormat.Field.PERMILLE));
381: return v;
382: }
383:
384: private static Vector getNegativeExponentVector() {
385: Vector v = new Vector();
386: v.add(new FieldContainer(0, 4, NumberFormat.Field.INTEGER));
387: v.add(new FieldContainer(4, 5,
388: NumberFormat.Field.DECIMAL_SEPARATOR));
389: v.add(new FieldContainer(5, 6, NumberFormat.Field.FRACTION));
390: v.add(new FieldContainer(6, 7,
391: NumberFormat.Field.EXPONENT_SYMBOL));
392: v
393: .add(new FieldContainer(7, 8,
394: NumberFormat.Field.EXPONENT_SIGN));
395: v.add(new FieldContainer(8, 9, NumberFormat.Field.EXPONENT));
396: return v;
397: }
398:
399: private static Vector getPositiveExponentVector() {
400: Vector v = new Vector();
401: v.add(new FieldContainer(0, 2, NumberFormat.Field.INTEGER));
402: v.add(new FieldContainer(2, 3,
403: NumberFormat.Field.DECIMAL_SEPARATOR));
404: v.add(new FieldContainer(3, 5, NumberFormat.Field.FRACTION));
405: v.add(new FieldContainer(5, 6,
406: NumberFormat.Field.EXPONENT_SYMBOL));
407: v.add(new FieldContainer(6, 7, NumberFormat.Field.EXPONENT));
408: return v;
409: }
410:
411: private static Vector getNumberVector2US() {
412: Vector v = new Vector();
413: v.add(new FieldContainer(0, 3, NumberFormat.Field.INTEGER));
414: v.add(new FieldContainer(3, 4,
415: NumberFormat.Field.GROUPING_SEPARATOR));
416: v.add(new FieldContainer(3, 4, NumberFormat.Field.INTEGER));
417: v.add(new FieldContainer(4, 7, NumberFormat.Field.INTEGER));
418: v.add(new FieldContainer(7, 8,
419: NumberFormat.Field.GROUPING_SEPARATOR));
420: v.add(new FieldContainer(7, 8, NumberFormat.Field.INTEGER));
421: v.add(new FieldContainer(8, 11, NumberFormat.Field.INTEGER));
422: return v;
423: }
424:
425: private static Vector getZeroVector() {
426: Vector v = new Vector();
427: v.add(new FieldContainer(0, 1, NumberFormat.Field.INTEGER));
428: return v;
429: }
430:
431: private void t_Format(int count, Object object, Format format,
432: Vector expectedResults) {
433: Vector results = findFields(format
434: .formatToCharacterIterator(object));
435: assertTrue("Test " + count
436: + ": Format returned incorrect CharacterIterator for "
437: + format.format(object), compare(results,
438: expectedResults));
439: }
440:
441: /**
442: * compares two vectors regardless of the order of their elements
443: */
444: private static boolean compare(Vector vector1, Vector vector2) {
445: return vector1.size() == vector2.size()
446: && vector1.containsAll(vector2);
447: }
448:
449: /**
450: * finds attributes with regards to char index in this
451: * AttributedCharacterIterator, and puts them in a vector
452: *
453: * @param iterator
454: * @return a vector, each entry in this vector are of type FieldContainer ,
455: * which stores start and end indexes and an attribute this range
456: * has
457: */
458: private static Vector findFields(
459: AttributedCharacterIterator iterator) {
460: Vector result = new Vector();
461: while (iterator.getIndex() != iterator.getEndIndex()) {
462: int start = iterator.getRunStart();
463: int end = iterator.getRunLimit();
464:
465: Iterator it = iterator.getAttributes().keySet().iterator();
466: while (it.hasNext()) {
467: AttributedCharacterIterator.Attribute attribute = (AttributedCharacterIterator.Attribute) it
468: .next();
469: Object value = iterator.getAttribute(attribute);
470: result.add(new FieldContainer(start, end, attribute,
471: value));
472: // System.out.println(start + " " + end + ": " + attribute + ",
473: // " + value );
474: // System.out.println("v.add(new FieldContainer(" + start +"," +
475: // end +"," + attribute+ "," + value+ "));");
476: }
477: iterator.setIndex(end);
478: }
479: return result;
480: }
481:
482: protected static class FieldContainer {
483: int start, end;
484:
485: AttributedCharacterIterator.Attribute attribute;
486:
487: Object value;
488:
489: // called from support_decimalformat and support_simpledateformat tests
490: public FieldContainer(int start, int end,
491: AttributedCharacterIterator.Attribute attribute) {
492: this (start, end, attribute, attribute);
493: }
494:
495: // called from support_messageformat tests
496: public FieldContainer(int start, int end,
497: AttributedCharacterIterator.Attribute attribute,
498: int value) {
499: this (start, end, attribute, new Integer(value));
500: }
501:
502: // called from support_messageformat tests
503: public FieldContainer(int start, int end,
504: AttributedCharacterIterator.Attribute attribute,
505: Object value) {
506: this .start = start;
507: this .end = end;
508: this .attribute = attribute;
509: this .value = value;
510: }
511:
512: public boolean equals(Object obj) {
513: if (!(obj instanceof FieldContainer))
514: return false;
515:
516: FieldContainer fc = (FieldContainer) obj;
517: return (start == fc.start && end == fc.end
518: && attribute == fc.attribute && value
519: .equals(fc.value));
520: }
521: }
522:
523: //#endif
524:
525: /*Helper functions */
526: public void verify(String message, String got, double expected) {
527: logln(message + got + " Expected : " + (long) expected);
528: String expectedStr = "";
529: expectedStr = expectedStr + (long) expected;
530: if (!got.equals(expectedStr)) {
531: errln("ERROR: Round() failed: " + message + got
532: + " Expected : " + expectedStr);
533: }
534: }
535: }
536: //eof
|