]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/ucaconf.cpp
1 /********************************************************************
3 * Copyright (c) 2002-2010, 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"
22 UCAConformanceTest::UCAConformanceTest() :
27 UCA
= ucol_open("root", &status
);
28 if(U_FAILURE(status
)) {
29 errln("ERROR - UCAConformanceTest: Unable to open UCA collator!");
32 const char *srcDir
= IntlTest::getSourceTestData(status
);
33 if (U_FAILURE(status
)) {
34 dataerrln("Could not open test data %s", u_errorName(status
));
37 uprv_strcpy(testDataPath
, srcDir
);
38 uprv_strcat(testDataPath
, "CollationTest_");
41 UCAConformanceTest::~UCAConformanceTest()
52 void UCAConformanceTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par */)
54 if (exec
) logln("TestSuite UCAConformanceTest: ");
55 if(U_SUCCESS(status
)) {
57 case 0: name
= "TestTableNonIgnorable"; if (exec
) TestTableNonIgnorable(/* par */); break;
58 case 1: name
= "TestTableShifted"; if (exec
) TestTableShifted(/* par */); break;
59 case 2: name
= "TestRulesNonIgnorable"; if (exec
) TestRulesNonIgnorable(/* par */); break;
60 case 3: name
= "TestRulesShifted"; if (exec
) TestRulesShifted(/* par */); break;
61 default: name
= ""; break;
68 void UCAConformanceTest::initRbUCA()
71 UParseError parseError
;
74 int32_t size
= ucol_getRulesEx(UCA
, UCOL_FULL_RULES
, NULL
, 0);
75 ucarules
= (UChar
*)malloc(size
* sizeof(UChar
));
77 size
= ucol_getRulesEx(UCA
, UCOL_FULL_RULES
, ucarules
, size
);
78 rbUCA
= ucol_openRules(ucarules
, size
, UCOL_DEFAULT
, UCOL_TERTIARY
,
79 &parseError
, &status
);
81 if (U_FAILURE(status
)) {
82 errln("Failure creating UCA rule-based collator: %s", u_errorName(status
));
88 void UCAConformanceTest::setCollNonIgnorable(UCollator
*coll
)
90 ucol_setAttribute(coll
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &status
);
91 ucol_setAttribute(coll
, UCOL_CASE_FIRST
, UCOL_OFF
, &status
);
92 ucol_setAttribute(coll
, UCOL_CASE_LEVEL
, UCOL_OFF
, &status
);
93 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_TERTIARY
, &status
);
94 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_NON_IGNORABLE
, &status
);
97 void UCAConformanceTest::setCollShifted(UCollator
*coll
)
99 ucol_setAttribute(coll
, UCOL_NORMALIZATION_MODE
, UCOL_ON
, &status
);
100 ucol_setAttribute(coll
, UCOL_CASE_FIRST
, UCOL_OFF
, &status
);
101 ucol_setAttribute(coll
, UCOL_CASE_LEVEL
, UCOL_OFF
, &status
);
102 ucol_setAttribute(coll
, UCOL_STRENGTH
, UCOL_QUATERNARY
, &status
);
103 ucol_setAttribute(coll
, UCOL_ALTERNATE_HANDLING
, UCOL_SHIFTED
, &status
);
106 void UCAConformanceTest::openTestFile(const char *type
)
108 const char *ext
= ".txt";
113 uprv_strcpy(buffer
, testDataPath
);
114 uprv_strcat(buffer
, type
);
115 int32_t bufLen
= (int32_t)uprv_strlen(buffer
);
117 // we try to open 3 files:
118 // path/CollationTest_type.txt
119 // path/CollationTest_type_SHORT.txt
120 // path/CollationTest_type_STUB.txt
121 // we are going to test with the first one that we manage to open.
123 uprv_strcpy(buffer
+bufLen
, ext
);
125 testFile
= fopen(buffer
, "rb");
128 uprv_strcpy(buffer
+bufLen
, "_SHORT");
129 uprv_strcat(buffer
, ext
);
130 testFile
= fopen(buffer
, "rb");
133 uprv_strcpy(buffer
+bufLen
, "_STUB");
134 uprv_strcat(buffer
, ext
);
135 testFile
= fopen(buffer
, "rb");
138 *(buffer
+bufLen
) = 0;
139 dataerrln("Could not open any of the conformance test files, tried opening base %s\n", buffer
);
143 "INFO: Working with the stub file.\n"
144 "If you need the full conformance test, please\n"
145 "download the appropriate data files from:\n"
146 "http://source.icu-project.org/repos/icu/tools/trunk/unicodetools/com/ibm/text/data/");
152 void UCAConformanceTest::testConformance(UCollator
*coll
)
160 UChar b1
[1024], b2
[1024];
161 UChar
*buffer
= b1
, *oldB
= NULL
;
163 char lineB1
[1024], lineB2
[1024];
164 char *lineB
= lineB1
, *oldLineB
= lineB2
;
166 uint8_t sk1
[1024], sk2
[1024];
167 uint8_t *oldSk
= NULL
, *newSk
= sk1
;
169 int32_t resLen
= 0, oldLen
= 0;
170 int32_t buflen
= 0, oldBlen
= 0;
173 UnicodeString oldS
, newS
;
176 while (fgets(lineB
, 1024, testFile
) != NULL
) {
177 // remove trailing whitespace
182 if(*lineB
== 0 || strlen(lineB
) < 3 || lineB
[0] == '#') {
185 offset
= u_parseString(lineB
, buffer
, 1024, &first
, &status
);
186 if(U_FAILURE(status
)) {
187 errln("Error parsing line %ld (%s): %s\n",
188 (long)line
, u_errorName(status
), lineB
);
189 status
= U_ZERO_ERROR
;
192 buffer
[offset
++] = 0;
194 resLen
= ucol_getSortKey(coll
, buffer
, buflen
, newSk
, 1024);
196 int32_t res
= 0, cmpres
= 0, cmpres2
= 0;
199 res
= strcmp((char *)oldSk
, (char *)newSk
);
200 cmpres
= ucol_strcoll(coll
, oldB
, oldBlen
, buffer
, buflen
);
201 cmpres2
= ucol_strcoll(coll
, buffer
, buflen
, oldB
, oldBlen
);
203 if(cmpres
!= -cmpres2
) {
204 errln("Compare result not symmetrical on line %i", line
);
207 if(((res
&0x80000000) != (cmpres
&0x80000000)) || (res
== 0 && cmpres
!= 0) || (res
!= 0 && cmpres
== 0)) {
208 errln("Difference between ucol_strcoll and sortkey compare on line %i", line
);
209 errln(" Previous data line %s", oldLineB
);
210 errln(" Current data line %s", lineB
);
214 errln("Line %i is not greater or equal than previous line", line
);
215 errln(" Previous data line %s", oldLineB
);
216 errln(" Current data line %s", lineB
);
217 prettify(CollationKey(oldSk
, oldLen
), oldS
);
218 prettify(CollationKey(newSk
, resLen
), newS
);
219 errln(" Previous key: "+oldS
);
220 errln(" Current key: "+newS
);
221 } else if(res
== 0) { /* equal */
222 res
= u_strcmpCodePointOrder(oldB
, buffer
);
224 errln("Probable error in test file on line %i (comparing identical strings)", line
);
225 errln(" Data line %s", lineB
);
228 * UCA 6.0 test files can have lines that compare == if they are
229 * different strings but canonically equivalent.
231 errln("Sortkeys are identical, but code point compare gives >0 on line %i", line);
232 errln(" Previous data line %s", oldLineB);
233 errln(" Current data line %s", lineB);
243 if(lineB
== lineB1
) {
257 void UCAConformanceTest::TestTableNonIgnorable(/* par */) {
258 setCollNonIgnorable(UCA
);
259 openTestFile("NON_IGNORABLE");
260 testConformance(UCA
);
263 void UCAConformanceTest::TestTableShifted(/* par */) {
265 openTestFile("SHIFTED");
266 testConformance(UCA
);
269 void UCAConformanceTest::TestRulesNonIgnorable(/* par */) {
272 if(U_SUCCESS(status
)) {
273 setCollNonIgnorable(rbUCA
);
274 openTestFile("NON_IGNORABLE");
275 testConformance(rbUCA
);
279 void UCAConformanceTest::TestRulesShifted(/* par */) {
280 logln("This test is currently disabled, as it is impossible to "
281 "wholly represent fractional UCA using tailoring rules.");
286 if(U_SUCCESS(status
)) {
287 setCollShifted(rbUCA
);
288 openTestFile("SHIFTED");
289 testConformance(rbUCA
);
293 #endif /* #if !UCONFIG_NO_COLLATION */