]>
git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/ucaconf.cpp
1 /********************************************************************
3 * Copyright (c) 2002-2008, 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("[DATA] 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("[DATA] 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];
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;
169 UnicodeString oldS
, newS
;
172 while (fgets(lineB
, 1024, testFile
) != NULL
) {
176 if(*lineB
== 0 || strlen(lineB
) < 3 || lineB
[0] == '#') {
179 offset
= u_parseString(lineB
, buffer
, 1024, &first
, &status
);
181 buffer
[offset
++] = 0;
183 resLen
= ucol_getSortKey(coll
, buffer
, buflen
, newSk
, 1024);
185 int32_t res
= 0, cmpres
= 0, cmpres2
= 0;
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
);
192 if(cmpres
!= -cmpres2
) {
193 errln("Compare result not symmetrical on line %i", line
);
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
);
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
);
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
);
222 newSk
= (newSk
== sk1
)?sk2
:sk1
;
225 buffer
= (buffer
== b1
)?b2
:b1
;
229 void UCAConformanceTest::TestTableNonIgnorable(/* par */) {
230 setCollNonIgnorable(UCA
);
231 openTestFile("NON_IGNORABLE");
232 testConformance(UCA
);
235 void UCAConformanceTest::TestTableShifted(/* par */) {
237 openTestFile("SHIFTED");
238 testConformance(UCA
);
241 void UCAConformanceTest::TestRulesNonIgnorable(/* par */) {
244 if(U_SUCCESS(status
)) {
245 setCollNonIgnorable(rbUCA
);
246 openTestFile("NON_IGNORABLE");
247 testConformance(rbUCA
);
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.");
258 if(U_SUCCESS(status
)) {
259 setCollShifted(rbUCA
);
260 openTestFile("SHIFTED");
261 testConformance(rbUCA
);
265 #endif /* #if !UCONFIG_NO_COLLATION */