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 PLURULTS.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 #include "unicode/numberformatter.h"
26 #include "unicode/numberrangeformatter.h"
27
28 #include "cmemory.h"
29 #include "cstr.h"
30 #include "plurrule_impl.h"
31 #include "plurults.h"
32 #include "uhash.h"
33 #include "number_decimalquantity.h"
34
35 using icu::number::impl::DecimalQuantity;
36 using namespace icu::number;
37
38 void setupResult(const int32_t testSource[], char result[], int32_t* max);
39 UBool checkEqual(const PluralRules &test, char *result, int32_t max);
40 UBool testEquality(const PluralRules &test);
41
42 // This is an API test, not a unit test. It doesn't test very many cases, and doesn't
43 // try to test the full functionality. It just calls each function in the class and
44 // verifies that it works on a basic level.
45
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)46 void PluralRulesTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par*/ )
47 {
48 if (exec) logln("TestSuite PluralRulesAPI");
49 TESTCASE_AUTO_BEGIN;
50 TESTCASE_AUTO(testAPI);
51 // TESTCASE_AUTO(testGetUniqueKeywordValue);
52 TESTCASE_AUTO(testGetSamples);
53 TESTCASE_AUTO(testGetFixedDecimalSamples);
54 TESTCASE_AUTO(testSamplesWithExponent);
55 TESTCASE_AUTO(testWithin);
56 TESTCASE_AUTO(testGetAllKeywordValues);
57 TESTCASE_AUTO(testCompactDecimalPluralKeyword);
58 TESTCASE_AUTO(testOrdinal);
59 TESTCASE_AUTO(testSelect);
60 TESTCASE_AUTO(testSelectRange);
61 TESTCASE_AUTO(testAvailbleLocales);
62 TESTCASE_AUTO(testParseErrors);
63 TESTCASE_AUTO(testFixedDecimal);
64 TESTCASE_AUTO(testSelectTrailingZeros);
65 TESTCASE_AUTO(testLocaleExtension);
66 TESTCASE_AUTO_END;
67 }
68
69
70 // Quick and dirty class for putting UnicodeStrings in char * messages.
71 // TODO: something like this should be generally available.
72 class US {
73 private:
74 char *buf;
75 public:
US(const UnicodeString & us)76 US(const UnicodeString &us) {
77 int32_t bufLen = us.extract((int32_t)0, us.length(), (char *)NULL, (uint32_t)0) + 1;
78 buf = (char *)uprv_malloc(bufLen);
79 us.extract(0, us.length(), buf, bufLen); }
cstr()80 const char *cstr() {return buf;}
~US()81 ~US() { uprv_free(buf);}
82 };
83
84
85
86
87
88 #define PLURAL_TEST_NUM 18
89 /**
90 * Test various generic API methods of PluralRules for API coverage.
91 */
testAPI()92 void PluralRulesTest::testAPI(/*char *par*/)
93 {
94 UnicodeString pluralTestData[PLURAL_TEST_NUM] = {
95 UNICODE_STRING_SIMPLE("a: n is 1"),
96 UNICODE_STRING_SIMPLE("a: n mod 10 is 2"),
97 UNICODE_STRING_SIMPLE("a: n is not 1"),
98 UNICODE_STRING_SIMPLE("a: n mod 3 is not 1"),
99 UNICODE_STRING_SIMPLE("a: n in 2..5"),
100 UNICODE_STRING_SIMPLE("a: n within 2..5"),
101 UNICODE_STRING_SIMPLE("a: n not in 2..5"),
102 UNICODE_STRING_SIMPLE("a: n not within 2..5"),
103 UNICODE_STRING_SIMPLE("a: n mod 10 in 2..5"),
104 UNICODE_STRING_SIMPLE("a: n mod 10 within 2..5"),
105 UNICODE_STRING_SIMPLE("a: n mod 10 is 2 and n is not 12"),
106 UNICODE_STRING_SIMPLE("a: n mod 10 in 2..3 or n mod 10 is 5"),
107 UNICODE_STRING_SIMPLE("a: n mod 10 within 2..3 or n mod 10 is 5"),
108 UNICODE_STRING_SIMPLE("a: n is 1 or n is 4 or n is 23"),
109 UNICODE_STRING_SIMPLE("a: n mod 2 is 1 and n is not 3 and n in 1..11"),
110 UNICODE_STRING_SIMPLE("a: n mod 2 is 1 and n is not 3 and n within 1..11"),
111 UNICODE_STRING_SIMPLE("a: n mod 2 is 1 or n mod 5 is 1 and n is not 6"),
112 "",
113 };
114 static const int32_t pluralTestResult[PLURAL_TEST_NUM][30] = {
115 {1, 0},
116 {2,12,22, 0},
117 {0,2,3,4,5,0},
118 {0,2,3,5,6,8,9,0},
119 {2,3,4,5,0},
120 {2,3,4,5,0},
121 {0,1,6,7,8, 0},
122 {0,1,6,7,8, 0},
123 {2,3,4,5,12,13,14,15,22,23,24,25,0},
124 {2,3,4,5,12,13,14,15,22,23,24,25,0},
125 {2,22,32,42,0},
126 {2,3,5,12,13,15,22,23,25,0},
127 {2,3,5,12,13,15,22,23,25,0},
128 {1,4,23,0},
129 {1,5,7,9,11,0},
130 {1,5,7,9,11,0},
131 {1,3,5,7,9,11,13,15,16,0},
132 };
133 UErrorCode status = U_ZERO_ERROR;
134
135 // ======= Test constructors
136 logln("Testing PluralRules constructors");
137
138
139 logln("\n start default locale test case ..\n");
140
141 PluralRules defRule(status);
142 LocalPointer<PluralRules> test(new PluralRules(status), status);
143 if(U_FAILURE(status)) {
144 dataerrln("ERROR: Could not create PluralRules (default) - exitting");
145 return;
146 }
147 LocalPointer<PluralRules> newEnPlural(test->forLocale(Locale::getEnglish(), status), status);
148 if(U_FAILURE(status)) {
149 dataerrln("ERROR: Could not create PluralRules (English) - exitting");
150 return;
151 }
152
153 // ======= Test clone, assignment operator && == operator.
154 LocalPointer<PluralRules> dupRule(defRule.clone());
155 if (dupRule==NULL) {
156 errln("ERROR: clone plural rules test failed!");
157 return;
158 } else {
159 if ( *dupRule != defRule ) {
160 errln("ERROR: clone plural rules test failed!");
161 }
162 }
163 *dupRule = *newEnPlural;
164 if (dupRule!=NULL) {
165 if ( *dupRule != *newEnPlural ) {
166 errln("ERROR: clone plural rules test failed!");
167 }
168 }
169
170 // ======= Test empty plural rules
171 logln("Testing Simple PluralRules");
172
173 LocalPointer<PluralRules> empRule(test->createRules(UNICODE_STRING_SIMPLE("a:n"), status));
174 UnicodeString key;
175 for (int32_t i=0; i<10; ++i) {
176 key = empRule->select(i);
177 if ( key.charAt(0)!= 0x61 ) { // 'a'
178 errln("ERROR: empty plural rules test failed! - exitting");
179 }
180 }
181
182 // ======= Test simple plural rules
183 logln("Testing Simple PluralRules");
184
185 char result[100];
186 int32_t max;
187
188 for (int32_t i=0; i<PLURAL_TEST_NUM-1; ++i) {
189 LocalPointer<PluralRules> newRules(test->createRules(pluralTestData[i], status));
190 setupResult(pluralTestResult[i], result, &max);
191 if ( !checkEqual(*newRules, result, max) ) {
192 errln("ERROR: simple plural rules failed! - exitting");
193 return;
194 }
195 }
196
197 // ======= Test complex plural rules
198 logln("Testing Complex PluralRules");
199 // TODO: the complex test data is hard coded. It's better to implement
200 // a parser to parse the test data.
201 UnicodeString complexRule = UNICODE_STRING_SIMPLE("a: n in 2..5; b: n in 5..8; c: n mod 2 is 1");
202 UnicodeString complexRule2 = UNICODE_STRING_SIMPLE("a: n within 2..5; b: n within 5..8; c: n mod 2 is 1");
203 char cRuleResult[] =
204 {
205 0x6F, // 'o'
206 0x63, // 'c'
207 0x61, // 'a'
208 0x61, // 'a'
209 0x61, // 'a'
210 0x61, // 'a'
211 0x62, // 'b'
212 0x62, // 'b'
213 0x62, // 'b'
214 0x63, // 'c'
215 0x6F, // 'o'
216 0x63 // 'c'
217 };
218 LocalPointer<PluralRules> newRules(test->createRules(complexRule, status));
219 if ( !checkEqual(*newRules, cRuleResult, 12) ) {
220 errln("ERROR: complex plural rules failed! - exitting");
221 return;
222 }
223 newRules.adoptInstead(test->createRules(complexRule2, status));
224 if ( !checkEqual(*newRules, cRuleResult, 12) ) {
225 errln("ERROR: complex plural rules failed! - exitting");
226 return;
227 }
228
229 // ======= Test decimal fractions plural rules
230 UnicodeString decimalRule= UNICODE_STRING_SIMPLE("a: n not in 0..100;");
231 UnicodeString KEYWORD_A = UNICODE_STRING_SIMPLE("a");
232 status = U_ZERO_ERROR;
233 newRules.adoptInstead(test->createRules(decimalRule, status));
234 if (U_FAILURE(status)) {
235 dataerrln("ERROR: Could not create PluralRules for testing fractions - exitting");
236 return;
237 }
238 double fData[] = {-101, -100, -1, -0.0, 0, 0.1, 1, 1.999, 2.0, 100, 100.001 };
239 UBool isKeywordA[] = {TRUE, FALSE, FALSE, FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, TRUE };
240 for (int32_t i=0; i<UPRV_LENGTHOF(fData); i++) {
241 if ((newRules->select(fData[i])== KEYWORD_A) != isKeywordA[i]) {
242 errln("File %s, Line %d, ERROR: plural rules for decimal fractions test failed!\n"
243 " number = %g, expected %s", __FILE__, __LINE__, fData[i], isKeywordA[i]?"TRUE":"FALSE");
244 }
245 }
246
247 // ======= Test Equality
248 logln("Testing Equality of PluralRules");
249
250 if ( !testEquality(*test) ) {
251 errln("ERROR: complex plural rules failed! - exitting");
252 return;
253 }
254
255
256 // ======= Test getStaticClassID()
257 logln("Testing getStaticClassID()");
258
259 if(test->getDynamicClassID() != PluralRules::getStaticClassID()) {
260 errln("ERROR: getDynamicClassID() didn't return the expected value");
261 }
262 // ====== Test fallback to parent locale
263 LocalPointer<PluralRules> en_UK(test->forLocale(Locale::getUK(), status));
264 LocalPointer<PluralRules> en(test->forLocale(Locale::getEnglish(), status));
265 if (en_UK.isValid() && en.isValid()) {
266 if ( *en_UK != *en ) {
267 errln("ERROR: test locale fallback failed!");
268 }
269 }
270
271 LocalPointer<PluralRules> zh_Hant(test->forLocale(Locale::getTaiwan(), status));
272 LocalPointer<PluralRules> zh(test->forLocale(Locale::getChinese(), status));
273 if (zh_Hant.isValid() && zh.isValid()) {
274 if ( *zh_Hant != *zh ) {
275 errln("ERROR: test locale fallback failed!");
276 }
277 }
278 }
279
setupResult(const int32_t testSource[],char result[],int32_t * max)280 void setupResult(const int32_t testSource[], char result[], int32_t* max) {
281 int32_t i=0;
282 int32_t curIndex=0;
283
284 do {
285 while (curIndex < testSource[i]) {
286 result[curIndex++]=0x6F; //'o' other
287 }
288 result[curIndex++]=0x61; // 'a'
289
290 } while(testSource[++i]>0);
291 *max=curIndex;
292 }
293
294
checkEqual(const PluralRules & test,char * result,int32_t max)295 UBool checkEqual(const PluralRules &test, char *result, int32_t max) {
296 UnicodeString key;
297 UBool isEqual = TRUE;
298 for (int32_t i=0; i<max; ++i) {
299 key= test.select(i);
300 if ( key.charAt(0)!=result[i] ) {
301 isEqual = FALSE;
302 }
303 }
304 return isEqual;
305 }
306
307
308
309 static const int32_t MAX_EQ_ROW = 2;
310 static const int32_t MAX_EQ_COL = 5;
testEquality(const PluralRules & test)311 UBool testEquality(const PluralRules &test) {
312 UnicodeString testEquRules[MAX_EQ_ROW][MAX_EQ_COL] = {
313 { UNICODE_STRING_SIMPLE("a: n in 2..3"),
314 UNICODE_STRING_SIMPLE("a: n is 2 or n is 3"),
315 UNICODE_STRING_SIMPLE( "a:n is 3 and n in 2..5 or n is 2"),
316 "",
317 },
318 { UNICODE_STRING_SIMPLE("a: n is 12; b:n mod 10 in 2..3"),
319 UNICODE_STRING_SIMPLE("b: n mod 10 in 2..3 and n is not 12; a: n in 12..12"),
320 UNICODE_STRING_SIMPLE("b: n is 13; a: n in 12..13; b: n mod 10 is 2 or n mod 10 is 3"),
321 "",
322 }
323 };
324 UErrorCode status = U_ZERO_ERROR;
325 UnicodeString key[MAX_EQ_COL];
326 UBool ret=TRUE;
327 for (int32_t i=0; i<MAX_EQ_ROW; ++i) {
328 PluralRules* rules[MAX_EQ_COL];
329
330 for (int32_t j=0; j<MAX_EQ_COL; ++j) {
331 rules[j]=NULL;
332 }
333 int32_t totalRules=0;
334 while((totalRules<MAX_EQ_COL) && (testEquRules[i][totalRules].length()>0) ) {
335 rules[totalRules]=test.createRules(testEquRules[i][totalRules], status);
336 totalRules++;
337 }
338 for (int32_t n=0; n<300 && ret ; ++n) {
339 for(int32_t j=0; j<totalRules;++j) {
340 key[j] = rules[j]->select(n);
341 }
342 for(int32_t j=0; j<totalRules-1;++j) {
343 if (key[j]!=key[j+1]) {
344 ret= FALSE;
345 break;
346 }
347 }
348
349 }
350 for (int32_t j=0; j<MAX_EQ_COL; ++j) {
351 if (rules[j]!=NULL) {
352 delete rules[j];
353 }
354 }
355 }
356
357 return ret;
358 }
359
360 void
assertRuleValue(const UnicodeString & rule,double expected)361 PluralRulesTest::assertRuleValue(const UnicodeString& rule, double expected) {
362 assertRuleKeyValue("a:" + rule, "a", expected);
363 }
364
365 void
assertRuleKeyValue(const UnicodeString & rule,const UnicodeString & key,double expected)366 PluralRulesTest::assertRuleKeyValue(const UnicodeString& rule,
367 const UnicodeString& key, double expected) {
368 UErrorCode status = U_ZERO_ERROR;
369 PluralRules *pr = PluralRules::createRules(rule, status);
370 double result = pr->getUniqueKeywordValue(key);
371 delete pr;
372 if (expected != result) {
373 errln("expected %g but got %g", expected, result);
374 }
375 }
376
377 // TODO: UniqueKeywordValue() is not currently supported.
378 // If it never will be, this test code should be removed.
testGetUniqueKeywordValue()379 void PluralRulesTest::testGetUniqueKeywordValue() {
380 assertRuleValue("n is 1", 1);
381 assertRuleValue("n in 2..2", 2);
382 assertRuleValue("n within 2..2", 2);
383 assertRuleValue("n in 3..4", UPLRULES_NO_UNIQUE_VALUE);
384 assertRuleValue("n within 3..4", UPLRULES_NO_UNIQUE_VALUE);
385 assertRuleValue("n is 2 or n is 2", 2);
386 assertRuleValue("n is 2 and n is 2", 2);
387 assertRuleValue("n is 2 or n is 3", UPLRULES_NO_UNIQUE_VALUE);
388 assertRuleValue("n is 2 and n is 3", UPLRULES_NO_UNIQUE_VALUE);
389 assertRuleValue("n is 2 or n in 2..3", UPLRULES_NO_UNIQUE_VALUE);
390 assertRuleValue("n is 2 and n in 2..3", 2);
391 assertRuleKeyValue("a: n is 1", "not_defined", UPLRULES_NO_UNIQUE_VALUE); // key not defined
392 assertRuleKeyValue("a: n is 1", "other", UPLRULES_NO_UNIQUE_VALUE); // key matches default rule
393 }
394
testGetSamples()395 void PluralRulesTest::testGetSamples() {
396 // TODO: fix samples, re-enable this test.
397
398 // no get functional equivalent API in ICU4C, so just
399 // test every locale...
400 UErrorCode status = U_ZERO_ERROR;
401 int32_t numLocales;
402 const Locale* locales = Locale::getAvailableLocales(numLocales);
403
404 double values[1000];
405 for (int32_t i = 0; U_SUCCESS(status) && i < numLocales; ++i) {
406 if (uprv_strcmp(locales[i].getLanguage(), "fr") == 0 &&
407 logKnownIssue("21299", "PluralRules::getSamples cannot distinguish 1e5 from 100000")) {
408 continue;
409 }
410 LocalPointer<PluralRules> rules(PluralRules::forLocale(locales[i], status));
411 if (U_FAILURE(status)) {
412 break;
413 }
414 LocalPointer<StringEnumeration> keywords(rules->getKeywords(status));
415 if (U_FAILURE(status)) {
416 break;
417 }
418 const UnicodeString* keyword;
419 while (NULL != (keyword = keywords->snext(status))) {
420 int32_t count = rules->getSamples(*keyword, values, UPRV_LENGTHOF(values), status);
421 if (U_FAILURE(status)) {
422 errln(UnicodeString(u"getSamples() failed for locale ") +
423 locales[i].getName() +
424 UnicodeString(u", keyword ") + *keyword);
425 continue;
426 }
427 if (count == 0) {
428 // TODO: Lots of these.
429 // errln(UnicodeString(u"no samples for keyword ") + *keyword + UnicodeString(u" in locale ") + locales[i].getName() );
430 }
431 if (count > UPRV_LENGTHOF(values)) {
432 errln(UnicodeString(u"getSamples()=") + count +
433 UnicodeString(u", too many values, for locale ") +
434 locales[i].getName() +
435 UnicodeString(u", keyword ") + *keyword);
436 count = UPRV_LENGTHOF(values);
437 }
438 for (int32_t j = 0; j < count; ++j) {
439 if (values[j] == UPLRULES_NO_UNIQUE_VALUE) {
440 errln("got 'no unique value' among values");
441 } else {
442 UnicodeString resultKeyword = rules->select(values[j]);
443 // if (strcmp(locales[i].getName(), "uk") == 0) { // Debug only.
444 // std::cout << " uk " << US(resultKeyword).cstr() << " " << values[j] << std::endl;
445 // }
446 if (*keyword != resultKeyword) {
447 errln("file %s, line %d, Locale %s, sample for keyword \"%s\": %g, select(%g) returns keyword \"%s\"",
448 __FILE__, __LINE__, locales[i].getName(), US(*keyword).cstr(), values[j], values[j], US(resultKeyword).cstr());
449 }
450 }
451 }
452 }
453 }
454 }
455
testGetFixedDecimalSamples()456 void PluralRulesTest::testGetFixedDecimalSamples() {
457 // TODO: fix samples, re-enable this test.
458
459 // no get functional equivalent API in ICU4C, so just
460 // test every locale...
461 UErrorCode status = U_ZERO_ERROR;
462 int32_t numLocales;
463 const Locale* locales = Locale::getAvailableLocales(numLocales);
464
465 FixedDecimal values[1000];
466 for (int32_t i = 0; U_SUCCESS(status) && i < numLocales; ++i) {
467 if (uprv_strcmp(locales[i].getLanguage(), "fr") == 0 &&
468 logKnownIssue("21299", "PluralRules::getSamples cannot distinguish 1e5 from 100000")) {
469 continue;
470 }
471 LocalPointer<PluralRules> rules(PluralRules::forLocale(locales[i], status));
472 if (U_FAILURE(status)) {
473 break;
474 }
475 LocalPointer<StringEnumeration> keywords(rules->getKeywords(status));
476 if (U_FAILURE(status)) {
477 break;
478 }
479 const UnicodeString* keyword;
480 while (NULL != (keyword = keywords->snext(status))) {
481 int32_t count = rules->getSamples(*keyword, values, UPRV_LENGTHOF(values), status);
482 if (U_FAILURE(status)) {
483 errln(UnicodeString(u"getSamples() failed for locale ") +
484 locales[i].getName() +
485 UnicodeString(u", keyword ") + *keyword);
486 continue;
487 }
488 if (count == 0) {
489 // TODO: Lots of these.
490 // errln(UnicodeString(u"no samples for keyword ") + *keyword + UnicodeString(u" in locale ") + locales[i].getName() );
491 }
492 if (count > UPRV_LENGTHOF(values)) {
493 errln(UnicodeString(u"getSamples()=") + count +
494 UnicodeString(u", too many values, for locale ") +
495 locales[i].getName() +
496 UnicodeString(u", keyword ") + *keyword);
497 count = UPRV_LENGTHOF(values);
498 }
499 for (int32_t j = 0; j < count; ++j) {
500 if (values[j] == UPLRULES_NO_UNIQUE_VALUE_DECIMAL) {
501 errln("got 'no unique value' among values");
502 } else {
503 UnicodeString resultKeyword = rules->select(values[j]);
504 // if (strcmp(locales[i].getName(), "uk") == 0) { // Debug only.
505 // std::cout << " uk " << US(resultKeyword).cstr() << " " << values[j] << std::endl;
506 // }
507 if (*keyword != resultKeyword) {
508 errln("file %s, line %d, Locale %s, sample for keyword \"%s\": %s, select(%s) returns keyword \"%s\"",
509 __FILE__, __LINE__, locales[i].getName(), US(*keyword).cstr(), values[j].toString().getBuffer(), values[j].toString().getBuffer(), US(resultKeyword).cstr());
510 }
511 }
512 }
513 }
514 }
515 }
516
testSamplesWithExponent()517 void PluralRulesTest::testSamplesWithExponent() {
518 // integer samples
519 UErrorCode status = U_ZERO_ERROR;
520 UnicodeString description(
521 u"one: i = 0,1 @integer 0, 1, 1e5 @decimal 0.0~1.5, 1.1e5; "
522 u"many: e = 0 and i != 0 and i % 1000000 = 0 and v = 0 or e != 0..5"
523 u" @integer 1000000, 2e6, 3e6, 4e6, 5e6, 6e6, 7e6, … @decimal 2.1e6, 3.1e6, 4.1e6, 5.1e6, 6.1e6, 7.1e6, …; "
524 u"other: @integer 2~17, 100, 1000, 10000, 100000, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, …"
525 u" @decimal 2.0~3.5, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 2.1e5, 3.1e5, 4.1e5, 5.1e5, 6.1e5, 7.1e5, …"
526 );
527 LocalPointer<PluralRules> test(PluralRules::createRules(description, status));
528 if (U_FAILURE(status)) {
529 errln("Couldn't create plural rules from a string using exponent notation, with error = %s", u_errorName(status));
530 return;
531 }
532 checkNewSamples(description, test, u"one", u"@integer 0, 1, 1e5", FixedDecimal(0));
533 checkNewSamples(description, test, u"many", u"@integer 1000000, 2e6, 3e6, 4e6, 5e6, 6e6, 7e6, …", FixedDecimal(1000000));
534 checkNewSamples(description, test, u"other", u"@integer 2~17, 100, 1000, 10000, 100000, 2e5, 3e5, 4e5, 5e5, 6e5, 7e5, …", FixedDecimal(2));
535
536 // decimal samples
537 status = U_ZERO_ERROR;
538 UnicodeString description2(
539 u"one: i = 0,1 @decimal 0.0~1.5, 1.1e5; "
540 u"many: e = 0 and i != 0 and i % 1000000 = 0 and v = 0 or e != 0..5"
541 u" @decimal 2.1e6, 3.1e6, 4.1e6, 5.1e6, 6.1e6, 7.1e6, …; "
542 u"other: @decimal 2.0~3.5, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 2.1e5, 3.1e5, 4.1e5, 5.1e5, 6.1e5, 7.1e5, …"
543 );
544 LocalPointer<PluralRules> test2(PluralRules::createRules(description2, status));
545 if (U_FAILURE(status)) {
546 errln("Couldn't create plural rules from a string using exponent notation, with error = %s", u_errorName(status));
547 return;
548 }
549 checkNewSamples(description2, test2, u"one", u"@decimal 0.0~1.5, 1.1e5", FixedDecimal(0, 1));
550 checkNewSamples(description2, test2, u"many", u"@decimal 2.1e6, 3.1e6, 4.1e6, 5.1e6, 6.1e6, 7.1e6, …", FixedDecimal::createWithExponent(2.1, 1, 6));
551 checkNewSamples(description2, test2, u"other", u"@decimal 2.0~3.5, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, 2.1e5, 3.1e5, 4.1e5, 5.1e5, 6.1e5, 7.1e5, …", FixedDecimal(2.0, 1));
552 }
553
checkNewSamples(UnicodeString description,const LocalPointer<PluralRules> & test,UnicodeString keyword,UnicodeString samplesString,FixedDecimal firstInRange)554 void PluralRulesTest::checkNewSamples(
555 UnicodeString description,
556 const LocalPointer<PluralRules> &test,
557 UnicodeString keyword,
558 UnicodeString samplesString,
559 FixedDecimal firstInRange) {
560
561 UErrorCode status = U_ZERO_ERROR;
562 FixedDecimal samples[1000];
563
564 test->getSamples(keyword, samples, UPRV_LENGTHOF(samples), status);
565 if (U_FAILURE(status)) {
566 errln("Couldn't retrieve plural samples, with error = %s", u_errorName(status));
567 return;
568 }
569 FixedDecimal actualFirstSample = samples[0];
570
571 if (!(firstInRange == actualFirstSample)) {
572 CStr descCstr(description);
573 CStr samplesCstr(samplesString);
574 char errMsg[1000];
575 snprintf(errMsg, sizeof(errMsg), "First parsed sample FixedDecimal not equal to expected for samples: %s in rule string: %s\n", descCstr(), samplesCstr());
576 errln(errMsg);
577 }
578 }
579
testWithin()580 void PluralRulesTest::testWithin() {
581 // goes to show you what lack of testing will do.
582 // of course, this has been broken for two years and no one has noticed...
583 UErrorCode status = U_ZERO_ERROR;
584 PluralRules *rules = PluralRules::createRules("a: n mod 10 in 5..8", status);
585 if (!rules) {
586 errln("couldn't instantiate rules");
587 return;
588 }
589
590 UnicodeString keyword = rules->select((int32_t)26);
591 if (keyword != "a") {
592 errln("expected 'a' for 26 but didn't get it.");
593 }
594
595 keyword = rules->select(26.5);
596 if (keyword != "other") {
597 errln("expected 'other' for 26.5 but didn't get it.");
598 }
599
600 delete rules;
601 }
602
603 void
testGetAllKeywordValues()604 PluralRulesTest::testGetAllKeywordValues() {
605 const char* data[] = {
606 "a: n in 2..5", "a: 2,3,4,5; other: null; b:",
607 "a: n not in 2..5", "a: null; other: null",
608 "a: n within 2..5", "a: null; other: null",
609 "a: n not within 2..5", "a: null; other: null",
610 "a: n in 2..5 or n within 6..8", "a: null", // ignore 'other' here on out, always null
611 "a: n in 2..5 and n within 6..8", "a:",
612 "a: n in 2..5 and n within 5..8", "a: 5",
613 "a: n within 2..5 and n within 6..8", "a:", // our sampling catches these
614 "a: n within 2..5 and n within 5..8", "a: 5", // ''
615 "a: n within 1..2 and n within 2..3 or n within 3..4 and n within 4..5", "a: 2,4",
616 "a: n within 1..2 and n within 2..3 or n within 3..4 and n within 4..5 "
617 "or n within 5..6 and n within 6..7", "a: null", // but not this...
618 "a: n mod 3 is 0", "a: null",
619 "a: n mod 3 is 0 and n within 1..2", "a:",
620 "a: n mod 3 is 0 and n within 0..5", "a: 0,3",
621 "a: n mod 3 is 0 and n within 0..6", "a: null", // similarly with mod, we don't catch...
622 "a: n mod 3 is 0 and n in 3..12", "a: 3,6,9,12",
623 NULL
624 };
625
626 for (int i = 0; data[i] != NULL; i += 2) {
627 UErrorCode status = U_ZERO_ERROR;
628 UnicodeString ruleDescription(data[i], -1, US_INV);
629 const char* result = data[i+1];
630
631 logln("[%d] %s", i >> 1, data[i]);
632
633 PluralRules *p = PluralRules::createRules(ruleDescription, status);
634 if (p == NULL || U_FAILURE(status)) {
635 errln("file %s, line %d: could not create rules from '%s'\n"
636 " ErrorCode: %s\n",
637 __FILE__, __LINE__, data[i], u_errorName(status));
638 continue;
639 }
640
641 // TODO: fix samples implementation, re-enable test.
642 (void)result;
643 #if 0
644
645 const char* rp = result;
646 while (*rp) {
647 while (*rp == ' ') ++rp;
648 if (!rp) {
649 break;
650 }
651
652 const char* ep = rp;
653 while (*ep && *ep != ':') ++ep;
654
655 status = U_ZERO_ERROR;
656 UnicodeString keyword(rp, ep - rp, US_INV);
657 double samples[4]; // no test above should have more samples than 4
658 int32_t count = p->getAllKeywordValues(keyword, &samples[0], 4, status);
659 if (U_FAILURE(status)) {
660 errln("error getting samples for %s", rp);
661 break;
662 }
663
664 if (count > 4) {
665 errln("count > 4 for keyword %s", rp);
666 count = 4;
667 }
668
669 if (*ep) {
670 ++ep; // skip colon
671 while (*ep && *ep == ' ') ++ep; // and spaces
672 }
673
674 UBool ok = TRUE;
675 if (count == -1) {
676 if (*ep != 'n') {
677 errln("expected values for keyword %s but got -1 (%s)", rp, ep);
678 ok = FALSE;
679 }
680 } else if (*ep == 'n') {
681 errln("expected count of -1, got %d, for keyword %s (%s)", count, rp, ep);
682 ok = FALSE;
683 }
684
685 // We'll cheat a bit here. The samples happend to be in order and so are our
686 // expected values, so we'll just test in order until a failure. If the
687 // implementation changes to return samples in an arbitrary order, this test
688 // must change. There's no actual restriction on the order of the samples.
689
690 for (int j = 0; ok && j < count; ++j ) { // we've verified count < 4
691 double val = samples[j];
692 if (*ep == 0 || *ep == ';') {
693 errln("got unexpected value[%d]: %g", j, val);
694 ok = FALSE;
695 break;
696 }
697 char* xp;
698 double expectedVal = strtod(ep, &xp);
699 if (xp == ep) {
700 // internal error
701 errln("yikes!");
702 ok = FALSE;
703 break;
704 }
705 ep = xp;
706 if (expectedVal != val) {
707 errln("expected %g but got %g", expectedVal, val);
708 ok = FALSE;
709 break;
710 }
711 if (*ep == ',') ++ep;
712 }
713
714 if (ok && count != -1) {
715 if (!(*ep == 0 || *ep == ';')) {
716 errln("file: %s, line %d, didn't get expected value: %s", __FILE__, __LINE__, ep);
717 ok = FALSE;
718 }
719 }
720
721 while (*ep && *ep != ';') ++ep;
722 if (*ep == ';') ++ep;
723 rp = ep;
724 }
725 #endif
726 delete p;
727 }
728 }
729
730 void
testCompactDecimalPluralKeyword()731 PluralRulesTest::testCompactDecimalPluralKeyword() {
732 IcuTestErrorCode errorCode(*this, "testCompactDecimalPluralKeyword");
733
734 LocalPointer<PluralRules> rules(PluralRules::createRules(
735 u"one: i = 0,1 @integer 0, 1 @decimal 0.0~1.5; "
736 u"many: e = 0 and i % 1000000 = 0 and v = 0 or e != 0 .. 5; "
737 u"other: @integer 2~17, 100, 1000, 10000, 100000, 1000000, "
738 u" @decimal 2.0~3.5, 10.0, 100.0, 1000.0, 10000.0, 100000.0, 1000000.0, …", errorCode));
739
740 if (U_FAILURE(errorCode)) {
741 errln("Couldn't instantiate plurals rules from string, with error = %s", u_errorName(errorCode));
742 return;
743 }
744
745 const char* localeName = "fr-FR";
746 Locale locale = Locale::createFromName(localeName);
747
748 struct TestCase {
749 const char16_t* skeleton;
750 const int input;
751 const char16_t* expectedFormattedOutput;
752 const char16_t* expectedPluralRuleKeyword;
753 } cases[] = {
754 // unlocalized formatter skeleton, input, string output, plural rule keyword
755 {u"", 0, u"0", u"one"},
756 {u"compact-long", 0, u"0", u"one"},
757
758 {u"", 1, u"1", u"one"},
759 {u"compact-long", 1, u"1", u"one"},
760
761 {u"", 2, u"2", u"other"},
762 {u"compact-long", 2, u"2", u"other"},
763
764 {u"", 1000000, u"1 000 000", u"many"},
765 {u"compact-long", 1000000, u"1 million", u"many"},
766
767 {u"", 1000001, u"1 000 001", u"other"},
768 {u"compact-long", 1000001, u"1 million", u"many"},
769
770 {u"", 120000, u"1 200 000", u"other"},
771 {u"compact-long", 1200000, u"1,2 millions", u"many"},
772
773 {u"", 1200001, u"1 200 001", u"other"},
774 {u"compact-long", 1200001, u"1,2 millions", u"many"},
775
776 {u"", 2000000, u"2 000 000", u"many"},
777 {u"compact-long", 2000000, u"2 millions", u"many"},
778 };
779 for (const auto& cas : cases) {
780 const char16_t* skeleton = cas.skeleton;
781 const int input = cas.input;
782 const char16_t* expectedPluralRuleKeyword = cas.expectedPluralRuleKeyword;
783
784 UnicodeString actualPluralRuleKeyword =
785 getPluralKeyword(rules, locale, input, skeleton);
786
787 UnicodeString message(UnicodeString(localeName) + u" " + DoubleToUnicodeString(input));
788 assertEquals(message, expectedPluralRuleKeyword, actualPluralRuleKeyword);
789 }
790 }
791
getPluralKeyword(const LocalPointer<PluralRules> & rules,Locale locale,double number,const char16_t * skeleton)792 UnicodeString PluralRulesTest::getPluralKeyword(const LocalPointer<PluralRules> &rules, Locale locale, double number, const char16_t* skeleton) {
793 IcuTestErrorCode errorCode(*this, "getPluralKeyword");
794 UnlocalizedNumberFormatter ulnf = NumberFormatter::forSkeleton(skeleton, errorCode);
795 if (errorCode.errIfFailureAndReset("PluralRules::getPluralKeyword(<PluralRules>, <locale>, %d, %s) failed", number, skeleton)) {
796 return nullptr;
797 }
798 LocalizedNumberFormatter formatter = ulnf.locale(locale);
799
800 const FormattedNumber fn = formatter.formatDouble(number, errorCode);
801 if (errorCode.errIfFailureAndReset("NumberFormatter::formatDouble(%d) failed", number)) {
802 return nullptr;
803 }
804
805 UnicodeString pluralKeyword = rules->select(fn, errorCode);
806 if (errorCode.errIfFailureAndReset("PluralRules->select(FormattedNumber of %d) failed", number)) {
807 return nullptr;
808 }
809 return pluralKeyword;
810 }
811
testOrdinal()812 void PluralRulesTest::testOrdinal() {
813 IcuTestErrorCode errorCode(*this, "testOrdinal");
814 LocalPointer<PluralRules> pr(PluralRules::forLocale("en", UPLURAL_TYPE_ORDINAL, errorCode));
815 if (errorCode.errIfFailureAndReset("PluralRules::forLocale(en, UPLURAL_TYPE_ORDINAL) failed")) {
816 return;
817 }
818 UnicodeString keyword = pr->select(2.);
819 if (keyword != UNICODE_STRING("two", 3)) {
820 dataerrln("PluralRules(en-ordinal).select(2) failed");
821 }
822 }
823
824
825 static const char * END_MARK = "999.999"; // Mark end of varargs data.
826
checkSelect(const LocalPointer<PluralRules> & rules,UErrorCode & status,int32_t line,const char * keyword,...)827 void PluralRulesTest::checkSelect(const LocalPointer<PluralRules> &rules, UErrorCode &status,
828 int32_t line, const char *keyword, ...) {
829 // The varargs parameters are a const char* strings, each being a decimal number.
830 // The formatting of the numbers as strings is significant, e.g.
831 // the difference between "2" and "2.0" can affect which rule matches (which keyword is selected).
832 // Note: rules parameter is a LocalPointer reference rather than a PluralRules * to avoid having
833 // to write getAlias() at every (numerous) call site.
834
835 if (U_FAILURE(status)) {
836 errln("file %s, line %d, ICU error status: %s.", __FILE__, line, u_errorName(status));
837 status = U_ZERO_ERROR;
838 return;
839 }
840
841 if (rules == NULL) {
842 errln("file %s, line %d: rules pointer is NULL", __FILE__, line);
843 return;
844 }
845
846 va_list ap;
847 va_start(ap, keyword);
848 for (;;) {
849 const char *num = va_arg(ap, const char *);
850 if (strcmp(num, END_MARK) == 0) {
851 break;
852 }
853
854 // DigitList is a convenient way to parse the decimal number string and get a double.
855 DecimalQuantity dl;
856 dl.setToDecNumber(StringPiece(num), status);
857 if (U_FAILURE(status)) {
858 errln("file %s, line %d, ICU error status: %s.", __FILE__, line, u_errorName(status));
859 status = U_ZERO_ERROR;
860 continue;
861 }
862 double numDbl = dl.toDouble();
863 const char *decimalPoint = strchr(num, '.');
864 int fractionDigitCount = decimalPoint == NULL ? 0 : static_cast<int>((num + strlen(num) - 1) - decimalPoint);
865 int fractionDigits = fractionDigitCount == 0 ? 0 : atoi(decimalPoint + 1);
866 FixedDecimal ni(numDbl, fractionDigitCount, fractionDigits);
867
868 UnicodeString actualKeyword = rules->select(ni);
869 if (actualKeyword != UnicodeString(keyword)) {
870 errln("file %s, line %d, select(%s) returned incorrect keyword. Expected %s, got %s",
871 __FILE__, line, num, keyword, US(actualKeyword).cstr());
872 }
873 }
874 va_end(ap);
875 }
876
testSelect()877 void PluralRulesTest::testSelect() {
878 UErrorCode status = U_ZERO_ERROR;
879 LocalPointer<PluralRules> pr(PluralRules::createRules("s: n in 1,3,4,6", status));
880 checkSelect(pr, status, __LINE__, "s", "1.0", "3.0", "4.0", "6.0", END_MARK);
881 checkSelect(pr, status, __LINE__, "other", "0.0", "2.0", "3.1", "7.0", END_MARK);
882
883 pr.adoptInstead(PluralRules::createRules("s: n not in 1,3,4,6", status));
884 checkSelect(pr, status, __LINE__, "other", "1.0", "3.0", "4.0", "6.0", END_MARK);
885 checkSelect(pr, status, __LINE__, "s", "0.0", "2.0", "3.1", "7.0", END_MARK);
886
887 pr.adoptInstead(PluralRules::createRules("r: n in 1..4, 7..10, 14 .. 17;"
888 "s: n is 29;", status));
889 checkSelect(pr, status, __LINE__, "r", "1.0", "3.0", "7.0", "8.0", "10.0", "14.0", "17.0", END_MARK);
890 checkSelect(pr, status, __LINE__, "s", "29.0", END_MARK);
891 checkSelect(pr, status, __LINE__, "other", "28.0", "29.1", END_MARK);
892
893 pr.adoptInstead(PluralRules::createRules("a: n mod 10 is 1; b: n mod 100 is 0 ", status));
894 checkSelect(pr, status, __LINE__, "a", "1", "11", "41", "101", "301.00", END_MARK);
895 checkSelect(pr, status, __LINE__, "b", "0", "100", "200.0", "300.", "1000", "1100", "110000", END_MARK);
896 checkSelect(pr, status, __LINE__, "other", "0.01", "1.01", "0.99", "2", "3", "99", "102", END_MARK);
897
898 // Rules that end with or without a ';' and with or without trailing spaces.
899 // (There was a rule parser bug here with these.)
900 pr.adoptInstead(PluralRules::createRules("a: n is 1", status));
901 checkSelect(pr, status, __LINE__, "a", "1", END_MARK);
902 checkSelect(pr, status, __LINE__, "other", "2", END_MARK);
903
904 pr.adoptInstead(PluralRules::createRules("a: n is 1 ", status));
905 checkSelect(pr, status, __LINE__, "a", "1", END_MARK);
906 checkSelect(pr, status, __LINE__, "other", "2", END_MARK);
907
908 pr.adoptInstead(PluralRules::createRules("a: n is 1;", status));
909 checkSelect(pr, status, __LINE__, "a", "1", END_MARK);
910 checkSelect(pr, status, __LINE__, "other", "2", END_MARK);
911
912 pr.adoptInstead(PluralRules::createRules("a: n is 1 ; ", status));
913 checkSelect(pr, status, __LINE__, "a", "1", END_MARK);
914 checkSelect(pr, status, __LINE__, "other", "2", END_MARK);
915
916 // First match when rules for different keywords are not disjoint.
917 // Also try spacing variations around ':' and '..'
918 pr.adoptInstead(PluralRules::createRules("c: n in 5..15; b : n in 1..10 ;a:n in 10 .. 20", status));
919 checkSelect(pr, status, __LINE__, "a", "20", END_MARK);
920 checkSelect(pr, status, __LINE__, "b", "1", END_MARK);
921 checkSelect(pr, status, __LINE__, "c", "10", END_MARK);
922 checkSelect(pr, status, __LINE__, "other", "0", "21", "10.1", END_MARK);
923
924 // in vs within
925 pr.adoptInstead(PluralRules::createRules("a: n in 2..10; b: n within 8..15", status));
926 checkSelect(pr, status, __LINE__, "a", "2", "8", "10", END_MARK);
927 checkSelect(pr, status, __LINE__, "b", "8.01", "9.5", "11", "14.99", "15", END_MARK);
928 checkSelect(pr, status, __LINE__, "other", "1", "7.7", "15.01", "16", END_MARK);
929
930 // OR and AND chains.
931 pr.adoptInstead(PluralRules::createRules("a: n in 2..10 and n in 4..12 and n not in 5..7", status));
932 checkSelect(pr, status, __LINE__, "a", "4", "8", "9", "10", END_MARK);
933 checkSelect(pr, status, __LINE__, "other", "2", "3", "5", "7", "11", END_MARK);
934 pr.adoptInstead(PluralRules::createRules("a: n is 2 or n is 5 or n in 7..11 and n in 11..13", status));
935 checkSelect(pr, status, __LINE__, "a", "2", "5", "11", END_MARK);
936 checkSelect(pr, status, __LINE__, "other", "3", "4", "6", "8", "10", "12", "13", END_MARK);
937
938 // Number attributes -
939 // n: the number itself
940 // i: integer digits
941 // f: visible fraction digits
942 // t: f with trailing zeros removed.
943 // v: number of visible fraction digits
944 // j: = n if there are no visible fraction digits
945 // != anything if there are visible fraction digits
946
947 pr.adoptInstead(PluralRules::createRules("a: i is 123", status));
948 checkSelect(pr, status, __LINE__, "a", "123", "123.0", "123.1", "0123.99", END_MARK);
949 checkSelect(pr, status, __LINE__, "other", "124", "122.0", END_MARK);
950
951 pr.adoptInstead(PluralRules::createRules("a: f is 120", status));
952 checkSelect(pr, status, __LINE__, "a", "1.120", "0.120", "11123.120", "0123.120", END_MARK);
953 checkSelect(pr, status, __LINE__, "other", "1.121", "122.1200", "1.12", "120", END_MARK);
954
955 pr.adoptInstead(PluralRules::createRules("a: t is 12", status));
956 checkSelect(pr, status, __LINE__, "a", "1.120", "0.12", "11123.12000", "0123.1200000", END_MARK);
957 checkSelect(pr, status, __LINE__, "other", "1.121", "122.1200001", "1.11", "12", END_MARK);
958
959 pr.adoptInstead(PluralRules::createRules("a: v is 3", status));
960 checkSelect(pr, status, __LINE__, "a", "1.120", "0.000", "11123.100", "0123.124", ".666", END_MARK);
961 checkSelect(pr, status, __LINE__, "other", "1.1212", "122.12", "1.1", "122", "0.0000", END_MARK);
962
963 pr.adoptInstead(PluralRules::createRules("a: v is 0 and i is 123", status));
964 checkSelect(pr, status, __LINE__, "a", "123", "123.", END_MARK);
965 checkSelect(pr, status, __LINE__, "other", "123.0", "123.1", "123.123", "0.123", END_MARK);
966
967 // The reserved words from the rule syntax will also function as keywords.
968 pr.adoptInstead(PluralRules::createRules("a: n is 21; n: n is 22; i: n is 23; f: n is 24;"
969 "t: n is 25; v: n is 26; w: n is 27; j: n is 28"
970 , status));
971 checkSelect(pr, status, __LINE__, "other", "20", "29", END_MARK);
972 checkSelect(pr, status, __LINE__, "a", "21", END_MARK);
973 checkSelect(pr, status, __LINE__, "n", "22", END_MARK);
974 checkSelect(pr, status, __LINE__, "i", "23", END_MARK);
975 checkSelect(pr, status, __LINE__, "f", "24", END_MARK);
976 checkSelect(pr, status, __LINE__, "t", "25", END_MARK);
977 checkSelect(pr, status, __LINE__, "v", "26", END_MARK);
978 checkSelect(pr, status, __LINE__, "w", "27", END_MARK);
979 checkSelect(pr, status, __LINE__, "j", "28", END_MARK);
980
981
982 pr.adoptInstead(PluralRules::createRules("not: n=31; and: n=32; or: n=33; mod: n=34;"
983 "in: n=35; within: n=36;is:n=37"
984 , status));
985 checkSelect(pr, status, __LINE__, "other", "30", "39", END_MARK);
986 checkSelect(pr, status, __LINE__, "not", "31", END_MARK);
987 checkSelect(pr, status, __LINE__, "and", "32", END_MARK);
988 checkSelect(pr, status, __LINE__, "or", "33", END_MARK);
989 checkSelect(pr, status, __LINE__, "mod", "34", END_MARK);
990 checkSelect(pr, status, __LINE__, "in", "35", END_MARK);
991 checkSelect(pr, status, __LINE__, "within", "36", END_MARK);
992 checkSelect(pr, status, __LINE__, "is", "37", END_MARK);
993
994 // Test cases from ICU4J PluralRulesTest.parseTestData
995
996 pr.adoptInstead(PluralRules::createRules("a: n is 1", status));
997 checkSelect(pr, status, __LINE__, "a", "1", END_MARK);
998 pr.adoptInstead(PluralRules::createRules("a: n mod 10 is 2", status));
999 checkSelect(pr, status, __LINE__, "a", "2", "12", "22", END_MARK);
1000 pr.adoptInstead(PluralRules::createRules("a: n is not 1", status));
1001 checkSelect(pr, status, __LINE__, "a", "0", "2", "3", "4", "5", END_MARK);
1002 pr.adoptInstead(PluralRules::createRules("a: n mod 3 is not 1", status));
1003 checkSelect(pr, status, __LINE__, "a", "0", "2", "3", "5", "6", "8", "9", END_MARK);
1004 pr.adoptInstead(PluralRules::createRules("a: n in 2..5", status));
1005 checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", END_MARK);
1006 pr.adoptInstead(PluralRules::createRules("a: n within 2..5", status));
1007 checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", END_MARK);
1008 pr.adoptInstead(PluralRules::createRules("a: n not in 2..5", status));
1009 checkSelect(pr, status, __LINE__, "a", "0", "1", "6", "7", "8", END_MARK);
1010 pr.adoptInstead(PluralRules::createRules("a: n not within 2..5", status));
1011 checkSelect(pr, status, __LINE__, "a", "0", "1", "6", "7", "8", END_MARK);
1012 pr.adoptInstead(PluralRules::createRules("a: n mod 10 in 2..5", status));
1013 checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", "12", "13", "14", "15", "22", "23", "24", "25", END_MARK);
1014 pr.adoptInstead(PluralRules::createRules("a: n mod 10 within 2..5", status));
1015 checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", "12", "13", "14", "15", "22", "23", "24", "25", END_MARK);
1016 pr.adoptInstead(PluralRules::createRules("a: n mod 10 is 2 and n is not 12", status));
1017 checkSelect(pr, status, __LINE__, "a", "2", "22", "32", "42", END_MARK);
1018 pr.adoptInstead(PluralRules::createRules("a: n mod 10 in 2..3 or n mod 10 is 5", status));
1019 checkSelect(pr, status, __LINE__, "a", "2", "3", "5", "12", "13", "15", "22", "23", "25", END_MARK);
1020 pr.adoptInstead(PluralRules::createRules("a: n mod 10 within 2..3 or n mod 10 is 5", status));
1021 checkSelect(pr, status, __LINE__, "a", "2", "3", "5", "12", "13", "15", "22", "23", "25", END_MARK);
1022 pr.adoptInstead(PluralRules::createRules("a: n is 1 or n is 4 or n is 23", status));
1023 checkSelect(pr, status, __LINE__, "a", "1", "4", "23", END_MARK);
1024 pr.adoptInstead(PluralRules::createRules("a: n mod 2 is 1 and n is not 3 and n in 1..11", status));
1025 checkSelect(pr, status, __LINE__, "a", "1", "5", "7", "9", "11", END_MARK);
1026 pr.adoptInstead(PluralRules::createRules("a: n mod 2 is 1 and n is not 3 and n within 1..11", status));
1027 checkSelect(pr, status, __LINE__, "a", "1", "5", "7", "9", "11", END_MARK);
1028 pr.adoptInstead(PluralRules::createRules("a: n mod 2 is 1 or n mod 5 is 1 and n is not 6", status));
1029 checkSelect(pr, status, __LINE__, "a", "1", "3", "5", "7", "9", "11", "13", "15", "16", END_MARK);
1030 pr.adoptInstead(PluralRules::createRules("a: n in 2..5; b: n in 5..8; c: n mod 2 is 1", status));
1031 checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", END_MARK);
1032 checkSelect(pr, status, __LINE__, "b", "6", "7", "8", END_MARK);
1033 checkSelect(pr, status, __LINE__, "c", "1", "9", "11", END_MARK);
1034 pr.adoptInstead(PluralRules::createRules("a: n within 2..5; b: n within 5..8; c: n mod 2 is 1", status));
1035 checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", END_MARK);
1036 checkSelect(pr, status, __LINE__, "b", "6", "7", "8", END_MARK);
1037 checkSelect(pr, status, __LINE__, "c", "1", "9", "11", END_MARK);
1038 pr.adoptInstead(PluralRules::createRules("a: n in 2, 4..6; b: n within 7..9,11..12,20", status));
1039 checkSelect(pr, status, __LINE__, "a", "2", "4", "5", "6", END_MARK);
1040 checkSelect(pr, status, __LINE__, "b", "7", "8", "9", "11", "12", "20", END_MARK);
1041 pr.adoptInstead(PluralRules::createRules("a: n in 2..8, 12 and n not in 4..6", status));
1042 checkSelect(pr, status, __LINE__, "a", "2", "3", "7", "8", "12", END_MARK);
1043 pr.adoptInstead(PluralRules::createRules("a: n mod 10 in 2, 3,5..7 and n is not 12", status));
1044 checkSelect(pr, status, __LINE__, "a", "2", "3", "5", "6", "7", "13", "15", "16", "17", END_MARK);
1045 pr.adoptInstead(PluralRules::createRules("a: n in 2..6, 3..7", status));
1046 checkSelect(pr, status, __LINE__, "a", "2", "3", "4", "5", "6", "7", END_MARK);
1047
1048 // Extended Syntax, with '=', '!=' and '%' operators.
1049 pr.adoptInstead(PluralRules::createRules("a: n = 1..8 and n!= 2,3,4,5", status));
1050 checkSelect(pr, status, __LINE__, "a", "1", "6", "7", "8", END_MARK);
1051 checkSelect(pr, status, __LINE__, "other", "0", "2", "3", "4", "5", "9", END_MARK);
1052 pr.adoptInstead(PluralRules::createRules("a:n % 10 != 1", status));
1053 checkSelect(pr, status, __LINE__, "a", "2", "6", "7", "8", END_MARK);
1054 checkSelect(pr, status, __LINE__, "other", "1", "21", "211", "91", END_MARK);
1055 }
1056
1057
testSelectRange()1058 void PluralRulesTest::testSelectRange() {
1059 IcuTestErrorCode status(*this, "testSelectRange");
1060
1061 int32_t d1 = 102;
1062 int32_t d2 = 201;
1063 Locale locale("sl");
1064
1065 // Locale sl has interesting data: one + two => few
1066 auto range = NumberRangeFormatter::withLocale(locale).formatFormattableRange(d1, d2, status);
1067 auto rules = LocalPointer<PluralRules>(PluralRules::forLocale(locale, status), status);
1068 if (status.errIfFailureAndReset()) {
1069 return;
1070 }
1071
1072 // For testing: get plural form of first and second numbers
1073 auto a = NumberFormatter::withLocale(locale).formatDouble(d1, status);
1074 auto b = NumberFormatter::withLocale(locale).formatDouble(d2, status);
1075 assertEquals("First plural", u"two", rules->select(a, status));
1076 assertEquals("Second plural", u"one", rules->select(b, status));
1077
1078 // Check the range plural now:
1079 auto form = rules->select(range, status);
1080 assertEquals("Range plural", u"few", form);
1081
1082 // Test after copying:
1083 PluralRules copy(*rules);
1084 form = copy.select(range, status);
1085 assertEquals("Range plural after copying", u"few", form);
1086
1087 // Test when plural ranges data is unavailable:
1088 auto bare = LocalPointer<PluralRules>(
1089 PluralRules::createRules(u"a: i = 0,1", status), status);
1090 if (status.errIfFailureAndReset()) { return; }
1091 form = bare->select(range, status);
1092 status.expectErrorAndReset(U_UNSUPPORTED_ERROR);
1093
1094 // However, they should not set an error when no data is available for a language.
1095 auto xyz = LocalPointer<PluralRules>(
1096 PluralRules::forLocale("xyz", status));
1097 form = xyz->select(range, status);
1098 assertEquals("Fallback form", u"other", form);
1099 }
1100
1101
testAvailbleLocales()1102 void PluralRulesTest::testAvailbleLocales() {
1103
1104 // Hash set of (char *) strings.
1105 UErrorCode status = U_ZERO_ERROR;
1106 UHashtable *localeSet = uhash_open(uhash_hashUnicodeString, uhash_compareUnicodeString, uhash_compareLong, &status);
1107 uhash_setKeyDeleter(localeSet, uprv_deleteUObject);
1108 if (U_FAILURE(status)) {
1109 errln("file %s, line %d: Error status = %s", __FILE__, __LINE__, u_errorName(status));
1110 return;
1111 }
1112
1113 // Check that each locale returned by the iterator is unique.
1114 StringEnumeration *localesEnum = PluralRules::getAvailableLocales(status);
1115 int localeCount = 0;
1116 for (;;) {
1117 const char *locale = localesEnum->next(NULL, status);
1118 if (U_FAILURE(status)) {
1119 dataerrln("file %s, line %d: Error status = %s", __FILE__, __LINE__, u_errorName(status));
1120 return;
1121 }
1122 if (locale == NULL) {
1123 break;
1124 }
1125 localeCount++;
1126 int32_t oldVal = uhash_puti(localeSet, new UnicodeString(locale), 1, &status);
1127 if (oldVal != 0) {
1128 errln("file %s, line %d: locale %s was seen before.", __FILE__, __LINE__, locale);
1129 }
1130 }
1131
1132 // Reset the iterator, verify that we get the same count.
1133 localesEnum->reset(status);
1134 int32_t localeCount2 = 0;
1135 while (localesEnum->next(NULL, status) != NULL) {
1136 if (U_FAILURE(status)) {
1137 errln("file %s, line %d: Error status = %s", __FILE__, __LINE__, u_errorName(status));
1138 break;
1139 }
1140 localeCount2++;
1141 }
1142 if (localeCount != localeCount2) {
1143 errln("file %s, line %d: locale counts differ. They are (%d, %d)",
1144 __FILE__, __LINE__, localeCount, localeCount2);
1145 }
1146
1147 // Instantiate plural rules for each available locale.
1148 localesEnum->reset(status);
1149 for (;;) {
1150 status = U_ZERO_ERROR;
1151 const char *localeName = localesEnum->next(NULL, status);
1152 if (U_FAILURE(status)) {
1153 errln("file %s, line %d: Error status = %s, locale = %s",
1154 __FILE__, __LINE__, u_errorName(status), localeName);
1155 return;
1156 }
1157 if (localeName == NULL) {
1158 break;
1159 }
1160 Locale locale = Locale::createFromName(localeName);
1161 PluralRules *pr = PluralRules::forLocale(locale, status);
1162 if (U_FAILURE(status)) {
1163 errln("file %s, line %d: Error %s creating plural rules for locale %s",
1164 __FILE__, __LINE__, u_errorName(status), localeName);
1165 continue;
1166 }
1167 if (pr == NULL) {
1168 errln("file %s, line %d: Null plural rules for locale %s", __FILE__, __LINE__, localeName);
1169 continue;
1170 }
1171
1172 // Pump some numbers through the plural rules. Can't check for correct results,
1173 // mostly this to tickle any asserts or crashes that may be lurking.
1174 for (double n=0; n<120.0; n+=0.5) {
1175 UnicodeString keyword = pr->select(n);
1176 if (keyword.length() == 0) {
1177 errln("file %s, line %d, empty keyword for n = %g, locale %s",
1178 __FILE__, __LINE__, n, localeName);
1179 }
1180 }
1181 delete pr;
1182 }
1183
1184 uhash_close(localeSet);
1185 delete localesEnum;
1186
1187 }
1188
1189
testParseErrors()1190 void PluralRulesTest::testParseErrors() {
1191 // Test rules with syntax errors.
1192 // Creation of PluralRules from them should fail.
1193
1194 static const char *testCases[] = {
1195 "a: n mod 10, is 1",
1196 "a: q is 13",
1197 "a n is 13",
1198 "a: n is 13,",
1199 "a: n is 13, 15, b: n is 4",
1200 "a: n is 1, 3, 4.. ",
1201 "a: n within 5..4",
1202 "A: n is 13", // Uppercase keywords not allowed.
1203 "a: n ! = 3", // spaces in != operator
1204 "a: n = not 3", // '=' not exact equivalent of 'is'
1205 "a: n ! in 3..4" // '!' not exact equivalent of 'not'
1206 "a: n % 37 ! in 3..4"
1207
1208 };
1209 for (int i=0; i<UPRV_LENGTHOF(testCases); i++) {
1210 const char *rules = testCases[i];
1211 UErrorCode status = U_ZERO_ERROR;
1212 PluralRules *pr = PluralRules::createRules(UnicodeString(rules), status);
1213 if (U_SUCCESS(status)) {
1214 errln("file %s, line %d, expected failure with \"%s\".", __FILE__, __LINE__, rules);
1215 }
1216 if (pr != NULL) {
1217 errln("file %s, line %d, expected NULL. Rules: \"%s\"", __FILE__, __LINE__, rules);
1218 }
1219 }
1220 return;
1221 }
1222
1223
testFixedDecimal()1224 void PluralRulesTest::testFixedDecimal() {
1225 struct DoubleTestCase {
1226 double n;
1227 int32_t fractionDigitCount;
1228 int64_t fractionDigits;
1229 };
1230
1231 // Check that the internal functions for extracting the decimal fraction digits from
1232 // a double value are working.
1233 static DoubleTestCase testCases[] = {
1234 {1.0, 0, 0},
1235 {123456.0, 0, 0},
1236 {1.1, 1, 1},
1237 {1.23, 2, 23},
1238 {1.234, 3, 234},
1239 {1.2345, 4, 2345},
1240 {1.23456, 5, 23456},
1241 {.1234, 4, 1234},
1242 {.01234, 5, 1234},
1243 {.001234, 6, 1234},
1244 {.0001234, 7, 1234},
1245 {100.1234, 4, 1234},
1246 {100.01234, 5, 1234},
1247 {100.001234, 6, 1234},
1248 {100.0001234, 7, 1234}
1249 };
1250
1251 for (int i=0; i<UPRV_LENGTHOF(testCases); ++i) {
1252 DoubleTestCase &tc = testCases[i];
1253 int32_t numFractionDigits = FixedDecimal::decimals(tc.n);
1254 if (numFractionDigits != tc.fractionDigitCount) {
1255 errln("file %s, line %d: decimals(%g) expected %d, actual %d",
1256 __FILE__, __LINE__, tc.n, tc.fractionDigitCount, numFractionDigits);
1257 continue;
1258 }
1259 int64_t actualFractionDigits = FixedDecimal::getFractionalDigits(tc.n, numFractionDigits);
1260 if (actualFractionDigits != tc.fractionDigits) {
1261 errln("file %s, line %d: getFractionDigits(%g, %d): expected %ld, got %ld",
1262 __FILE__, __LINE__, tc.n, numFractionDigits, tc.fractionDigits, actualFractionDigits);
1263 }
1264 }
1265 }
1266
1267
testSelectTrailingZeros()1268 void PluralRulesTest::testSelectTrailingZeros() {
1269 IcuTestErrorCode status(*this, "testSelectTrailingZeros");
1270 number::UnlocalizedNumberFormatter unf = number::NumberFormatter::with()
1271 .precision(number::Precision::fixedFraction(2));
1272 struct TestCase {
1273 const char* localeName;
1274 const char16_t* expectedDoubleKeyword;
1275 const char16_t* expectedFormattedKeyword;
1276 double number;
1277 } cases[] = {
1278 {"bs", u"few", u"other", 5.2}, // 5.2 => two, but 5.20 => other
1279 {"si", u"one", u"one", 0.0},
1280 {"si", u"one", u"one", 1.0},
1281 {"si", u"one", u"other", 0.1}, // 0.1 => one, but 0.10 => other
1282 {"si", u"one", u"one", 0.01}, // 0.01 => one
1283 {"hsb", u"few", u"few", 1.03}, // (f % 100 == 3) => few
1284 {"hsb", u"few", u"other", 1.3}, // 1.3 => few, but 1.30 => other
1285 };
1286 for (const auto& cas : cases) {
1287 UnicodeString message(UnicodeString(cas.localeName) + u" " + DoubleToUnicodeString(cas.number));
1288 status.setScope(message);
1289 Locale locale(cas.localeName);
1290 LocalPointer<PluralRules> rules(PluralRules::forLocale(locale, status));
1291 if (U_FAILURE(status)) {
1292 dataerrln("Failed to create PluralRules by PluralRules::forLocale(%s): %s\n",
1293 cas.localeName, u_errorName(status));
1294 return;
1295 }
1296 assertEquals(message, cas.expectedDoubleKeyword, rules->select(cas.number));
1297 number::FormattedNumber fn = unf.locale(locale).formatDouble(cas.number, status);
1298 assertEquals(message, cas.expectedFormattedKeyword, rules->select(fn, status));
1299 status.errIfFailureAndReset();
1300 }
1301 }
1302
compareLocaleResults(const char * loc1,const char * loc2,const char * loc3)1303 void PluralRulesTest::compareLocaleResults(const char* loc1, const char* loc2, const char* loc3) {
1304 UErrorCode status = U_ZERO_ERROR;
1305 LocalPointer<PluralRules> rules1(PluralRules::forLocale(loc1, status));
1306 LocalPointer<PluralRules> rules2(PluralRules::forLocale(loc2, status));
1307 LocalPointer<PluralRules> rules3(PluralRules::forLocale(loc3, status));
1308 if (U_FAILURE(status)) {
1309 dataerrln("Failed to create PluralRules for one of %s, %s, %s: %s\n", loc1, loc2, loc3, u_errorName(status));
1310 return;
1311 }
1312 for (int32_t value = 0; value <= 12; value++) {
1313 UnicodeString result1 = rules1->select(value);
1314 UnicodeString result2 = rules2->select(value);
1315 UnicodeString result3 = rules3->select(value);
1316 if (result1 != result2 || result1 != result3) {
1317 errln("PluralRules.select(%d) does not return the same values for %s, %s, %s\n", value, loc1, loc2, loc3);
1318 }
1319 }
1320 }
1321
testLocaleExtension()1322 void PluralRulesTest::testLocaleExtension() {
1323 IcuTestErrorCode errorCode(*this, "testLocaleExtension");
1324 LocalPointer<PluralRules> rules(PluralRules::forLocale("pt@calendar=gregorian", errorCode));
1325 if (errorCode.errIfFailureAndReset("PluralRules::forLocale()")) { return; }
1326 UnicodeString key = rules->select(1);
1327 assertEquals("pt@calendar=gregorian select(1)", u"one", key);
1328 compareLocaleResults("ar", "ar_SA", "ar_SA@calendar=gregorian");
1329 compareLocaleResults("ru", "ru_UA", "ru-u-cu-RUB");
1330 compareLocaleResults("fr", "fr_CH", "fr@ms=uksystem");
1331 }
1332
1333 #endif /* #if !UCONFIG_NO_FORMATTING */
1334