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