2 *******************************************************************************
\r
3 * Copyright (C) 2007-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.format;
\r
9 import java.util.HashMap;
\r
10 import java.util.Map;
\r
12 import com.ibm.icu.dev.test.TestFmwk;
\r
13 import com.ibm.icu.impl.Utility;
\r
14 import com.ibm.icu.text.CurrencyPluralInfo;
\r
15 import com.ibm.icu.text.PluralFormat;
\r
16 import com.ibm.icu.util.ULocale;
\r
19 * @author tschumann (Tim Schumann)
\r
22 public class PluralFormatTest extends TestFmwk {
\r
24 public static void main(String[] args) throws Exception {
\r
25 new PluralFormatTest().run(args);
\r
28 private void helperTestRules(String localeIDs, String testPattern, Map changes) {
\r
29 String[] locales = Utility.split(localeIDs, ',');
\r
31 // Create example outputs for all supported locales.
\r
33 System.out.println("\n" + localeIDs);
\r
34 String lastValue = (String) changes.get(new Integer(0));
\r
35 int lastNumber = 0;
\r
37 for (int i = 1; i < 199; ++i) {
\r
38 if (changes.get(new Integer(i)) != null) {
\r
39 if (lastNumber == i-1) {
\r
40 System.out.println(lastNumber + ": " + lastValue);
\r
42 System.out.println(lastNumber + "... " + (i-1) + ": " + lastValue);
\r
45 lastValue = (String) changes.get(new Integer(i));
\r
48 System.out.println(lastNumber + "..." + 199 + ": " + lastValue);
\r
50 log("test pattern: '" + testPattern + "'");
\r
51 for (int i = 0; i < locales.length; ++i) {
\r
53 PluralFormat plf = new PluralFormat(new ULocale(locales[i]), testPattern);
\r
55 String expected = (String) changes.get(new Integer(0));
\r
56 for (int n = 0; n < 200; ++n) {
\r
57 if (changes.get(new Integer(n)) != null) {
\r
58 expected = (String) changes.get(new Integer(n));
\r
60 assertEquals("Locale: " + locales[i] + ", number: " + n,
\r
61 expected, plf.format(n));
\r
63 } catch (IllegalArgumentException e) {
\r
64 errln(e.getMessage() + " locale: " + locales[i] + " pattern: '" + testPattern + "' " + System.currentTimeMillis());
\r
69 public void TestOneFormLocales() {
\r
70 String localeIDs = "ja,ko,tr,vi";
\r
71 String testPattern = "other{other}";
\r
72 Map changes = new HashMap();
\r
73 changes.put(new Integer(0), "other");
\r
74 helperTestRules(localeIDs, testPattern, changes);
\r
77 public void TestSingular1Locales() {
\r
78 String localeIDs = "bem,da,de,el,en,eo,es,et,fi,fo,he,it,nb,nl,nn,no,pt,pt_PT,sv,af,bg,bn,ca,eu,fur,fy,gu,ha,is,ku,lb,ml," +
\r
79 "mr,nah,ne,om,or,pa,pap,ps,so,sq,sw,ta,te,tk,ur,zu,mn,gsw,rm";
\r
80 String testPattern = "one{one} other{other}";
\r
81 Map changes = new HashMap();
\r
82 changes.put(new Integer(0), "other");
\r
83 changes.put(new Integer(1), "one");
\r
84 changes.put(new Integer(2), "other");
\r
85 helperTestRules(localeIDs, testPattern, changes);
\r
88 public void TestSingular01Locales() {
\r
89 String localeIDs = "ff,fr,kab";
\r
90 String testPattern = "one{one} other{other}";
\r
91 Map changes = new HashMap();
\r
92 changes.put(new Integer(0), "one");
\r
93 changes.put(new Integer(2), "other");
\r
94 helperTestRules(localeIDs, testPattern, changes);
\r
97 public void TestZeroSingularLocales() {
\r
98 String localeIDs = "lv";
\r
99 String testPattern = "zero{zero} one{one} other{other}";
\r
100 Map changes = new HashMap();
\r
101 changes.put(new Integer(0), "zero");
\r
102 changes.put(new Integer(1), "one");
\r
103 changes.put(new Integer(2), "other");
\r
104 for (int i = 2; i < 20; ++i) {
\r
108 changes.put(new Integer(i*10 + 1), "one");
\r
109 changes.put(new Integer(i*10 + 2), "other");
\r
111 helperTestRules(localeIDs, testPattern, changes);
\r
114 public void TestSingularDual() {
\r
115 String localeIDs = "ga";
\r
116 String testPattern = "one{one} two{two} other{other}";
\r
117 Map changes = new HashMap();
\r
118 changes.put(new Integer(0), "other");
\r
119 changes.put(new Integer(1), "one");
\r
120 changes.put(new Integer(2), "two");
\r
121 changes.put(new Integer(3), "other");
\r
122 helperTestRules(localeIDs, testPattern, changes);
\r
125 public void TestSingularZeroSome() {
\r
126 String localeIDs = "ro";
\r
127 String testPattern = "few{few} one{one} other{other}";
\r
128 Map changes = new HashMap();
\r
129 changes.put(new Integer(0), "few");
\r
130 changes.put(new Integer(1), "one");
\r
131 changes.put(new Integer(2), "few");
\r
132 changes.put(new Integer(20), "other");
\r
133 changes.put(new Integer(101), "few");
\r
134 changes.put(new Integer(120), "other");
\r
135 helperTestRules(localeIDs, testPattern, changes);
\r
138 public void TestSpecial12_19() {
\r
139 String localeIDs = "lt";
\r
140 String testPattern = "one{one} few{few} other{other}";
\r
141 Map changes = new HashMap();
\r
142 changes.put(new Integer(0), "other");
\r
143 changes.put(new Integer(1), "one");
\r
144 changes.put(new Integer(2), "few");
\r
145 changes.put(new Integer(10), "other");
\r
146 for (int i = 2; i < 20; ++i) {
\r
150 changes.put(new Integer(i*10 + 1), "one");
\r
151 changes.put(new Integer(i*10 + 2), "few");
\r
152 changes.put(new Integer((i+1)*10), "other");
\r
154 helperTestRules(localeIDs, testPattern, changes);
\r
157 public void TestPaucalExcept11_14() {
\r
158 String localeIDs = "hr,ru,sr,uk";
\r
159 String testPattern = "one{one} few{few} other{other}";
\r
160 Map changes = new HashMap();
\r
161 changes.put(new Integer(0), "other");
\r
162 changes.put(new Integer(1), "one");
\r
163 changes.put(new Integer(2), "few");
\r
164 changes.put(new Integer(5), "other");
\r
165 for (int i = 2; i < 20; ++i) {
\r
169 changes.put(new Integer(i*10 + 1), "one");
\r
170 changes.put(new Integer(i*10 + 2), "few");
\r
171 changes.put(new Integer(i*10 + 5), "other");
\r
173 helperTestRules(localeIDs, testPattern, changes);
\r
176 public void TestSingularPaucal() {
\r
177 String localeIDs = "cs,sk";
\r
178 String testPattern = "one{one} few{few} other{other}";
\r
179 Map changes = new HashMap();
\r
180 changes.put(new Integer(0), "other");
\r
181 changes.put(new Integer(1), "one");
\r
182 changes.put(new Integer(2), "few");
\r
183 changes.put(new Integer(5), "other");
\r
184 helperTestRules(localeIDs, testPattern, changes);
\r
187 public void TestPaucal1_234() {
\r
188 String localeIDs = "pl";
\r
189 String testPattern = "one{one} few{few} other{other}";
\r
190 Map changes = new HashMap();
\r
191 changes.put(new Integer(0), "other");
\r
192 changes.put(new Integer(1), "one");
\r
193 changes.put(new Integer(2), "few");
\r
194 changes.put(new Integer(5), "other");
\r
195 for (int i = 2; i < 20; ++i) {
\r
199 changes.put(new Integer(i*10 + 2), "few");
\r
200 changes.put(new Integer(i*10 + 5), "other");
\r
202 helperTestRules(localeIDs, testPattern, changes);
\r
205 public void TestPaucal1_2_34() {
\r
206 String localeIDs = "sl";
\r
207 String testPattern = "one{one} two{two} few{few} other{other}";
\r
208 Map changes = new HashMap();
\r
209 changes.put(new Integer(0), "other");
\r
210 changes.put(new Integer(1), "one");
\r
211 changes.put(new Integer(2), "two");
\r
212 changes.put(new Integer(3), "few");
\r
213 changes.put(new Integer(5), "other");
\r
214 changes.put(new Integer(101), "one");
\r
215 changes.put(new Integer(102), "two");
\r
216 changes.put(new Integer(103), "few");
\r
217 changes.put(new Integer(105), "other");
\r
218 helperTestRules(localeIDs, testPattern, changes);
\r
221 /* Tests the method public PluralRules getPluralRules() */
\r
222 public void TestGetPluralRules() {
\r
223 CurrencyPluralInfo cpi = new CurrencyPluralInfo();
\r
225 cpi.getPluralRules();
\r
226 } catch (Exception e) {
\r
227 errln("CurrencyPluralInfo.getPluralRules() was not suppose to " + "return an exception.");
\r
231 /* Tests the method public ULocale getLocale() */
\r
232 public void TestGetLocale() {
\r
233 CurrencyPluralInfo cpi = new CurrencyPluralInfo(new ULocale("en_US"));
\r
234 if (!cpi.getLocale().equals(new ULocale("en_US"))) {
\r
235 errln("CurrencyPluralInfo.getLocale() was suppose to return true " + "when passing the same ULocale");
\r
237 if (cpi.getLocale().equals(new ULocale("jp_JP"))) {
\r
238 errln("CurrencyPluralInfo.getLocale() was not suppose to return true " + "when passing a different ULocale");
\r
242 /* Tests the method public void setLocale(ULocale loc) */
\r
243 public void TestSetLocale() {
\r
244 CurrencyPluralInfo cpi = new CurrencyPluralInfo();
\r
245 cpi.setLocale(new ULocale("en_US"));
\r
246 if (!cpi.getLocale().equals(new ULocale("en_US"))) {
\r
247 errln("CurrencyPluralInfo.setLocale() was suppose to return true when passing the same ULocale");
\r
249 if (cpi.getLocale().equals(new ULocale("jp_JP"))) {
\r
250 errln("CurrencyPluralInfo.setLocale() was not suppose to return true when passing a different ULocale");
\r
254 /* Tests the method public boolean equals(Object a) */
\r
255 public void TestEquals(){
\r
256 CurrencyPluralInfo cpi = new CurrencyPluralInfo();
\r
258 errln("CurrencyPluralInfo.equals(Object) was not suppose to return true when comparing to an invalid object for integer 0.");
\r
260 if(cpi.equals(0.0)){
\r
261 errln("CurrencyPluralInfo.equals(Object) was not suppose to return true when comparing to an invalid object for float 0.");
\r
263 if(cpi.equals("0")){
\r
264 errln("CurrencyPluralInfo.equals(Object) was not suppose to return true when comparing to an invalid object for string 0.");
\r