]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/tests/core/src/com/ibm/icu/dev/test/format/PluralFormatTest.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / tests / core / src / com / ibm / icu / dev / test / format / PluralFormatTest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2007-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7 package com.ibm.icu.dev.test.format;
8
9 import java.util.HashMap;
10 import java.util.Map;
11
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;
17
18 /**
19  * @author tschumann (Tim Schumann)
20  *
21  */
22 public class PluralFormatTest extends TestFmwk {
23   
24   public static void main(String[] args) throws Exception {
25     new PluralFormatTest().run(args);
26   }
27   
28   private void helperTestRules(String localeIDs, String testPattern, Map changes) {
29     String[] locales = Utility.split(localeIDs, ',');
30     
31     // Create example outputs for all supported locales.
32     /*
33     System.out.println("\n" + localeIDs);
34     String lastValue = (String) changes.get(new Integer(0));
35     int  lastNumber = 0; 
36     
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);
41             } else {
42                 System.out.println(lastNumber + "... " + (i-1) + ": " + lastValue);
43             }
44             lastNumber = i;
45             lastValue = (String) changes.get(new Integer(i));
46         }
47     }
48     System.out.println(lastNumber + "..." + 199 + ": " + lastValue);
49     */
50     log("test pattern: '" + testPattern + "'");
51     for (int i = 0; i < locales.length; ++i) {
52       try {
53         PluralFormat plf = new PluralFormat(new ULocale(locales[i]), testPattern);
54         log("plf: " + plf);
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));
59           }
60           assertEquals("Locale: " + locales[i] + ", number: " + n,
61                        expected, plf.format(n));
62         }
63       } catch (IllegalArgumentException e) {
64         errln(e.getMessage() + " locale: " + locales[i] + " pattern: '" + testPattern + "' " + System.currentTimeMillis());
65       }
66     }
67   }
68   
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);
75   }
76   
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);
86   }
87   
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);
95   }
96   
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) {
105       if (i == 11) {
106         continue;
107       }
108       changes.put(new Integer(i*10 + 1), "one");
109       changes.put(new Integer(i*10 + 2), "other");
110     }
111     helperTestRules(localeIDs, testPattern, changes);
112   }
113   
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);
123   }
124   
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);
136   }
137   
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) {
147         if (i == 11) {
148           continue;
149         }
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");
153       }
154       helperTestRules(localeIDs, testPattern, changes);
155   }
156   
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) {
166         if (i == 11) {
167           continue;
168         }
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");
172       }
173       helperTestRules(localeIDs, testPattern, changes);
174   }
175   
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);
185   }
186   
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) {
196         if (i == 11) {
197           continue;
198         }
199         changes.put(new Integer(i*10 + 2), "few");
200         changes.put(new Integer(i*10 + 5), "other");
201       }
202       helperTestRules(localeIDs, testPattern, changes);
203   }
204   
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);
219   }
220   
221     /* Tests the method public PluralRules getPluralRules() */
222     public void TestGetPluralRules() {
223         CurrencyPluralInfo cpi = new CurrencyPluralInfo();
224         try {
225             cpi.getPluralRules();
226         } catch (Exception e) {
227             errln("CurrencyPluralInfo.getPluralRules() was not suppose to " + "return an exception.");
228         }
229     }
230
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");
236         }
237         if (cpi.getLocale().equals(new ULocale("jp_JP"))) {
238             errln("CurrencyPluralInfo.getLocale() was not suppose to return true " + "when passing a different ULocale");
239         }
240     }
241     
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");
248         }
249         if (cpi.getLocale().equals(new ULocale("jp_JP"))) {
250             errln("CurrencyPluralInfo.setLocale() was not suppose to return true when passing a different ULocale");
251         }
252     }
253     
254     /* Tests the method public boolean equals(Object a) */
255     public void TestEquals(){
256         CurrencyPluralInfo cpi = new CurrencyPluralInfo();
257         if(cpi.equals(0)){
258             errln("CurrencyPluralInfo.equals(Object) was not suppose to return true when comparing to an invalid object for integer 0.");
259         }
260         if(cpi.equals(0.0)){
261             errln("CurrencyPluralInfo.equals(Object) was not suppose to return true when comparing to an invalid object for float 0.");
262         }
263         if(cpi.equals("0")){
264             errln("CurrencyPluralInfo.equals(Object) was not suppose to return true when comparing to an invalid object for string 0.");
265         }
266     }
267 }