]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/plurfmts.cpp
ICU-461.18.tar.gz
[apple/icu.git] / icuSources / test / intltest / plurfmts.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 2007-2010, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7 #include "unicode/utypes.h"
8
9 #if !UCONFIG_NO_FORMATTING
10
11 #include "plurults.h"
12 #include "plurfmts.h"
13 #include "cmemory.h"
14 #include "unicode/plurrule.h"
15 #include "unicode/plurfmt.h"
16
17
18 #define PLURAL_PATTERN_DATA 4
19 #define PLURAL_TEST_ARRAY_SIZE 256
20
21 #define PLURAL_SYNTAX_DATA 8
22
23 // The value must be same as PLKeywordLookups[] order.
24 #define PFT_ZERO 0
25 #define PFT_ONE 1
26 #define PFT_TWO 2
27 #define PFT_FEW 3
28 #define PFT_MANY 4
29 #define PFT_OTHER 5
30
31 void PluralFormatTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
32 {
33 if (exec) logln("TestSuite PluralFormat");
34 switch (index) {
35 TESTCASE(0, pluralFormatBasicTest);
36 TESTCASE(1, pluralFormatUnitTest);
37 TESTCASE(2, pluralFormatLocaleTest);
38 default: name = "";
39 break;
40 }
41 }
42
43 /**
44 * Test various generic API methods of PluralFormat for Basic usage.
45 */
46 void PluralFormatTest::pluralFormatBasicTest(/*char *par*/)
47 {
48 UErrorCode status[8];
49 PluralFormat* plFmt[8];
50 Locale locale = Locale::getDefault();
51 UnicodeString otherPattern = UnicodeString("other{#}");
52 UnicodeString message=UnicodeString("ERROR: PluralFormat basic test");
53
54 // ========= Test constructors
55 logln(" Testing PluralFormat constructors ...");
56 status[0] = U_ZERO_ERROR;
57 PluralRules* plRules = PluralRules::createDefaultRules(status[0]);
58
59 status[0] = U_ZERO_ERROR;
60 NumberFormat *numFmt = NumberFormat::createInstance(status[0]);
61 if (U_FAILURE(status[0])) {
62 dataerrln("ERROR: Could not create NumberFormat instance with default locale ");
63 }
64
65 for (int32_t i=0; i< 8; ++i) {
66 status[i] = U_ZERO_ERROR;
67 }
68 plFmt[0] = new PluralFormat(status[0]);
69 plFmt[1] = new PluralFormat(*plRules, status[1]);
70 plFmt[2] = new PluralFormat(locale, status[2]);
71 plFmt[3] = new PluralFormat(locale, *plRules, status[3]);
72 plFmt[4] = new PluralFormat(otherPattern, status[4]);
73 plFmt[5] = new PluralFormat(*plRules, otherPattern, status[5]);
74 plFmt[6] = new PluralFormat(locale, otherPattern, status[6]);
75 plFmt[7] = new PluralFormat(locale, *plRules, otherPattern, status[7]);
76
77 for (int32_t i=0; i< 8; ++i) {
78 if (U_SUCCESS(status[i])) {
79 numberFormatTest(plFmt[i], numFmt, 1, 12, NULL, NULL, FALSE, &message);
80 numberFormatTest(plFmt[i], numFmt, 100, 112, NULL, NULL, FALSE, &message);
81 }
82 else {
83 dataerrln("ERROR: PluralFormat constructor failed!");
84 }
85 delete plFmt[i];
86 }
87 // ======= Test clone, assignment operator && == operator.
88 plFmt[0]= new PluralFormat(status[0]);
89 plFmt[0]->setNumberFormat(numFmt,status[0]);
90 UnicodeString us = UnicodeString("");
91 plFmt[0]->toPattern(us);
92 plFmt[1]= new PluralFormat(locale, status[1]);
93 if ( U_SUCCESS(status[0]) && U_SUCCESS(status[1]) ) {
94 *plFmt[1] = *plFmt[0];
95 if (plFmt[1]!=NULL) {
96 if ( *plFmt[1] != *plFmt[0] ) {
97 errln("ERROR: clone plural format test failed!");
98 }
99 }
100 }
101 else {
102 dataerrln("ERROR: PluralFormat constructor failed! - [0]%s [1]%s", u_errorName(status[0]), u_errorName(status[1]));
103 }
104 delete plFmt[0];
105
106 status[0] = U_ZERO_ERROR;
107 plFmt[0]= new PluralFormat(locale, status[0]);
108 if ( U_SUCCESS(status[0]) ) {
109 *plFmt[1] = *plFmt[0];
110 if (plFmt[1]!=NULL) {
111 if ( *plFmt[1] != *plFmt[0] ) {
112 errln("ERROR: assignment operator test failed!");
113 }
114 }
115 }
116 else {
117 dataerrln("ERROR: PluralFormat constructor failed! - %s", u_errorName(status[1]));
118 }
119
120 if ( U_SUCCESS(status[1]) ) {
121 plFmt[2] = (PluralFormat*) plFmt[1]->clone();
122
123 if (plFmt[1]!=NULL) {
124 if ( *plFmt[1] != *plFmt[2] ) {
125 errln("ERROR: clone function test failed!");
126 }
127 }
128 delete plFmt[1];
129 delete plFmt[2];
130 }
131 else {
132 dataerrln("ERROR: PluralFormat clone failed! - %s", u_errorName(status[1]));
133 }
134
135 delete plFmt[0];
136 delete numFmt;
137 delete plRules;
138
139 // Tests parseObject
140 UErrorCode stat = U_ZERO_ERROR;
141 PluralFormat *pf = new PluralFormat(stat);
142 Formattable *f = new Formattable();
143 ParsePosition *pp = new ParsePosition();
144 pf->parseObject((UnicodeString)"",*f,*pp);
145 if(U_FAILURE(stat)) {
146 dataerrln("ERROR: PluralFormat::parseObject: %s", u_errorName(stat));
147 }
148 delete pf;
149 delete f;
150 delete pp;
151 }
152
153 /**
154 * Unit tests of PluralFormat class.
155 */
156 void PluralFormatTest::pluralFormatUnitTest(/*char *par*/)
157 {
158 UnicodeString patternTestData[PLURAL_PATTERN_DATA] = {
159 UNICODE_STRING_SIMPLE("odd {# is odd.} other{# is even.}"),
160 UNICODE_STRING_SIMPLE("other{# is odd or even.}"),
161 UNICODE_STRING_SIMPLE("odd{The number {0, number, #.#0} is odd.}other{The number {0, number, #.#0} is even.}"),
162 UNICODE_STRING_SIMPLE("odd{The number {#} is odd.}other{The number {#} is even.}"),
163 };
164 UnicodeString patternOddTestResult[PLURAL_PATTERN_DATA] = {
165 UNICODE_STRING_SIMPLE(" is odd."),
166 UNICODE_STRING_SIMPLE(" is odd or even."),
167 UNICODE_STRING_SIMPLE("The number {0, number, #.#0} is odd."),
168 UNICODE_STRING_SIMPLE("The number {#} is odd."),
169 };
170 UnicodeString patternEvenTestResult[PLURAL_PATTERN_DATA] = {
171 UNICODE_STRING_SIMPLE(" is even."),
172 UNICODE_STRING_SIMPLE(" is odd or even."),
173 UNICODE_STRING_SIMPLE("The number {0, number, #.#0} is even."),
174 UNICODE_STRING_SIMPLE("The number {#} is even."),
175 };
176 UnicodeString checkSyntaxtData[PLURAL_SYNTAX_DATA] = {
177 UNICODE_STRING_SIMPLE("odd{foo} odd{bar} other{foobar}"),
178 UNICODE_STRING_SIMPLE("odd{foo} other{bar} other{foobar}"),
179 UNICODE_STRING_SIMPLE("odd{foo}"),
180 UNICODE_STRING_SIMPLE("otto{foo} other{bar}"),
181 UNICODE_STRING_SIMPLE("1odd{foo} other{bar}"),
182 UNICODE_STRING_SIMPLE("odd{foo},other{bar}"),
183 UNICODE_STRING_SIMPLE("od d{foo} other{bar}"),
184 UNICODE_STRING_SIMPLE("odd{foo}{foobar}other{foo}"),
185 };
186
187 UErrorCode status = U_ZERO_ERROR;
188 UnicodeString oddAndEvenRule = UNICODE_STRING_SIMPLE("odd: n mod 2 is 1");
189 PluralRules* plRules = PluralRules::createRules(oddAndEvenRule, status);
190 if (U_FAILURE(status)) {
191 dataerrln("ERROR: create PluralRules instance failed in unit tests.- exitting");
192 return;
193 }
194
195 // ======= Test PluralRules pattern syntax.
196 logln("Testing PluralRules pattern syntax.");
197 for (int32_t i=0; i<PLURAL_SYNTAX_DATA; ++i) {
198 status = U_ZERO_ERROR;
199
200 PluralFormat plFmt=PluralFormat(*plRules, status);
201 if (U_FAILURE(status)) {
202 dataerrln("ERROR: PluralFormat constructor failed in unit tests.- exitting");
203 return;
204 }
205 plFmt.applyPattern(checkSyntaxtData[i], status);
206 if (U_SUCCESS(status)) {
207 errln("ERROR: PluralFormat failed to detect syntax error with pattern: "+checkSyntaxtData[i]);
208 }
209 }
210
211
212
213 // ======= Test applying various pattern
214 logln("Testing various patterns");
215 status = U_ZERO_ERROR;
216 UBool overwrite[PLURAL_PATTERN_DATA] = {FALSE, FALSE, TRUE, TRUE};
217
218 NumberFormat *numFmt = NumberFormat::createInstance(status);
219 UnicodeString message=UnicodeString("ERROR: PluralFormat tests various pattern ...");
220 if (U_FAILURE(status)) {
221 dataerrln("ERROR: Could not create NumberFormat instance with default locale ");
222 }
223 for(int32_t i=0; i<PLURAL_PATTERN_DATA; ++i) {
224 status = U_ZERO_ERROR;
225 PluralFormat plFmt=PluralFormat(*plRules, status);
226 if (U_FAILURE(status)) {
227 dataerrln("ERROR: PluralFormat constructor failed in unit tests.- exitting");
228 return;
229 }
230 plFmt.applyPattern(patternTestData[i], status);
231 if (U_FAILURE(status)) {
232 errln("ERROR: PluralFormat failed to apply pattern- "+patternTestData[i]);
233 continue;
234 }
235 numberFormatTest(&plFmt, numFmt, 1, 10, (UnicodeString *)&patternOddTestResult[i],
236 (UnicodeString *)&patternEvenTestResult[i], overwrite[i], &message);
237 }
238 delete plRules;
239 delete numFmt;
240
241 // ======= Test set locale
242 status = U_ZERO_ERROR;
243 plRules = PluralRules::createRules(UNICODE_STRING_SIMPLE("odd: n mod 2 is 1"), status);
244 PluralFormat pluralFmt = PluralFormat(*plRules, status);
245 if (U_FAILURE(status)) {
246 dataerrln("ERROR: Could not create PluralFormat instance in setLocale() test - exitting. ");
247 delete plRules;
248 return;
249 }
250 pluralFmt.applyPattern(UNICODE_STRING_SIMPLE("odd{odd} other{even}"), status);
251 pluralFmt.setLocale(Locale::getEnglish(), status);
252 if (U_FAILURE(status)) {
253 dataerrln("ERROR: Could not setLocale() with English locale ");
254 delete plRules;
255 return;
256 }
257 message = UNICODE_STRING_SIMPLE("Error set locale: pattern is not reset!");
258
259 // Check that pattern gets deleted.
260 logln("\n Test setLocale() ..\n");
261 numFmt = NumberFormat::createInstance(Locale::getEnglish(), status);
262 if (U_FAILURE(status)) {
263 dataerrln("ERROR: Could not create NumberFormat instance with English locale ");
264 }
265 numberFormatTest(&pluralFmt, numFmt, 5, 5, NULL, NULL, FALSE, &message);
266 pluralFmt.applyPattern(UNICODE_STRING_SIMPLE("odd__{odd} other{even}"), status);
267 if (U_SUCCESS(status)) {
268 errln("SetLocale should reset rules but did not.");
269 }
270 status = U_ZERO_ERROR;
271 pluralFmt.applyPattern(UNICODE_STRING_SIMPLE("one{one} other{not one}"), status);
272 if (U_FAILURE(status)) {
273 errln("SetLocale should reset rules but did not.");
274 }
275 UnicodeString one = UNICODE_STRING_SIMPLE("one");
276 UnicodeString notOne = UNICODE_STRING_SIMPLE("not one");
277 UnicodeString plResult, numResult;
278 for (int32_t i=0; i<20; ++i) {
279 plResult = pluralFmt.format(i, status);
280 if ( i==1 ) {
281 numResult = one;
282 }
283 else {
284 numResult = notOne;
285 }
286 if ( numResult != plResult ) {
287 errln("Wrong ruleset loaded by setLocale() - got:"+plResult+ UnicodeString(" expecting:")+numResult);
288 }
289 }
290
291 // =========== Test copy constructor
292 logln("Test copy constructor and == operator of PluralFormat");
293 PluralFormat dupPFmt = PluralFormat(pluralFmt);
294 if (pluralFmt != dupPFmt) {
295 errln("Failed in PluralFormat copy constructor or == operator");
296 }
297
298 delete plRules;
299 delete numFmt;
300 }
301
302
303
304 /**
305 * Test locale data used in PluralFormat class.
306 */
307 void
308 PluralFormatTest::pluralFormatLocaleTest(/*char *par*/)
309 {
310 int8_t pluralResults[PLURAL_TEST_ARRAY_SIZE]; // 0: is for default
311
312 // ======= Test DefaultRule
313 logln("Testing PluralRules with no rule.");
314 const char* oneRuleLocales[4] = {"ja", "ko", "tr", "vi"};
315 UnicodeString testPattern = UNICODE_STRING_SIMPLE("other{other}");
316 uprv_memset(pluralResults, -1, sizeof(pluralResults));
317 pluralResults[0]= PFT_OTHER; // other
318 helperTestRusults(oneRuleLocales, 4, testPattern, pluralResults);
319
320 // ====== Test Singular1 locales.
321 logln("Testing singular1 locales.");
322 const char* singular1Locales[52] = {"bem","da","de","el","en","eo","es","et","fi",
323 "fo","gl","he","it","nb","nl","nn","no","pt","pt_PT","sv","af","bg","bn","ca","eu","fur","fy",
324 "gu","ha","is","ku","lb","ml","mr","nah","ne","om","or","pa","pap","ps","so","sq","sw","ta",
325 "te","tk","ur","zu","mn","gsw","rm"};
326 testPattern = UNICODE_STRING_SIMPLE("one{one} other{other}");
327 uprv_memset(pluralResults, -1, sizeof(pluralResults));
328 pluralResults[0]= PFT_OTHER;
329 pluralResults[1]= PFT_ONE;
330 pluralResults[2]= PFT_OTHER;
331 helperTestRusults(singular1Locales, 52, testPattern, pluralResults);
332
333 // ======== Test Singular01 locales.
334 logln("Testing singular1 locales.");
335 const char* singular01Locales[3] = {"ff","fr","kab"};
336 testPattern = UNICODE_STRING_SIMPLE("one{one} other{other}");
337 uprv_memset(pluralResults, -1, sizeof(pluralResults));
338 pluralResults[0]= PFT_ONE;
339 pluralResults[2]= PFT_OTHER;
340 helperTestRusults(singular01Locales, 3, testPattern, pluralResults);
341
342 // ======== Test ZeroSingular locales.
343 logln("Testing singular1 locales.");
344 const char* zeroSingularLocales[1] = {"lv"};
345 testPattern = UNICODE_STRING_SIMPLE("zero{zero} one{one} other{other}");
346 uprv_memset(pluralResults, -1, sizeof(pluralResults));
347 pluralResults[0]= PFT_ZERO;
348 pluralResults[1]= PFT_ONE;
349 pluralResults[2]= PFT_OTHER;
350 for (int32_t i=2; i<20; ++i) {
351 if (i==11) continue;
352 pluralResults[i*10+1] = PFT_ONE;
353 pluralResults[i*10+2] = PFT_OTHER;
354 }
355 helperTestRusults(zeroSingularLocales, 1, testPattern, pluralResults);
356
357 // ======== Test singular dual locales.
358 logln("Testing singular1 locales.");
359 const char* singularDualLocales[1] = {"ga"};
360 testPattern = UNICODE_STRING_SIMPLE("one{one} two{two} other{other}");
361 uprv_memset(pluralResults, -1, sizeof(pluralResults));
362 pluralResults[0]= PFT_OTHER;
363 pluralResults[1]= PFT_ONE;
364 pluralResults[2]= PFT_TWO;
365 pluralResults[3]= PFT_OTHER;
366 helperTestRusults(singularDualLocales, 1, testPattern, pluralResults);
367
368 // ======== Test Singular Zero Some locales.
369 logln("Testing singular1 locales.");
370 const char* singularZeroSomeLocales[1] = {"ro"};
371 testPattern = UNICODE_STRING_SIMPLE("few{few} one{one} other{other}");
372 uprv_memset(pluralResults, -1, sizeof(pluralResults));
373 pluralResults[0]= PFT_FEW;
374 for (int32_t i=1; i<20; ++i) {
375 if (i==11) continue;
376 pluralResults[i] = PFT_FEW;
377 pluralResults[100+i] = PFT_FEW;
378 }
379 pluralResults[1]= PFT_ONE;
380 helperTestRusults(singularZeroSomeLocales, 1, testPattern, pluralResults);
381
382 // ======== Test Special 12/19.
383 logln("Testing special 12 and 19.");
384 const char* special12_19Locales[1] = {"lt"};
385 testPattern = UNICODE_STRING_SIMPLE("one{one} few{few} other{other}");
386 uprv_memset(pluralResults, -1, sizeof(pluralResults));
387 pluralResults[0]= PFT_OTHER;
388 pluralResults[1]= PFT_ONE;
389 pluralResults[2]= PFT_FEW;
390 pluralResults[10]= PFT_OTHER;
391 for (int32_t i=2; i<20; ++i) {
392 if (i==11) continue;
393 pluralResults[i*10+1] = PFT_ONE;
394 pluralResults[i*10+2] = PFT_FEW;
395 pluralResults[(i+1)*10] = PFT_OTHER;
396 }
397 helperTestRusults(special12_19Locales, 1, testPattern, pluralResults);
398
399 // ======== Test Paucal Except 11 14.
400 logln("Testing Paucal Except 11 and 14.");
401 const char* paucal01Locales[4] = {"hr","ru","sr","uk"};
402 testPattern = UNICODE_STRING_SIMPLE("one{one} many{many} few{few} other{other}");
403 uprv_memset(pluralResults, -1, sizeof(pluralResults));
404 pluralResults[0]= PFT_MANY;
405 pluralResults[1]= PFT_ONE;
406 pluralResults[2]= PFT_FEW;
407 pluralResults[5]= PFT_MANY;
408 pluralResults[6]= PFT_MANY;
409 pluralResults[7]= PFT_MANY;
410 pluralResults[8]= PFT_MANY;
411 pluralResults[9]= PFT_MANY;
412 for (int32_t i=2; i<20; ++i) {
413 if (i==11) continue;
414 pluralResults[i*10+1] = PFT_ONE;
415 pluralResults[i*10+2] = PFT_FEW;
416 pluralResults[i*10+5] = PFT_MANY;
417 pluralResults[i*10+6] = PFT_MANY;
418 pluralResults[i*10+7] = PFT_MANY;
419 pluralResults[i*10+8] = PFT_MANY;
420 pluralResults[i*10+9] = PFT_MANY;
421 }
422 helperTestRusults(paucal01Locales, 4, testPattern, pluralResults);
423
424 // ======== Test Singular Paucal.
425 logln("Testing Singular Paucal.");
426 const char* singularPaucalLocales[2] = {"cs","sk"};
427 testPattern = UNICODE_STRING_SIMPLE("one{one} few{few} other{other}");
428 uprv_memset(pluralResults, -1, sizeof(pluralResults));
429 pluralResults[0]= PFT_OTHER;
430 pluralResults[1]= PFT_ONE;
431 pluralResults[2]= PFT_FEW;
432 pluralResults[5]= PFT_OTHER;
433 helperTestRusults(singularPaucalLocales, 2, testPattern, pluralResults);
434
435 // ======== Test Paucal (1), (2,3,4).
436 logln("Testing Paucal (1), (2,3,4).");
437 const char* paucal02Locales[1] = {"pl"};
438 testPattern = UNICODE_STRING_SIMPLE("one{one} few{few} other{other}");
439 uprv_memset(pluralResults, -1, sizeof(pluralResults));
440 pluralResults[0]= PFT_OTHER;
441 pluralResults[1]= PFT_ONE;
442 pluralResults[5]= PFT_OTHER;
443 for (int32_t i=0; i<20; ++i) {
444 if ((i==1)||(i==11)) {
445 pluralResults[i*10+2] = PFT_OTHER;
446 pluralResults[i*10+3] = PFT_OTHER;
447 pluralResults[i*10+4] = PFT_OTHER;
448 }
449 else {
450 pluralResults[i*10+2] = PFT_FEW;
451 pluralResults[i*10+3] = PFT_FEW;
452 pluralResults[i*10+4] = PFT_FEW;
453 pluralResults[i*10+5] = PFT_OTHER;
454 }
455 }
456 helperTestRusults(paucal02Locales, 1, testPattern, pluralResults);
457
458 // ======== Test Paucal (1), (2), (3,4).
459 logln("Testing Paucal (1), (2), (3,4).");
460 const char* paucal03Locales[1] = {"sl"};
461 testPattern = UNICODE_STRING_SIMPLE("one{one} two{two} few{few} other{other}");
462 uprv_memset(pluralResults, -1, sizeof(pluralResults));
463 pluralResults[0]= PFT_OTHER;
464 pluralResults[1]= PFT_ONE;
465 pluralResults[2]= PFT_TWO;
466 pluralResults[3]= PFT_FEW;
467 pluralResults[5]= PFT_OTHER;
468 pluralResults[101]= PFT_ONE;
469 pluralResults[102]= PFT_TWO;
470 pluralResults[103]= PFT_FEW;
471 pluralResults[105]= PFT_OTHER;
472 helperTestRusults(paucal03Locales, 1, testPattern, pluralResults);
473
474 // TODO: move this test to Unit Test after CLDR 1.6 is final and we support float
475 // ======= Test French "WITHIN rule
476 logln("Testing PluralRules with fr rule.");
477 testPattern = UNICODE_STRING_SIMPLE("one{one} other{other}");
478 Locale ulocale((const char *)"fr");
479 UErrorCode status = U_ZERO_ERROR;
480 PluralFormat plFmt(ulocale, testPattern, status);
481 if (U_FAILURE(status)) {
482 dataerrln("Failed to apply pattern to fr locale - %s", u_errorName(status));
483 }
484 else {
485 status = U_ZERO_ERROR;
486 UnicodeString plResult = plFmt.format(0.0, status); // retrun ONE
487 plResult = plFmt.format(0.5, status); // retrun ONE
488 plResult = plFmt.format(1.0, status); // retrun ONE
489 plResult = plFmt.format(1.9, status); // retrun ONE
490 plResult = plFmt.format(2.0, status); // retrun OTHER
491 }
492 }
493
494 void
495 PluralFormatTest::numberFormatTest(PluralFormat* plFmt,
496 NumberFormat *numFmt,
497 int32_t start,
498 int32_t end,
499 UnicodeString *numOddAppendStr,
500 UnicodeString *numEvenAppendStr,
501 UBool overwrite, // overwrite the numberFormat.format result
502 UnicodeString *message) {
503 UErrorCode status = U_ZERO_ERROR;
504
505 if ( (plFmt==NULL) || (numFmt==NULL) ) {
506 dataerrln("ERROR: Could not create PluralFormat or NumberFormat - exitting");
507 return;
508 }
509 UnicodeString plResult, numResult ;
510
511 for (int32_t i=start; i<= end; ++i ) {
512 numResult.remove();
513 numResult = numFmt->format(i, numResult);
514 plResult = plFmt->format(i, status);
515 if ((numOddAppendStr!= NULL)&&(numEvenAppendStr!=NULL)) {
516 if (overwrite) {
517 if (i&1) {
518 numResult = *numOddAppendStr;
519 }
520 else {
521 numResult = *numEvenAppendStr;
522 }
523 }
524 else { // Append the string
525 if (i&1) {
526 numResult += *numOddAppendStr;
527 }
528 else{
529 numResult += *numEvenAppendStr;
530 }
531 }
532 }
533 if ( (numResult!=plResult) || U_FAILURE(status) ) {
534 if ( message == NULL ) {
535 errln("ERROR: Unexpected plural format - got:"+plResult+ UnicodeString(" expecting:")+numResult);
536 }
537 else {
538 errln( *message+UnicodeString(" got:")+plResult+UnicodeString(" expecting:")+numResult);
539
540 }
541 }
542 }
543 return;
544 }
545
546
547 void
548 PluralFormatTest::helperTestRusults(const char** localeArray,
549 int32_t capacityOfArray,
550 UnicodeString& testPattern,
551 int8_t *expResults) {
552 UErrorCode status;
553 UnicodeString plResult;
554 const UnicodeString PLKeywordLookups[6] = {
555 UNICODE_STRING_SIMPLE("zero"),
556 UNICODE_STRING_SIMPLE("one"),
557 UNICODE_STRING_SIMPLE("two"),
558 UNICODE_STRING_SIMPLE("few"),
559 UNICODE_STRING_SIMPLE("many"),
560 UNICODE_STRING_SIMPLE("other"),
561 };
562
563 for (int32_t i=0; i<capacityOfArray; ++i) {
564 const char *locale = localeArray[i];
565 Locale ulocale((const char *)locale);
566 status = U_ZERO_ERROR;
567 PluralFormat plFmt(ulocale, testPattern, status);
568 if (U_FAILURE(status)) {
569 dataerrln("Failed to apply pattern to locale:"+UnicodeString(localeArray[i]) + " - " + u_errorName(status));
570 continue;
571 }
572 for (int32_t n=0; n<PLURAL_TEST_ARRAY_SIZE; ++n) {
573 if (expResults[n]!=-1) {
574 status = U_ZERO_ERROR;
575 plResult = plFmt.format(n, status);
576 if (U_FAILURE(status)) {
577 errln("ERROR: Failed to format number in locale data tests with locale: "+
578 UnicodeString(localeArray[i]));
579 }
580 if (plResult != PLKeywordLookups[expResults[n]]){
581 plResult = plFmt.format(n, status);
582 errln("ERROR: Unexpected format result in locale: "+UnicodeString(localeArray[i])+
583 UnicodeString(" got:")+plResult+ UnicodeString(" expecting:")+
584 PLKeywordLookups[expResults[n]]);
585 }
586 }
587 }
588 }
589 }
590
591 #endif /* #if !UCONFIG_NO_FORMATTING */