1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /*
4 *******************************************************************************
5 * Copyright (C) 2007-2014, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************************
8
9 * File PLURRULTS.cpp
10 *
11 ********************************************************************************
12 */
13
14 #include "unicode/utypes.h"
15
16 #if !UCONFIG_NO_FORMATTING
17
18 #include <stdlib.h>
19 #include <stdarg.h>
20 #include <string.h>
21
22 #include "unicode/localpointer.h"
23 #include "unicode/plurrule.h"
24 #include "unicode/stringpiece.h"
25
26 #include "cmemory.h"
27 #include "plurrule_impl.h"
28 #include "plurults.h"
29 #include "uhash.h"
30 #include "number_decimalquantity.h"
31
32 using icu::number::impl::DecimalQuantity;
33
34 void setupResult(const int32_t testSource[], char result[], int32_t* max);
35 UBool checkEqual(const PluralRules &test, char *result, int32_t max);
36 UBool testEquality(const PluralRules &test);
37
38 // This is an API test, not a unit test. It doesn't test very many cases, and doesn't
39 // try to test the full functionality. It just calls each function in the class and
40 // verifies that it works on a basic level.
41
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)42 void PluralRulesTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
43 {
44 if (exec) logln("TestSuite PluralRulesAPI");
45 TESTCASE_AUTO_BEGIN;
46 TESTCASE_AUTO(testAPI);
47 // TESTCASE_AUTO(testGetUniqueKeywordValue);
48 TESTCASE_AUTO(testGetSamples);
49 TESTCASE_AUTO(testWithin);
50 TESTCASE_AUTO(testGetAllKeywordValues);
51 TESTCASE_AUTO(testOrdinal);
52 TESTCASE_AUTO(testSelect);
53 TESTCASE_AUTO(testAvailbleLocales);
54 TESTCASE_AUTO(testParseErrors);
55 TESTCASE_AUTO(testFixedDecimal);
56 TESTCASE_AUTO_END;
57 }
58
59
60 // Quick and dirty class for putting UnicodeStrings in char * messages.
61 // TODO: something like this should be generally available.
62 class US {
63 private:
64 char *buf;
65 public:
US(const UnicodeString & us)66 US(const UnicodeString &us) {
67 int32_t bufLen = us.extract((int32_t)0, us.length(), (char *)NULL, (uint32_t)0) + 1;
68 buf = (char *)uprv_malloc(bufLen);
69 us.extract(0, us.length(), buf, bufLen); };
cstr()70 const char *cstr() {return buf;};
~US()71 ~US() { uprv_free(buf);};
72 };
73
74
75
76
77
78 #define PLURAL_TEST_NUM 18
79 /**
80 * Test various generic API methods of PluralRules for API coverage.
81 */
testAPI()82 void PluralRulesTest::testAPI(/*char *par*/)
83 {
84 UnicodeString pluralTestData[PLURAL_TEST_NUM] = {
85 UNICODE_STRING_SIMPLE("a: n is 1"),
86 UNICODE_STRING_SIMPLE("a: n mod 10 is 2"),
87 UNICODE_STRING_SIMPLE("a: n is not 1"),
88 UNICODE_STRING_SIMPLE("a: n mod 3 is not 1"),
89 UNICODE_STRING_SIMPLE("a: n in 2..5"),
90 UNICODE_STRING_SIMPLE("a: n within 2..5"),
91 UNICODE_STRING_SIMPLE("a: n not in 2..5"),
92 UNICODE_STRING_SIMPLE("a: n not within 2..5"),
93 UNICODE_STRING_SIMPLE("a: n mod 10 in 2..5"),
94 UNICODE_STRING_SIMPLE("a: n mod 10 within 2..5"),
95 UNICODE_STRING_SIMPLE("a: n mod 10 is 2 and n is not 12"),
96 UNICODE_STRING_SIMPLE("a: n mod 10 in 2..3 or n mod 10 is 5"),
97 UNICODE_STRING_SIMPLE("a: n mod 10 within 2..3 or n mod 10 is 5"),
98 UNICODE_STRING_SIMPLE("a: n is 1 or n is 4 or n is 23"),
99 UNICODE_STRING_SIMPLE("a: n mod 2 is 1 and n is not 3 and n in 1..11"),
100 UNICODE_STRING_SIMPLE("a: n mod 2 is 1 and n is not 3 and n within 1..11"),
101 UNICODE_STRING_SIMPLE("a: n mod 2 is 1 or n mod 5 is 1 and n is not 6"),
102 "",
103 };
104 static const int32_t pluralTestResult[PLURAL_TEST_NUM][30] = {
105 {1, 0},
106 {2,12,22, 0},
107 {0,2,3,4,5,0},
108 {0,2,3,5,6,8,9,0},
109 {2,3,4,5,0},
110 {2,3,4,5,0},
111 {0,1,6,7,8, 0},
112 {0,1,6,7,8, 0},
113 {2,3,4,5,12,13,14,15,22,23,24,25,0},
114 {2,3,4,5,12,13,14,15,22,23,24,25,0},
115 {2,22,32,42,0},
116 {2,3,5,12,13,15,22,23,25,0},
117 {2,3,5,12,13,15,22,23,25,0},
118 {1,4,23,0},
119 {1,5,7,9,11,0},
120 {1,5,7,9,11,0},
121 {1,3,5,7,9,11,13,15,16,0},
122 };
123 UErrorCode status = U_ZERO_ERROR;
124
125 // ======= Test constructors
126 logln("Testing PluralRules constructors");
127
128
129 logln("\n start default locale test case ..\n");
130
131 PluralRules defRule(status);
132 LocalPointer<PluralRules> test(new PluralRules(status), status);
133 if(U_FAILURE(status)) {
134 dataerrln("ERROR: Could not create PluralRules (default) - exitting");
135 return;
136 }
137 LocalPointer<PluralRules> newEnPlural(test->forLocale(Locale::getEnglish(), status), status);
138 if(U_FAILURE(status)) {
139 dataerrln("ERROR: Could not create PluralRules (English) - exitting");
140 return;
141 }
142
143 // ======= Test clone, assignment operator && == operator.
144 LocalPointer<PluralRules> dupRule(defRule.clone());
145 if (dupRule==NULL) {
146 errln("ERROR: clone plural rules test failed!");
147 return;
148 } else {
149 if ( *dupRule != defRule ) {
150 errln("ERROR: clone plural rules test failed!");
151 }
152 }
153 *dupRule = *newEnPlural;
154 if (dupRule!=NULL) {
155 if ( *dupRule != *newEnPlural ) {
156 errln("ERROR: clone plural rules test failed!");
157 }
158 }
159
160 // ======= Test empty plural rules
161 logln("Testing Simple PluralRules");
162
163 LocalPointer<PluralRules> empRule(test->createRules(UNICODE_STRING_SIMPLE("a:n"), status));
164 UnicodeString key;
165 for (int32_t i=0; i<10; ++i) {
166 key = empRule->select(i);
167 if ( key.charAt(0)!= 0x61 ) { // 'a'
168 errln("ERROR: empty plural rules test failed! - exitting");
169 }
170 }
171
172 // ======= Test simple plural rules
173 logln("Testing Simple PluralRules");
174
175 char result[100];
176 int32_t max;
177
178 for (int32_t i=0; i<PLURAL_TEST_NUM-1; ++i) {
179 LocalPointer<PluralRules> newRules(test->createRules(pluralTestData[i], status));
180 setupResult(pluralTestResult[i], result, &max);
181 if ( !checkEqual(*newRules, result, max) ) {
182 errln("ERROR: simple plural rules failed! - exitting");
183 return;
184 }
185 }
186
187 // ======= Test complex plural rules
188 logln("Testing Complex PluralRules");
189 // TODO: the complex test data is hard coded. It's better to implement
190 // a parser to parse the test data.
191 UnicodeString complexRule = UNICODE_STRING_SIMPLE("a: n in 2..5; b: n in 5..8; c: n mod 2 is 1");
192 UnicodeString complexRule2 = UNICODE_STRING_SIMPLE("a: n within 2..5; b: n within 5..8; c: n mod 2 is 1");
193 char cRuleResult[] =
194 {
195 0x6F, // 'o'
196 0x63, // 'c'
197 0x61, // 'a'
198 0x61, // 'a'
199 0x61, // 'a'
200 0x61, // 'a'
201 0x62, // 'b'
202 0x62, // 'b'
203 0x62, // 'b'
204 0x63, // 'c'
205 0x6F, // 'o'
206 0x63 // 'c'
207 };
208 LocalPointer<PluralRules> newRules(test->createRules(complexRule, status));
209 if ( !checkEqual(*newRules, cRuleResult, 12) ) {
210 errln("ERROR: complex plural rules failed! - exitting");
211 return;
212 }
213 newRules.adoptInstead(test->createRules(complexRule2, status));
214 if ( !checkEqual(*newRules, cRuleResult, 12) ) {
215 errln("ERROR: complex plural rules failed! - exitting");
216 return;
217 }
218
219 // ======= Test decimal fractions plural rules
220 UnicodeString decimalRule= UNICODE_STRING_SIMPLE("a: n not in 0..100;");
221 UnicodeString KEYWORD_A = UNICODE_STRING_SIMPLE("a");
222 status = U_ZERO_ERROR;
223 newRules.adoptInstead(test->createRules(decimalRule, status));
224 if (U_FAILURE(status)) {
225 dataerrln("ERROR: Could not create PluralRules for testing fractions - exitting");
226 return;
227 }
228 double fData[] = {-101, -100, -1, -0.0, 0, 0.1, 1, 1.999, 2.0, 100, 100.001 };
229 UBool isKeywordA[] = {TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE };
230 for (int32_t i=0; i<UPRV_LENGTHOF(fData); i++) {
231 if ((newRules->select(fData[i])== KEYWORD_A) != isKeywordA[i]) {
232 errln("File %s, Line %d, ERROR: plural rules for decimal fractions test failed!\n"
233 " number = %g, expected %s", __FILE__, __LINE__, fData[i], isKeywordA[i]?"TRUE":"FALSE");
234 }
235 }
236
237 // ======= Test Equality
238 logln("Testing Equality of PluralRules");
239
240 if ( !testEquality(*test) ) {
241 errln("ERROR: complex plural rules failed! - exitting");
242 return;
243 }
244
245
246 // ======= Test getStaticClassID()
247 logln("Testing getStaticClassID()");
248
249 if(test->getDynamicClassID() != PluralRules::getStaticClassID()) {
250 errln("ERROR: getDynamicClassID() didn't return the expected value");
251 }
252 // ====== Test fallback to parent locale
253 LocalPointer<PluralRules> en_UK(test->forLocale(Locale::getUK(), status));
254 LocalPointer<PluralRules> en(test->forLocale(Locale::getEnglish(), status));
255 if (en_UK.isValid() && en.isValid()) {
256 if ( *en_UK != *en ) {
257 errln("ERROR: test locale fallback failed!");
258 }
259 }
260
261 LocalPointer<PluralRules> zh_Hant(test->forLocale(Locale::getTaiwan(), status));
262 LocalPointer<PluralRules> zh(test->forLocale(Locale::getChinese(), status));
263 if (zh_Hant.isValid() && zh.isValid()) {
264 if ( *zh_Hant != *zh ) {
265 errln("ERROR: test locale fallback failed!");
266 }
267 }
268 }
269
setupResult(const int32_t testSource[],char result[],int32_t * max)270 void setupResult(const int32_t testSource[], char result[], int32_t* max) {
271 int32_t i=0;
272 int32_t curIndex=0;
273
274 do {
275 while (curIndex < testSource[i]) {
276 result[curIndex++]=0x6F; //'o' other
277 }
278 result[curIndex++]=0x61; // 'a'
279
280 } while(testSource[++i]>0);
281 *max=curIndex;
282 }
283
284
checkEqual(const PluralRules & test,char * result,int32_t max)285 UBool checkEqual(const PluralRules &test, char *result, int32_t max) {
286 UnicodeString key;
287 UBool isEqual = TRUE;
288 for (int32_t i=0; i<max; ++i) {
289 key= test.select(i);
290 if ( key.charAt(0)!=result[i] ) {
291 isEqual = FALSE;
292 }
293 }
294 return isEqual;
295 }
296
297
298
299 static const int32_t MAX_EQ_ROW = 2;
300 static const int32_t MAX_EQ_COL = 5;
testEquality(const PluralRules & test)301 UBool testEquality(const PluralRules &test) {
302 UnicodeString testEquRules[MAX_EQ_ROW][MAX_EQ_COL] = {
303 { UNICODE_STRING_SIMPLE("a: n in 2..3"),
304 UNICODE_STRING_SIMPLE("a: n is 2 or n is 3"),
305 UNICODE_STRING_SIMPLE( "a:n is 3 and n in 2..5 or n is 2"),
306 "",
307 },
308 { UNICODE_STRING_SIMPLE("a: n is 12; b:n mod 10 in 2..3"),
309 UNICODE_STRING_SIMPLE("b: n mod 10 in 2..3 and n is not 12; a: n in 12..12"),
310 UNICODE_STRING_SIMPLE("b: n is 13; a: n in 12..13; b: n mod 10 is 2 or n mod 10 is 3"),
311 "",
312 }
313 };
314 UErrorCode status = U_ZERO_ERROR;
315 UnicodeString key[MAX_EQ_COL];
316 UBool ret=TRUE;
317 for (int32_t i=0; i<MAX_EQ_ROW; ++i) {
318 PluralRules* rules[MAX_EQ_COL];
319
320 for (int32_t j=0; j<MAX_EQ_COL; ++j) {
321 rules[j]=NULL;
322 }
323 int32_t totalRules=0;
324 while((totalRules<MAX_EQ_COL) && (testEquRules[i][totalRules].length()>0) ) {
325 rules[totalRules]=test.createRules(testEquRules[i][totalRules], status);
326 totalRules++;
327 }
328 for (int32_t n=0; n<300 && ret ; ++n) {
329 for(int32_t j=0; j<totalRules;++j) {
330 key[j] = rules[j]->select(n);
331 }
332 for(int32_t j=0; j<totalRules-1;++j) {
333 if (key[j]!=key[j+1]) {
334 ret= FALSE;
335 break;
336 }
337 }
338
339 }
340 for (int32_t j=0; j<MAX_EQ_COL; ++j) {
341 if (rules[j]!=NULL) {
342 delete rules[j];
343 }
344 }
345 }
346
347 return ret;
348 }
349
350 void
assertRuleValue(const UnicodeString & rule,double expected)351 PluralRulesTest::assertRuleValue(const UnicodeString& rule, double expected) {
352 assertRuleKeyValue("a:" + rule, "a", expected);
353 }
354
355 void
assertRuleKeyValue(const UnicodeString & rule,const UnicodeString & key,double expected)356 PluralRulesTest::assertRuleKeyValue(const UnicodeString& rule,
357 const UnicodeString& key, double expected) {
358 UErrorCode status = U_ZERO_ERROR;
359 PluralRules *pr = PluralRules::createRules(rule, status);
360 double result = pr->getUniqueKeywordValue(key);
361 delete pr;
362 if (expected != result) {
363 errln("expected %g but got %g", expected, result);
364 }
365 }
366
367 // TODO: UniqueKeywordValue() is not currently supported.
368 // If it never will be, this test code should be removed.
testGetUniqueKeywordValue()369 void PluralRulesTest::testGetUniqueKeywordValue() {
370 assertRuleValue("n is 1", 1);
371 assertRuleValue("n in 2..2", 2);
372 assertRuleValue("n within 2..2", 2);
373 assertRuleValue("n in 3..4", UPLRULES_NO_UNIQUE_VALUE);
374 assertRuleValue("n within 3..4", UPLRULES_NO_UNIQUE_VALUE);
375 assertRuleValue("n is 2 or n is 2", 2);
376 assertRuleValue("n is 2 and n is 2", 2);
377 assertRuleValue("n is 2 or n is 3", UPLRULES_NO_UNIQUE_VALUE);
378 assertRuleValue("n is 2 and n is 3", UPLRULES_NO_UNIQUE_VALUE);
379 assertRuleValue("n is 2 or n in 2..3", UPLRULES_NO_UNIQUE_VALUE);
380 assertRuleValue("n is 2 and n in 2..3", 2);
381 assertRuleKeyValue("a: n is 1", "not_defined", UPLRULES_NO_UNIQUE_VALUE); // key not defined
382 assertRuleKeyValue("a: n is 1", "other", UPLRULES_NO_UNIQUE_VALUE); // key matches default rule
383 }
384
testGetSamples()385 void PluralRulesTest::testGetSamples() {
386 // TODO: fix samples, re-enable this test.
387
388 // no get functional equivalent API in ICU4C, so just
389 // test every locale...
390 UErrorCode status = U_ZERO_ERROR;
391 int32_t numLocales;
392 const Locale* locales = Locale::getAvailableLocales(numLocales);
393
394 double values[1000];
395 for (int32_t i = 0; U_SUCCESS(status) && i < numLocales; ++i) {
396 PluralRules *rules = PluralRules::forLocale(locales[i], status);
397 if (U_FAILURE(status)) {
398 break;
399 }
400 StringEnumeration *keywords = rules->getKeywords(status);
401 if (U_FAILURE(status)) {
402 delete rules;
403 break;
404 }
405 const UnicodeString* keyword;
406 while (NULL != (keyword = keywords->snext(status))) {
407 int32_t count = rules->getSamples(*keyword, values, UPRV_LENGTHOF(values), status);
408 if (U_FAILURE(status)) {
409 errln(UNICODE_STRING_SIMPLE("getSamples() failed for locale ") +
410 locales[i].getName() +
411 UNICODE_STRING_SIMPLE(", keyword ") + *keyword);
412 continue;
413 }
414 if (count == 0) {
415 // TODO: Lots of these.
416 // errln(UNICODE_STRING_SIMPLE("no samples for keyword ") + *keyword + UNICODE_STRING_SIMPLE(" in locale ") + locales[i].getName() );
417 }
418 if (count > UPRV_LENGTHOF(values)) {
419 errln(UNICODE_STRING_SIMPLE("getSamples()=") + count +
420 UNICODE_STRING_SIMPLE(", too many values, for locale ") +
421 locales[i].getName() +
422 UNICODE_STRING_SIMPLE(", keyword ") + *keyword);
423 count = UPRV_LENGTHOF(values);
424 }
425 for (int32_t j = 0; j < count; ++j) {
426 if (values[j] == UPLRULES_NO_UNIQUE_VALUE) {
427 errln("got 'no unique value' among values");
428 } else {
429 UnicodeString resultKeyword = rules->select(values[j]);
430 // if (strcmp(locales[i].getName(), "uk") == 0) { // Debug only.
431 // std::cout << " uk " << US(resultKeyword).cstr() << " " << values[j] << std::endl;
432 // }
433 if (*keyword != resultKeyword) {
434 errln("file %s, line %d, Locale %s, sample for keyword \"%s\": %g, select(%g) returns keyword \"%s\"",
435 __FILE__, __LINE__, locales[i].getName(), US(*keyword).cstr(), values[j], values[j], US(resultKeyword).cstr());
436 }
437 }
438 }
439 }
440 delete keywords;
441 delete rules;
442 }
443 }
444
testWithin()445 void PluralRulesTest::testWithin() {
446 // goes to show you what lack of testing will do.
447 // of course, this has been broken for two years and no one has noticed...
448 UErrorCode status = U_ZERO_ERROR;
449 PluralRules *rules = PluralRules::createRules("a: n mod 10 in 5..8", status);
450 if (!rules) {
451 errln("couldn't instantiate rules");
452 return;
453 }
454
455 UnicodeString keyword = rules->select((int32_t)26);
456 if (keyword != "a") {
457 errln("expected 'a' for 26 but didn't get it.");
458 }
459
460 keyword = rules->select(26.5);
461 if (keyword != "other") {
462 errln("expected 'other' for 26.5 but didn't get it.");
463 }
464
465 delete rules;
466 }
467
468 void
testGetAllKeywordValues()469 PluralRulesTest::testGetAllKeywordValues() {
470 const char* data[] = {
471 "a: n in 2..5", "a: 2,3,4,5; other: null; b:",
472 "a: n not in 2..5", "a: null; other: null",
473 "a: n within 2..5", "a: null; other: null",
474 "a: n not within 2..5", "a: null; other: null",
475 "a: n in 2..5 or n within 6..8", "a: null", // ignore 'other' here on out, always null
476 "a: n in 2..5 and n within 6..8", "a:",
477 "a: n in 2..5 and n within 5..8", "a: 5",
478 "a: n within 2..5 and n within 6..8", "a:", // our sampling catches these
479 "a: n within 2..5 and n within 5..8", "a: 5", // ''
480 "a: n within 1..2 and n within 2..3 or n within 3..4 and n within 4..5", "a: 2,4",
481 "a: n within 1..2 and n within 2..3 or n within 3..4 and n within 4..5 "
482 "or n within 5..6 and n within 6..7", "a: null", // but not this...
483 "a: n mod 3 is 0", "a: null",
484 "a: n mod 3 is 0 and n within 1..2", "a:",
485 "a: n mod 3 is 0 and n within 0..5", "a: 0,3",
486 "a: n mod 3 is 0 and n within 0..6", "a: null", // similarly with mod, we don't catch...
487 "a: n mod 3 is 0 and n in 3..12", "a: 3,6,9,12",
488 NULL
489 };
490
491 for (int i = 0; data[i] != NULL; i += 2) {
492 UErrorCode status = U_ZERO_ERROR;
493 UnicodeString ruleDescription(data[i], -1, US_INV);
494 const char* result = data[i+1];
495
496 logln("[%d] %s", i >> 1, data[i]);
497
498 PluralRules *p = PluralRules::createRules(ruleDescription, status);
499 if (p == NULL || U_FAILURE(status)) {
500 errln("file %s, line %d: could not create rules from '%s'\n"
501 " ErrorCode: %s\n",
502 __FILE__, __LINE__, data[i], u_errorName(status));
503 continue;
504 }
505
506 // TODO: fix samples implementation, re-enable test.
507 (void)result;
508 #if 0
509
510 const char* rp = result;
511 while (*rp) {
512 while (*rp == ' ') ++rp;
513 if (!rp) {
514 break;
515 }
516
517 const char* ep = rp;
518 while (*ep && *ep != ':') ++ep;
519
520 status = U_ZERO_ERROR;
521 UnicodeString keyword(rp, ep - rp, US_INV);
522 double samples[4]; // no test above should have more samples than 4
523 int32_t count = p->getAllKeywordValues(keyword, &samples[0], 4, status);
524 if (U_FAILURE(status)) {
525 errln("error getting samples for %s", rp);
526 break;
527 }
528
529 if (count > 4) {
530 errln("count > 4 for keyword %s", rp);
531 count = 4;
532 }
533
534 if (*ep) {
535 ++ep; // skip colon
536 while (*ep && *ep == ' ') ++ep; // and spaces
537 }
538
539 UBool ok = TRUE;
540 if (count == -1) {
541 if (*ep != 'n') {
542 errln("expected values for keyword %s but got -1 (%s)", rp, ep);
543 ok = FALSE;
544 }
545 } else if (*ep == 'n') {
546 errln("expected count of -1, got %d, for keyword %s (%s)", count, rp, ep);
547 ok = FALSE;
548 }
549
550 // We'll cheat a bit here. The samples happend to be in order and so are our
551 // expected values, so we'll just test in order until a failure. If the
552 // implementation changes to return samples in an arbitrary order, this test
553 // must change. There's no actual restriction on the order of the samples.
554
555 for (int j = 0; ok && j < count; ++j ) { // we've verified count < 4
556 double val = samples[j];
557 if (*ep == 0 || *ep == ';') {
558 errln("got unexpected value[%d]: %g", j, val);
559 ok = FALSE;
560 break;
561 }
562 char* xp;
563 double expectedVal = strtod(ep, &xp);
564 if (xp == ep) {
565 // internal error
566 errln("yikes!");
567 ok = FALSE;
568 break;
569 }
570 ep = xp;
571 if (expectedVal != val) {
572 errln("expected %g but got %g", expectedVal, val);
573 ok = FALSE;
574 break;
575 }
576 if (*ep == ',') ++ep;
577 }
578
579 if (ok && count != -1) {
580 if (!(*ep == 0 || *ep == ';')) {
581 errln("file: %s, line %d, didn't get expected value: %s", __FILE__, __LINE__, ep);
582 ok = FALSE;
583 }
584 }
585
586 while (*ep && *ep != ';') ++ep;
587 if (*ep == ';') ++ep;
588 rp = ep;
589 }
590 #endif
591 delete p;
592 }
593 }
594
testOrdinal()595 void PluralRulesTest::testOrdinal() {
596 IcuTestErrorCode errorCode(*this, "testOrdinal");
597 LocalPointer<PluralRules> pr(PluralRules::forLocale("en", UPLURAL_TYPE_ORDINAL, errorCode));
598 if (errorCode.errIfFailureAndReset("PluralRules::forLocale(en, UPLURAL_TYPE_ORDINAL) failed")) {
599 return;
600 }
601 UnicodeString keyword = pr->select(2.);
602 if (keyword != UNICODE_STRING("two", 3)) {
603 dataerrln("PluralRules(en-ordinal).select(2) failed");
604 }
605 }
606
607
608 static const char * END_MARK = "999.999"; // Mark end of varargs data.
609
checkSelect(const LocalPointer<PluralRules> & rules,UErrorCode & status,int32_t line,const char * keyword,...)610 void PluralRulesTest::checkSelect(const LocalPointer<PluralRules> &rules, UErrorCode &status,
611 int32_t line, const char *keyword, ...) {
612 // The varargs parameters are a const char* strings, each being a decimal number.
613 // The formatting of the numbers as strings is significant, e.g.
614 // the difference between "2" and "2.0" can affect which rule matches (which keyword is selected).
615 // Note: rules parameter is a LocalPointer reference rather than a PluralRules * to avoid having
616 // to write getAlias() at every (numerous) call site.
617
618 if (U_FAILURE(status)) {
619 errln("file %s, line %d, ICU error status: %s.", __FILE__, line, u_errorName(status));
620 status = U_ZERO_ERROR;
621 return;
622 }
623
624 if (rules == NULL) {
625 errln("file %s, line %d: rules pointer is NULL", __FILE__, line);
626 return;
627 }
628
629 va_list ap;
630 va_start(ap, keyword);
631 for (;;) {
632 const char *num = va_arg(ap, const char *);
633 if (strcmp(num, END_MARK) == 0) {
634 break;
635 }
636
637 // DigitList is a convenient way to parse the decimal number string and get a double.
638 DecimalQuantity dl;
639 dl.setToDecNumber(StringPiece(num), status);
640 if (U_FAILURE(status)) {
641 errln("file %s, line %d, ICU error status: %s.", __FILE__, line, u_errorName(status));
642 status = U_ZERO_ERROR;
643 continue;
644 }
645 double numDbl = dl.toDouble();
646 const char *decimalPoint = strchr(num, '.');
647 int fractionDigitCount = decimalPoint == NULL ? 0 : (num + strlen(num) - 1) - decimalPoint;
648 int fractionDigits = fractionDigitCount == 0 ? 0 : atoi(decimalPoint + 1);
649 FixedDecimal ni(numDbl, fractionDigitCount, fractionDigits);
650
651 UnicodeString actualKeyword = rules->select(ni);
652 if (actualKeyword != UnicodeString(keyword)) {
653 errln("file %s, line %d, select(%s) returned incorrect keyword. Expected %s, got %s",
654 __FILE__, line, num, keyword, US(actualKeyword).cstr());
655 }
656 }
657 va_end(ap);
658 }
659
testSelect()660 void PluralRulesTest::testSelect() {
661 UErrorCode status = U_ZERO_ERROR;
662 LocalPointer<PluralRules> pr(PluralRules::createRules("s: n in 1,3,4,6", status));
663 checkSelect(pr, status, __LINE__, "s", "1.0", "3.0", "4.0", "6.0", END_MARK);
664 checkSelect(pr, status, __LINE__, "other", "0.0", "2.0", "3.1", "7.0", END_MARK);
665
666 pr.adoptInstead(PluralRules::createRules("s: n not in 1,3,4,6", status));
667 checkSelect(pr, status, __LINE__, "other", "1.0", "3.0", "4.0", "6.0", END_MARK);
668 checkSelect(pr, status, __LINE__, "s", "0.0", "2.0", "3.1", "7.0", END_MARK);
669
670 pr.adoptInstead(PluralRules::createRules("r: n in 1..4, 7..10, 14 .. 17;"
671 "s: n is 29;", status));
672 checkSelect(pr, status, __LINE__, "r", "1.0", "3.0", "7.0", "8.0", "10.0", "14.0", "17.0", END_MARK);
673 checkSelect(pr, status, __LINE__, "s", "29.0", END_MARK);
674 checkSelect(pr, status, __LINE__, "other", "28.0", "29.1", END_MARK);
675
676 pr.adoptInstead(PluralRules::createRules("a: n mod 10 is 1; b: n mod 100 is 0 ", status));
677 checkSelect(pr, status, __LINE__, "a", "1", "11", "41", "101", "301.00", END_MARK);
678 checkSelect(pr, status, __LINE__, "b", "0", "100", "200.0", "300.", "1000", "1100", "110000", END_MARK);
679 checkSelect(pr, status, __LINE__, "other", "0.01", "1.01", "0.99", "2", "3", "99", "102", END_MARK);
680
681 // Rules that end with or without a ';' and with or without trailing spaces.
682 // (There was a rule parser bug here with these.)
683 pr.adoptInstead(PluralRules::createRules("a: n is 1", status));
684 checkSelect(pr, status, __LINE__, "a", "1", END_MARK);
685 checkSelect(pr, status, __LINE__, "other", "2", END_MARK);
686
687 pr.adoptInstead(PluralRules::createRules("a: n is 1 ", status));
688 checkSelect(pr, status, __LINE__, "a", "1", END_MARK);
689 checkSelect(pr, status, __LINE__, "other", "2", END_MARK);
690
691 pr.adoptInstead(PluralRules::createRules("a: n is 1;", status));
692 checkSelect(pr, status, __LINE__, "a", "1", END_MARK);
693 checkSelect(pr, status, __LINE__, "other", "2", END_MARK);
694
695 pr.adoptInstead(PluralRules::createRules("a: n is 1 ; ", status));
696 checkSelect(pr, status, __LINE__, "a", "1", END_MARK);
697 checkSelect(pr, status, __LINE__, "other", "2", END_MARK);
698
699 // First match when rules for different keywords are not disjoint.
700 // Also try spacing variations around ':' and '..'
701 pr.adoptInstead(PluralRules::createRules("c: n in 5..15; b : n in 1..10 ;a:n in 10 .. 20", status));
702 checkSelect(pr, status, __LINE__, "a", "20", END_MARK);
703 checkSelect(pr, status, __LINE__, "b", "1", END_MARK);
704 checkSelect(pr, status, __LINE__, "c", "10", END_MARK);
705 checkSelect(pr, status, __LINE__, "other", "0", "21", "10.1", END_MARK);
706
707 // in vs within
708 pr.adoptInstead(PluralRules::createRules("a: n in 2..10; b: n within 8..15", status));
709 checkSelect(pr, status, __LINE__, "a", "2", "8", "10", END_MARK);
710 checkSelect(pr, status, __LINE__, "b", "8.01", "9.5", "11", "14.99", "15", END_MARK);
711 checkSelect(pr, status, __LINE__, "other", "1", "7.7", "15.01", "16", END_MARK);
712
713 // OR and AND chains.
714 pr.adoptInstead(PluralRules::createRules("a: n in 2..10 and n in 4..12 and n not in 5..7", status));
715 checkSelect(pr, status, __LINE__, "a", "4", "8", "9", "10", END_MARK);
716 checkSelect(pr, status, __LINE__, "other", "2", "3", "5", "7", "11", END_MARK);
717 pr.adoptInstead(PluralRules::createRules("a: n is 2 or n is 5 or n in 7..11 and n in 11..13", status));
718 checkSelect(pr, status, __LINE__, "a", "2", "5", "11", END_MARK);
719 checkSelect(pr, status, __LINE__, "other", "3", "4", "6", "8", "10", "12", "13", END_MARK);
720
721 // Number attributes -
722 // n: the number itself
723 // i: integer digits
724 // f: visible fraction digits
725 // t: f with trailing zeros removed.
726 // v: number of visible fraction digits
727 // j: = n if there are no visible fraction digits
728 // != anything if there are visible fraction digits
729
730 pr.adoptInstead(PluralRules::createRules("a: i is 123", status));
731 checkSelect(pr, status, __LINE__, "a", "123", "123.0", "123.1", "0123.99", END_MARK);
732 checkSelect(pr, status, __LINE__, "other", "124", "122.0", END_MARK);
733
734 pr.adoptInstead(PluralRules::createRules("a: f is 120", status));
735 checkSelect(pr, status, __LINE__, "a", "1.120", "0.120", "11123.120", "0123.120", END_MARK);
736 checkSelect(pr, status, __LINE__, "other", "1.121", "122.1200", "1.12", "120", END_MARK);
737
738 pr.adoptInstead(PluralRules::createRules("a: t is 12", status));
739 checkSelect(pr, status, __LINE__, "a", "1.120", "0.12", "11123.12000", "0123.1200000", END_MARK);
740 checkSelect(pr, status, __LINE__, "other", "1.121", "122.1200001", "1.11", "12", END_MARK);
741
742 pr.adoptInstead(PluralRules::createRules("a: v is 3", status));
743 checkSelect(pr, status, __LINE__, "a", "1.120", "0.000", "11123.100", "0123.124", ".666", END_MARK);
744 checkSelect(pr, status, __LINE__, "other", "1.1212", "122.12", "1.1", "122", "0.0000", END_MARK);
745
746 pr.adoptInstead(PluralRules::createRules("a: v is 0 and i is 123", status));
747 checkSelect(pr, status, __LINE__, "a", "123", "123.", END_MARK);
748 checkSelect(pr, status, __LINE__, "other", "123.0", "123.1", "123.123", "0.123", END_MARK);
749
750 // The reserved words from the rule syntax will also function as keywords.
751 pr.adoptInstead(PluralRules::createRules("a: n is 21; n: n is 22; i: n is 23; f: n is 24;"
752 "t: n is 25; v: n is 26; w: n is 27; j: n is 28"
753 , status));
754 checkSelect(pr, status, __LINE__, "other", "20", "29", END_MARK);
755 checkSelect(pr, status, __LINE__, "a", "21", END_MARK);
756 checkSelect(pr, status, __LINE__, "n", "22", END_MARK);
757 checkSelect(pr, status, __LINE__, "i", "23", END_MARK);
758 checkSelect(pr, status, __LINE__, "f", "24", END_MARK);
759 checkSelect(pr, status, __LINE__, "t", "25", END_MARK);
760 checkSelect(pr, status, __LINE__, "v", "26", END_MARK);
761 checkSelect(pr, status, __LINE__, "w", "27", END_MARK);
762 checkSelect(pr, status, __LINE__, "j", "28", END_MARK);
763
764
765 pr.adoptInstead(PluralRules::createRules("not: n=31; and: n=32; or: n=33; mod: n=34;"
766 "in: n=35; within: n=36;is:n=37"
767 , status));
768 checkSelect(pr, status, __LINE__, "other", "30", "39", END_MARK);
769 checkSelect(pr, status, __LINE__, "not", "31", END_MARK);
770 checkSelect(pr, status, __LINE__, "and", "32", END_MARK);
771 checkSelect(pr, status, __LINE__, "or", "33", END_MARK);
772 checkSelect(pr, status, __LINE__, "mod", "34", END_MARK);
773 checkSelect(pr, status, __LINE__, "in", "35", END_MARK);
774 checkSelect(pr, status, __LINE__, "within", "36", END_MARK);
775 checkSelect(pr, status, __LINE__, "is", "37", END_MARK);
776
777 // Test cases from ICU4J PluralRulesTest.parseTestData
778
779 pr.adoptInstead(PluralRules::createRules("a: n is 1", status));
780 checkSelect(pr, status, __LINE__, "a", "1", END_MARK);
781 pr.adoptInstead(PluralRules::createRules("a: n mod 10 is 2", status));
782 checkSelect(pr, status, __LINE__, "a", "2", "12", "22", END_MARK);
783 pr.adoptInstead(PluralRules::createRules("a: n is not 1", status));
784 checkSelect(pr, status, __LINE__, "a", "0", "2", "3", "4", "5", END_MARK);
785 pr.adoptInstead(PluralRules::createRules("a: n mod 3 is not 1", status));
786 checkSelect(pr, status, __LINE__, "a", "0", "2", "3", "5", "6", "8", "9", END_MARK);
787 pr.adoptInstead(PluralRules::createRules("a: n in 2..5", status));
788 checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", END_MARK);
789 pr.adoptInstead(PluralRules::createRules("a: n within 2..5", status));
790 checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", END_MARK);
791 pr.adoptInstead(PluralRules::createRules("a: n not in 2..5", status));
792 checkSelect(pr, status, __LINE__, "a", "0", "1", "6", "7", "8", END_MARK);
793 pr.adoptInstead(PluralRules::createRules("a: n not within 2..5", status));
794 checkSelect(pr, status, __LINE__, "a", "0", "1", "6", "7", "8", END_MARK);
795 pr.adoptInstead(PluralRules::createRules("a: n mod 10 in 2..5", status));
796 checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", "12", "13", "14", "15", "22", "23", "24", "25", END_MARK);
797 pr.adoptInstead(PluralRules::createRules("a: n mod 10 within 2..5", status));
798 checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", "12", "13", "14", "15", "22", "23", "24", "25", END_MARK);
799 pr.adoptInstead(PluralRules::createRules("a: n mod 10 is 2 and n is not 12", status));
800 checkSelect(pr, status, __LINE__, "a", "2", "22", "32", "42", END_MARK);
801 pr.adoptInstead(PluralRules::createRules("a: n mod 10 in 2..3 or n mod 10 is 5", status));
802 checkSelect(pr, status, __LINE__, "a", "2", "3", "5", "12", "13", "15", "22", "23", "25", END_MARK);
803 pr.adoptInstead(PluralRules::createRules("a: n mod 10 within 2..3 or n mod 10 is 5", status));
804 checkSelect(pr, status, __LINE__, "a", "2", "3", "5", "12", "13", "15", "22", "23", "25", END_MARK);
805 pr.adoptInstead(PluralRules::createRules("a: n is 1 or n is 4 or n is 23", status));
806 checkSelect(pr, status, __LINE__, "a", "1", "4", "23", END_MARK);
807 pr.adoptInstead(PluralRules::createRules("a: n mod 2 is 1 and n is not 3 and n in 1..11", status));
808 checkSelect(pr, status, __LINE__, "a", "1", "5", "7", "9", "11", END_MARK);
809 pr.adoptInstead(PluralRules::createRules("a: n mod 2 is 1 and n is not 3 and n within 1..11", status));
810 checkSelect(pr, status, __LINE__, "a", "1", "5", "7", "9", "11", END_MARK);
811 pr.adoptInstead(PluralRules::createRules("a: n mod 2 is 1 or n mod 5 is 1 and n is not 6", status));
812 checkSelect(pr, status, __LINE__, "a", "1", "3", "5", "7", "9", "11", "13", "15", "16", END_MARK);
813 pr.adoptInstead(PluralRules::createRules("a: n in 2..5; b: n in 5..8; c: n mod 2 is 1", status));
814 checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", END_MARK);
815 checkSelect(pr, status, __LINE__, "b", "6", "7", "8", END_MARK);
816 checkSelect(pr, status, __LINE__, "c", "1", "9", "11", END_MARK);
817 pr.adoptInstead(PluralRules::createRules("a: n within 2..5; b: n within 5..8; c: n mod 2 is 1", status));
818 checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", END_MARK);
819 checkSelect(pr, status, __LINE__, "b", "6", "7", "8", END_MARK);
820 checkSelect(pr, status, __LINE__, "c", "1", "9", "11", END_MARK);
821 pr.adoptInstead(PluralRules::createRules("a: n in 2, 4..6; b: n within 7..9,11..12,20", status));
822 checkSelect(pr, status, __LINE__, "a", "2", "4", "5", "6", END_MARK);
823 checkSelect(pr, status, __LINE__, "b", "7", "8", "9", "11", "12", "20", END_MARK);
824 pr.adoptInstead(PluralRules::createRules("a: n in 2..8, 12 and n not in 4..6", status));
825 checkSelect(pr, status, __LINE__, "a", "2", "3", "7", "8", "12", END_MARK);
826 pr.adoptInstead(PluralRules::createRules("a: n mod 10 in 2, 3,5..7 and n is not 12", status));
827 checkSelect(pr, status, __LINE__, "a", "2", "3", "5", "6", "7", "13", "15", "16", "17", END_MARK);
828 pr.adoptInstead(PluralRules::createRules("a: n in 2..6, 3..7", status));
829 checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", "6", "7", END_MARK);
830
831 // Extended Syntax, with '=', '!=' and '%' operators.
832 pr.adoptInstead(PluralRules::createRules("a: n = 1..8 and n!= 2,3,4,5", status));
833 checkSelect(pr, status, __LINE__, "a", "1", "6", "7", "8", END_MARK);
834 checkSelect(pr, status, __LINE__, "other", "0", "2", "3", "4", "5", "9", END_MARK);
835 pr.adoptInstead(PluralRules::createRules("a:n % 10 != 1", status));
836 checkSelect(pr, status, __LINE__, "a", "2", "6", "7", "8", END_MARK);
837 checkSelect(pr, status, __LINE__, "other", "1", "21", "211", "91", END_MARK);
838 }
839
840
testAvailbleLocales()841 void PluralRulesTest::testAvailbleLocales() {
842
843 // Hash set of (char *) strings.
844 UErrorCode status = U_ZERO_ERROR;
845 UHashtable *localeSet = uhash_open(uhash_hashUnicodeString, uhash_compareUnicodeString, uhash_compareLong, &status);
846 uhash_setKeyDeleter(localeSet, uprv_deleteUObject);
847 if (U_FAILURE(status)) {
848 errln("file %s, line %d: Error status = %s", __FILE__, __LINE__, u_errorName(status));
849 return;
850 }
851
852 // Check that each locale returned by the iterator is unique.
853 StringEnumeration *localesEnum = PluralRules::getAvailableLocales(status);
854 int localeCount = 0;
855 for (;;) {
856 const char *locale = localesEnum->next(NULL, status);
857 if (U_FAILURE(status)) {
858 dataerrln("file %s, line %d: Error status = %s", __FILE__, __LINE__, u_errorName(status));
859 return;
860 }
861 if (locale == NULL) {
862 break;
863 }
864 localeCount++;
865 int32_t oldVal = uhash_puti(localeSet, new UnicodeString(locale), 1, &status);
866 if (oldVal != 0) {
867 errln("file %s, line %d: locale %s was seen before.", __FILE__, __LINE__, locale);
868 }
869 }
870
871 // Reset the iterator, verify that we get the same count.
872 localesEnum->reset(status);
873 int32_t localeCount2 = 0;
874 while (localesEnum->next(NULL, status) != NULL) {
875 if (U_FAILURE(status)) {
876 errln("file %s, line %d: Error status = %s", __FILE__, __LINE__, u_errorName(status));
877 break;
878 }
879 localeCount2++;
880 }
881 if (localeCount != localeCount2) {
882 errln("file %s, line %d: locale counts differ. They are (%d, %d)",
883 __FILE__, __LINE__, localeCount, localeCount2);
884 }
885
886 // Instantiate plural rules for each available locale.
887 localesEnum->reset(status);
888 for (;;) {
889 status = U_ZERO_ERROR;
890 const char *localeName = localesEnum->next(NULL, status);
891 if (U_FAILURE(status)) {
892 errln("file %s, line %d: Error status = %s, locale = %s",
893 __FILE__, __LINE__, u_errorName(status), localeName);
894 return;
895 }
896 if (localeName == NULL) {
897 break;
898 }
899 Locale locale = Locale::createFromName(localeName);
900 PluralRules *pr = PluralRules::forLocale(locale, status);
901 if (U_FAILURE(status)) {
902 errln("file %s, line %d: Error %s creating plural rules for locale %s",
903 __FILE__, __LINE__, u_errorName(status), localeName);
904 continue;
905 }
906 if (pr == NULL) {
907 errln("file %s, line %d: Null plural rules for locale %s", __FILE__, __LINE__, localeName);
908 continue;
909 }
910
911 // Pump some numbers through the plural rules. Can't check for correct results,
912 // mostly this to tickle any asserts or crashes that may be lurking.
913 for (double n=0; n<120.0; n+=0.5) {
914 UnicodeString keyword = pr->select(n);
915 if (keyword.length() == 0) {
916 errln("file %s, line %d, empty keyword for n = %g, locale %s",
917 __FILE__, __LINE__, n, localeName);
918 }
919 }
920 delete pr;
921 }
922
923 uhash_close(localeSet);
924 delete localesEnum;
925
926 }
927
928
testParseErrors()929 void PluralRulesTest::testParseErrors() {
930 // Test rules with syntax errors.
931 // Creation of PluralRules from them should fail.
932
933 static const char *testCases[] = {
934 "a: n mod 10, is 1",
935 "a: q is 13",
936 "a n is 13",
937 "a: n is 13,",
938 "a: n is 13, 15, b: n is 4",
939 "a: n is 1, 3, 4.. ",
940 "a: n within 5..4",
941 "A: n is 13", // Uppercase keywords not allowed.
942 "a: n ! = 3", // spaces in != operator
943 "a: n = not 3", // '=' not exact equivalent of 'is'
944 "a: n ! in 3..4" // '!' not exact equivalent of 'not'
945 "a: n % 37 ! in 3..4"
946
947 };
948 for (int i=0; i<UPRV_LENGTHOF(testCases); i++) {
949 const char *rules = testCases[i];
950 UErrorCode status = U_ZERO_ERROR;
951 PluralRules *pr = PluralRules::createRules(UnicodeString(rules), status);
952 if (U_SUCCESS(status)) {
953 errln("file %s, line %d, expected failure with \"%s\".", __FILE__, __LINE__, rules);
954 }
955 if (pr != NULL) {
956 errln("file %s, line %d, expected NULL. Rules: \"%s\"", __FILE__, __LINE__, rules);
957 }
958 }
959 return;
960 }
961
962
testFixedDecimal()963 void PluralRulesTest::testFixedDecimal() {
964 struct DoubleTestCase {
965 double n;
966 int32_t fractionDigitCount;
967 int64_t fractionDigits;
968 };
969
970 // Check that the internal functions for extracting the decimal fraction digits from
971 // a double value are working.
972 static DoubleTestCase testCases[] = {
973 {1.0, 0, 0},
974 {123456.0, 0, 0},
975 {1.1, 1, 1},
976 {1.23, 2, 23},
977 {1.234, 3, 234},
978 {1.2345, 4, 2345},
979 {1.23456, 5, 23456},
980 {.1234, 4, 1234},
981 {.01234, 5, 1234},
982 {.001234, 6, 1234},
983 {.0001234, 7, 1234},
984 {100.1234, 4, 1234},
985 {100.01234, 5, 1234},
986 {100.001234, 6, 1234},
987 {100.0001234, 7, 1234}
988 };
989
990 for (int i=0; i<UPRV_LENGTHOF(testCases); ++i) {
991 DoubleTestCase &tc = testCases[i];
992 int32_t numFractionDigits = FixedDecimal::decimals(tc.n);
993 if (numFractionDigits != tc.fractionDigitCount) {
994 errln("file %s, line %d: decimals(%g) expected %d, actual %d",
995 __FILE__, __LINE__, tc.n, tc.fractionDigitCount, numFractionDigits);
996 continue;
997 }
998 int64_t actualFractionDigits = FixedDecimal::getFractionalDigits(tc.n, numFractionDigits);
999 if (actualFractionDigits != tc.fractionDigits) {
1000 errln("file %s, line %d: getFractionDigits(%g, %d): expected %ld, got %ld",
1001 __FILE__, __LINE__, tc.n, numFractionDigits, tc.fractionDigits, actualFractionDigits);
1002 }
1003 }
1004 }
1005
1006
1007
1008 #endif /* #if !UCONFIG_NO_FORMATTING */
1009