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