]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/ucaconf.cpp
ICU-6.2.4.tar.gz
[apple/icu.git] / icuSources / test / intltest / ucaconf.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 2002-2004, 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
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
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
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
68 static const int BUFFER_SIZE_ = 330000;
69 static UChar buffer[BUFFER_SIZE_];
70
71 void UCAConformanceTest::initRbUCA()
72 {
73 if(!rbUCA) {
74 UParseError parseError;
75 UChar *ucarules = buffer;
76 int32_t size = ucol_getRulesEx(UCA, UCOL_FULL_RULES, ucarules,
77 BUFFER_SIZE_);
78 if (size > BUFFER_SIZE_) {
79 ucarules = (UChar *)malloc(size * sizeof(UChar));
80 size = ucol_getRulesEx(UCA, UCOL_FULL_RULES, ucarules, size);
81 }
82 rbUCA = ucol_openRules(ucarules, size, UCOL_DEFAULT, UCOL_TERTIARY,
83 &parseError, &status);
84 if (U_FAILURE(status)) {
85 errln("Failure creating UCA rule-based collator: %s", u_errorName(status));
86 return;
87 }
88 }
89 }
90
91 void UCAConformanceTest::setCollNonIgnorable(UCollator *coll)
92 {
93 ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
94 ucol_setAttribute(coll, UCOL_CASE_FIRST, UCOL_OFF, &status);
95 ucol_setAttribute(coll, UCOL_CASE_LEVEL, UCOL_OFF, &status);
96 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_TERTIARY, &status);
97 ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_NON_IGNORABLE, &status);
98 }
99
100 void UCAConformanceTest::setCollShifted(UCollator *coll)
101 {
102 ucol_setAttribute(coll, UCOL_NORMALIZATION_MODE, UCOL_ON, &status);
103 ucol_setAttribute(coll, UCOL_CASE_FIRST, UCOL_OFF, &status);
104 ucol_setAttribute(coll, UCOL_CASE_LEVEL, UCOL_OFF, &status);
105 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_QUATERNARY, &status);
106 ucol_setAttribute(coll, UCOL_ALTERNATE_HANDLING, UCOL_SHIFTED, &status);
107 }
108
109 void UCAConformanceTest::openTestFile(const char *type)
110 {
111 const char *ext = ".txt";
112 if(testFile) {
113 fclose(testFile);
114 }
115 char buffer[1024];
116 uprv_strcpy(buffer, testDataPath);
117 uprv_strcat(buffer, type);
118 int32_t bufLen = (int32_t)uprv_strlen(buffer);
119
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.
125
126 uprv_strcpy(buffer+bufLen, ext);
127
128 testFile = fopen(buffer, "rb");
129
130 if(testFile == 0) {
131 uprv_strcpy(buffer+bufLen, "_SHORT");
132 uprv_strcat(buffer, ext);
133 testFile = fopen(buffer, "rb");
134
135 if(testFile == 0) {
136 uprv_strcpy(buffer+bufLen, "_STUB");
137 uprv_strcat(buffer, ext);
138 testFile = fopen(buffer, "rb");
139
140 if (testFile == 0) {
141 *(buffer+bufLen) = 0;
142 errln("ERROR: could not open any of the conformance test files, tried opening base %s\n", buffer);
143 return;
144 } else {
145 infoln(
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://oss.software.ibm.com/cvs/icu4j/unicodetools/com/ibm/text/data/");
150 }
151 }
152 }
153 }
154
155 void UCAConformanceTest::testConformance(UCollator *coll)
156 {
157 if(testFile == 0) {
158 return;
159 }
160
161 int32_t line = 0;
162
163 UChar b1[1024], b2[1024];
164 char lineB[1024];
165 UChar *buffer = b1, *oldB = NULL;
166 uint8_t sk1[1024], sk2[1024];
167 uint8_t *oldSk = NULL, *newSk = sk1;
168 int32_t resLen = 0, oldLen = 0;
169 int32_t buflen = 0, oldBlen = 0;
170 uint32_t first = 0;
171 uint32_t offset = 0;
172
173
174 while (fgets(lineB, 1024, testFile) != NULL) {
175 offset = 0;
176
177 line++;
178 if(*lineB == 0 || lineB[0] == '#') {
179 continue;
180 }
181 offset = u_parseString(lineB, buffer, 1024, &first, &status);
182 buflen = offset;
183 buffer[offset++] = 0;
184
185 resLen = ucol_getSortKey(coll, buffer, buflen, newSk, 1024);
186
187 int32_t res = 0, cmpres = 0, cmpres2 = 0;
188
189 if(oldSk != NULL) {
190 res = strcmp((char *)oldSk, (char *)newSk);
191 cmpres = ucol_strcoll(coll, oldB, oldBlen, buffer, buflen);
192 cmpres2 = ucol_strcoll(coll, buffer, buflen, oldB, oldBlen);
193
194 if(cmpres != -cmpres2) {
195 errln("Compare result not symmetrical on line %i", line);
196 }
197
198 if(((res&0x80000000) != (cmpres&0x80000000)) || (res == 0 && cmpres != 0) || (res != 0 && cmpres == 0)) {
199 errln("Difference between ucol_strcoll and sortkey compare on line %i", line);
200 logln("Data line %s", lineB);
201 }
202
203 if(res > 0) {
204 errln("Line %i is not greater or equal than previous line", line);
205 logln("Data line %s", lineB);
206 } else if(res == 0) { /* equal */
207 res = u_strcmpCodePointOrder(oldB, buffer);
208 if (res == 0) {
209 errln("Probable error in test file on line %i (comparing identical strings)", line);
210 logln("Data line %s", lineB);
211 } else if (res > 0) {
212 errln("Sortkeys are identical, but code point comapare gives >0 on line %i", line);
213 logln("Data line %s", lineB);
214 }
215 }
216 }
217
218 oldSk = newSk;
219 oldLen = resLen;
220
221 newSk = (newSk == sk1)?sk2:sk1;
222 oldB = buffer;
223 oldBlen = buflen;
224 buffer = (buffer == b1)?b2:b1;
225 }
226 }
227
228 void UCAConformanceTest::TestTableNonIgnorable(/* par */) {
229 setCollNonIgnorable(UCA);
230 openTestFile("NON_IGNORABLE");
231 testConformance(UCA);
232 }
233
234 void UCAConformanceTest::TestTableShifted(/* par */) {
235 setCollShifted(UCA);
236 openTestFile("SHIFTED");
237 testConformance(UCA);
238 }
239
240 void UCAConformanceTest::TestRulesNonIgnorable(/* par */) {
241 initRbUCA();
242
243 if(U_SUCCESS(status)) {
244 setCollNonIgnorable(rbUCA);
245 openTestFile("NON_IGNORABLE");
246 testConformance(rbUCA);
247 }
248 }
249
250 void UCAConformanceTest::TestRulesShifted(/* par */) {
251 logln("This test is currently disabled, as it is impossible to "
252 "wholly represent fractional UCA using tailoring rules.");
253 return;
254
255 initRbUCA();
256
257 if(U_SUCCESS(status)) {
258 setCollShifted(rbUCA);
259 openTestFile("SHIFTED");
260 testConformance(rbUCA);
261 }
262 }
263
264 #endif /* #if !UCONFIG_NO_COLLATION */