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