001: /*
002: *******************************************************************************
003: * Copyright (C) 1996-2004, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007: package com.ibm.icu.dev.test.format;
008:
009: import com.ibm.icu.text.RuleBasedNumberFormat;
010: import com.ibm.icu.dev.test.TestFmwk;
011:
012: import java.util.Locale;
013:
014: public class RbnfRoundTripTest extends TestFmwk {
015: /**
016: * Puts a copyright in the .class file
017: */
018: private static final String copyrightNotice = "Copyright \u00a91997-1998 IBM Corp. All rights reserved.";
019:
020: public static void main(String[] args) {
021: RbnfRoundTripTest test = new RbnfRoundTripTest();
022:
023: try {
024: test.run(args);
025: } catch (Throwable e) {
026: System.out
027: .println("Entire test failed because of exception: "
028: + e.toString());
029: e.printStackTrace();
030: }
031: }
032:
033: /**
034: * Perform an exhaustive round-trip test on the English spellout rules
035: */
036: public void TestEnglishSpelloutRT() {
037: RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(
038: Locale.US, RuleBasedNumberFormat.SPELLOUT);
039:
040: doTest(formatter, -12345678, 12345678);
041: }
042:
043: /**
044: * Perform an exhaustive round-trip test on the duration-formatting rules
045: */
046: public void TestDurationsRT() {
047: RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(
048: Locale.US, RuleBasedNumberFormat.DURATION);
049:
050: doTest(formatter, 0, 12345678);
051: }
052:
053: /**
054: * Perform an exhaustive round-trip test on the Spanish spellout rules
055: */
056: public void TestSpanishSpelloutRT() {
057: RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(
058: new Locale("es", "es", ""),
059: RuleBasedNumberFormat.SPELLOUT);
060:
061: doTest(formatter, -12345678, 12345678);
062: }
063:
064: /**
065: * Perform an exhaustive round-trip test on the French spellout rules
066: */
067: public void TestFrenchSpelloutRT() {
068: RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(
069: Locale.FRANCE, RuleBasedNumberFormat.SPELLOUT);
070:
071: doTest(formatter, -12345678, 12345678);
072: }
073:
074: /**
075: * Perform an exhaustive round-trip test on the Swiss French spellout rules
076: */
077: public void TestSwissFrenchSpelloutRT() {
078: RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(
079: new Locale("fr", "CH", ""),
080: RuleBasedNumberFormat.SPELLOUT);
081:
082: doTest(formatter, -12345678, 12345678);
083: }
084:
085: /**
086: * Perform an exhaustive round-trip test on the Italian spellout rules
087: */
088: public void TestItalianSpelloutRT() {
089: RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(
090: Locale.ITALIAN, RuleBasedNumberFormat.SPELLOUT);
091:
092: doTest(formatter, -999999, 999999);
093: }
094:
095: /**
096: * Perform an exhaustive round-trip test on the German spellout rules
097: */
098: public void TestGermanSpelloutRT() {
099: RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(
100: Locale.GERMANY, RuleBasedNumberFormat.SPELLOUT);
101:
102: doTest(formatter, 0, 12345678);
103: }
104:
105: /**
106: * Perform an exhaustive round-trip test on the Swedish spellout rules
107: */
108: public void TestSwedishSpelloutRT() {
109: RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(
110: new Locale("sv", "SE", ""),
111: RuleBasedNumberFormat.SPELLOUT);
112:
113: doTest(formatter, 0, 12345678);
114: }
115:
116: /**
117: * Perform an exhaustive round-trip test on the Dutch spellout rules
118: */
119: public void TestDutchSpelloutRT() {
120: RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(
121: new Locale("nl", "NL", ""),
122: RuleBasedNumberFormat.SPELLOUT);
123:
124: doTest(formatter, -12345678, 12345678);
125: }
126:
127: /**
128: * Perform an exhaustive round-trip test on the Japanese spellout rules
129: */
130: public void TestJapaneseSpelloutRT() {
131: RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(
132: Locale.JAPAN, RuleBasedNumberFormat.SPELLOUT);
133:
134: doTest(formatter, 0, 12345678);
135: }
136:
137: /**
138: * Perform an exhaustive round-trip test on the Russian spellout rules
139: */
140: public void TestRussianSpelloutRT() {
141: RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(
142: new Locale("ru", "RU", ""),
143: RuleBasedNumberFormat.SPELLOUT);
144:
145: doTest(formatter, 0, 12345678);
146: }
147:
148: /**
149: * Perform an exhaustive round-trip test on the Greek spellout rules
150: */
151: public void TestGreekSpelloutRT() {
152: RuleBasedNumberFormat formatter = new RuleBasedNumberFormat(
153: new Locale("el", "GR", ""),
154: RuleBasedNumberFormat.SPELLOUT);
155:
156: doTest(formatter, 0, 12345678);
157: }
158:
159: void doTest(RuleBasedNumberFormat formatter, long lowLimit,
160: long highLimit) {
161: try {
162: long count = 0;
163: long increment = 1;
164: for (long i = lowLimit; i <= highLimit; i += increment) {
165: if (count % 1000 == 0)
166: logln(Long.toString(i));
167:
168: if (Math.abs(i) < 5000)
169: increment = 1;
170: else if (Math.abs(i) < 500000)
171: increment = 2737;
172: else
173: increment = 267437;
174:
175: String text = formatter.format(i);
176: long rt = formatter.parse(text).longValue();
177:
178: if (rt != i) {
179: errln("Round-trip failed: " + i + " -> " + text
180: + " -> " + rt);
181: }
182:
183: ++count;
184: }
185:
186: if (lowLimit < 0) {
187: double d = 1.234;
188: while (d < 1000) {
189: String text = formatter.format(d);
190: double rt = formatter.parse(text).doubleValue();
191:
192: if (rt != d) {
193: errln("Round-trip failed: " + d + " -> " + text
194: + " -> " + rt);
195: }
196: d *= 10;
197: }
198: }
199: } catch (Throwable e) {
200: errln("Test failed with exception: " + e.toString());
201: e.printStackTrace();
202: }
203: }
204: }
|