1 /********************************************************************
3 * Copyright (c) 1997-2004, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
8 * IntlTestCollator is the medium level test class for everything in the directory "collate".
11 /***********************************************************************
12 * Modification history
13 * Date Name Description
14 * 02/14/2001 synwee Compare with cintltst and commented away tests
16 ***********************************************************************/
18 #include "unicode/utypes.h"
20 #if !UCONFIG_NO_COLLATION
22 #include "unicode/uchar.h"
23 #include "unicode/tstdtmod.h"
30 static void U_CALLCONV
deleteSeqElement(void *elem
) {
31 delete((SeqElement
*)elem
);
35 DataDrivenCollatorTest::DataDrivenCollatorTest()
36 : seq(StringCharacterIterator("")),
40 driver
= TestDataModule::getTestDataModule("DataDrivenCollationTest", *this, status
);
41 sequences
.setDeleter(deleteSeqElement
);
44 DataDrivenCollatorTest::~DataDrivenCollatorTest()
49 void DataDrivenCollatorTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par */)
54 logln("TestSuite Collator: ");
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
));
67 processTest(testData
);
74 errln("collate/DataDrivenTest data not initialized!");
82 DataDrivenCollatorTest::setTestSequence(const UnicodeString
&setSequence
, SeqElement
&el
) {
83 seq
.setText(setSequence
);
84 return getNextInSequence(el
);
87 // Parses the sequence to be tested
89 DataDrivenCollatorTest::getNextInSequence(SeqElement
&el
) {
90 el
.source
.truncate(0);
92 UBool quotedsingle
= FALSE
;
95 while(currChar
!= CharacterIterator::DONE
) {
96 currChar
= seq
.next32PostInc();
98 if(u_isWhitespace(currChar
)) {
102 case CharacterIterator::DONE
:
105 el
.relation
= Collator::LESS
;
106 currChar
= CharacterIterator::DONE
;
109 el
.relation
= Collator::EQUAL
;
110 currChar
= CharacterIterator::DONE
;
113 el
.relation
= Collator::GREATER
;
114 currChar
= CharacterIterator::DONE
;
116 case 0x0027 /* ' */: /* very basic quoting */
118 quotedsingle
= FALSE
;
120 case 0x005c /* \ */: /* single quote */
125 el
.source
.append(currChar
);
128 if(currChar
== CharacterIterator::DONE
) {
129 status
= U_ILLEGAL_ARGUMENT_ERROR
;
130 errln("Quote in sequence not closed!");
132 } else if(currChar
== 0x0027) {
135 el
.source
.append(currChar
);
142 return seq
.hasNext();
145 // Reads the options string and sets appropriate attributes in collator
147 DataDrivenCollatorTest::processArguments(Collator
*col
, const UChar
*start
, int32_t optLen
) {
148 const UChar
*end
= start
+optLen
;
149 UColAttribute attrib
;
150 UColAttributeValue value
;
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
);
161 start
= ucol_tok_getNextArgument(start
, end
, &attrib
, &value
, &status
);
166 DataDrivenCollatorTest::processTest(TestData
*testData
) {
167 Collator
*col
= NULL
;
168 const UChar
*arguments
= NULL
;
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
);
185 errln("Unable to instantiate collator for locale "+testSetting
);
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
);
197 errln("Unable to instantiate collator for rules "+testSetting
);
201 errln("No collator definition!");
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");
217 intStatus
= U_ZERO_ERROR
;
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
);
227 errln("Couldn't instantiate a collator!");
235 DataDrivenCollatorTest::processSequence(Collator
* col
, const UnicodeString
&sequence
) {
236 Collator::EComparisonResult relation
= Collator::EQUAL
;
238 SeqElement
*source
= NULL
;
239 SeqElement
*target
= NULL
;
242 sequences
.removeAllElements();
244 target
= new SeqElement();
246 setTestSequence(sequence
, *target
);
247 sequences
.addElement(target
, status
);
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
;
258 doTest(col
, source
->source
, target
->source
, relation
);
260 sequences
.addElement(target
, status
);
265 #endif /* #if !UCONFIG_NO_COLLATION */