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
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2014, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/**
* IntlTestCollator is the medium level test class for everything in the directory "collate".
*/
/***********************************************************************
* Modification history
* Date Name Description
* 02/14/2001 synwee Compare with cintltst and commented away tests
* that are not run.
***********************************************************************/
#include "unicode/utypes.h"
#if !UCONFIG_NO_COLLATION
#include "unicode/localpointer.h"
#include "unicode/sortkey.h"
#include "unicode/uchar.h"
#include "unicode/ustring.h"
#include "encoll.h"
#include "frcoll.h"
#include "decoll.h"
#include "escoll.h"
#include "ficoll.h"
#include "jacoll.h"
#include "trcoll.h"
#include "allcoll.h"
#include "g7coll.h"
#include "mnkytst.h"
#include "apicoll.h"
#include "regcoll.h"
#include "currcoll.h"
#include "itercoll.h"
#include "tstnorm.h"
#include "normconf.h"
#include "thcoll.h"
#include "srchtest.h"
#include "ssearch.h"
#include "lcukocol.h"
#include "ucaconf.h"
#include "svccoll.h"
#include "cmemory.h"
#include "alphaindextst.h"
// Set to 1 to test offsets in backAndForth()
#define TEST_OFFSETS 0
extern IntlTest *createCollationTest();
void IntlTestCollator::runIndexedTest( int32_t index, UBool exec, const char* &name, char* par )
{
if (exec) {
logln("TestSuite Collator: ");
}
TESTCASE_AUTO_BEGIN;
TESTCASE_AUTO_CLASS(CollationEnglishTest);
TESTCASE_AUTO_CLASS(CollationFrenchTest);
TESTCASE_AUTO_CLASS(CollationGermanTest);
TESTCASE_AUTO_CLASS(CollationSpanishTest);
TESTCASE_AUTO_CLASS(CollationKanaTest);
TESTCASE_AUTO_CLASS(CollationTurkishTest);
TESTCASE_AUTO_CLASS(CollationDummyTest);
TESTCASE_AUTO_CLASS(G7CollationTest);
TESTCASE_AUTO_CLASS(CollationMonkeyTest);
TESTCASE_AUTO_CLASS(CollationAPITest);
TESTCASE_AUTO_CLASS(CollationRegressionTest);
TESTCASE_AUTO_CLASS(CollationCurrencyTest);
TESTCASE_AUTO_CLASS(CollationIteratorTest);
TESTCASE_AUTO_CLASS(CollationThaiTest);
TESTCASE_AUTO_CLASS(LotusCollationKoreanTest);
TESTCASE_AUTO_CLASS(StringSearchTest);
TESTCASE_AUTO_CLASS(UCAConformanceTest);
TESTCASE_AUTO_CLASS(CollationServiceTest);
TESTCASE_AUTO_CLASS(CollationFinnishTest); // removed by weiv - we have changed Finnish collation
TESTCASE_AUTO_CLASS(SSearchTest);
#if !UCONFIG_NO_NORMALIZATION
TESTCASE_AUTO_CLASS(AlphabeticIndexTest);
#endif
TESTCASE_AUTO_CREATE_CLASS(CollationTest);
TESTCASE_AUTO_END;
}
UCollationResult
IntlTestCollator::compareUsingPartials(UCollator *coll, const char16_t source[], int32_t sLen, const char16_t target[], int32_t tLen, int32_t pieceSize, UErrorCode &status) {
int32_t partialSKResult = 0;
uint8_t sBuf[512], tBuf[512];
UCharIterator sIter, tIter;
uint32_t sState[2], tState[2];
int32_t sSize = pieceSize, tSize = pieceSize;
int32_t i = 0;
status = U_ZERO_ERROR;
sState[0] = 0; sState[1] = 0;
tState[0] = 0; tState[1] = 0;
while(sSize == pieceSize && tSize == pieceSize && partialSKResult == 0) {
uiter_setString(&sIter, source, sLen);
uiter_setString(&tIter, target, tLen);
sSize = ucol_nextSortKeyPart(coll, &sIter, sState, sBuf, pieceSize, &status);
tSize = ucol_nextSortKeyPart(coll, &tIter, tState, tBuf, pieceSize, &status);
if(sState[0] != 0 || tState[0] != 0) {
log("State != 0 : %08X %08X\n", sState[0], tState[0]);
}
log("%i ", i++);
partialSKResult = memcmp(sBuf, tBuf, pieceSize);
}
if(partialSKResult < 0) {
return UCOL_LESS;
} else if(partialSKResult > 0) {
return UCOL_GREATER;
} else {
return UCOL_EQUAL;
}
}
void
IntlTestCollator::doTestVariant(Collator* col, const UnicodeString &source, const UnicodeString &target, Collator::EComparisonResult result)
{
UErrorCode status = U_ZERO_ERROR;
UCollator *myCollation = col->toUCollator();
Collator::EComparisonResult compareResult = col->compare(source, target);
CollationKey srckey, tgtkey;
col->getCollationKey(source, srckey, status);
col->getCollationKey(target, tgtkey, status);
if (U_FAILURE(status)){
errln("Creation of collation keys failed\n");
}
Collator::EComparisonResult keyResult = srckey.compareTo(tgtkey);
reportCResult(source, target, srckey, tgtkey, compareResult, keyResult, result, result);
UColAttributeValue norm = ucol_getAttribute(myCollation, UCOL_NORMALIZATION_MODE, &status);
int32_t sLen = source.length(), tLen = target.length();
const char16_t* src = source.getBuffer();
const char16_t* trg = target.getBuffer();
UCollationResult compareResultIter = static_cast<UCollationResult>(result);
{
UCharIterator sIter, tIter;
uiter_setString(&sIter, src, sLen);
uiter_setString(&tIter, trg, tLen);
compareResultIter = ucol_strcollIter(myCollation, &sIter, &tIter, &status);
if (compareResultIter != static_cast<UCollationResult>(result)) {
errln("Different result for iterative comparison "+source+" "+target);
}
}
/* convert the strings to UTF-8 and do try comparing with char iterator */
if(!quick) { /*!QUICK*/
char utf8Source[256], utf8Target[256];
int32_t utf8SourceLen = 0, utf8TargetLen = 0;
u_strToUTF8(utf8Source, 256, &utf8SourceLen, src, sLen, &status);
if(U_FAILURE(status)) { /* probably buffer is not big enough */
log("Src UTF-8 buffer too small! Will not compare!\n");
} else {
u_strToUTF8(utf8Target, 256, &utf8TargetLen, trg, tLen, &status);
if(U_SUCCESS(status)) { /* probably buffer is not big enough */
UCollationResult compareResultUTF8 = static_cast<UCollationResult>(result), compareResultUTF8Norm = static_cast<UCollationResult>(result);
UCharIterator sIter, tIter;
/*log_verbose("Strings converted to UTF-8:%s, %s\n", aescstrdup(source,-1), aescstrdup(target,-1));*/
uiter_setUTF8(&sIter, utf8Source, utf8SourceLen);
uiter_setUTF8(&tIter, utf8Target, utf8TargetLen);
/*uiter_setString(&sIter, source, sLen);
uiter_setString(&tIter, target, tLen);*/
compareResultUTF8 = ucol_strcollIter(myCollation, &sIter, &tIter, &status);
ucol_setAttribute(myCollation, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
sIter.move(&sIter, 0, UITER_START);
tIter.move(&tIter, 0, UITER_START);
compareResultUTF8Norm = ucol_strcollIter(myCollation, &sIter, &tIter, &status);
ucol_setAttribute(myCollation, UCOL_NORMALIZATION_MODE, norm, &status);
if(compareResultUTF8 != compareResultIter) {
errln("different results in iterative comparison for UTF-16 and UTF-8 encoded strings. "+source+", "+target);
}
if(compareResultUTF8 != compareResultUTF8Norm) {
errln("different results in iterative when normalization is turned on with UTF-8 strings. "+source+", "+target);
}
} else {
log("Target UTF-8 buffer too small! Did not compare!\n");
}
if(U_FAILURE(status)) {
log("UTF-8 strcoll failed! Ignoring result\n");
}
}
}
/* testing the partial sortkeys */
{ /*!QUICK*/
int32_t partialSizes[] = { 3, 1, 2, 4, 8, 20, 80 }; /* just size 3 in the quick mode */
int32_t partialSizesSize = 1;
if(!quick) {
partialSizesSize = 7;
}
int32_t i = 0;
log("partial sortkey test piecesize=");
for(i = 0; i < partialSizesSize; i++) {
UCollationResult partialSKResult = static_cast<UCollationResult>(result), partialNormalizedSKResult = static_cast<UCollationResult>(result);
log("%i ", partialSizes[i]);
partialSKResult = compareUsingPartials(myCollation, src, sLen, trg, tLen, partialSizes[i], status);
if (partialSKResult != static_cast<UCollationResult>(result)) {
errln("Partial sortkey comparison returned wrong result: "+source+", "+target+" (size "+partialSizes[i]+")");
}
if(norm != UCOL_ON && !quick) {
log("N ");
ucol_setAttribute(myCollation, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
partialNormalizedSKResult = compareUsingPartials(myCollation, src, sLen, trg, tLen, partialSizes[i], status);
ucol_setAttribute(myCollation, UCOL_NORMALIZATION_MODE, norm, &status);
if(partialSKResult != partialNormalizedSKResult) {
errln("Partial sortkey comparison gets different result when normalization is on: "+source+", "+target+" (size "+partialSizes[i]+")");
}
}
}
log("\n");
}
/*
if (compareResult != result) {
errln("String comparison failed in variant test\n");
}
if (keyResult != result) {
errln("Collation key comparison failed in variant test\n");
}
*/
}
void
IntlTestCollator::doTest(Collator* col, const char16_t *source, const char16_t *target, Collator::EComparisonResult result) {
doTest(col, UnicodeString(source), UnicodeString(target), result);
}
void
IntlTestCollator::doTest(Collator* col, const UnicodeString &source, const UnicodeString &target, Collator::EComparisonResult result)
{
if(col) {
doTestVariant(col, source, target, result);
if(result == Collator::LESS) {
doTestVariant(col, target, source, Collator::GREATER);
} else if (result == Collator::GREATER) {
doTestVariant(col, target, source, Collator::LESS);
}
UErrorCode status = U_ZERO_ERROR;
LocalPointer<CollationElementIterator> c((dynamic_cast<RuleBasedCollator *>(col))->createCollationElementIterator(source));
logln("Testing iterating source: "+source);
backAndForth(*c);
c->setText(target, status);
logln("Testing iterating target: "+target);
backAndForth(*c);
}
}
// used for collation result reporting, defined here for convenience
// (maybe moved later)
void
IntlTestCollator::reportCResult( const UnicodeString &source, const UnicodeString &target,
CollationKey &sourceKey, CollationKey &targetKey,
Collator::EComparisonResult compareResult,
Collator::EComparisonResult keyResult,
Collator::EComparisonResult incResult,
Collator::EComparisonResult expectedResult )
{
if (expectedResult < -1 || expectedResult > 1)
{
errln("***** invalid call to reportCResult ****");
return;
}
UBool ok1 = (compareResult == expectedResult);
UBool ok2 = (keyResult == expectedResult);
UBool ok3 = (incResult == expectedResult);
if (ok1 && ok2 && ok3 && !verbose) {
// Keep non-verbose, passing tests fast
return;
} else {
UnicodeString msg1(ok1 ? "Ok: compare(" : "FAIL: compare(");
UnicodeString msg2(", "), msg3(") returned "), msg4("; expected ");
UnicodeString prettySource, prettyTarget, sExpect, sResult;
IntlTest::prettify(source, prettySource);
IntlTest::prettify(target, prettyTarget);
appendCompareResult(compareResult, sResult);
appendCompareResult(expectedResult, sExpect);
if (ok1) {
logln(msg1 + prettySource + msg2 + prettyTarget + msg3 + sResult);
} else {
errln(msg1 + prettySource + msg2 + prettyTarget + msg3 + sResult + msg4 + sExpect);
}
msg1 = UnicodeString(ok2 ? "Ok: key(" : "FAIL: key(");
msg2 = ").compareTo(key(";
msg3 = ")) returned ";
appendCompareResult(keyResult, sResult);
if (ok2) {
logln(msg1 + prettySource + msg2 + prettyTarget + msg3 + sResult);
} else {
errln(msg1 + prettySource + msg2 + prettyTarget + msg3 + sResult + msg4 + sExpect);
msg1 = " ";
msg2 = " vs. ";
prettify(sourceKey, prettySource);
prettify(targetKey, prettyTarget);
errln(msg1 + prettySource + msg2 + prettyTarget);
}
msg1 = UnicodeString (ok3 ? "Ok: incCompare(" : "FAIL: incCompare(");
msg2 = ", ";
msg3 = ") returned ";
appendCompareResult(incResult, sResult);
if (ok3) {
logln(msg1 + prettySource + msg2 + prettyTarget + msg3 + sResult);
} else {
errln(msg1 + prettySource + msg2 + prettyTarget + msg3 + sResult + msg4 + sExpect);
}
}
}
UnicodeString&
IntlTestCollator::appendCompareResult(Collator::EComparisonResult result,
UnicodeString& target)
{
if (result == Collator::LESS)
{
target += "LESS";
}
else if (result == Collator::EQUAL)
{
target += "EQUAL";
}
else if (result == Collator::GREATER)
{
target += "GREATER";
}
else
{
UnicodeString huh = "?";
target += (huh + static_cast<int32_t>(result));
}
return target;
}
// Produce a printable representation of a CollationKey
UnicodeString &IntlTestCollator::prettify(const CollationKey &source, UnicodeString &target)
{
int32_t i, byteCount;
const uint8_t *bytes = source.getByteArray(byteCount);
target.remove();
target += "[";
for (i = 0; i < byteCount; i += 1)
{
if (i != 0) {
target += " ";
}
appendHex(bytes[i], 2, target);
}
target += "]";
return target;
}
void IntlTestCollator::backAndForth(CollationElementIterator &iter)
{
// Run through the iterator forwards and stick it into an array
int32_t orderLength = 0;
LocalArray<Order> orders(getOrders(iter, orderLength));
UErrorCode status = U_ZERO_ERROR;
// Now go through it backwards and make sure we get the same values
int32_t index = orderLength;
int32_t o;
// reset the iterator
iter.reset();
while ((o = iter.previous(status)) != CollationElementIterator::NULLORDER)
{
/*int32_t offset = */iter.getOffset();
if (index == 0) {
if(o == 0) {
continue;
} else { // this is an error, orders exhausted but there are non-ignorable CEs from
// going backwards
errln("Backward iteration returned a non ignorable after orders are exhausted");
break;
}
}
index -= 1;
if (o != orders[index].order) {
if (o == 0)
index += 1;
else {
while (index > 0 && orders[--index].order == 0) {
// nothing...
}
if (o != orders[index].order) {
errln("Mismatched order at index %d: 0x%0:8X vs. 0x%0:8X", index,
orders[index].order, o);
//break;
return;
}
}
}
#if TEST_OFFSETS
if (offset != orders[index].offset) {
errln("Mismatched offset at index %d: %d vs. %d", index,
orders[index].offset, offset);
//break;
return;
}
#endif
}
while (index != 0 && orders[index - 1].order == 0)
{
index --;
}
if (index != 0)
{
UnicodeString msg("Didn't get back to beginning - index is ");
errln(msg + index);
iter.reset();
err("next: ");
while ((o = iter.next(status)) != CollationElementIterator::NULLORDER)
{
UnicodeString hexString("0x");
appendHex(o, 8, hexString);
hexString += " ";
err(hexString);
}
errln("");
err("prev: ");
while ((o = iter.previous(status)) != CollationElementIterator::NULLORDER)
{
UnicodeString hexString("0x");
appendHex(o, 8, hexString);
hexString += " ";
err(hexString);
}
errln("");
}
}
/**
* Return an integer array containing all of the collation orders
* returned by calls to next on the specified iterator
*/
IntlTestCollator::Order *IntlTestCollator::getOrders(CollationElementIterator &iter, int32_t &orderLength)
{
int32_t maxSize = 100;
int32_t size = 0;
LocalArray<Order> orders(new Order[maxSize]);
UErrorCode status = U_ZERO_ERROR;
int32_t offset = iter.getOffset();
int32_t order;
while ((order = iter.next(status)) != CollationElementIterator::NULLORDER)
{
if (size == maxSize)
{
maxSize *= 2;
Order *temp = new Order[maxSize];
uprv_memcpy(temp, orders.getAlias(), size * sizeof(Order));
orders.adoptInstead(temp);
}
orders[size].order = order;
orders[size].offset = offset;
offset = iter.getOffset();
size += 1;
}
if (U_FAILURE(status)) {
errln("CollationElementIterator.next() failed - %s",
u_errorName(status));
}
if (maxSize > size)
{
Order *temp = new Order[size];
uprv_memcpy(temp, orders.getAlias(), size * sizeof(Order));
orders.adoptInstead(temp);
}
orderLength = size;
return orders.orphan();
}
#endif /* #if !UCONFIG_NO_COLLATION */
|