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
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* COPYRIGHT:
* Copyright (c) 2002-2014, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
/**
* UCAConformanceTest performs conformance tests defined in the data
* files. ICU ships with stub data files, as the whole test are too
* long. To do the whole test, download the test files.
*/
#include "unicode/utypes.h"
#if !UCONFIG_NO_COLLATION
#include "ucaconf.h"
#include "unicode/sortkey.h"
#include "unicode/tblcoll.h"
#include "unicode/ustring.h"
#include "cmemory.h"
#include "cstring.h"
#include "uparse.h"
UCAConformanceTest::UCAConformanceTest() :
rbUCA(nullptr),
testFile(nullptr),
status(U_ZERO_ERROR)
{
UCA = dynamic_cast<RuleBasedCollator*>(Collator::createInstance(Locale::getRoot(), status));
if(U_FAILURE(status)) {
dataerrln("Error - UCAConformanceTest: Unable to open UCA collator! - %s", u_errorName(status));
}
const char *srcDir = IntlTest::getSourceTestData(status);
if (U_FAILURE(status)) {
dataerrln("Could not open test data %s", u_errorName(status));
return;
}
uprv_strcpy(testDataPath, srcDir);
uprv_strcat(testDataPath, "CollationTest_");
UVersionInfo uniVersion;
static const UVersionInfo v62 = { 6, 2, 0, 0 };
u_getUnicodeVersion(uniVersion);
isAtLeastUCA62 = uprv_memcmp(uniVersion, v62, 4) >= 0;
}
UCAConformanceTest::~UCAConformanceTest()
{
delete UCA;
delete rbUCA;
if (testFile) {
fclose(testFile);
}
}
void UCAConformanceTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par */)
{
if(exec) {
logln("TestSuite UCAConformanceTest: ");
}
TESTCASE_AUTO_BEGIN;
TESTCASE_AUTO(TestTableNonIgnorable);
TESTCASE_AUTO(TestTableShifted);
TESTCASE_AUTO(TestRulesNonIgnorable);
TESTCASE_AUTO(TestRulesShifted);
TESTCASE_AUTO_END;
}
void UCAConformanceTest::initRbUCA()
{
if(!rbUCA) {
if (UCA) {
UnicodeString ucarules;
UCA->getRules(UCOL_FULL_RULES, ucarules);
rbUCA = new RuleBasedCollator(ucarules, status);
if (U_FAILURE(status)) {
dataerrln("Failure creating UCA rule-based collator: %s", u_errorName(status));
return;
}
} else {
dataerrln("Failure creating UCA rule-based collator: %s", u_errorName(status));
return;
}
}
}
void UCAConformanceTest::setCollNonIgnorable(Collator *coll)
{
coll->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
coll->setAttribute(UCOL_CASE_FIRST, UCOL_OFF, status);
coll->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, status);
coll->setAttribute(UCOL_STRENGTH, isAtLeastUCA62 ? UCOL_IDENTICAL : UCOL_TERTIARY, status);
coll->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, status);
}
void UCAConformanceTest::setCollShifted(Collator *coll)
{
coll->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
coll->setAttribute(UCOL_CASE_FIRST, UCOL_OFF, status);
coll->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, status);
coll->setAttribute(UCOL_STRENGTH, isAtLeastUCA62 ? UCOL_IDENTICAL : UCOL_QUATERNARY, status);
coll->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, status);
}
void UCAConformanceTest::openTestFile(const char *type)
{
const char *ext = ".txt";
if(testFile) {
fclose(testFile);
}
char buffer[1024];
uprv_strcpy(buffer, testDataPath);
uprv_strcat(buffer, type);
int32_t bufLen = static_cast<int32_t>(uprv_strlen(buffer));
// we try to open 3 files:
// path/CollationTest_type.txt
// path/CollationTest_type_SHORT.txt
// path/CollationTest_type_STUB.txt
// we are going to test with the first one that we manage to open.
uprv_strcpy(buffer+bufLen, ext);
testFile = fopen(buffer, "rb");
if (testFile == nullptr) {
uprv_strcpy(buffer+bufLen, "_SHORT");
uprv_strcat(buffer, ext);
testFile = fopen(buffer, "rb");
if (testFile == nullptr) {
uprv_strcpy(buffer+bufLen, "_STUB");
uprv_strcat(buffer, ext);
testFile = fopen(buffer, "rb");
if (testFile == nullptr) {
*(buffer+bufLen) = 0;
dataerrln("Could not open any of the conformance test files, tried opening base %s\n", buffer);
return;
} else {
infoln(
"INFO: Working with the stub file.\n"
"If you need the full conformance test, please\n"
"download the appropriate data files from:\n"
"http://unicode.org/cldr/trac/browser/trunk/common/uca");
}
}
}
}
static const uint32_t IS_SHIFTED = 1;
static const uint32_t FROM_RULES = 2;
static UBool
skipLineBecauseOfBug(const char16_t *s, int32_t length, uint32_t flags) {
// Add temporary exceptions here if there are ICU bugs, until we can fix them.
// For examples see the ICU 52 version of this file.
(void)s;
(void)length;
(void)flags;
return false;
}
static UCollationResult
normalizeResult(int32_t result) {
return result<0 ? UCOL_LESS : result==0 ? UCOL_EQUAL : UCOL_GREATER;
}
void UCAConformanceTest::testConformance(const Collator *coll)
{
if (testFile == nullptr) {
return;
}
uint32_t skipFlags = 0;
if(coll->getAttribute(UCOL_ALTERNATE_HANDLING, status) == UCOL_SHIFTED) {
skipFlags |= IS_SHIFTED;
}
if(coll == rbUCA) {
skipFlags |= FROM_RULES;
}
logln("-prop:ucaconfnosortkeys=1 turns off getSortKey() in UCAConformanceTest");
UBool withSortKeys = getProperty("ucaconfnosortkeys") == nullptr;
int32_t line = 0;
char16_t b1[1024], b2[1024];
char16_t *buffer = b1, *oldB = nullptr;
char lineB1[1024], lineB2[1024];
char *lineB = lineB1, *oldLineB = lineB2;
uint8_t sk1[1024], sk2[1024];
uint8_t *oldSk = nullptr, *newSk = sk1;
int32_t oldLen = 0;
int32_t oldBlen = 0;
uint32_t first = 0;
while (fgets(lineB, 1024, testFile) != nullptr) {
// remove trailing whitespace
u_rtrim(lineB);
line++;
if(*lineB == 0 || lineB[0] == '#') {
continue;
}
int32_t buflen = u_parseString(lineB, buffer, 1024, &first, &status);
if(U_FAILURE(status)) {
errln("Error parsing line %ld (%s): %s\n",
static_cast<long>(line), u_errorName(status), lineB);
status = U_ZERO_ERROR;
}
buffer[buflen] = 0;
if(skipLineBecauseOfBug(buffer, buflen, skipFlags)) {
logln("Skipping line %i because of a known bug", line);
continue;
}
int32_t resLen = withSortKeys ? coll->getSortKey(buffer, buflen, newSk, 1024) : 0;
if(oldSk != nullptr) {
UBool ok=true;
int32_t skres = withSortKeys ? strcmp(reinterpret_cast<char*>(oldSk), reinterpret_cast<char*>(newSk)) : 0;
int32_t cmpres = coll->compare(oldB, oldBlen, buffer, buflen, status);
int32_t cmpres2 = coll->compare(buffer, buflen, oldB, oldBlen, status);
if(cmpres != -cmpres2) {
errln("Compare result not symmetrical on line %i: "
"previous vs. current (%d) / current vs. previous (%d)",
line, cmpres, cmpres2);
ok = false;
}
// TODO: Compare with normalization turned off if the input passes the FCD test.
if(withSortKeys && cmpres != normalizeResult(skres)) {
errln("Difference between coll->compare (%d) and sortkey compare (%d) on line %i",
cmpres, skres, line);
ok = false;
}
int32_t res = cmpres;
if(res == 0 && !isAtLeastUCA62) {
// Up to UCA 6.1, the collation test files use a custom tie-breaker,
// comparing the raw input strings.
res = u_strcmpCodePointOrder(oldB, buffer);
// Starting with UCA 6.2, the collation test files use the standard UCA tie-breaker,
// comparing the NFD versions of the input strings,
// which we do via setting strength=identical.
}
if(res > 0) {
errln("Line %i is not greater or equal than previous line", line);
ok = false;
}
if(!ok) {
errln(" Previous data line %s", oldLineB);
errln(" Current data line %s", lineB);
if(withSortKeys) {
UnicodeString oldS, newS;
prettify(CollationKey(oldSk, oldLen), oldS);
prettify(CollationKey(newSk, resLen), newS);
errln(" Previous key: "+oldS);
errln(" Current key: "+newS);
}
}
}
// swap buffers
oldLineB = lineB;
oldB = buffer;
oldSk = newSk;
if(lineB == lineB1) {
lineB = lineB2;
buffer = b2;
newSk = sk2;
} else {
lineB = lineB1;
buffer = b1;
newSk = sk1;
}
oldLen = resLen;
oldBlen = buflen;
}
}
void UCAConformanceTest::TestTableNonIgnorable(/* par */) {
if (U_FAILURE(status)) {
dataerrln("Error running UCA Conformance Test: %s", u_errorName(status));
return;
}
setCollNonIgnorable(UCA);
openTestFile("NON_IGNORABLE");
testConformance(UCA);
}
void UCAConformanceTest::TestTableShifted(/* par */) {
if (U_FAILURE(status)) {
dataerrln("Error running UCA Conformance Test: %s", u_errorName(status));
return;
}
setCollShifted(UCA);
openTestFile("SHIFTED");
testConformance(UCA);
}
void UCAConformanceTest::TestRulesNonIgnorable(/* par */) {
if(logKnownIssue("cldrbug:6745", "UCARules.txt has problems")) { return; }
initRbUCA();
if(U_SUCCESS(status)) {
setCollNonIgnorable(rbUCA);
openTestFile("NON_IGNORABLE");
testConformance(rbUCA);
}
}
void UCAConformanceTest::TestRulesShifted(/* par */) {
logln("This test is currently disabled, as it is impossible to "
"wholly represent fractional UCA using tailoring rules.");
return;
initRbUCA();
if(U_SUCCESS(status)) {
setCollShifted(rbUCA);
openTestFile("SHIFTED");
testConformance(rbUCA);
}
}
#endif /* #if !UCONFIG_NO_COLLATION */
|