1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4 * COPYRIGHT:
5 * Copyright (c) 2007-2016, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
8
9 #include "unicode/utypes.h"
10
11 #if !UCONFIG_NO_FORMATTING
12
13 #include "unicode/dcfmtsym.h"
14 #include "unicode/decimfmt.h"
15 #include "unicode/msgfmt.h"
16 #include "unicode/plurfmt.h"
17 #include "unicode/plurrule.h"
18 #include "cmemory.h"
19 #include "plurfmts.h"
20 #include "plurults.h"
21
22 #define PLURAL_PATTERN_DATA 4
23 #define PLURAL_TEST_ARRAY_SIZE 256
24
25 #define PLURAL_SYNTAX_DATA 8
26
27 // The value must be same as PLKeywordLookups[] order.
28 #define PFT_ZERO 0
29 #define PFT_ONE 1
30 #define PFT_TWO 2
31 #define PFT_FEW 3
32 #define PFT_MANY 4
33 #define PFT_OTHER 5
34
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)35 void PluralFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
36 {
37 if (exec) logln("TestSuite PluralFormat");
38 TESTCASE_AUTO_BEGIN;
39 TESTCASE_AUTO(pluralFormatBasicTest);
40 TESTCASE_AUTO(pluralFormatUnitTest);
41 TESTCASE_AUTO(pluralFormatLocaleTest);
42 TESTCASE_AUTO(pluralFormatExtendedTest);
43 TESTCASE_AUTO(pluralFormatExtendedParseTest);
44 TESTCASE_AUTO(ordinalFormatTest);
45 TESTCASE_AUTO(TestDecimals);
46 TESTCASE_AUTO_END;
47 }
48
49 /**
50 * Test various generic API methods of PluralFormat for Basic usage.
51 */
pluralFormatBasicTest()52 void PluralFormatTest::pluralFormatBasicTest(/*char *par*/)
53 {
54 UErrorCode status[8];
55 PluralFormat* plFmt[8];
56 Locale locale = Locale::getDefault();
57 UnicodeString otherPattern = UnicodeString("other{#}");
58 UnicodeString message=UnicodeString("PluralFormat basic test");
59
60 // ========= Test constructors
61 logln(" Testing PluralFormat constructors ...");
62 status[0] = U_ZERO_ERROR;
63 PluralRules* plRules = PluralRules::createDefaultRules(status[0]);
64
65 status[0] = U_ZERO_ERROR;
66 NumberFormat *numFmt = NumberFormat::createInstance(status[0]);
67 if (U_FAILURE(status[0])) {
68 dataerrln("ERROR: Could not create NumberFormat instance with default locale ");
69 }
70
71 for (int32_t i=0; i< 8; ++i) {
72 status[i] = U_ZERO_ERROR;
73 }
74 plFmt[0] = new PluralFormat(status[0]);
75 plFmt[1] = new PluralFormat(*plRules, status[1]);
76 plFmt[2] = new PluralFormat(locale, status[2]);
77 plFmt[3] = new PluralFormat(locale, *plRules, status[3]);
78 plFmt[4] = new PluralFormat(otherPattern, status[4]);
79 plFmt[5] = new PluralFormat(*plRules, otherPattern, status[5]);
80 plFmt[6] = new PluralFormat(locale, otherPattern, status[6]);
81 plFmt[7] = new PluralFormat(locale, *plRules, otherPattern, status[7]);
82
83 for (int32_t i=0; i< 8; ++i) {
84 if (U_SUCCESS(status[i])) {
85 numberFormatTest(plFmt[i], numFmt, 1, 12, NULL, NULL, FALSE, &message);
86 numberFormatTest(plFmt[i], numFmt, 100, 112, NULL, NULL, FALSE, &message);
87 }
88 else {
89 dataerrln("ERROR: PluralFormat constructor failed!");
90 }
91 delete plFmt[i];
92 }
93 // ======= Test clone, assignment operator && == operator.
94 plFmt[0]= new PluralFormat(status[0]);
95 plFmt[0]->setNumberFormat(numFmt,status[0]);
96 UnicodeString us = UnicodeString("");
97 plFmt[0]->toPattern(us);
98 plFmt[1]= new PluralFormat(locale, status[1]);
99 if ( U_SUCCESS(status[0]) && U_SUCCESS(status[1]) ) {
100 *plFmt[1] = *plFmt[0];
101 if (plFmt[1]!=NULL) {
102 if ( *plFmt[1] != *plFmt[0] ) {
103 errln("ERROR: clone plural format test failed!");
104 }
105 }
106 }
107 else {
108 dataerrln("ERROR: PluralFormat constructor failed! - [0]%s [1]%s", u_errorName(status[0]), u_errorName(status[1]));
109 }
110 delete plFmt[0];
111
112 status[0] = U_ZERO_ERROR;
113 plFmt[0]= new PluralFormat(locale, status[0]);
114 if ( U_SUCCESS(status[0]) ) {
115 *plFmt[1] = *plFmt[0];
116 if (plFmt[1]!=NULL) {
117 if ( *plFmt[1] != *plFmt[0] ) {
118 errln("ERROR: assignment operator test failed!");
119 }
120 }
121 }
122 else {
123 dataerrln("ERROR: PluralFormat constructor failed! - %s", u_errorName(status[1]));
124 }
125
126 if ( U_SUCCESS(status[1]) ) {
127 plFmt[2] = plFmt[1]->clone();
128
129 if (plFmt[1]!=NULL) {
130 if ( *plFmt[1] != *plFmt[2] ) {
131 errln("ERROR: clone function test failed!");
132 }
133 }
134 delete plFmt[1];
135 delete plFmt[2];
136 }
137 else {
138 dataerrln("ERROR: PluralFormat clone failed! - %s", u_errorName(status[1]));
139 }
140
141 delete plFmt[0];
142 delete numFmt;
143 delete plRules;
144
145 // Tests parseObject
146 UErrorCode stat = U_ZERO_ERROR;
147 PluralFormat *pf = new PluralFormat(stat);
148 Formattable *f = new Formattable();
149 ParsePosition *pp = new ParsePosition();
150 pf->parseObject((UnicodeString)"",*f,*pp);
151 if(U_FAILURE(stat)) {
152 dataerrln("ERROR: PluralFormat::parseObject: %s", u_errorName(stat));
153 }
154 delete pf;
155 delete f;
156 delete pp;
157 }
158
159 /**
160 * Unit tests of PluralFormat class.
161 */
pluralFormatUnitTest()162 void PluralFormatTest::pluralFormatUnitTest(/*char *par*/)
163 {
164 UnicodeString patternTestData[PLURAL_PATTERN_DATA] = {
165 UNICODE_STRING_SIMPLE("odd {# is odd.} other{# is even.}"),
166 UNICODE_STRING_SIMPLE("other{# is odd or even.}"),
167 UNICODE_STRING_SIMPLE("odd{The number {0, number, #.#0} is odd.}other{The number {0, number, #.#0} is even.}"),
168 UNICODE_STRING_SIMPLE("odd{The number {1, number, #} is odd.}other{The number {2, number, #} is even.}"),
169 };
170 UnicodeString patternOddTestResult[PLURAL_PATTERN_DATA] = {
171 UNICODE_STRING_SIMPLE(" is odd."),
172 UNICODE_STRING_SIMPLE(" is odd or even."),
173 UNICODE_STRING_SIMPLE("The number {0, number, #.#0} is odd."),
174 UNICODE_STRING_SIMPLE("The number {1, number, #} is odd."),
175 };
176 UnicodeString patternEvenTestResult[PLURAL_PATTERN_DATA] = {
177 UNICODE_STRING_SIMPLE(" is even."),
178 UNICODE_STRING_SIMPLE(" is odd or even."),
179 UNICODE_STRING_SIMPLE("The number {0, number, #.#0} is even."),
180 UNICODE_STRING_SIMPLE("The number {2, number, #} is even."),
181 };
182 UnicodeString checkSyntaxtData[PLURAL_SYNTAX_DATA] = {
183 // ICU 4.8 does not check for duplicate keywords any more.
184 //UNICODE_STRING_SIMPLE("odd{foo} odd{bar} other{foobar}"),
185 //UNICODE_STRING_SIMPLE("odd{foo} other{bar} other{foobar}"),
186 UNICODE_STRING_SIMPLE("odd{foo}"),
187 // ICU 4.8 does not check for unknown keywords any more.
188 //UNICODE_STRING_SIMPLE("otto{foo} other{bar}"),
189 UNICODE_STRING_SIMPLE("*odd{foo} other{bar}"),
190 UNICODE_STRING_SIMPLE("odd{foo},other{bar}"),
191 UNICODE_STRING_SIMPLE("od d{foo} other{bar}"),
192 UNICODE_STRING_SIMPLE("odd{foo}{foobar}other{foo}"),
193 };
194
195 UErrorCode status = U_ZERO_ERROR;
196 UnicodeString oddAndEvenRule = UNICODE_STRING_SIMPLE("odd: n mod 2 is 1");
197 LocalPointer<PluralRules>plRules(PluralRules::createRules(oddAndEvenRule, status));
198 if (U_FAILURE(status)) {
199 dataerrln("ERROR: create PluralRules instance failed in unit tests.- exitting");
200 return;
201 }
202
203 // ======= Test PluralRules pattern syntax.
204 logln("Testing PluralRules pattern syntax.");
205 for (int32_t i=0; i<PLURAL_SYNTAX_DATA; ++i) {
206 status = U_ZERO_ERROR;
207
208 PluralFormat plFmt=PluralFormat(*plRules, status);
209 if (U_FAILURE(status)) {
210 dataerrln("ERROR: PluralFormat constructor failed in unit tests.- exitting");
211 return;
212 }
213 plFmt.applyPattern(checkSyntaxtData[i], status);
214 if (U_SUCCESS(status)) {
215 errln("ERROR: PluralFormat failed to detect syntax error with pattern: "+checkSyntaxtData[i]);
216 }
217 }
218
219
220
221 // ======= Test applying various pattern
222 logln("Testing various patterns");
223 status = U_ZERO_ERROR;
224 UBool overwrite[PLURAL_PATTERN_DATA] = {FALSE, FALSE, TRUE, TRUE};
225
226 LocalPointer<NumberFormat> numFmt(NumberFormat::createInstance(status));
227 UnicodeString message=UnicodeString("ERROR: PluralFormat tests various pattern ...");
228 if (U_FAILURE(status)) {
229 dataerrln("ERROR: Could not create NumberFormat instance with default locale ");
230 }
231 for(int32_t i=0; i<PLURAL_PATTERN_DATA; ++i) {
232 status = U_ZERO_ERROR;
233 PluralFormat plFmt=PluralFormat(*plRules, status);
234 if (U_FAILURE(status)) {
235 dataerrln("ERROR: PluralFormat constructor failed in unit tests.- exitting");
236 return;
237 }
238 plFmt.applyPattern(patternTestData[i], status);
239 if (U_FAILURE(status)) {
240 errln("ERROR: PluralFormat failed to apply pattern- "+patternTestData[i]);
241 continue;
242 }
243 numberFormatTest(&plFmt, numFmt.getAlias(), 1, 10, (UnicodeString *)&patternOddTestResult[i],
244 (UnicodeString *)&patternEvenTestResult[i], overwrite[i], &message);
245 }
246
247 // ======= Test set locale
248 status = U_ZERO_ERROR;
249 plRules.adoptInstead(PluralRules::createRules(UNICODE_STRING_SIMPLE("odd: n mod 2 is 1"), status));
250 PluralFormat pluralFmt = PluralFormat(*plRules, status);
251 if (U_FAILURE(status)) {
252 dataerrln("ERROR: Could not create PluralFormat instance in setLocale() test - exitting. ");
253 return;
254 }
255 pluralFmt.applyPattern(UNICODE_STRING_SIMPLE("odd{odd} other{even}"), status);
256 pluralFmt.setLocale(Locale::getEnglish(), status);
257 if (U_FAILURE(status)) {
258 dataerrln("ERROR: Could not setLocale() with English locale ");
259 return;
260 }
261 message = UNICODE_STRING_SIMPLE("Error set locale: pattern is not reset!");
262
263 // Check that pattern gets deleted.
264 logln("\n Test setLocale() ..\n");
265 numFmt.adoptInstead(NumberFormat::createInstance(Locale::getEnglish(), status));
266 if (U_FAILURE(status)) {
267 dataerrln("ERROR: Could not create NumberFormat instance with English locale ");
268 }
269 numberFormatTest(&pluralFmt, numFmt.getAlias(), 5, 5, NULL, NULL, FALSE, &message);
270 pluralFmt.applyPattern(UNICODE_STRING_SIMPLE("odd__{odd} other{even}"), status);
271 if (pluralFmt.format((int32_t)1, status) != UNICODE_STRING_SIMPLE("even")) {
272 errln("SetLocale should reset rules but did not.");
273 }
274 status = U_ZERO_ERROR;
275 pluralFmt.applyPattern(UNICODE_STRING_SIMPLE("one{one} other{not one}"), status);
276 if (U_FAILURE(status)) {
277 errln("SetLocale should reset rules but did not.");
278 }
279 UnicodeString one = UNICODE_STRING_SIMPLE("one");
280 UnicodeString notOne = UNICODE_STRING_SIMPLE("not one");
281 UnicodeString plResult, numResult;
282 for (int32_t i=0; i<20; ++i) {
283 plResult = pluralFmt.format(i, status);
284 if ( i==1 ) {
285 numResult = one;
286 }
287 else {
288 numResult = notOne;
289 }
290 if ( numResult != plResult ) {
291 errln("Wrong ruleset loaded by setLocale() - got:"+plResult+ UnicodeString(" expecting:")+numResult);
292 }
293 }
294
295 // =========== Test copy constructor
296 logln("Test copy constructor and == operator of PluralFormat");
297 PluralFormat dupPFmt = PluralFormat(pluralFmt);
298 if (pluralFmt != dupPFmt) {
299 errln("Failed in PluralFormat copy constructor or == operator");
300 }
301 }
302
303
304
305 /**
306 * Test locale data used in PluralFormat class.
307 */
308 void
pluralFormatLocaleTest()309 PluralFormatTest::pluralFormatLocaleTest(/*char *par*/)
310 {
311 int8_t pluralResults[PLURAL_TEST_ARRAY_SIZE]; // 0: is for default
312
313 // ======= Test DefaultRule
314 logln("Testing PluralRules with no rule.");
315 // for CLDR 24, here delete tr,
316 // add id lo ms th zh
317 const char* oneRuleLocales[8] = {"id", "ja", "ko", "lo", "ms", "th", "vi", "zh"};
318 UnicodeString testPattern = UNICODE_STRING_SIMPLE("other{other}");
319 uprv_memset(pluralResults, -1, sizeof(pluralResults));
320 pluralResults[0]= PFT_OTHER; // other
321 helperTestResults(oneRuleLocales, 8, testPattern, pluralResults);
322
323 // ====== Test Singular1 locales.
324 logln("Testing singular1 locales.");
325 // for CLDR 24, here delete da de en et fi gl he it nl pt pt sv bn ca gu is mr pa sw ur zu
326 // add hu tr others
327 const char* singular1Locales[56] = {"af","asa","az","bem","bez","bg","brx","chr",
328 "ckb","dv","ee","el","eo","es","eu","fo","fur","fy","gsw","ha",
329 "haw","hu","jgo","ka","kk","kl","ks","ku","lb","ml","mn","nah",
330 "nb","ne","nn","no","nr","om","or","pap","ps","rm","rof","sn",
331 "so", "sq","ta","te","tk","tn","tr","ts","vo","wae","xh","xog"};
332 testPattern = UNICODE_STRING_SIMPLE("one{one} other{other}");
333 uprv_memset(pluralResults, -1, sizeof(pluralResults));
334 pluralResults[0]= PFT_OTHER;
335 pluralResults[1]= PFT_ONE;
336 pluralResults[2]= PFT_OTHER;
337 helperTestResults(singular1Locales, 56, testPattern, pluralResults);
338
339 // ======== Test Singular01 locales.
340 logln("Testing singular1 locales.");
341 // for CLDR 24, here add hy
342 const char* singular01Locales[4] = {"ff","fr","hy","kab"};
343 testPattern = UNICODE_STRING_SIMPLE("one{one} other{other}");
344 uprv_memset(pluralResults, -1, sizeof(pluralResults));
345 pluralResults[0]= PFT_ONE;
346 pluralResults[2]= PFT_OTHER;
347 helperTestResults(singular01Locales, 4, testPattern, pluralResults);
348
349 // ======== Test ZeroSingular locales.
350 logln("Testing singular1 locales.");
351 const char* zeroSingularLocales[1] = {"lv"};
352 testPattern = UNICODE_STRING_SIMPLE("zero{zero} one{one} other{other}");
353 uprv_memset(pluralResults, -1, sizeof(pluralResults));
354 pluralResults[0]= PFT_ZERO;
355 pluralResults[1]= PFT_ONE;
356 for (int32_t i=2; i<20; ++i) {
357 pluralResults[i]= (i < 10)? PFT_OTHER: PFT_ZERO;
358 pluralResults[i*10] = PFT_ZERO;
359 pluralResults[i*10+1] = PFT_ONE; // note override after loop
360 pluralResults[i*10+2] = PFT_OTHER; // note override after loop
361 }
362 pluralResults[111]= PFT_ZERO;
363 pluralResults[112]= PFT_ZERO;
364 helperTestResults(zeroSingularLocales, 1, testPattern, pluralResults);
365
366 // ======== Test singular dual locales.
367 logln("Testing singular1 locales.");
368 const char* singularDualLocales[1] = {"ga"};
369 testPattern = UNICODE_STRING_SIMPLE("one{one} two{two} other{other}");
370 uprv_memset(pluralResults, -1, sizeof(pluralResults));
371 pluralResults[0]= PFT_OTHER;
372 pluralResults[1]= PFT_ONE;
373 pluralResults[2]= PFT_TWO;
374 pluralResults[3]= PFT_OTHER;
375 helperTestResults(singularDualLocales, 1, testPattern, pluralResults);
376
377 // ======== Test Singular Zero Some locales.
378 logln("Testing singular1 locales.");
379 const char* singularZeroSomeLocales[1] = {"ro"};
380 testPattern = UNICODE_STRING_SIMPLE("few{few} one{one} other{other}");
381 uprv_memset(pluralResults, -1, sizeof(pluralResults));
382 pluralResults[0]= PFT_FEW;
383 for (int32_t i=1; i<20; ++i) {
384 pluralResults[i] = PFT_FEW; // note override after loop
385 pluralResults[100+i] = PFT_FEW; // note override after loop
386 }
387 pluralResults[1]= PFT_ONE;
388 pluralResults[101]= PFT_OTHER;
389 helperTestResults(singularZeroSomeLocales, 1, testPattern, pluralResults);
390
391 // ======== Test Special 12/19.
392 logln("Testing special 12 and 19.");
393 const char* special12_19Locales[1] = {"lt"};
394 testPattern = UNICODE_STRING_SIMPLE("one{one} few{few} other{other}");
395 uprv_memset(pluralResults, -1, sizeof(pluralResults));
396 pluralResults[0]= PFT_OTHER;
397 pluralResults[1]= PFT_ONE;
398 for (int32_t i=2; i<20; ++i) {
399 pluralResults[i]= (i < 10)? PFT_FEW: PFT_OTHER;
400 pluralResults[i*10] = PFT_OTHER;
401 if (i==11) continue;
402 pluralResults[i*10+1] = PFT_ONE;
403 pluralResults[i*10+2] = PFT_FEW;
404 }
405 helperTestResults(special12_19Locales, 1, testPattern, pluralResults);
406
407 // ======== Test Paucal Except 11 14.
408 logln("Testing Paucal Except 11 and 14, set A.");
409 const char* paucal01LocalesA[2] = {"hr","sr"};
410 testPattern = UNICODE_STRING_SIMPLE("one{one} few{few} other{other}");
411 uprv_memset(pluralResults, -1, sizeof(pluralResults));
412 pluralResults[0]= PFT_OTHER;
413 pluralResults[1]= PFT_ONE;
414 for (int32_t i=2; i<20; ++i) {
415 pluralResults[i]= (i < 5)? PFT_FEW: PFT_OTHER;
416 if (i==11) continue;
417 pluralResults[i*10+1] = PFT_ONE;
418 pluralResults[i*10+2] = PFT_FEW;
419 pluralResults[i*10+5] = PFT_OTHER;
420 pluralResults[i*10+6] = PFT_OTHER;
421 pluralResults[i*10+7] = PFT_OTHER;
422 pluralResults[i*10+8] = PFT_OTHER;
423 pluralResults[i*10+9] = PFT_OTHER;
424 }
425 helperTestResults(paucal01LocalesA, 2, testPattern, pluralResults);
426
427 logln("Testing Paucal Except 11 and 14, set B.");
428 const char* paucal01LocalesB[1] = {"ru"};
429 testPattern = UNICODE_STRING_SIMPLE("one{one} many{many} other{other}");
430 uprv_memset(pluralResults, -1, sizeof(pluralResults));
431 pluralResults[0]= PFT_MANY;
432 pluralResults[1]= PFT_ONE;
433 for (int32_t i=2; i<20; ++i) {
434 pluralResults[i]= (i < 5)? PFT_OTHER: PFT_MANY;
435 if (i==11) continue;
436 pluralResults[i*10] = PFT_MANY;
437 pluralResults[i*10+1] = PFT_ONE;
438 pluralResults[i*10+2] = PFT_OTHER;
439 pluralResults[i*10+5] = PFT_MANY;
440 pluralResults[i*10+6] = PFT_MANY;
441 pluralResults[i*10+7] = PFT_MANY;
442 pluralResults[i*10+8] = PFT_MANY;
443 pluralResults[i*10+9] = PFT_MANY;
444 }
445 helperTestResults(paucal01LocalesB, 1, testPattern, pluralResults);
446
447 logln("Testing Paucal Except 11 and 14, set C.");
448 const char* paucal01LocalesC[1] = {"uk"};
449 testPattern = UNICODE_STRING_SIMPLE("one{one} many{many} few{few} other{other}");
450 uprv_memset(pluralResults, -1, sizeof(pluralResults));
451 pluralResults[0]= PFT_MANY;
452 pluralResults[1]= PFT_ONE;
453 for (int32_t i=2; i<20; ++i) {
454 pluralResults[i]= (i < 5)? PFT_FEW: PFT_MANY;
455 if (i==11) continue;
456 pluralResults[i*10] = PFT_MANY;
457 pluralResults[i*10+1] = PFT_ONE;
458 pluralResults[i*10+2] = PFT_FEW;
459 pluralResults[i*10+5] = PFT_MANY;
460 pluralResults[i*10+6] = PFT_MANY;
461 pluralResults[i*10+7] = PFT_MANY;
462 pluralResults[i*10+8] = PFT_MANY;
463 pluralResults[i*10+9] = PFT_MANY;
464 }
465 helperTestResults(paucal01LocalesC, 1, testPattern, pluralResults);
466
467 // ======== Test Singular Paucal.
468 logln("Testing Singular Paucal.");
469 const char* singularPaucalLocales[2] = {"cs","sk"};
470 testPattern = UNICODE_STRING_SIMPLE("one{one} few{few} other{other}");
471 uprv_memset(pluralResults, -1, sizeof(pluralResults));
472 pluralResults[0]= PFT_OTHER;
473 pluralResults[1]= PFT_ONE;
474 pluralResults[2]= PFT_FEW;
475 pluralResults[5]= PFT_OTHER;
476 helperTestResults(singularPaucalLocales, 2, testPattern, pluralResults);
477
478 // ======== Test Paucal (1), (2,3,4).
479 logln("Testing Paucal (1), (2,3,4).");
480 const char* paucal02Locales[1] = {"pl"};
481 testPattern = UNICODE_STRING_SIMPLE("one{one} many{many} few{few} other{other}");
482 uprv_memset(pluralResults, -1, sizeof(pluralResults));
483 for (int32_t i=0; i<20; ++i) {
484 pluralResults[i*10+0] = PFT_MANY;
485 pluralResults[i*10+1] = PFT_MANY; // note override after loop
486 if ((i==1)||(i==11)) {
487 pluralResults[i*10+2] = PFT_MANY;
488 pluralResults[i*10+3] = PFT_MANY;
489 pluralResults[i*10+4] = PFT_MANY;
490 }
491 else {
492 pluralResults[i*10+2] = PFT_FEW;
493 pluralResults[i*10+3] = PFT_FEW;
494 pluralResults[i*10+4] = PFT_FEW;
495 }
496 pluralResults[i*10+5] = PFT_MANY;
497 }
498 pluralResults[1]= PFT_ONE;
499 helperTestResults(paucal02Locales, 1, testPattern, pluralResults);
500
501 // ======== Test Paucal (1), (2), (3,4).
502 logln("Testing Paucal (1), (2), (3,4).");
503 const char* paucal03Locales[1] = {"sl"};
504 testPattern = UNICODE_STRING_SIMPLE("one{one} two{two} few{few} other{other}");
505 uprv_memset(pluralResults, -1, sizeof(pluralResults));
506 pluralResults[0]= PFT_OTHER;
507 pluralResults[1]= PFT_ONE;
508 pluralResults[2]= PFT_TWO;
509 pluralResults[3]= PFT_FEW;
510 pluralResults[5]= PFT_OTHER;
511 pluralResults[101]= PFT_ONE;
512 pluralResults[102]= PFT_TWO;
513 pluralResults[103]= PFT_FEW;
514 pluralResults[105]= PFT_OTHER;
515 helperTestResults(paucal03Locales, 1, testPattern, pluralResults);
516
517 // TODO: move this test to Unit Test after CLDR 1.6 is final and we support float
518 // ======= Test French "WITHIN rule
519 logln("Testing PluralRules with fr rule.");
520 testPattern = UNICODE_STRING_SIMPLE("one{one} other{other}");
521 Locale ulocale((const char *)"fr");
522 UErrorCode status = U_ZERO_ERROR;
523 PluralFormat plFmt(ulocale, testPattern, status);
524 if (U_FAILURE(status)) {
525 dataerrln("Failed to apply pattern to fr locale - %s", u_errorName(status));
526 }
527 else {
528 status = U_ZERO_ERROR;
529 UnicodeString plResult = plFmt.format(0.0, status); // retrun ONE
530 plResult = plFmt.format(0.5, status); // retrun ONE
531 plResult = plFmt.format(1.0, status); // retrun ONE
532 plResult = plFmt.format(1.9, status); // retrun ONE
533 plResult = plFmt.format(2.0, status); // retrun OTHER
534 }
535 }
536
537 void
pluralFormatExtendedTest(void)538 PluralFormatTest::pluralFormatExtendedTest(void) {
539 const char *targets[] = {
540 "There are no widgets.",
541 "There is one widget.",
542 "There is a bling widget and one other widget.",
543 "There is a bling widget and 2 other widgets.",
544 "There is a bling widget and 3 other widgets.",
545 "Widgets, five (5-1=4) there be.",
546 "There is a bling widget and 5 other widgets.",
547 "There is a bling widget and 6 other widgets.",
548 };
549
550 const char* fmt =
551 "offset:1.0 "
552 "=0 {There are no widgets.} "
553 "=1.0 {There is one widget.} "
554 "=5 {Widgets, five (5-1=#) there be.} "
555 "one {There is a bling widget and one other widget.} "
556 "other {There is a bling widget and # other widgets.}";
557
558 UErrorCode status = U_ZERO_ERROR;
559 UnicodeString fmtString(fmt, -1, US_INV);
560 PluralFormat pf(Locale::getEnglish(), fmtString, status);
561 MessageFormat mf(UNICODE_STRING_SIMPLE("{0,plural,").append(fmtString).append((UChar)0x7d /* '}' */),
562 Locale::getEnglish(), status);
563 Formattable args;
564 FieldPosition ignore;
565 if (U_FAILURE(status)) {
566 dataerrln("Failed to apply pattern - %s", u_errorName(status));
567 return;
568 }
569 for (int32_t i = 0; i <= 7; ++i) {
570 UnicodeString result = pf.format(i, status);
571 if (U_FAILURE(status)) {
572 errln("PluralFormat.format(value %d) failed - %s", i, u_errorName(status));
573 return;
574 }
575 UnicodeString expected(targets[i], -1, US_INV);
576 if (expected != result) {
577 UnicodeString message("PluralFormat.format(): Expected '", -1, US_INV);
578 message.append(expected);
579 message.append(UnicodeString("' but got '", -1, US_INV));
580 message.append(result);
581 message.append("'", -1, US_INV);
582 errln(message);
583 }
584 args.setLong(i);
585 mf.format(&args, 1, result.remove(), ignore, status);
586 if (U_FAILURE(status)) {
587 errln("MessageFormat.format(value %d) failed - %s", i, u_errorName(status));
588 return;
589 }
590 if (expected != result) {
591 UnicodeString message("MessageFormat.format(): Expected '", -1, US_INV);
592 message.append(expected);
593 message.append(UnicodeString("' but got '", -1, US_INV));
594 message.append(result);
595 message.append("'", -1, US_INV);
596 errln(message);
597 }
598 }
599 }
600
601 void
pluralFormatExtendedParseTest(void)602 PluralFormatTest::pluralFormatExtendedParseTest(void) {
603 const char *failures[] = {
604 "offset:1..0 =0 {Foo}",
605 "offset:1.0 {Foo}",
606 "=0= {Foo}",
607 "=0 {Foo} =0.0 {Bar}",
608 " = {Foo}",
609 };
610 int len = UPRV_LENGTHOF(failures);
611
612 for (int i = 0; i < len; ++i) {
613 UErrorCode status = U_ZERO_ERROR;
614 UnicodeString fmt(failures[i], -1, US_INV);
615 PluralFormat pf(fmt, status);
616 if (U_SUCCESS(status)) {
617 errln("expected failure when parsing '" + fmt + "'");
618 }
619 }
620 }
621
622 void
ordinalFormatTest(void)623 PluralFormatTest::ordinalFormatTest(void) {
624 IcuTestErrorCode errorCode(*this, "ordinalFormatTest");
625 UnicodeString pattern("one{#st file}two{#nd file}few{#rd file}other{#th file}");
626 PluralFormat pf(Locale::getEnglish(), UPLURAL_TYPE_ORDINAL, pattern, errorCode);
627 if (errorCode.errDataIfFailureAndReset("PluralFormat(en, UPLURAL_TYPE_ORDINAL, pattern) failed")) {
628 return;
629 }
630 UnicodeString result = pf.format((int32_t)321, errorCode);
631 if (!errorCode.errIfFailureAndReset("PluralFormat.format(321) failed") &&
632 result != UNICODE_STRING_SIMPLE("321st file")) {
633 errln(UnicodeString("PluralFormat.format(321) wrong result string: ") + result);
634 }
635 result = pf.format((int32_t)22, errorCode);
636 if (!errorCode.errIfFailureAndReset("PluralFormat.format(22) failed") &&
637 result != UNICODE_STRING_SIMPLE("22nd file")) {
638 errln(UnicodeString("PluralFormat.format(22) wrong result string: ") + result);
639 }
640 result = pf.format((int32_t)3, errorCode);
641 if (!errorCode.errIfFailureAndReset("PluralFormat.format(3) failed") &&
642 result != UNICODE_STRING_SIMPLE("3rd file")) {
643 errln(UnicodeString("PluralFormat.format(3) wrong result string: ") + result);
644 }
645
646 // Code coverage: Use the other new-for-UPluralType constructor as well.
647 PluralFormat pf2(Locale::getEnglish(), UPLURAL_TYPE_ORDINAL, errorCode);
648 pf2.applyPattern(pattern, errorCode);
649 if (errorCode.errIfFailureAndReset("PluralFormat(en, UPLURAL_TYPE_ORDINAL, pattern) failed")) {
650 return;
651 }
652 result = pf2.format((int32_t)456, errorCode);
653 if (!errorCode.errIfFailureAndReset("PluralFormat.format(456) failed") &&
654 result != UNICODE_STRING_SIMPLE("456th file")) {
655 errln(UnicodeString("PluralFormat.format(456) wrong result string: ") + result);
656 }
657 result = pf2.format((int32_t)111, errorCode);
658 if (!errorCode.errIfFailureAndReset("PluralFormat.format(111) failed") &&
659 result != UNICODE_STRING_SIMPLE("111th file")) {
660 errln(UnicodeString("PluralFormat.format(111) wrong result string: ") + result);
661 }
662 }
663
664 void
TestDecimals()665 PluralFormatTest::TestDecimals() {
666 IcuTestErrorCode errorCode(*this, "TestDecimals");
667 // Simple number replacement.
668 PluralFormat pf(Locale::getEnglish(), "one{one meter}other{# meters}", errorCode);
669 assertEquals("simple format(1)", "one meter", pf.format((int32_t)1, errorCode), TRUE);
670 assertEquals("simple format(1.5)", "1.5 meters", pf.format(1.5, errorCode), TRUE);
671 PluralFormat pf2(Locale::getEnglish(),
672 "offset:1 one{another meter}other{another # meters}", errorCode);
673 DecimalFormat df("0.0", new DecimalFormatSymbols(Locale::getEnglish(), errorCode), errorCode);
674 pf2.setNumberFormat(&df, errorCode);
675 assertEquals("offset-decimals format(1)", "another 0.0 meters", pf2.format((int32_t)1, errorCode), TRUE);
676 assertEquals("offset-decimals format(2)", "another 1.0 meters", pf2.format((int32_t)2, errorCode), TRUE);
677 assertEquals("offset-decimals format(2.5)", "another 1.5 meters", pf2.format(2.5, errorCode), TRUE);
678 errorCode.reset();
679 }
680
681 void
numberFormatTest(PluralFormat * plFmt,NumberFormat * numFmt,int32_t start,int32_t end,UnicodeString * numOddAppendStr,UnicodeString * numEvenAppendStr,UBool overwrite,UnicodeString * message)682 PluralFormatTest::numberFormatTest(PluralFormat* plFmt,
683 NumberFormat *numFmt,
684 int32_t start,
685 int32_t end,
686 UnicodeString *numOddAppendStr,
687 UnicodeString *numEvenAppendStr,
688 UBool overwrite, // overwrite the numberFormat.format result
689 UnicodeString *message) {
690 UErrorCode status = U_ZERO_ERROR;
691
692 if ( (plFmt==NULL) || (numFmt==NULL) ) {
693 dataerrln("ERROR: Could not create PluralFormat or NumberFormat - exitting");
694 return;
695 }
696 UnicodeString plResult, numResult ;
697
698 for (int32_t i=start; i<= end; ++i ) {
699 numResult.remove();
700 numResult = numFmt->format(i, numResult);
701 plResult = plFmt->format(i, status);
702 if ((numOddAppendStr!= NULL)&&(numEvenAppendStr!=NULL)) {
703 if (overwrite) {
704 if (i&1) {
705 numResult = *numOddAppendStr;
706 }
707 else {
708 numResult = *numEvenAppendStr;
709 }
710 }
711 else { // Append the string
712 if (i&1) {
713 numResult += *numOddAppendStr;
714 }
715 else{
716 numResult += *numEvenAppendStr;
717 }
718 }
719 }
720 if (U_FAILURE(status)) {
721 assertSuccess(*message + " in numberFormatTest", status);
722 }
723 if (numResult!=plResult) {
724 if ( message == NULL ) {
725 errln("ERROR: Unexpected plural format - got:"+plResult+ UnicodeString(" expecting:")+numResult);
726 }
727 else {
728 assertEquals(*message + " in numberFormatTest", numResult, plResult);
729 }
730 }
731 }
732 return;
733 }
734
735
736 void
helperTestResults(const char ** localeArray,int32_t capacityOfArray,UnicodeString & testPattern,int8_t * expResults)737 PluralFormatTest::helperTestResults(const char** localeArray,
738 int32_t capacityOfArray,
739 UnicodeString& testPattern,
740 int8_t *expResults) {
741 UErrorCode status;
742 UnicodeString plResult;
743 const UnicodeString PLKeywordLookups[6] = {
744 UNICODE_STRING_SIMPLE("zero"),
745 UNICODE_STRING_SIMPLE("one"),
746 UNICODE_STRING_SIMPLE("two"),
747 UNICODE_STRING_SIMPLE("few"),
748 UNICODE_STRING_SIMPLE("many"),
749 UNICODE_STRING_SIMPLE("other"),
750 };
751
752 for (int32_t i=0; i<capacityOfArray; ++i) {
753 const char *locale = localeArray[i];
754 Locale ulocale((const char *)locale);
755 status = U_ZERO_ERROR;
756 PluralFormat plFmt(ulocale, testPattern, status);
757 if (U_FAILURE(status)) {
758 dataerrln("Failed to apply pattern to locale:"+UnicodeString(localeArray[i]) + " - " + u_errorName(status));
759 continue;
760 }
761 for (int32_t n=0; n<PLURAL_TEST_ARRAY_SIZE; ++n) {
762 if (expResults[n]!=-1) {
763 status = U_ZERO_ERROR;
764 plResult = plFmt.format(n, status);
765 if (U_FAILURE(status)) {
766 errln("ERROR: Failed to format number in locale data tests with locale: "+
767 UnicodeString(localeArray[i]));
768 }
769 if (plResult != PLKeywordLookups[expResults[n]]){
770 plResult = plFmt.format(n, status);
771 errln("ERROR: Unexpected format result in locale: "+UnicodeString(localeArray[i])+
772 UnicodeString(" for value: ")+n+
773 UnicodeString(" got:")+plResult+
774 UnicodeString(" expecting:")+ PLKeywordLookups[expResults[n]]);
775 }
776 }
777 }
778 }
779 }
780
781 #endif /* #if !UCONFIG_NO_FORMATTING */
782