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