]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/ucaconf.cpp
1 /********************************************************************
3 * Copyright (c) 2002-2014, 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/sortkey.h"
19 #include "unicode/tblcoll.h"
20 #include "unicode/ustring.h"
25 UCAConformanceTest::UCAConformanceTest() :
30 UCA
= (RuleBasedCollator
*)Collator::createInstance(Locale::getRoot(), status
);
31 if(U_FAILURE(status
)) {
32 dataerrln("Error - UCAConformanceTest: Unable to open UCA collator! - %s", u_errorName(status
));
35 const char *srcDir
= IntlTest::getSourceTestData(status
);
36 if (U_FAILURE(status
)) {
37 dataerrln("Could not open test data %s", u_errorName(status
));
40 uprv_strcpy(testDataPath
, srcDir
);
41 uprv_strcat(testDataPath
, "CollationTest_");
43 UVersionInfo uniVersion
;
44 static const UVersionInfo v62
= { 6, 2, 0, 0 };
45 u_getUnicodeVersion(uniVersion
);
46 isAtLeastUCA62
= uprv_memcmp(uniVersion
, v62
, 4) >= 0;
49 UCAConformanceTest::~UCAConformanceTest()
58 void UCAConformanceTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par */)
61 logln("TestSuite UCAConformanceTest: ");
64 TESTCASE_AUTO(TestTableNonIgnorable
);
65 TESTCASE_AUTO(TestTableShifted
);
66 TESTCASE_AUTO(TestRulesNonIgnorable
);
67 TESTCASE_AUTO(TestRulesShifted
);
71 void UCAConformanceTest::initRbUCA()
75 UnicodeString ucarules
;
76 UCA
->getRules(UCOL_FULL_RULES
, ucarules
);
77 rbUCA
= new RuleBasedCollator(ucarules
, status
);
78 if (U_FAILURE(status
)) {
79 dataerrln("Failure creating UCA rule-based collator: %s", u_errorName(status
));
83 dataerrln("Failure creating UCA rule-based collator: %s", u_errorName(status
));
89 void UCAConformanceTest::setCollNonIgnorable(Collator
*coll
)
91 coll
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_ON
, status
);
92 coll
->setAttribute(UCOL_CASE_FIRST
, UCOL_OFF
, status
);
93 coll
->setAttribute(UCOL_CASE_LEVEL
, UCOL_OFF
, status
);
94 coll
->setAttribute(UCOL_STRENGTH
, isAtLeastUCA62
? UCOL_IDENTICAL
: UCOL_TERTIARY
, status
);
95 coll
->setAttribute(UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
, status
);
98 void UCAConformanceTest::setCollShifted(Collator
*coll
)
100 coll
->setAttribute(UCOL_NORMALIZATION_MODE
, UCOL_ON
, status
);
101 coll
->setAttribute(UCOL_CASE_FIRST
, UCOL_OFF
, status
);
102 coll
->setAttribute(UCOL_CASE_LEVEL
, UCOL_OFF
, status
);
103 coll
->setAttribute(UCOL_STRENGTH
, isAtLeastUCA62
? UCOL_IDENTICAL
: UCOL_QUATERNARY
, status
);
104 coll
->setAttribute(UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, status
);
107 void UCAConformanceTest::openTestFile(const char *type
)
109 const char *ext
= ".txt";
114 uprv_strcpy(buffer
, testDataPath
);
115 uprv_strcat(buffer
, type
);
116 int32_t bufLen
= (int32_t)uprv_strlen(buffer
);
118 // we try to open 3 files:
119 // path/CollationTest_type.txt
120 // path/CollationTest_type_SHORT.txt
121 // path/CollationTest_type_STUB.txt
122 // we are going to test with the first one that we manage to open.
124 uprv_strcpy(buffer
+bufLen
, ext
);
126 testFile
= fopen(buffer
, "rb");
129 uprv_strcpy(buffer
+bufLen
, "_SHORT");
130 uprv_strcat(buffer
, ext
);
131 testFile
= fopen(buffer
, "rb");
134 uprv_strcpy(buffer
+bufLen
, "_STUB");
135 uprv_strcat(buffer
, ext
);
136 testFile
= fopen(buffer
, "rb");
139 *(buffer
+bufLen
) = 0;
140 dataerrln("Could not open any of the conformance test files, tried opening base %s\n", buffer
);
144 "INFO: Working with the stub file.\n"
145 "If you need the full conformance test, please\n"
146 "download the appropriate data files from:\n"
147 "http://unicode.org/cldr/trac/browser/trunk/common/uca");
153 static const uint32_t IS_SHIFTED
= 1;
154 static const uint32_t FROM_RULES
= 2;
157 skipLineBecauseOfBug(const UChar
*s
, int32_t length
, uint32_t flags
) {
158 // Add temporary exceptions here if there are ICU bugs, until we can fix them.
159 // For examples see the ICU 52 version of this file.
166 static UCollationResult
167 normalizeResult(int32_t result
) {
168 return result
<0 ? UCOL_LESS
: result
==0 ? UCOL_EQUAL
: UCOL_GREATER
;
171 void UCAConformanceTest::testConformance(const Collator
*coll
)
176 uint32_t skipFlags
= 0;
177 if(coll
->getAttribute(UCOL_ALTERNATE_HANDLING
, status
) == UCOL_SHIFTED
) {
178 skipFlags
|= IS_SHIFTED
;
181 skipFlags
|= FROM_RULES
;
184 logln("-prop:ucaconfnosortkeys=1 turns off getSortKey() in UCAConformanceTest");
185 UBool withSortKeys
= getProperty("ucaconfnosortkeys") == NULL
;
189 UChar b1
[1024], b2
[1024];
190 UChar
*buffer
= b1
, *oldB
= NULL
;
192 char lineB1
[1024], lineB2
[1024];
193 char *lineB
= lineB1
, *oldLineB
= lineB2
;
195 uint8_t sk1
[1024], sk2
[1024];
196 uint8_t *oldSk
= NULL
, *newSk
= sk1
;
202 while (fgets(lineB
, 1024, testFile
) != NULL
) {
203 // remove trailing whitespace
207 if(*lineB
== 0 || lineB
[0] == '#') {
210 int32_t buflen
= u_parseString(lineB
, buffer
, 1024, &first
, &status
);
211 if(U_FAILURE(status
)) {
212 errln("Error parsing line %ld (%s): %s\n",
213 (long)line
, u_errorName(status
), lineB
);
214 status
= U_ZERO_ERROR
;
218 if(skipLineBecauseOfBug(buffer
, buflen
, skipFlags
)) {
219 logln("Skipping line %i because of a known bug", line
);
223 int32_t resLen
= withSortKeys
? coll
->getSortKey(buffer
, buflen
, newSk
, 1024) : 0;
227 int32_t skres
= withSortKeys
? strcmp((char *)oldSk
, (char *)newSk
) : 0;
228 int32_t cmpres
= coll
->compare(oldB
, oldBlen
, buffer
, buflen
, status
);
229 int32_t cmpres2
= coll
->compare(buffer
, buflen
, oldB
, oldBlen
, status
);
231 if(cmpres
!= -cmpres2
) {
232 errln("Compare result not symmetrical on line %i: "
233 "previous vs. current (%d) / current vs. previous (%d)",
234 line
, cmpres
, cmpres2
);
238 // TODO: Compare with normalization turned off if the input passes the FCD test.
240 if(withSortKeys
&& cmpres
!= normalizeResult(skres
)) {
241 errln("Difference between coll->compare (%d) and sortkey compare (%d) on line %i",
242 cmpres
, skres
, line
);
246 int32_t res
= cmpres
;
247 if(res
== 0 && !isAtLeastUCA62
) {
248 // Up to UCA 6.1, the collation test files use a custom tie-breaker,
249 // comparing the raw input strings.
250 res
= u_strcmpCodePointOrder(oldB
, buffer
);
251 // Starting with UCA 6.2, the collation test files use the standard UCA tie-breaker,
252 // comparing the NFD versions of the input strings,
253 // which we do via setting strength=identical.
256 errln("Line %i is not greater or equal than previous line", line
);
261 errln(" Previous data line %s", oldLineB
);
262 errln(" Current data line %s", lineB
);
264 UnicodeString oldS
, newS
;
265 prettify(CollationKey(oldSk
, oldLen
), oldS
);
266 prettify(CollationKey(newSk
, resLen
), newS
);
267 errln(" Previous key: "+oldS
);
268 errln(" Current key: "+newS
);
277 if(lineB
== lineB1
) {
291 void UCAConformanceTest::TestTableNonIgnorable(/* par */) {
292 if (U_FAILURE(status
)) {
293 dataerrln("Error running UCA Conformance Test: %s", u_errorName(status
));
296 setCollNonIgnorable(UCA
);
297 openTestFile("NON_IGNORABLE");
298 testConformance(UCA
);
301 void UCAConformanceTest::TestTableShifted(/* par */) {
302 if (U_FAILURE(status
)) {
303 dataerrln("Error running UCA Conformance Test: %s", u_errorName(status
));
307 openTestFile("SHIFTED");
308 testConformance(UCA
);
311 void UCAConformanceTest::TestRulesNonIgnorable(/* par */) {
312 if(logKnownIssue("cldrbug:6745", "UCARules.txt has problems")) { return; }
315 if(U_SUCCESS(status
)) {
316 setCollNonIgnorable(rbUCA
);
317 openTestFile("NON_IGNORABLE");
318 testConformance(rbUCA
);
322 void UCAConformanceTest::TestRulesShifted(/* par */) {
323 logln("This test is currently disabled, as it is impossible to "
324 "wholly represent fractional UCA using tailoring rules.");
329 if(U_SUCCESS(status
)) {
330 setCollShifted(rbUCA
);
331 openTestFile("SHIFTED");
332 testConformance(rbUCA
);
336 #endif /* #if !UCONFIG_NO_COLLATION */