1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
|
// © 2019 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
// localematchertest.cpp
// created: 2019jul04 Markus W. Scherer
#include <string>
#include <vector>
#include <utility>
#include "unicode/utypes.h"
#include "unicode/localematcher.h"
#include "unicode/locid.h"
#include "charstr.h"
#include "cmemory.h"
#include "intltest.h"
#include "localeprioritylist.h"
#include "ucbuf.h"
#define ARRAY_RANGE(array) (array), ((array) + UPRV_LENGTHOF(array))
namespace {
const char *locString(const Locale *loc) {
return loc != nullptr ? loc->getName() : "(null)";
}
struct TestCase {
int32_t lineNr = 0;
CharString supported;
CharString def;
UnicodeString favor;
UnicodeString threshold;
CharString desired;
CharString expMatch;
CharString expDesired;
CharString expCombined;
void reset() {
supported.clear();
def.clear();
favor.remove();
threshold.remove();
}
};
} // namespace
class LocaleMatcherTest : public IntlTest {
public:
LocaleMatcherTest() {}
void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=nullptr) override;
void testEmpty();
void testCopyErrorTo();
void testBasics();
void testSupportedDefault();
void testUnsupportedDefault();
void testNoDefault();
void testDemotion();
void testDirection();
void testMaxDistanceAndIsMatch();
void testMatch();
void testResolvedLocale();
void testDataDriven();
private:
UBool dataDriven(const TestCase &test, IcuTestErrorCode &errorCode);
};
extern IntlTest *createLocaleMatcherTest() {
return new LocaleMatcherTest();
}
void LocaleMatcherTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) {
if(exec) {
logln("TestSuite LocaleMatcherTest: ");
}
TESTCASE_AUTO_BEGIN;
TESTCASE_AUTO(testEmpty);
TESTCASE_AUTO(testCopyErrorTo);
TESTCASE_AUTO(testBasics);
TESTCASE_AUTO(testSupportedDefault);
TESTCASE_AUTO(testUnsupportedDefault);
TESTCASE_AUTO(testNoDefault);
TESTCASE_AUTO(testDemotion);
TESTCASE_AUTO(testDirection);
TESTCASE_AUTO(testMaxDistanceAndIsMatch);
TESTCASE_AUTO(testMatch);
TESTCASE_AUTO(testResolvedLocale);
TESTCASE_AUTO(testDataDriven);
TESTCASE_AUTO_END;
}
void LocaleMatcherTest::testEmpty() {
IcuTestErrorCode errorCode(*this, "testEmpty");
LocaleMatcher matcher = LocaleMatcher::Builder().build(errorCode);
const Locale *best = matcher.getBestMatch(Locale::getFrench(), errorCode);
assertEquals("getBestMatch(fr)", "(null)", locString(best));
LocaleMatcher::Result result = matcher.getBestMatchResult("fr", errorCode);
assertEquals("getBestMatchResult(fr).des", "(null)", locString(result.getDesiredLocale()));
assertEquals("getBestMatchResult(fr).desIndex", -1, result.getDesiredIndex());
assertEquals("getBestMatchResult(fr).supp",
"(null)", locString(result.getSupportedLocale()));
assertEquals("getBestMatchResult(fr).suppIndex",
-1, result.getSupportedIndex());
}
void LocaleMatcherTest::testCopyErrorTo() {
IcuTestErrorCode errorCode(*this, "testCopyErrorTo");
// The builder does not set any errors except out-of-memory.
// Test what we can.
LocaleMatcher::Builder builder;
UErrorCode success = U_ZERO_ERROR;
assertFalse("no error", builder.copyErrorTo(success));
assertTrue("still success", U_SUCCESS(success));
UErrorCode failure = U_INVALID_FORMAT_ERROR;
assertTrue("failure passed in", builder.copyErrorTo(failure));
assertEquals("same failure", U_INVALID_FORMAT_ERROR, failure);
}
void LocaleMatcherTest::testBasics() {
IcuTestErrorCode errorCode(*this, "testBasics");
Locale locales[] = { "fr", "en_GB", "en" };
{
LocaleMatcher matcher = LocaleMatcher::Builder().
setSupportedLocales(ARRAY_RANGE(locales)).build(errorCode);
const Locale *best = matcher.getBestMatch("en_GB", errorCode);
assertEquals("fromRange.getBestMatch(en_GB)", "en_GB", locString(best));
best = matcher.getBestMatch("en_US", errorCode);
assertEquals("fromRange.getBestMatch(en_US)", "en", locString(best));
best = matcher.getBestMatch("fr_FR", errorCode);
assertEquals("fromRange.getBestMatch(fr_FR)", "fr", locString(best));
best = matcher.getBestMatch("ja_JP", errorCode);
assertEquals("fromRange.getBestMatch(ja_JP)", "fr", locString(best));
}
// Code coverage: Variations of setting supported locales.
{
std::vector<Locale> locales{ "fr", "en_GB", "en" };
LocaleMatcher matcher = LocaleMatcher::Builder().
setSupportedLocales(locales.begin(), locales.end()).build(errorCode);
const Locale *best = matcher.getBestMatch("en_GB", errorCode);
assertEquals("fromRange.getBestMatch(en_GB)", "en_GB", locString(best));
best = matcher.getBestMatch("en_US", errorCode);
assertEquals("fromRange.getBestMatch(en_US)", "en", locString(best));
best = matcher.getBestMatch("fr_FR", errorCode);
assertEquals("fromRange.getBestMatch(fr_FR)", "fr", locString(best));
best = matcher.getBestMatch("ja_JP", errorCode);
assertEquals("fromRange.getBestMatch(ja_JP)", "fr", locString(best));
}
{
Locale::RangeIterator<Locale *> iter(ARRAY_RANGE(locales));
LocaleMatcher matcher = LocaleMatcher::Builder().
setSupportedLocales(iter).build(errorCode);
const Locale *best = matcher.getBestMatch("en_GB", errorCode);
assertEquals("fromIter.getBestMatch(en_GB)", "en_GB", locString(best));
best = matcher.getBestMatch("en_US", errorCode);
assertEquals("fromIter.getBestMatch(en_US)", "en", locString(best));
best = matcher.getBestMatch("fr_FR", errorCode);
assertEquals("fromIter.getBestMatch(fr_FR)", "fr", locString(best));
best = matcher.getBestMatch("ja_JP", errorCode);
assertEquals("fromIter.getBestMatch(ja_JP)", "fr", locString(best));
}
{
Locale *pointers[] = { locales, locales + 1, locales + 2 };
// Lambda with explicit reference return type to prevent copy-constructing a temporary
// which would be destructed right away.
LocaleMatcher matcher = LocaleMatcher::Builder().
setSupportedLocalesViaConverter(
ARRAY_RANGE(pointers), [](const Locale *p) -> const Locale & { return *p; }).
build(errorCode);
const Locale *best = matcher.getBestMatch("en_GB", errorCode);
assertEquals("viaConverter.getBestMatch(en_GB)", "en_GB", locString(best));
best = matcher.getBestMatch("en_US", errorCode);
assertEquals("viaConverter.getBestMatch(en_US)", "en", locString(best));
best = matcher.getBestMatch("fr_FR", errorCode);
assertEquals("viaConverter.getBestMatch(fr_FR)", "fr", locString(best));
best = matcher.getBestMatch("ja_JP", errorCode);
assertEquals("viaConverter.getBestMatch(ja_JP)", "fr", locString(best));
}
{
LocaleMatcher matcher = LocaleMatcher::Builder().
addSupportedLocale(locales[0]).
addSupportedLocale(locales[1]).
addSupportedLocale(locales[2]).
build(errorCode);
const Locale *best = matcher.getBestMatch("en_GB", errorCode);
assertEquals("added.getBestMatch(en_GB)", "en_GB", locString(best));
best = matcher.getBestMatch("en_US", errorCode);
assertEquals("added.getBestMatch(en_US)", "en", locString(best));
best = matcher.getBestMatch("fr_FR", errorCode);
assertEquals("added.getBestMatch(fr_FR)", "fr", locString(best));
best = matcher.getBestMatch("ja_JP", errorCode);
assertEquals("added.getBestMatch(ja_JP)", "fr", locString(best));
}
{
LocaleMatcher matcher = LocaleMatcher::Builder().
setSupportedLocalesFromListString(
" el, fr;q=0.555555, en-GB ; q = 0.88 , el; q =0, en;q=0.88 , fr ").
build(errorCode);
const Locale *best = matcher.getBestMatchForListString("el, fr, fr;q=0, en-GB", errorCode);
assertEquals("fromList.getBestMatch(en_GB)", "en_GB", locString(best));
best = matcher.getBestMatch("en_US", errorCode);
assertEquals("fromList.getBestMatch(en_US)", "en", locString(best));
best = matcher.getBestMatch("fr_FR", errorCode);
assertEquals("fromList.getBestMatch(fr_FR)", "fr", locString(best));
best = matcher.getBestMatch("ja_JP", errorCode);
assertEquals("fromList.getBestMatch(ja_JP)", "fr", locString(best));
}
// more API coverage
{
LocalePriorityList list("fr, en-GB", errorCode);
LocalePriorityList::Iterator iter(list.iterator());
LocaleMatcher matcher = LocaleMatcher::Builder().
setSupportedLocales(iter).
addSupportedLocale(Locale::getEnglish()).
setDefaultLocale(&Locale::getGerman()).
build(errorCode);
const Locale *best = matcher.getBestMatch("en_GB", errorCode);
assertEquals("withDefault.getBestMatch(en_GB)", "en_GB", locString(best));
best = matcher.getBestMatch("en_US", errorCode);
assertEquals("withDefault.getBestMatch(en_US)", "en", locString(best));
best = matcher.getBestMatch("fr_FR", errorCode);
assertEquals("withDefault.getBestMatch(fr_FR)", "fr", locString(best));
best = matcher.getBestMatch("ja_JP", errorCode);
assertEquals("withDefault.getBestMatch(ja_JP)", "de", locString(best));
Locale desired("en_GB"); // distinct object from Locale.UK
LocaleMatcher::Result result = matcher.getBestMatchResult(desired, errorCode);
assertTrue("withDefault: exactly desired en-GB object",
&desired == result.getDesiredLocale());
assertEquals("withDefault: en-GB desired index", 0, result.getDesiredIndex());
assertEquals("withDefault: en-GB supported",
"en_GB", locString(result.getSupportedLocale()));
assertEquals("withDefault: en-GB supported index", 1, result.getSupportedIndex());
LocalePriorityList list2("ja-JP, en-US", errorCode);
LocalePriorityList::Iterator iter2(list2.iterator());
result = matcher.getBestMatchResult(iter2, errorCode);
assertEquals("withDefault: ja-JP, en-US desired index", 1, result.getDesiredIndex());
assertEquals("withDefault: ja-JP, en-US desired",
"en_US", locString(result.getDesiredLocale()));
desired = Locale("en", "US"); // distinct object from Locale.US
result = matcher.getBestMatchResult(desired, errorCode);
assertTrue("withDefault: exactly desired en-US object",
&desired == result.getDesiredLocale());
assertEquals("withDefault: en-US desired index", 0, result.getDesiredIndex());
assertEquals("withDefault: en-US supported", "en", locString(result.getSupportedLocale()));
assertEquals("withDefault: en-US supported index", 2, result.getSupportedIndex());
result = matcher.getBestMatchResult("ja_JP", errorCode);
assertEquals("withDefault: ja-JP desired", "(null)", locString(result.getDesiredLocale()));
assertEquals("withDefault: ja-JP desired index", -1, result.getDesiredIndex());
assertEquals("withDefault: ja-JP supported", "de", locString(result.getSupportedLocale()));
assertEquals("withDefault: ja-JP supported index", -1, result.getSupportedIndex());
}
}
void LocaleMatcherTest::testSupportedDefault() {
// The default locale is one of the supported locales.
IcuTestErrorCode errorCode(*this, "testSupportedDefault");
Locale locales[] = { "fr", "en_GB", "en" };
LocaleMatcher matcher = LocaleMatcher::Builder().
setSupportedLocales(ARRAY_RANGE(locales)).
setDefaultLocale(&locales[1]).
build(errorCode);
const Locale *best = matcher.getBestMatch("en_GB", errorCode);
assertEquals("getBestMatch(en_GB)", "en_GB", locString(best));
best = matcher.getBestMatch("en_US", errorCode);
assertEquals("getBestMatch(en_US)", "en", locString(best));
best = matcher.getBestMatch("fr_FR", errorCode);
assertEquals("getBestMatch(fr_FR)", "fr", locString(best));
best = matcher.getBestMatch("ja_JP", errorCode);
assertEquals("getBestMatch(ja_JP)", "en_GB", locString(best));
LocaleMatcher::Result result = matcher.getBestMatchResult("ja_JP", errorCode);
assertEquals("getBestMatchResult(ja_JP).supp",
"en_GB", locString(result.getSupportedLocale()));
assertEquals("getBestMatchResult(ja_JP).suppIndex",
-1, result.getSupportedIndex());
}
void LocaleMatcherTest::testUnsupportedDefault() {
// The default locale does not match any of the supported locales.
IcuTestErrorCode errorCode(*this, "testUnsupportedDefault");
Locale locales[] = { "fr", "en_GB", "en" };
Locale def("de");
LocaleMatcher matcher = LocaleMatcher::Builder().
setSupportedLocales(ARRAY_RANGE(locales)).
setDefaultLocale(&def).
build(errorCode);
const Locale *best = matcher.getBestMatch("en_GB", errorCode);
assertEquals("getBestMatch(en_GB)", "en_GB", locString(best));
best = matcher.getBestMatch("en_US", errorCode);
assertEquals("getBestMatch(en_US)", "en", locString(best));
best = matcher.getBestMatch("fr_FR", errorCode);
assertEquals("getBestMatch(fr_FR)", "fr", locString(best));
best = matcher.getBestMatch("ja_JP", errorCode);
assertEquals("getBestMatch(ja_JP)", "de", locString(best));
LocaleMatcher::Result result = matcher.getBestMatchResult("ja_JP", errorCode);
assertEquals("getBestMatchResult(ja_JP).supp",
"de", locString(result.getSupportedLocale()));
assertEquals("getBestMatchResult(ja_JP).suppIndex",
-1, result.getSupportedIndex());
}
void LocaleMatcherTest::testNoDefault() {
// We want nullptr instead of any default locale.
IcuTestErrorCode errorCode(*this, "testNoDefault");
Locale locales[] = { "fr", "en_GB", "en" };
LocaleMatcher matcher = LocaleMatcher::Builder().
setSupportedLocales(ARRAY_RANGE(locales)).
setNoDefaultLocale().
build(errorCode);
const Locale *best = matcher.getBestMatch("en_GB", errorCode);
assertEquals("getBestMatch(en_GB)", "en_GB", locString(best));
best = matcher.getBestMatch("en_US", errorCode);
assertEquals("getBestMatch(en_US)", "en", locString(best));
best = matcher.getBestMatch("fr_FR", errorCode);
assertEquals("getBestMatch(fr_FR)", "fr", locString(best));
best = matcher.getBestMatch("ja_JP", errorCode);
assertEquals("getBestMatch(ja_JP)", "(null)", locString(best));
LocaleMatcher::Result result = matcher.getBestMatchResult("ja_JP", errorCode);
assertEquals("getBestMatchResult(ja_JP).supp",
"(null)", locString(result.getSupportedLocale()));
assertEquals("getBestMatchResult(ja_JP).suppIndex",
-1, result.getSupportedIndex());
}
void LocaleMatcherTest::testDemotion() {
IcuTestErrorCode errorCode(*this, "testDemotion");
Locale supported[] = { "fr", "de-CH", "it" };
Locale desired[] = { "fr-CH", "de-CH", "it" };
{
LocaleMatcher noDemotion = LocaleMatcher::Builder().
setSupportedLocales(ARRAY_RANGE(supported)).
setDemotionPerDesiredLocale(ULOCMATCH_DEMOTION_NONE).build(errorCode);
Locale::RangeIterator<Locale *> desiredIter(ARRAY_RANGE(desired));
assertEquals("no demotion",
"de_CH", locString(noDemotion.getBestMatch(desiredIter, errorCode)));
}
{
LocaleMatcher regionDemotion = LocaleMatcher::Builder().
setSupportedLocales(ARRAY_RANGE(supported)).
setDemotionPerDesiredLocale(ULOCMATCH_DEMOTION_REGION).build(errorCode);
Locale::RangeIterator<Locale *> desiredIter(ARRAY_RANGE(desired));
assertEquals("region demotion",
"fr", locString(regionDemotion.getBestMatch(desiredIter, errorCode)));
}
}
void LocaleMatcherTest::testDirection() {
IcuTestErrorCode errorCode(*this, "testDirection");
Locale supported[] = { "ar", "nn" };
Locale desired[] = { "arz-EG", "nb-DK" };
LocaleMatcher::Builder builder;
builder.setSupportedLocales(ARRAY_RANGE(supported));
{
// arz is a close one-way match to ar, and the region matches.
// (Egyptian Arabic vs. Arabic)
// Also explicitly exercise the move copy constructor.
LocaleMatcher built = builder.build(errorCode);
LocaleMatcher withOneWay(std::move(built));
Locale::RangeIterator<Locale *> desiredIter(ARRAY_RANGE(desired));
assertEquals("with one-way", "ar",
locString(withOneWay.getBestMatch(desiredIter, errorCode)));
}
{
// nb is a less close two-way match to nn, and the regions differ.
// (Norwegian Bokmal vs. Nynorsk)
// Also explicitly exercise the move assignment operator.
LocaleMatcher onlyTwoWay = builder.build(errorCode);
LocaleMatcher built =
builder.setDirection(ULOCMATCH_DIRECTION_ONLY_TWO_WAY).build(errorCode);
onlyTwoWay = std::move(built);
Locale::RangeIterator<Locale *> desiredIter(ARRAY_RANGE(desired));
assertEquals("only two-way", "nn",
locString(onlyTwoWay.getBestMatch(desiredIter, errorCode)));
}
}
void LocaleMatcherTest::testMaxDistanceAndIsMatch() {
IcuTestErrorCode errorCode(*this, "testMaxDistanceAndIsMatch");
LocaleMatcher::Builder builder;
LocaleMatcher standard = builder.build(errorCode);
Locale germanLux("de-LU");
Locale germanPhoenician("de-Phnx-AT");
Locale greek("el");
assertTrue("standard de-LU / de", standard.isMatch(germanLux, Locale::getGerman(), errorCode));
assertFalse("standard de-Phnx-AT / de",
standard.isMatch(germanPhoenician, Locale::getGerman(), errorCode));
// Allow a script difference to still match.
LocaleMatcher loose =
builder.setMaxDistance(germanPhoenician, Locale::getGerman()).build(errorCode);
assertTrue("loose de-LU / de", loose.isMatch(germanLux, Locale::getGerman(), errorCode));
assertTrue("loose de-Phnx-AT / de",
loose.isMatch(germanPhoenician, Locale::getGerman(), errorCode));
assertFalse("loose el / de", loose.isMatch(greek, Locale::getGerman(), errorCode));
// Allow at most a regional difference.
LocaleMatcher regional =
builder.setMaxDistance(Locale("de-AT"), Locale::getGerman()).build(errorCode);
assertTrue("regional de-LU / de",
regional.isMatch(Locale("de-LU"), Locale::getGerman(), errorCode));
assertFalse("regional da / no", regional.isMatch(Locale("da"), Locale("no"), errorCode));
assertFalse("regional zh-Hant / zh",
regional.isMatch(Locale::getChinese(), Locale::getTraditionalChinese(), errorCode));
}
void LocaleMatcherTest::testMatch() {
IcuTestErrorCode errorCode(*this, "testMatch");
LocaleMatcher matcher = LocaleMatcher::Builder().build(errorCode);
// Java test function testMatch_exact()
Locale en_CA("en_CA");
assertEquals("exact match", 1.0, matcher.internalMatch(en_CA, en_CA, errorCode));
// testMatch_none
Locale ar_MK("ar_MK");
double match = matcher.internalMatch(ar_MK, en_CA, errorCode);
assertTrue("mismatch: 0<=match<0.2", 0 <= match && match < 0.2);
// testMatch_matchOnMaximized
Locale und_TW("und_TW");
Locale zh("zh");
Locale zh_Hant("zh_Hant");
double matchZh = matcher.internalMatch(und_TW, zh, errorCode);
double matchZhHant = matcher.internalMatch(und_TW, zh_Hant, errorCode);
assertTrue("und_TW should be closer to zh_Hant than to zh",
matchZh < matchZhHant);
Locale en_Hant_TW("en_Hant_TW");
double matchEnHantTw = matcher.internalMatch(en_Hant_TW, zh_Hant, errorCode);
assertTrue("zh_Hant should be closer to und_TW than to en_Hant_TW",
matchEnHantTw < matchZhHant);
assertTrue("zh should not match und_TW or en_Hant_TW",
matchZh == 0.0 && matchEnHantTw == 0.0); // with changes in CLDR-1435
}
void LocaleMatcherTest::testResolvedLocale() {
IcuTestErrorCode errorCode(*this, "testResolvedLocale");
LocaleMatcher matcher = LocaleMatcher::Builder().
addSupportedLocale("ar-EG").
build(errorCode);
Locale desired("ar-SA-u-nu-latn");
LocaleMatcher::Result result = matcher.getBestMatchResult(desired, errorCode);
assertEquals("best", "ar_EG", locString(result.getSupportedLocale()));
Locale resolved = result.makeResolvedLocale(errorCode);
assertEquals("ar-EG + ar-SA-u-nu-latn = ar-SA-u-nu-latn",
"ar-SA-u-nu-latn",
resolved.toLanguageTag<std::string>(errorCode).data());
}
namespace {
bool toInvariant(const UnicodeString &s, CharString &inv, IcuTestErrorCode &errorCode) {
if (errorCode.isSuccess()) {
inv.clear().appendInvariantChars(s, errorCode);
return errorCode.isSuccess();
}
return false;
}
bool getSuffixAfterPrefix(const UnicodeString &s, int32_t limit,
const UnicodeString &prefix, UnicodeString &suffix) {
if (prefix.length() <= limit && s.startsWith(prefix)) {
suffix.setTo(s, prefix.length(), limit - prefix.length());
return true;
} else {
return false;
}
}
bool getInvariantSuffixAfterPrefix(const UnicodeString &s, int32_t limit,
const UnicodeString &prefix, CharString &suffix,
IcuTestErrorCode &errorCode) {
UnicodeString u_suffix;
return getSuffixAfterPrefix(s, limit, prefix, u_suffix) &&
toInvariant(u_suffix, suffix, errorCode);
}
bool readTestCase(const UnicodeString &line, TestCase &test, IcuTestErrorCode &errorCode) {
if (errorCode.isFailure()) { return false; }
++test.lineNr;
// Start of comment, or end of line, minus trailing spaces.
int32_t limit = line.indexOf(u'#');
if (limit < 0) {
limit = line.length();
// Remove trailing CR LF.
char16_t c;
while (limit > 0 && ((c = line.charAt(limit - 1)) == u'\n' || c == u'\r')) {
--limit;
}
}
// Remove spaces before comment or at the end of the line.
char16_t c;
while (limit > 0 && ((c = line.charAt(limit - 1)) == u' ' || c == u'\t')) {
--limit;
}
if (limit == 0) { // empty line
return false;
}
if (line.startsWith(u"** test: ")) {
test.reset();
} else if (getInvariantSuffixAfterPrefix(line, limit, u"@supported=",
test.supported, errorCode)) {
} else if (getInvariantSuffixAfterPrefix(line, limit, u"@default=",
test.def, errorCode)) {
} else if (getSuffixAfterPrefix(line, limit, u"@favor=", test.favor)) {
} else if (getSuffixAfterPrefix(line, limit, u"@threshold=", test.threshold)) {
} else {
int32_t matchSep = line.indexOf(u">>");
// >> before an inline comment, and followed by more than white space.
if (0 <= matchSep && (matchSep + 2) < limit) {
toInvariant(line.tempSubStringBetween(0, matchSep).trim(), test.desired, errorCode);
test.expDesired.clear();
test.expCombined.clear();
int32_t start = matchSep + 2;
int32_t expLimit = line.indexOf(u'|', start);
if (expLimit < 0) {
toInvariant(line.tempSubStringBetween(start, limit).trim(),
test.expMatch, errorCode);
} else {
toInvariant(line.tempSubStringBetween(start, expLimit).trim(),
test.expMatch, errorCode);
start = expLimit + 1;
expLimit = line.indexOf(u'|', start);
if (expLimit < 0) {
toInvariant(line.tempSubStringBetween(start, limit).trim(),
test.expDesired, errorCode);
} else {
toInvariant(line.tempSubStringBetween(start, expLimit).trim(),
test.expDesired, errorCode);
toInvariant(line.tempSubStringBetween(expLimit + 1, limit).trim(),
test.expCombined, errorCode);
}
}
return errorCode.isSuccess();
} else {
errorCode.set(U_INVALID_FORMAT_ERROR);
}
}
return false;
}
Locale *getLocaleOrNull(const CharString &s, Locale &locale) {
if (s == "null") {
return nullptr;
} else {
return &(locale = Locale(s.data()));
}
}
} // namespace
UBool LocaleMatcherTest::dataDriven(const TestCase &test, IcuTestErrorCode &errorCode) {
LocaleMatcher::Builder builder;
builder.setSupportedLocalesFromListString(test.supported.toStringPiece());
if (!test.def.isEmpty()) {
Locale defaultLocale(test.def.data());
builder.setDefaultLocale(&defaultLocale);
}
if (!test.favor.isEmpty()) {
ULocMatchFavorSubtag favor;
if (test.favor == u"normal") {
favor = ULOCMATCH_FAVOR_LANGUAGE;
} else if (test.favor == u"script") {
favor = ULOCMATCH_FAVOR_SCRIPT;
} else {
errln(UnicodeString(u"unsupported FavorSubtag value ") + test.favor);
return false;
}
builder.setFavorSubtag(favor);
}
if (!test.threshold.isEmpty()) {
infoln("skipping test case on line %d with non-default threshold: not exposed via API",
static_cast<int>(test.lineNr));
return true;
// int32_t threshold = Integer.valueOf(test.threshold);
// builder.internalSetThresholdDistance(threshold);
}
LocaleMatcher matcher = builder.build(errorCode);
if (errorCode.errIfFailureAndReset("LocaleMatcher::Builder::build()")) {
return false;
}
Locale expMatchLocale("");
Locale *expMatch = getLocaleOrNull(test.expMatch, expMatchLocale);
if (test.expDesired.isEmpty() && test.expCombined.isEmpty()) {
StringPiece desiredSP = test.desired.toStringPiece();
const Locale *bestSupported = matcher.getBestMatchForListString(desiredSP, errorCode);
if (!assertEquals("bestSupported from string",
locString(expMatch), locString(bestSupported))) {
return false;
}
LocalePriorityList desired(test.desired.toStringPiece(), errorCode);
LocalePriorityList::Iterator desiredIter = desired.iterator();
if (desired.getLength() == 1) {
const Locale &desiredLocale = desiredIter.next();
bestSupported = matcher.getBestMatch(desiredLocale, errorCode);
UBool ok = assertEquals("bestSupported from Locale",
locString(expMatch), locString(bestSupported));
LocaleMatcher::Result result = matcher.getBestMatchResult(desiredLocale, errorCode);
return ok & assertEquals("result.getSupportedLocale from Locale",
locString(expMatch), locString(result.getSupportedLocale()));
} else {
bestSupported = matcher.getBestMatch(desiredIter, errorCode);
return assertEquals("bestSupported from Locale iterator",
locString(expMatch), locString(bestSupported));
}
} else {
LocalePriorityList desired(test.desired.toStringPiece(), errorCode);
LocalePriorityList::Iterator desiredIter = desired.iterator();
LocaleMatcher::Result result = matcher.getBestMatchResult(desiredIter, errorCode);
UBool ok = assertEquals("result.getSupportedLocale from Locales",
locString(expMatch), locString(result.getSupportedLocale()));
if (!test.expDesired.isEmpty()) {
Locale expDesiredLocale("");
Locale *expDesired = getLocaleOrNull(test.expDesired, expDesiredLocale);
ok &= assertEquals("result.getDesiredLocale from Locales",
locString(expDesired), locString(result.getDesiredLocale()));
}
if (!test.expCombined.isEmpty()) {
if (test.expMatch.contains("-u-")) {
logKnownIssue("20727",
UnicodeString(u"ignoring makeResolvedLocale() line ") + test.lineNr);
return ok;
}
Locale expCombinedLocale("");
Locale *expCombined = getLocaleOrNull(test.expCombined, expCombinedLocale);
Locale combined = result.makeResolvedLocale(errorCode);
ok &= assertEquals("combined Locale from Locales",
locString(expCombined), locString(&combined));
}
return ok;
}
}
void LocaleMatcherTest::testDataDriven() {
IcuTestErrorCode errorCode(*this, "testDataDriven");
CharString path(getSourceTestData(errorCode), errorCode);
path.appendPathPart("localeMatcherTest.txt", errorCode);
const char *codePage = "UTF-8";
LocalUCHARBUFPointer f(ucbuf_open(path.data(), &codePage, true, false, errorCode));
if(errorCode.errIfFailureAndReset("ucbuf_open(localeMatcherTest.txt)")) {
return;
}
int32_t lineLength;
const char16_t *p;
UnicodeString line;
TestCase test;
int32_t numPassed = 0;
while ((p = ucbuf_readline(f.getAlias(), &lineLength, errorCode)) != nullptr &&
errorCode.isSuccess()) {
line.setTo(false, p, lineLength);
if (!readTestCase(line, test, errorCode)) {
if (errorCode.errIfFailureAndReset(
"test data syntax error on line %d", static_cast<int>(test.lineNr))) {
infoln(line);
}
continue;
}
UBool ok = dataDriven(test, errorCode);
if (errorCode.errIfFailureAndReset("test error on line %d", static_cast<int>(test.lineNr))) {
infoln(line);
} else if (!ok) {
infoln("test failure on line %d", static_cast<int>(test.lineNr));
infoln(line);
} else {
++numPassed;
}
}
infoln("number of passing test cases: %d", static_cast<int>(numPassed));
}
|