]> git.saurik.com Git - apple/icu.git/blob - icuSources/test/intltest/dadrcoll.cpp
ICU-6.2.4.tar.gz
[apple/icu.git] / icuSources / test / intltest / dadrcoll.cpp
1 /********************************************************************
2 * COPYRIGHT:
3 * Copyright (c) 1997-2004, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
6
7 /**
8 * IntlTestCollator is the medium level test class for everything in the directory "collate".
9 */
10
11 /***********************************************************************
12 * Modification history
13 * Date Name Description
14 * 02/14/2001 synwee Compare with cintltst and commented away tests
15 * that are not run.
16 ***********************************************************************/
17
18 #include "unicode/utypes.h"
19
20 #if !UCONFIG_NO_COLLATION
21
22 #include "unicode/uchar.h"
23 #include "unicode/tstdtmod.h"
24 #include "cstring.h"
25 #include "ucol_tok.h"
26 #include "tscoll.h"
27 #include "dadrcoll.h"
28
29 U_CDECL_BEGIN
30 static void U_CALLCONV deleteSeqElement(void *elem) {
31 delete((SeqElement *)elem);
32 }
33 U_CDECL_END
34
35 DataDrivenCollatorTest::DataDrivenCollatorTest()
36 : seq(StringCharacterIterator("")),
37 status(U_ZERO_ERROR),
38 sequences(status)
39 {
40 driver = TestDataModule::getTestDataModule("DataDrivenCollationTest", *this, status);
41 sequences.setDeleter(deleteSeqElement);
42 }
43
44 DataDrivenCollatorTest::~DataDrivenCollatorTest()
45 {
46 delete driver;
47 }
48
49 void DataDrivenCollatorTest::runIndexedTest( int32_t index, UBool exec, const char* &name, char* /*par */)
50 {
51 if(driver != NULL) {
52 if (exec)
53 {
54 logln("TestSuite Collator: ");
55 }
56 const DataMap *info = NULL;
57 TestData *testData = driver->createTestData(index, status);
58 if(U_SUCCESS(status)) {
59 name = testData->getName();
60 if(testData->getInfo(info, status)) {
61 log(info->getString("Description", status));
62 }
63 if(exec) {
64 log(name);
65 logln("---");
66 logln("");
67 processTest(testData);
68 }
69 delete testData;
70 } else {
71 name = "";
72 }
73 } else {
74 errln("collate/DataDrivenTest data not initialized!");
75 name = "";
76 }
77
78
79 }
80
81 UBool
82 DataDrivenCollatorTest::setTestSequence(const UnicodeString &setSequence, SeqElement &el) {
83 seq.setText(setSequence);
84 return getNextInSequence(el);
85 }
86
87 // Parses the sequence to be tested
88 UBool
89 DataDrivenCollatorTest::getNextInSequence(SeqElement &el) {
90 el.source.truncate(0);
91 UBool quoted = FALSE;
92 UBool quotedsingle = FALSE;
93 UChar32 currChar = 0;
94
95 while(currChar != CharacterIterator::DONE) {
96 currChar= seq.next32PostInc();
97 if(!quoted) {
98 if(u_isWhitespace(currChar)) {
99 continue;
100 }
101 switch(currChar) {
102 case CharacterIterator::DONE:
103 break;
104 case 0x003C /* < */:
105 el.relation = Collator::LESS;
106 currChar = CharacterIterator::DONE;
107 break;
108 case 0x003D /* = */:
109 el.relation = Collator::EQUAL;
110 currChar = CharacterIterator::DONE;
111 break;
112 case 0x003E /* > */:
113 el.relation = Collator::GREATER;
114 currChar = CharacterIterator::DONE;
115 break;
116 case 0x0027 /* ' */: /* very basic quoting */
117 quoted = TRUE;
118 quotedsingle = FALSE;
119 break;
120 case 0x005c /* \ */: /* single quote */
121 quoted = TRUE;
122 quotedsingle = TRUE;
123 break;
124 default:
125 el.source.append(currChar);
126 }
127 } else {
128 if(currChar == CharacterIterator::DONE) {
129 status = U_ILLEGAL_ARGUMENT_ERROR;
130 errln("Quote in sequence not closed!");
131 return FALSE;
132 } else if(currChar == 0x0027) {
133 quoted = FALSE;
134 } else {
135 el.source.append(currChar);
136 }
137 if(quotedsingle) {
138 quoted = FALSE;
139 }
140 }
141 }
142 return seq.hasNext();
143 }
144
145 // Reads the options string and sets appropriate attributes in collator
146 void
147 DataDrivenCollatorTest::processArguments(Collator *col, const UChar *start, int32_t optLen) {
148 const UChar *end = start+optLen;
149 UColAttribute attrib;
150 UColAttributeValue value;
151
152 if(optLen == 0) {
153 return;
154 }
155
156 start = ucol_tok_getNextArgument(start, end, &attrib, &value, &status);
157 while(start != NULL) {
158 if(U_SUCCESS(status)) {
159 col->setAttribute(attrib, value, status);
160 }
161 start = ucol_tok_getNextArgument(start, end, &attrib, &value, &status);
162 }
163 }
164
165 void
166 DataDrivenCollatorTest::processTest(TestData *testData) {
167 Collator *col = NULL;
168 const UChar *arguments = NULL;
169 int32_t argLen = 0;
170 const DataMap *settings = NULL;
171 const DataMap *currentCase = NULL;
172 UErrorCode intStatus = U_ZERO_ERROR;
173 UnicodeString testSetting;
174 while(testData->nextSettings(settings, status)) {
175 intStatus = U_ZERO_ERROR;
176 // try to get a locale
177 testSetting = settings->getString("TestLocale", intStatus);
178 if(U_SUCCESS(intStatus)) {
179 char localeName[256];
180 testSetting.extract(0, testSetting.length(), localeName, "");
181 col = Collator::createInstance(localeName, status);
182 if(U_SUCCESS(status)) {
183 logln("Testing collator for locale "+testSetting);
184 } else {
185 errln("Unable to instantiate collator for locale "+testSetting);
186 return;
187 }
188 } else {
189 // if no locale, try from rules
190 intStatus = U_ZERO_ERROR;
191 testSetting = settings->getString("Rules", intStatus);
192 if(U_SUCCESS(intStatus)) {
193 col = new RuleBasedCollator(testSetting, status);
194 if(U_SUCCESS(status)) {
195 logln("Testing collator for rules "+testSetting);
196 } else {
197 errln("Unable to instantiate collator for rules "+testSetting);
198 return;
199 }
200 } else {
201 errln("No collator definition!");
202 }
203 }
204 if(col != NULL) {
205 // get attributes
206 testSetting = settings->getString("Arguments", intStatus);
207 if(U_SUCCESS(intStatus)) {
208 logln("Arguments: "+testSetting);
209 argLen = testSetting.length();
210 arguments = testSetting.getBuffer();
211 processArguments(col, arguments, argLen);
212 if(U_FAILURE(status)) {
213 errln("Couldn't process arguments");
214 break;
215 }
216 } else {
217 intStatus = U_ZERO_ERROR;
218 }
219 // Start the processing
220 while(testData->nextCase(currentCase, status)) {
221 UnicodeString sequence = currentCase->getString("sequence", status);
222 if(U_SUCCESS(status)) {
223 processSequence(col, sequence);
224 }
225 }
226 } else {
227 errln("Couldn't instantiate a collator!");
228 }
229 delete col;
230 col = NULL;
231 }
232 }
233
234 void
235 DataDrivenCollatorTest::processSequence(Collator* col, const UnicodeString &sequence) {
236 Collator::EComparisonResult relation = Collator::EQUAL;
237 UBool hasNext;
238 SeqElement *source = NULL;
239 SeqElement *target = NULL;
240 int32_t j = 0;
241
242 sequences.removeAllElements();
243
244 target = new SeqElement();
245
246 setTestSequence(sequence, *target);
247 sequences.addElement(target, status);
248
249 do {
250 relation = Collator::EQUAL;
251 target = new SeqElement();
252 hasNext = getNextInSequence(*target);
253 for(j = sequences.size(); j > 0; j--) {
254 source = (SeqElement *)sequences.elementAt(j-1);
255 if(relation == Collator::EQUAL && source->relation != Collator::EQUAL) {
256 relation = source->relation;
257 }
258 doTest(col, source->source, target->source, relation);
259 }
260 sequences.addElement(target, status);
261 source = target;
262 } while(hasNext);
263 }
264
265 #endif /* #if !UCONFIG_NO_COLLATION */