]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/ucaconf.cpp
1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
5 * Copyright (c) 2002-2014, International Business Machines Corporation and
6 * others. All Rights Reserved.
7 ********************************************************************/
10 * UCAConformanceTest performs conformance tests defined in the data
11 * files. ICU ships with stub data files, as the whole test are too
12 * long. To do the whole test, download the test files.
15 #include "unicode/utypes.h"
17 #if !UCONFIG_NO_COLLATION
20 #include "unicode/sortkey.h"
21 #include "unicode/tblcoll.h"
22 #include "unicode/ustring.h"
27 UCAConformanceTest::UCAConformanceTest() :
32 UCA
= (RuleBasedCollator
*)Collator::createInstance(Locale::getRoot(), status
);
33 if(U_FAILURE(status
)) {
34 dataerrln("Error - UCAConformanceTest: Unable to open UCA collator! - %s", u_errorName(status
));
37 const char *srcDir
= IntlTest::getSourceTestData(status
);
38 if (U_FAILURE(status
)) {
39 dataerrln("Could not open test data %s", u_errorName(status
));
42 uprv_strcpy(testDataPath
, srcDir
);
43 uprv_strcat(testDataPath
, "CollationTest_");
45 UVersionInfo uniVersion
;
46 static const UVersionInfo v62
= { 6, 2, 0, 0 };
47 u_getUnicodeVersion(uniVersion
);
48 isAtLeastUCA62
= uprv_memcmp(uniVersion
, v62
, 4) >= 0;
51 UCAConformanceTest::~UCAConformanceTest()
60 void UCAConformanceTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par */)
63 logln("TestSuite UCAConformanceTest: ");
66 TESTCASE_AUTO(TestTableNonIgnorable
);
67 TESTCASE_AUTO(TestTableShifted
);
68 TESTCASE_AUTO(TestRulesNonIgnorable
);
69 TESTCASE_AUTO(TestRulesShifted
);
73 void UCAConformanceTest::initRbUCA()
77 UnicodeString ucarules
;
78 UCA
->getRules(UCOL_FULL_RULES
, ucarules
);
79 rbUCA
= new RuleBasedCollator(ucarules
, status
);
80 if (U_FAILURE(status
)) {
81 dataerrln("Failure creating UCA rule-based collator: %s", u_errorName(status
));
85 dataerrln("Failure creating UCA rule-based collator: %s", u_errorName(status
));
91 void UCAConformanceTest::setCollNonIgnorable(Collator
*coll
)
93 coll
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_ON
, status
);
94 coll
->setAttribute(UCOL_CASE_FIRST
, UCOL_OFF
, status
);
95 coll
->setAttribute(UCOL_CASE_LEVEL
, UCOL_OFF
, status
);
96 coll
->setAttribute(UCOL_STRENGTH
, isAtLeastUCA62
? UCOL_IDENTICAL
: UCOL_TERTIARY
, status
);
97 coll
->setAttribute(UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
, status
);
100 void UCAConformanceTest::setCollShifted(Collator
*coll
)
102 coll
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_ON
, status
);
103 coll
->setAttribute(UCOL_CASE_FIRST
, UCOL_OFF
, status
);
104 coll
->setAttribute(UCOL_CASE_LEVEL
, UCOL_OFF
, status
);
105 coll
->setAttribute(UCOL_STRENGTH
, isAtLeastUCA62
? UCOL_IDENTICAL
: UCOL_QUATERNARY
, status
);
106 coll
->setAttribute(UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, status
);
109 void UCAConformanceTest::openTestFile(const char *type
)
111 const char *ext
= ".txt";
116 uprv_strcpy(buffer
, testDataPath
);
117 uprv_strcat(buffer
, type
);
118 int32_t bufLen
= (int32_t)uprv_strlen(buffer
);
120 // we try to open 3 files:
121 // path/CollationTest_type.txt
122 // path/CollationTest_type_SHORT.txt
123 // path/CollationTest_type_STUB.txt
124 // we are going to test with the first one that we manage to open.
126 uprv_strcpy(buffer
+bufLen
, ext
);
128 testFile
= fopen(buffer
, "rb");
131 uprv_strcpy(buffer
+bufLen
, "_SHORT");
132 uprv_strcat(buffer
, ext
);
133 testFile
= fopen(buffer
, "rb");
136 uprv_strcpy(buffer
+bufLen
, "_STUB");
137 uprv_strcat(buffer
, ext
);
138 testFile
= fopen(buffer
, "rb");
141 *(buffer
+bufLen
) = 0;
142 dataerrln("Could not open any of the conformance test files, tried opening base %s\n", buffer
);
146 "INFO: Working with the stub file.\n"
147 "If you need the full conformance test, please\n"
148 "download the appropriate data files from:\n"
149 "http://unicode.org/cldr/trac/browser/trunk/common/uca");
155 static const uint32_t IS_SHIFTED
= 1;
156 static const uint32_t FROM_RULES
= 2;
159 skipLineBecauseOfBug(const UChar
*s
, int32_t length
, uint32_t flags
) {
160 // Add temporary exceptions here if there are ICU bugs, until we can fix them.
161 // For examples see the ICU 52 version of this file.
168 static UCollationResult
169 normalizeResult(int32_t result
) {
170 return result
<0 ? UCOL_LESS
: result
==0 ? UCOL_EQUAL
: UCOL_GREATER
;
173 void UCAConformanceTest::testConformance(const Collator
*coll
)
178 uint32_t skipFlags
= 0;
179 if(coll
->getAttribute(UCOL_ALTERNATE_HANDLING
, status
) == UCOL_SHIFTED
) {
180 skipFlags
|= IS_SHIFTED
;
183 skipFlags
|= FROM_RULES
;
186 logln("-prop:ucaconfnosortkeys=1 turns off getSortKey() in UCAConformanceTest");
187 UBool withSortKeys
= getProperty("ucaconfnosortkeys") == NULL
;
191 UChar b1
[1024], b2
[1024];
192 UChar
*buffer
= b1
, *oldB
= NULL
;
194 char lineB1
[1024], lineB2
[1024];
195 char *lineB
= lineB1
, *oldLineB
= lineB2
;
197 uint8_t sk1
[1024], sk2
[1024];
198 uint8_t *oldSk
= NULL
, *newSk
= sk1
;
204 while (fgets(lineB
, 1024, testFile
) != NULL
) {
205 // remove trailing whitespace
209 if(*lineB
== 0 || lineB
[0] == '#') {
212 int32_t buflen
= u_parseString(lineB
, buffer
, 1024, &first
, &status
);
213 if(U_FAILURE(status
)) {
214 errln("Error parsing line %ld (%s): %s\n",
215 (long)line
, u_errorName(status
), lineB
);
216 status
= U_ZERO_ERROR
;
220 if(skipLineBecauseOfBug(buffer
, buflen
, skipFlags
)) {
221 logln("Skipping line %i because of a known bug", line
);
225 int32_t resLen
= withSortKeys
? coll
->getSortKey(buffer
, buflen
, newSk
, 1024) : 0;
229 int32_t skres
= withSortKeys
? strcmp((char *)oldSk
, (char *)newSk
) : 0;
230 int32_t cmpres
= coll
->compare(oldB
, oldBlen
, buffer
, buflen
, status
);
231 int32_t cmpres2
= coll
->compare(buffer
, buflen
, oldB
, oldBlen
, status
);
233 if(cmpres
!= -cmpres2
) {
234 errln("Compare result not symmetrical on line %i: "
235 "previous vs. current (%d) / current vs. previous (%d)",
236 line
, cmpres
, cmpres2
);
240 // TODO: Compare with normalization turned off if the input passes the FCD test.
242 if(withSortKeys
&& cmpres
!= normalizeResult(skres
)) {
243 errln("Difference between coll->compare (%d) and sortkey compare (%d) on line %i",
244 cmpres
, skres
, line
);
248 int32_t res
= cmpres
;
249 if(res
== 0 && !isAtLeastUCA62
) {
250 // Up to UCA 6.1, the collation test files use a custom tie-breaker,
251 // comparing the raw input strings.
252 res
= u_strcmpCodePointOrder(oldB
, buffer
);
253 // Starting with UCA 6.2, the collation test files use the standard UCA tie-breaker,
254 // comparing the NFD versions of the input strings,
255 // which we do via setting strength=identical.
258 errln("Line %i is not greater or equal than previous line", line
);
263 errln(" Previous data line %s", oldLineB
);
264 errln(" Current data line %s", lineB
);
266 UnicodeString oldS
, newS
;
267 prettify(CollationKey(oldSk
, oldLen
), oldS
);
268 prettify(CollationKey(newSk
, resLen
), newS
);
269 errln(" Previous key: "+oldS
);
270 errln(" Current key: "+newS
);
279 if(lineB
== lineB1
) {
293 void UCAConformanceTest::TestTableNonIgnorable(/* par */) {
294 if (U_FAILURE(status
)) {
295 dataerrln("Error running UCA Conformance Test: %s", u_errorName(status
));
298 setCollNonIgnorable(UCA
);
299 openTestFile("NON_IGNORABLE");
300 testConformance(UCA
);
303 void UCAConformanceTest::TestTableShifted(/* par */) {
304 if (U_FAILURE(status
)) {
305 dataerrln("Error running UCA Conformance Test: %s", u_errorName(status
));
309 openTestFile("SHIFTED");
310 testConformance(UCA
);
313 void UCAConformanceTest::TestRulesNonIgnorable(/* par */) {
314 if(logKnownIssue("cldrbug:6745", "UCARules.txt has problems")) { return; }
317 if(U_SUCCESS(status
)) {
318 setCollNonIgnorable(rbUCA
);
319 openTestFile("NON_IGNORABLE");
320 testConformance(rbUCA
);
324 void UCAConformanceTest::TestRulesShifted(/* par */) {
325 logln("This test is currently disabled, as it is impossible to "
326 "wholly represent fractional UCA using tailoring rules.");
331 if(U_SUCCESS(status
)) {
332 setCollShifted(rbUCA
);
333 openTestFile("SHIFTED");
334 testConformance(rbUCA
);
338 #endif /* #if !UCONFIG_NO_COLLATION */