• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* GENERATED SOURCE. DO NOT MODIFY. */
2 // © 2016 and later: Unicode, Inc. and others.
3 // License & terms of use: http://www.unicode.org/copyright.html#License
4 /*
5  *******************************************************************************
6  * Copyright (C) 2007-2014, International Business Machines Corporation and    *
7  * others. All Rights Reserved.                                                *
8  *******************************************************************************
9  */
10 package ohos.global.icu.dev.test.format;
11 
12 import java.util.HashMap;
13 import java.util.Locale;
14 import java.util.Map;
15 
16 import org.junit.Test;
17 import org.junit.runner.RunWith;
18 import org.junit.runners.JUnit4;
19 
20 import ohos.global.icu.dev.test.TestFmwk;
21 import ohos.global.icu.impl.Utility;
22 import ohos.global.icu.text.CurrencyPluralInfo;
23 import ohos.global.icu.text.NumberFormat;
24 import ohos.global.icu.text.PluralFormat;
25 import ohos.global.icu.util.ULocale;
26 
27 
28 /**
29  * @author tschumann (Tim Schumann)
30  *
31  */
32 
33 @RunWith(JUnit4.class)
34 public class PluralFormatTest extends TestFmwk {
helperTestRules(String localeIDs, String testPattern, Map<Integer,String> changes)35   private void helperTestRules(String localeIDs, String testPattern, Map<Integer,String> changes) {
36     String[] locales = Utility.split(localeIDs, ',');
37 
38     // Create example outputs for all supported locales.
39     /*
40     System.out.println("\n" + localeIDs);
41     String lastValue = (String) changes.get(new Integer(0));
42     int  lastNumber = 0;
43 
44     for (int i = 1; i < 199; ++i) {
45         if (changes.get(new Integer(i)) != null) {
46             if (lastNumber == i-1) {
47                 System.out.println(lastNumber + ": " + lastValue);
48             } else {
49                 System.out.println(lastNumber + "... " + (i-1) + ": " + lastValue);
50             }
51             lastNumber = i;
52             lastValue = (String) changes.get(new Integer(i));
53         }
54     }
55     System.out.println(lastNumber + "..." + 199 + ": " + lastValue);
56     */
57     log("test pattern: '" + testPattern + "'");
58     for (int i = 0; i < locales.length; ++i) {
59       try {
60         PluralFormat plf = new PluralFormat(new ULocale(locales[i]), testPattern);
61         log("plf: " + plf);
62         String expected = changes.get(new Integer(0));
63         for (int n = 0; n < 200; ++n) {
64           String value = changes.get(n);
65           if (value != null) {
66             expected = value;
67           }
68           assertEquals("Locale: " + locales[i] + ", number: " + n,
69                        expected, plf.format(n));
70         }
71       } catch (IllegalArgumentException e) {
72         errln(e.getMessage() + " locale: " + locales[i] + " pattern: '" + testPattern + "' " + System.currentTimeMillis());
73       }
74     }
75   }
76 
77   @Test
TestOneFormLocales()78   public void TestOneFormLocales() {
79     String localeIDs = "ja,ko,tr,vi";
80     String testPattern = "other{other}";
81     Map changes = new HashMap();
82     changes.put(new Integer(0), "other");
83     helperTestRules(localeIDs, testPattern, changes);
84   }
85 
86   @Test
TestSingular1Locales()87   public void TestSingular1Locales() {
88     String localeIDs = "bem,da,de,el,en,eo,es,et,fi,fo,he,it,mr,nb,nl,nn,no,pt_PT,sv,af,bg,ca,eu,fur,fy,ha,ku,lb,ml," +
89         "nah,ne,om,or,pap,ps,so,sq,sw,ta,te,tk,ur,mn,gsw,rm";
90     String testPattern = "one{one} other{other}";
91     Map changes = new HashMap();
92     changes.put(new Integer(0), "other");
93     changes.put(new Integer(1), "one");
94     changes.put(new Integer(2), "other");
95     helperTestRules(localeIDs, testPattern, changes);
96   }
97 
98   @Test
TestSingular01Locales()99   public void TestSingular01Locales() {
100     String localeIDs = "ff,fr,kab,gu,pa,pt,zu,bn";
101     String testPattern = "one{one} other{other}";
102     Map changes = new HashMap();
103     changes.put(new Integer(0), "one");
104     changes.put(new Integer(2), "other");
105     helperTestRules(localeIDs, testPattern, changes);
106   }
107 
108   @Test
TestZeroSingularLocales()109   public void TestZeroSingularLocales() {
110     String localeIDs = "lv";
111     String testPattern = "zero{zero} one{one} other{other}";
112     Map changes = new HashMap();
113     changes.put(new Integer(0), "zero");
114     changes.put(new Integer(1), "one");
115     for (int i = 2; i < 20; ++i) {
116       if (i < 10) {
117         changes.put(new Integer(i), "other");
118       } else {
119         changes.put(new Integer(i), "zero");
120       }
121       changes.put(new Integer(i*10), "zero");
122       if (i == 11) {
123         changes.put(new Integer(i*10 + 1), "zero");
124         changes.put(new Integer(i*10 + 2), "zero");
125       } else {
126         changes.put(new Integer(i*10 + 1), "one");
127         changes.put(new Integer(i*10 + 2), "other");
128       }
129     }
130     helperTestRules(localeIDs, testPattern, changes);
131   }
132 
133   @Test
TestSingularDual()134   public void TestSingularDual() {
135       String localeIDs = "ga";
136       String testPattern = "one{one} two{two} other{other}";
137       Map changes = new HashMap();
138       changes.put(new Integer(0), "other");
139       changes.put(new Integer(1), "one");
140       changes.put(new Integer(2), "two");
141       changes.put(new Integer(3), "other");
142       helperTestRules(localeIDs, testPattern, changes);
143   }
144 
145   @Test
TestSingularZeroSome()146   public void TestSingularZeroSome() {
147       String localeIDs = "ro";
148       String testPattern = "few{few} one{one} other{other}";
149       Map changes = new HashMap();
150       changes.put(new Integer(0), "few");
151       changes.put(new Integer(1), "one");
152       changes.put(new Integer(2), "few");
153       changes.put(new Integer(20), "other");
154       changes.put(new Integer(101), "other");
155       changes.put(new Integer(102), "few");
156       changes.put(new Integer(120), "other");
157       helperTestRules(localeIDs, testPattern, changes);
158   }
159 
160   @Test
TestSpecial12_19()161   public void TestSpecial12_19() {
162       String localeIDs = "lt";
163       String testPattern = "one{one} few{few} other{other}";
164       Map changes = new HashMap();
165       changes.put(new Integer(0), "other");
166       changes.put(new Integer(1), "one");
167       changes.put(new Integer(2), "few");
168       changes.put(new Integer(10), "other");
169       for (int i = 2; i < 20; ++i) {
170         if (i == 11) {
171           continue;
172         }
173         changes.put(new Integer(i*10 + 1), "one");
174         changes.put(new Integer(i*10 + 2), "few");
175         changes.put(new Integer((i+1)*10), "other");
176       }
177       helperTestRules(localeIDs, testPattern, changes);
178   }
179 
180   @Test
TestPaucalExcept11_14()181   public void TestPaucalExcept11_14() {
182       String localeIDs = "hr,sr,uk";
183       String testPattern = "one{one} few{few} other{other}";
184       Map changes = new HashMap();
185       changes.put(new Integer(0), "other");
186       changes.put(new Integer(1), "one");
187       changes.put(new Integer(2), "few");
188       changes.put(new Integer(5), "other");
189       for (int i = 2; i < 20; ++i) {
190         if (i == 11) {
191           continue;
192         }
193         changes.put(new Integer(i*10 + 1), "one");
194         changes.put(new Integer(i*10 + 2), "few");
195         changes.put(new Integer(i*10 + 5), "other");
196       }
197       helperTestRules(localeIDs, testPattern, changes);
198   }
199 
200   @Test
TestPaucalRu()201   public void TestPaucalRu() {
202       String localeIDs = "ru";
203       String testPattern = "one{one} many{many} other{other}";
204       Map changes = new HashMap();
205       for (int i = 0; i < 200; i+=10) {
206           if (i == 10 || i == 110) {
207               put(i, 0, 9, "many", changes);
208               continue;
209           }
210           put(i, 0, "many", changes);
211           put(i, 1, "one", changes);
212           put(i, 2, 4, "other", changes);
213           put(i, 5, 9, "many", changes);
214       }
215       helperTestRules(localeIDs, testPattern, changes);
216   }
217 
put(int base, int start, int end, T value, Map<Integer, T> m)218   public <T> void put(int base, int start, int end, T value, Map<Integer, T> m) {
219       for (int i = start; i <= end; ++i) {
220           if (m.containsKey(base + i)) {
221               throw new IllegalArgumentException();
222           }
223           m.put(base + i, value);
224       }
225   }
226 
put(int base, int start, T value, Map<Integer, T> m)227   public <T> void put(int base, int start, T value, Map<Integer, T> m) {
228       put(base, start, start, value, m);
229   }
230 
231   @Test
TestSingularPaucal()232   public void TestSingularPaucal() {
233       String localeIDs = "cs,sk";
234       String testPattern = "one{one} few{few} other{other}";
235       Map changes = new HashMap();
236       changes.put(new Integer(0), "other");
237       changes.put(new Integer(1), "one");
238       changes.put(new Integer(2), "few");
239       changes.put(new Integer(5), "other");
240       helperTestRules(localeIDs, testPattern, changes);
241   }
242 
243   @Test
TestPaucal1_234()244   public void TestPaucal1_234() {
245       String localeIDs = "pl";
246       String testPattern = "one{one} few{few} other{other}";
247       Map changes = new HashMap();
248       changes.put(new Integer(0), "other");
249       changes.put(new Integer(1), "one");
250       changes.put(new Integer(2), "few");
251       changes.put(new Integer(5), "other");
252       for (int i = 2; i < 20; ++i) {
253         if (i == 11) {
254           continue;
255         }
256         changes.put(new Integer(i*10 + 2), "few");
257         changes.put(new Integer(i*10 + 5), "other");
258       }
259       helperTestRules(localeIDs, testPattern, changes);
260   }
261 
262   @Test
TestPaucal1_2_34()263   public void TestPaucal1_2_34() {
264       String localeIDs = "sl";
265       String testPattern = "one{one} two{two} few{few} other{other}";
266       Map changes = new HashMap();
267       changes.put(new Integer(0), "other");
268       changes.put(new Integer(1), "one");
269       changes.put(new Integer(2), "two");
270       changes.put(new Integer(3), "few");
271       changes.put(new Integer(5), "other");
272       changes.put(new Integer(101), "one");
273       changes.put(new Integer(102), "two");
274       changes.put(new Integer(103), "few");
275       changes.put(new Integer(105), "other");
276       helperTestRules(localeIDs, testPattern, changes);
277   }
278 
279     /* Tests the method public PluralRules getPluralRules() */
280     @Test
TestGetPluralRules()281     public void TestGetPluralRules() {
282         CurrencyPluralInfo cpi = new CurrencyPluralInfo();
283         try {
284             cpi.getPluralRules();
285         } catch (Exception e) {
286             errln("CurrencyPluralInfo.getPluralRules() was not suppose to " + "return an exception.");
287         }
288     }
289 
290     /* Tests the method public ULocale getLocale() */
291     @Test
TestGetLocale()292     public void TestGetLocale() {
293         CurrencyPluralInfo cpi = new CurrencyPluralInfo(new ULocale("en_US"));
294         if (!cpi.getLocale().equals(new ULocale("en_US"))) {
295             errln("CurrencyPluralInfo.getLocale() was suppose to return true " + "when passing the same ULocale");
296         }
297         if (cpi.getLocale().equals(new ULocale("jp_JP"))) {
298             errln("CurrencyPluralInfo.getLocale() was not suppose to return true " + "when passing a different ULocale");
299         }
300     }
301 
302     /* Tests the method public void setLocale(ULocale loc) */
303     @Test
TestSetLocale()304     public void TestSetLocale() {
305         CurrencyPluralInfo cpi = new CurrencyPluralInfo();
306         cpi.setLocale(new ULocale("en_US"));
307         if (!cpi.getLocale().equals(new ULocale("en_US"))) {
308             errln("CurrencyPluralInfo.setLocale() was suppose to return true when passing the same ULocale");
309         }
310         if (cpi.getLocale().equals(new ULocale("jp_JP"))) {
311             errln("CurrencyPluralInfo.setLocale() was not suppose to return true when passing a different ULocale");
312         }
313     }
314 
315     /* Tests the method public boolean equals(Object a) */
316     @Test
TestEquals()317     public void TestEquals(){
318         CurrencyPluralInfo cpi = new CurrencyPluralInfo();
319         if(cpi.equals(0)){
320             errln("CurrencyPluralInfo.equals(Object) was not suppose to return true when comparing to an invalid object for integer 0.");
321         }
322         if(cpi.equals(0.0)){
323             errln("CurrencyPluralInfo.equals(Object) was not suppose to return true when comparing to an invalid object for float 0.");
324         }
325         if(cpi.equals("0")){
326             errln("CurrencyPluralInfo.equals(Object) was not suppose to return true when comparing to an invalid object for string 0.");
327         }
328     }
329 
330     /* Test for http://bugs.icu-project.org/trac/ticket/13151 */
331     @Test
TestFractionRounding()332     public void TestFractionRounding() {
333         NumberFormat nf = NumberFormat.getInstance(Locale.ENGLISH);
334         nf.setMaximumFractionDigits(0);
335         PluralFormat pf = new PluralFormat(ULocale.ENGLISH, "one{#kg}other{#kgs}");
336         pf.setNumberFormat(nf);
337         assertEquals("1.2kg", "1kg", pf.format(1.2));
338     }
339 }
340