1 /********************************************************************
3 * Copyright (c) 2002-2012, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
8 * UCAConformanceTest performs conformance tests defined in the data
9 * files. ICU ships with stub data files, as the whole test are too
10 * long. To do the whole test, download the test files.
13 #include "unicode/utypes.h"
15 #if !UCONFIG_NO_COLLATION
18 #include "unicode/ustring.h"
23 UCAConformanceTest::UCAConformanceTest() :
28 UCA = (RuleBasedCollator *)Collator::createInstance(Locale::getRoot(), status);
29 if(U_FAILURE(status)) {
30 dataerrln("Error - UCAConformanceTest: Unable to open UCA collator! - %s", u_errorName(status));
33 const char *srcDir = IntlTest::getSourceTestData(status);
34 if (U_FAILURE(status)) {
35 dataerrln("Could not open test data %s", u_errorName(status));
38 uprv_strcpy(testDataPath, srcDir);
39 uprv_strcat(testDataPath, "CollationTest_");
41 UVersionInfo uniVersion;
42 static const UVersionInfo v62 = { 6, 2, 0, 0 };
43 u_getUnicodeVersion(uniVersion);
44 isAtLeastUCA62 = uprv_memcmp(uniVersion, v62, 4) >= 0;
47 UCAConformanceTest::~UCAConformanceTest()
56 void UCAConformanceTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par */)
59 logln("TestSuite UCAConformanceTest: ");
62 TESTCASE_AUTO(TestTableNonIgnorable);
63 TESTCASE_AUTO(TestTableShifted);
64 TESTCASE_AUTO(TestRulesNonIgnorable);
65 TESTCASE_AUTO(TestRulesShifted);
69 void UCAConformanceTest::initRbUCA()
72 UnicodeString ucarules;
74 UCA->getRules(UCOL_FULL_RULES, ucarules);
75 rbUCA = new RuleBasedCollator(ucarules, status);
76 if (U_FAILURE(status)) {
77 dataerrln("Failure creating UCA rule-based collator: %s", u_errorName(status));
81 dataerrln("Failure creating UCA rule-based collator: %s", u_errorName(status));
87 void UCAConformanceTest::setCollNonIgnorable(Collator *coll)
89 coll->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
90 coll->setAttribute(UCOL_CASE_FIRST, UCOL_OFF, status);
91 coll->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, status);
92 coll->setAttribute(UCOL_STRENGTH, isAtLeastUCA62 ? UCOL_IDENTICAL : UCOL_TERTIARY, status);
93 coll->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, status);
96 void UCAConformanceTest::setCollShifted(Collator *coll)
98 coll->setAttribute(UCOL_NORMALIZATION_MODE, UCOL_ON, status);
99 coll->setAttribute(UCOL_CASE_FIRST, UCOL_OFF, status);
100 coll->setAttribute(UCOL_CASE_LEVEL, UCOL_OFF, status);
101 coll->setAttribute(UCOL_STRENGTH, isAtLeastUCA62 ? UCOL_IDENTICAL : UCOL_QUATERNARY, status);
102 coll->setAttribute(UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, status);
105 void UCAConformanceTest::openTestFile(const char *type)
107 const char *ext = ".txt";
112 uprv_strcpy(buffer, testDataPath);
113 uprv_strcat(buffer, type);
114 int32_t bufLen = (int32_t)uprv_strlen(buffer);
116 // we try to open 3 files:
117 // path/CollationTest_type.txt
118 // path/CollationTest_type_SHORT.txt
119 // path/CollationTest_type_STUB.txt
120 // we are going to test with the first one that we manage to open.
122 uprv_strcpy(buffer+bufLen, ext);
124 testFile = fopen(buffer, "rb");
127 uprv_strcpy(buffer+bufLen, "_SHORT");
128 uprv_strcat(buffer, ext);
129 testFile = fopen(buffer, "rb");
132 uprv_strcpy(buffer+bufLen, "_STUB");
133 uprv_strcat(buffer, ext);
134 testFile = fopen(buffer, "rb");
137 *(buffer+bufLen) = 0;
138 dataerrln("Could not open any of the conformance test files, tried opening base %s\n", buffer);
142 "INFO: Working with the stub file.\n"
143 "If you need the full conformance test, please\n"
144 "download the appropriate data files from:\n"
145 "http://source.icu-project.org/repos/icu/tools/trunk/unicodetools/com/ibm/text/data/");
151 static const uint32_t IS_SHIFTED = 1;
152 static const uint32_t FROM_RULES = 2;
155 skipLineBecauseOfBug(const UChar *s, int32_t length, uint32_t flags) {
156 // TODO: Fix ICU ticket #8052
158 (s[0] == 0xfb2 || s[0] == 0xfb3) &&
160 (s[2] == 0xf73 || s[2] == 0xf75 || s[2] == 0xf81)) {
163 // TODO: Fix ICU ticket #9361
164 if((flags & IS_SHIFTED) != 0 && length >= 2 && s[0] == 0xfffe) {
167 // TODO: Fix tailoring builder, ICU ticket #9593.
169 if((flags & FROM_RULES) != 0 && length >= 2 && ((c = s[1]) == 0xedc || c == 0xedd)) {
175 static UCollationResult
176 normalizeResult(int32_t result) {
177 return result<0 ? UCOL_LESS : result==0 ? UCOL_EQUAL : UCOL_GREATER;
180 void UCAConformanceTest::testConformance(const Collator *coll)
185 uint32_t skipFlags = 0;
186 if(coll->getAttribute(UCOL_ALTERNATE_HANDLING, status) == UCOL_SHIFTED) {
187 skipFlags |= IS_SHIFTED;
190 skipFlags |= FROM_RULES;
195 UChar b1[1024], b2[1024];
196 UChar *buffer = b1, *oldB = NULL;
198 char lineB1[1024], lineB2[1024];
199 char *lineB = lineB1, *oldLineB = lineB2;
201 uint8_t sk1[1024], sk2[1024];
202 uint8_t *oldSk = NULL, *newSk = sk1;
208 while (fgets(lineB, 1024, testFile) != NULL) {
209 // remove trailing whitespace
213 if(*lineB == 0 || lineB[0] == '#') {
216 int32_t buflen = u_parseString(lineB, buffer, 1024, &first, &status);
217 if(U_FAILURE(status)) {
218 errln("Error parsing line %ld (%s): %s\n",
219 (long)line, u_errorName(status), lineB);
220 status = U_ZERO_ERROR;
224 if(skipLineBecauseOfBug(buffer, buflen, skipFlags)) {
225 logln("Skipping line %i because of a known bug", line);
229 int32_t resLen = coll->getSortKey(buffer, buflen, newSk, 1024);
232 int32_t skres = strcmp((char *)oldSk, (char *)newSk);
233 int32_t cmpres = coll->compare(oldB, oldBlen, buffer, buflen, status);
234 int32_t cmpres2 = coll->compare(buffer, buflen, oldB, oldBlen, status);
236 if(cmpres != -cmpres2) {
237 errln("Compare result not symmetrical on line %i", line);
240 if(cmpres != normalizeResult(skres)) {
241 errln("Difference between coll->compare (%d) and sortkey compare (%d) on line %i",
242 cmpres, skres, line);
243 errln(" Previous data line %s", oldLineB);
244 errln(" Current data line %s", lineB);
247 int32_t res = cmpres;
248 if(res == 0 && !isAtLeastUCA62) {
249 // Up to UCA 6.1, the collation test files use a custom tie-breaker,
250 // comparing the raw input strings.
251 res = u_strcmpCodePointOrder(oldB, buffer);
252 // Starting with UCA 6.2, the collation test files use the standard UCA tie-breaker,
253 // comparing the NFD versions of the input strings,
254 // which we do via setting strength=identical.
257 errln("Line %i is not greater or equal than previous line", line);
258 errln(" Previous data line %s", oldLineB);
259 errln(" Current data line %s", lineB);
260 UnicodeString oldS, newS;
261 prettify(CollationKey(oldSk, oldLen), oldS);
262 prettify(CollationKey(newSk, resLen), newS);
263 errln(" Previous key: "+oldS);
264 errln(" Current key: "+newS);
272 if(lineB == lineB1) {
286 void UCAConformanceTest::TestTableNonIgnorable(/* par */) {
287 if (U_FAILURE(status)) {
288 dataerrln("Error running UCA Conformance Test: %s", u_errorName(status));
291 setCollNonIgnorable(UCA);
292 openTestFile("NON_IGNORABLE");
293 testConformance(UCA);
296 void UCAConformanceTest::TestTableShifted(/* par */) {
297 if (U_FAILURE(status)) {
298 dataerrln("Error running UCA Conformance Test: %s", u_errorName(status));
302 openTestFile("SHIFTED");
303 testConformance(UCA);
306 void UCAConformanceTest::TestRulesNonIgnorable(/* par */) {
309 if(U_SUCCESS(status)) {
310 setCollNonIgnorable(rbUCA);
311 openTestFile("NON_IGNORABLE");
312 testConformance(rbUCA);
316 void UCAConformanceTest::TestRulesShifted(/* par */) {
317 logln("This test is currently disabled, as it is impossible to "
318 "wholly represent fractional UCA using tailoring rules.");
323 if(U_SUCCESS(status)) {
324 setCollShifted(rbUCA);
325 openTestFile("SHIFTED");
326 testConformance(rbUCA);
330 #endif /* #if !UCONFIG_NO_COLLATION */