1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 2002-2007, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7 /**
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.
11 */
12
13 #include "unicode/utypes.h"
14
15 #if !UCONFIG_NO_COLLATION
16
17 #include "ucaconf.h"
18 #include "unicode/ustring.h"
19 #include "cstring.h"
20 #include "uparse.h"
21
UCAConformanceTest()22 UCAConformanceTest::UCAConformanceTest() :
23 rbUCA(NULL),
24 testFile(NULL),
25 status(U_ZERO_ERROR)
26 {
27 UCA = ucol_open("root", &status);
28 if(U_FAILURE(status)) {
29 errln("ERROR - UCAConformanceTest: Unable to open UCA collator!");
30 }
31
32 const char *srcDir = IntlTest::getSourceTestData(status);
33 if (U_FAILURE(status)) {
34 errln("ERROR: could not open test data %s", u_errorName(status));
35 return;
36 }
37 uprv_strcpy(testDataPath, srcDir);
38 uprv_strcat(testDataPath, "CollationTest_");
39 }
40
~UCAConformanceTest()41 UCAConformanceTest::~UCAConformanceTest()
42 {
43 ucol_close(UCA);
44 if(rbUCA) {
45 ucol_close(rbUCA);
46 }
47 if(testFile) {
48 fclose(testFile);
49 }
50 }
51
runIndexedTest(int32_t index,UBool exec,const char * & name,char *)52 void UCAConformanceTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par */)
53 {
54 if (exec) logln("TestSuite UCAConformanceTest: ");
55 if(U_SUCCESS(status)) {
56 switch (index) {
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;
62 }
63 } else {
64 name = "";
65 }
66 }
67
initRbUCA()68 void UCAConformanceTest::initRbUCA()
69 {
70 if(!rbUCA) {
71 UParseError parseError;
72 UChar *ucarules;
73 // preflight rules
74 int32_t size = ucol_getRulesEx(UCA, UCOL_FULL_RULES, NULL, 0);
75 ucarules = (UChar *)malloc(size * sizeof(UChar));
76
77 size = ucol_getRulesEx(UCA, UCOL_FULL_RULES, ucarules, size);
78 rbUCA = ucol_openRules(ucarules, size, UCOL_DEFAULT, UCOL_TERTIARY,
79 &parseError, &status);
80 free(ucarules);
81 if (U_FAILURE(status)) {
82 errln("Failure creating UCA rule-based collator: %s", u_errorName(status));
83 return;
84 }
85 }
86 }
87
setCollNonIgnorable(UCollator * coll)88 void UCAConformanceTest::setCollNonIgnorable(UCollator *coll)
89 {
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);
95 }
96
setCollShifted(UCollator * coll)97 void UCAConformanceTest::setCollShifted(UCollator *coll)
98 {
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);
104 }
105
openTestFile(const char * type)106 void UCAConformanceTest::openTestFile(const char *type)
107 {
108 const char *ext = ".txt";
109 if(testFile) {
110 fclose(testFile);
111 }
112 char buffer[1024];
113 uprv_strcpy(buffer, testDataPath);
114 uprv_strcat(buffer, type);
115 int32_t bufLen = (int32_t)uprv_strlen(buffer);
116
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.
122
123 uprv_strcpy(buffer+bufLen, ext);
124
125 testFile = fopen(buffer, "rb");
126
127 if(testFile == 0) {
128 uprv_strcpy(buffer+bufLen, "_SHORT");
129 uprv_strcat(buffer, ext);
130 testFile = fopen(buffer, "rb");
131
132 if(testFile == 0) {
133 uprv_strcpy(buffer+bufLen, "_STUB");
134 uprv_strcat(buffer, ext);
135 testFile = fopen(buffer, "rb");
136
137 if (testFile == 0) {
138 *(buffer+bufLen) = 0;
139 errln("ERROR: could not open any of the conformance test files, tried opening base %s\n", buffer);
140 return;
141 } else {
142 infoln(
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/");
147 }
148 }
149 }
150 }
151
testConformance(UCollator * coll)152 void UCAConformanceTest::testConformance(UCollator *coll)
153 {
154 if(testFile == 0) {
155 return;
156 }
157
158 int32_t line = 0;
159
160 UChar b1[1024], b2[1024];
161 char lineB[1024];
162 UChar *buffer = b1, *oldB = NULL;
163 uint8_t sk1[1024], sk2[1024];
164 uint8_t *oldSk = NULL, *newSk = sk1;
165 int32_t resLen = 0, oldLen = 0;
166 int32_t buflen = 0, oldBlen = 0;
167 uint32_t first = 0;
168 uint32_t offset = 0;
169 UnicodeString oldS, newS;
170
171
172 while (fgets(lineB, 1024, testFile) != NULL) {
173 offset = 0;
174
175 line++;
176 if(*lineB == 0 || strlen(lineB) < 3 || lineB[0] == '#') {
177 continue;
178 }
179 offset = u_parseString(lineB, buffer, 1024, &first, &status);
180 buflen = offset;
181 buffer[offset++] = 0;
182
183 resLen = ucol_getSortKey(coll, buffer, buflen, newSk, 1024);
184
185 int32_t res = 0, cmpres = 0, cmpres2 = 0;
186
187 if(oldSk != NULL) {
188 res = strcmp((char *)oldSk, (char *)newSk);
189 cmpres = ucol_strcoll(coll, oldB, oldBlen, buffer, buflen);
190 cmpres2 = ucol_strcoll(coll, buffer, buflen, oldB, oldBlen);
191
192 if(cmpres != -cmpres2) {
193 errln("Compare result not symmetrical on line %i", line);
194 }
195
196 if(((res&0x80000000) != (cmpres&0x80000000)) || (res == 0 && cmpres != 0) || (res != 0 && cmpres == 0)) {
197 errln("Difference between ucol_strcoll and sortkey compare on line %i", line);
198 logln("Data line %s", lineB);
199 }
200
201 if(res > 0) {
202 errln("Line %i is not greater or equal than previous line", line);
203 logln("Data line %s", lineB);
204 prettify(CollationKey(oldSk, oldLen), oldS);
205 prettify(CollationKey(newSk, resLen), newS);
206 logln("Keys: "+oldS+" and "+newS);
207 } else if(res == 0) { /* equal */
208 res = u_strcmpCodePointOrder(oldB, buffer);
209 if (res == 0) {
210 errln("Probable error in test file on line %i (comparing identical strings)", line);
211 logln("Data line %s", lineB);
212 } else if (res > 0) {
213 errln("Sortkeys are identical, but code point comapare gives >0 on line %i", line);
214 logln("Data line %s", lineB);
215 }
216 }
217 }
218
219 oldSk = newSk;
220 oldLen = resLen;
221
222 newSk = (newSk == sk1)?sk2:sk1;
223 oldB = buffer;
224 oldBlen = buflen;
225 buffer = (buffer == b1)?b2:b1;
226 }
227 }
228
TestTableNonIgnorable()229 void UCAConformanceTest::TestTableNonIgnorable(/* par */) {
230 setCollNonIgnorable(UCA);
231 openTestFile("NON_IGNORABLE");
232 testConformance(UCA);
233 }
234
TestTableShifted()235 void UCAConformanceTest::TestTableShifted(/* par */) {
236 setCollShifted(UCA);
237 openTestFile("SHIFTED");
238 testConformance(UCA);
239 }
240
TestRulesNonIgnorable()241 void UCAConformanceTest::TestRulesNonIgnorable(/* par */) {
242 initRbUCA();
243
244 if(U_SUCCESS(status)) {
245 setCollNonIgnorable(rbUCA);
246 openTestFile("NON_IGNORABLE");
247 testConformance(rbUCA);
248 }
249 }
250
TestRulesShifted()251 void UCAConformanceTest::TestRulesShifted(/* par */) {
252 logln("This test is currently disabled, as it is impossible to "
253 "wholly represent fractional UCA using tailoring rules.");
254 return;
255
256 initRbUCA();
257
258 if(U_SUCCESS(status)) {
259 setCollShifted(rbUCA);
260 openTestFile("SHIFTED");
261 testConformance(rbUCA);
262 }
263 }
264
265 #endif /* #if !UCONFIG_NO_COLLATION */
266