1 /* 2 ******************************************************************************* 3 * Copyright (C) 2007-2014, 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 main(String[] args)24 public static void main(String[] args) throws Exception { 25 new PluralFormatTest().run(args); 26 } 27 helperTestRules(String localeIDs, String testPattern, Map<Integer,String> changes)28 private void helperTestRules(String localeIDs, String testPattern, Map<Integer,String> 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 String value = changes.get(n); 58 if (value != null) { 59 expected = value; 60 } 61 assertEquals("Locale: " + locales[i] + ", number: " + n, 62 expected, plf.format(n)); 63 } 64 } catch (IllegalArgumentException e) { 65 errln(e.getMessage() + " locale: " + locales[i] + " pattern: '" + testPattern + "' " + System.currentTimeMillis()); 66 } 67 } 68 } 69 TestOneFormLocales()70 public void TestOneFormLocales() { 71 String localeIDs = "ja,ko,tr,vi"; 72 String testPattern = "other{other}"; 73 Map changes = new HashMap(); 74 changes.put(new Integer(0), "other"); 75 helperTestRules(localeIDs, testPattern, changes); 76 } 77 TestSingular1Locales()78 public void TestSingular1Locales() { 79 String localeIDs = "bem,da,de,el,en,eo,es,et,fi,fo,he,it,nb,nl,nn,no,pt_PT,sv,af,bg,ca,eu,fur,fy,ha,ku,lb,ml," + 80 "nah,ne,om,or,pap,ps,so,sq,sw,ta,te,tk,ur,mn,gsw,rm"; 81 String testPattern = "one{one} other{other}"; 82 Map changes = new HashMap(); 83 changes.put(new Integer(0), "other"); 84 changes.put(new Integer(1), "one"); 85 changes.put(new Integer(2), "other"); 86 helperTestRules(localeIDs, testPattern, changes); 87 } 88 TestSingular01Locales()89 public void TestSingular01Locales() { 90 String localeIDs = "ff,fr,kab,gu,mr,pa,pt,zu,bn"; 91 String testPattern = "one{one} other{other}"; 92 Map changes = new HashMap(); 93 changes.put(new Integer(0), "one"); 94 changes.put(new Integer(2), "other"); 95 helperTestRules(localeIDs, testPattern, changes); 96 } 97 TestZeroSingularLocales()98 public void TestZeroSingularLocales() { 99 String localeIDs = "lv"; 100 String testPattern = "zero{zero} one{one} other{other}"; 101 Map changes = new HashMap(); 102 changes.put(new Integer(0), "zero"); 103 changes.put(new Integer(1), "one"); 104 for (int i = 2; i < 20; ++i) { 105 if (i < 10) { 106 changes.put(new Integer(i), "other"); 107 } else { 108 changes.put(new Integer(i), "zero"); 109 } 110 changes.put(new Integer(i*10), "zero"); 111 if (i == 11) { 112 changes.put(new Integer(i*10 + 1), "zero"); 113 changes.put(new Integer(i*10 + 2), "zero"); 114 } else { 115 changes.put(new Integer(i*10 + 1), "one"); 116 changes.put(new Integer(i*10 + 2), "other"); 117 } 118 } 119 helperTestRules(localeIDs, testPattern, changes); 120 } 121 TestSingularDual()122 public void TestSingularDual() { 123 String localeIDs = "ga"; 124 String testPattern = "one{one} two{two} other{other}"; 125 Map changes = new HashMap(); 126 changes.put(new Integer(0), "other"); 127 changes.put(new Integer(1), "one"); 128 changes.put(new Integer(2), "two"); 129 changes.put(new Integer(3), "other"); 130 helperTestRules(localeIDs, testPattern, changes); 131 } 132 TestSingularZeroSome()133 public void TestSingularZeroSome() { 134 String localeIDs = "ro"; 135 String testPattern = "few{few} one{one} other{other}"; 136 Map changes = new HashMap(); 137 changes.put(new Integer(0), "few"); 138 changes.put(new Integer(1), "one"); 139 changes.put(new Integer(2), "few"); 140 changes.put(new Integer(20), "other"); 141 changes.put(new Integer(101), "few"); 142 changes.put(new Integer(120), "other"); 143 helperTestRules(localeIDs, testPattern, changes); 144 } 145 TestSpecial12_19()146 public void TestSpecial12_19() { 147 String localeIDs = "lt"; 148 String testPattern = "one{one} few{few} other{other}"; 149 Map changes = new HashMap(); 150 changes.put(new Integer(0), "other"); 151 changes.put(new Integer(1), "one"); 152 changes.put(new Integer(2), "few"); 153 changes.put(new Integer(10), "other"); 154 for (int i = 2; i < 20; ++i) { 155 if (i == 11) { 156 continue; 157 } 158 changes.put(new Integer(i*10 + 1), "one"); 159 changes.put(new Integer(i*10 + 2), "few"); 160 changes.put(new Integer((i+1)*10), "other"); 161 } 162 helperTestRules(localeIDs, testPattern, changes); 163 } 164 TestPaucalExcept11_14()165 public void TestPaucalExcept11_14() { 166 String localeIDs = "hr,sr,uk"; 167 String testPattern = "one{one} few{few} other{other}"; 168 Map changes = new HashMap(); 169 changes.put(new Integer(0), "other"); 170 changes.put(new Integer(1), "one"); 171 changes.put(new Integer(2), "few"); 172 changes.put(new Integer(5), "other"); 173 for (int i = 2; i < 20; ++i) { 174 if (i == 11) { 175 continue; 176 } 177 changes.put(new Integer(i*10 + 1), "one"); 178 changes.put(new Integer(i*10 + 2), "few"); 179 changes.put(new Integer(i*10 + 5), "other"); 180 } 181 helperTestRules(localeIDs, testPattern, changes); 182 } 183 TestPaucalRu()184 public void TestPaucalRu() { 185 String localeIDs = "ru"; 186 String testPattern = "one{one} many{many} other{other}"; 187 Map changes = new HashMap(); 188 for (int i = 0; i < 200; i+=10) { 189 if (i == 10 || i == 110) { 190 put(i, 0, 9, "many", changes); 191 continue; 192 } 193 put(i, 0, "many", changes); 194 put(i, 1, "one", changes); 195 put(i, 2, 4, "other", changes); 196 put(i, 5, 9, "many", changes); 197 } 198 helperTestRules(localeIDs, testPattern, changes); 199 } 200 put(int base, int start, int end, T value, Map<Integer, T> m)201 public <T> void put(int base, int start, int end, T value, Map<Integer, T> m) { 202 for (int i = start; i <= end; ++i) { 203 if (m.containsKey(base + i)) { 204 throw new IllegalArgumentException(); 205 } 206 m.put(base + i, value); 207 } 208 } 209 put(int base, int start, T value, Map<Integer, T> m)210 public <T> void put(int base, int start, T value, Map<Integer, T> m) { 211 put(base, start, start, value, m); 212 } 213 TestSingularPaucal()214 public void TestSingularPaucal() { 215 String localeIDs = "cs,sk"; 216 String testPattern = "one{one} few{few} other{other}"; 217 Map changes = new HashMap(); 218 changes.put(new Integer(0), "other"); 219 changes.put(new Integer(1), "one"); 220 changes.put(new Integer(2), "few"); 221 changes.put(new Integer(5), "other"); 222 helperTestRules(localeIDs, testPattern, changes); 223 } 224 TestPaucal1_234()225 public void TestPaucal1_234() { 226 String localeIDs = "pl"; 227 String testPattern = "one{one} few{few} other{other}"; 228 Map changes = new HashMap(); 229 changes.put(new Integer(0), "other"); 230 changes.put(new Integer(1), "one"); 231 changes.put(new Integer(2), "few"); 232 changes.put(new Integer(5), "other"); 233 for (int i = 2; i < 20; ++i) { 234 if (i == 11) { 235 continue; 236 } 237 changes.put(new Integer(i*10 + 2), "few"); 238 changes.put(new Integer(i*10 + 5), "other"); 239 } 240 helperTestRules(localeIDs, testPattern, changes); 241 } 242 TestPaucal1_2_34()243 public void TestPaucal1_2_34() { 244 String localeIDs = "sl"; 245 String testPattern = "one{one} two{two} few{few} other{other}"; 246 Map changes = new HashMap(); 247 changes.put(new Integer(0), "other"); 248 changes.put(new Integer(1), "one"); 249 changes.put(new Integer(2), "two"); 250 changes.put(new Integer(3), "few"); 251 changes.put(new Integer(5), "other"); 252 changes.put(new Integer(101), "one"); 253 changes.put(new Integer(102), "two"); 254 changes.put(new Integer(103), "few"); 255 changes.put(new Integer(105), "other"); 256 helperTestRules(localeIDs, testPattern, changes); 257 } 258 259 /* Tests the method public PluralRules getPluralRules() */ TestGetPluralRules()260 public void TestGetPluralRules() { 261 CurrencyPluralInfo cpi = new CurrencyPluralInfo(); 262 try { 263 cpi.getPluralRules(); 264 } catch (Exception e) { 265 errln("CurrencyPluralInfo.getPluralRules() was not suppose to " + "return an exception."); 266 } 267 } 268 269 /* Tests the method public ULocale getLocale() */ TestGetLocale()270 public void TestGetLocale() { 271 CurrencyPluralInfo cpi = new CurrencyPluralInfo(new ULocale("en_US")); 272 if (!cpi.getLocale().equals(new ULocale("en_US"))) { 273 errln("CurrencyPluralInfo.getLocale() was suppose to return true " + "when passing the same ULocale"); 274 } 275 if (cpi.getLocale().equals(new ULocale("jp_JP"))) { 276 errln("CurrencyPluralInfo.getLocale() was not suppose to return true " + "when passing a different ULocale"); 277 } 278 } 279 280 /* Tests the method public void setLocale(ULocale loc) */ TestSetLocale()281 public void TestSetLocale() { 282 CurrencyPluralInfo cpi = new CurrencyPluralInfo(); 283 cpi.setLocale(new ULocale("en_US")); 284 if (!cpi.getLocale().equals(new ULocale("en_US"))) { 285 errln("CurrencyPluralInfo.setLocale() was suppose to return true when passing the same ULocale"); 286 } 287 if (cpi.getLocale().equals(new ULocale("jp_JP"))) { 288 errln("CurrencyPluralInfo.setLocale() was not suppose to return true when passing a different ULocale"); 289 } 290 } 291 292 /* Tests the method public boolean equals(Object a) */ TestEquals()293 public void TestEquals(){ 294 CurrencyPluralInfo cpi = new CurrencyPluralInfo(); 295 if(cpi.equals(0)){ 296 errln("CurrencyPluralInfo.equals(Object) was not suppose to return true when comparing to an invalid object for integer 0."); 297 } 298 if(cpi.equals(0.0)){ 299 errln("CurrencyPluralInfo.equals(Object) was not suppose to return true when comparing to an invalid object for float 0."); 300 } 301 if(cpi.equals("0")){ 302 errln("CurrencyPluralInfo.equals(Object) was not suppose to return true when comparing to an invalid object for string 0."); 303 } 304 } 305 } 306