1 /********************************************************************
3 * Copyright (c) 1997-2009, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************/
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_COLLATION
11 #include "unicode/coll.h"
12 #include "unicode/tblcoll.h"
13 #include "unicode/unistr.h"
14 #include "unicode/sortkey.h"
23 #define MIN(x,y) ((x) < (y) ? (x) : (y))
27 #define MAX(x,y) ((x) > (y) ? (x) : (y))
30 CollationMonkeyTest::CollationMonkeyTest()
31 : source("-abcdefghijklmnopqrstuvwxyz#&^$@", ""),
34 UErrorCode status
= U_ZERO_ERROR
;
35 myCollator
= Collator::createInstance("en_US", status
);
38 CollationMonkeyTest::~CollationMonkeyTest()
45 CollationMonkeyTest::report(UnicodeString
& s
, UnicodeString
& t
, int32_t result
, int32_t revResult
)
47 if (revResult
!= -result
)
54 msg
+= " round trip comparison failed";
55 msg
+= (UnicodeString
) " (result " + result
+ ", reverse Result " + revResult
+ ")";
62 CollationMonkeyTest::checkValue(int32_t value
)
72 void CollationMonkeyTest::TestCollationKey(/* char* par */)
74 if(source
.length() == 0) {
75 errln(UNICODE_STRING("CollationMonkeyTest::TestCollationKey(): source is empty - ICU_DATA not set or data missing?", 92));
79 srand( (unsigned)time( NULL
) );
80 int32_t s
= checkValue(rand() % source
.length());
81 int32_t t
= checkValue(rand() % source
.length());
82 int32_t slen
= checkValue((rand() - source
.length()) % source
.length());
83 int32_t tlen
= checkValue((rand() - source
.length()) % source
.length());
84 UnicodeString subs
, subt
;
86 source
.extract(MIN(s
, slen
), MAX(s
, slen
), subs
);
87 source
.extract(MIN(t
, tlen
), MAX(t
, tlen
), subt
);
89 CollationKey collationKey1
, collationKey2
;
90 UErrorCode status1
= U_ZERO_ERROR
, status2
= U_ZERO_ERROR
;
92 myCollator
->setStrength(Collator::TERTIARY
);
93 myCollator
->getCollationKey(subs
, collationKey1
, status1
);
94 myCollator
->getCollationKey(subt
, collationKey2
, status2
);
95 int32_t result
= collationKey1
.compareTo(collationKey2
); // Tertiary
96 int32_t revResult
= collationKey2
.compareTo(collationKey1
); // Tertiary
97 report( subs
, subt
, result
, revResult
);
99 myCollator
->setStrength(Collator::SECONDARY
);
100 myCollator
->getCollationKey(subs
, collationKey1
, status1
);
101 myCollator
->getCollationKey(subt
, collationKey2
, status2
);
102 result
= collationKey1
.compareTo(collationKey2
); // Secondary
103 revResult
= collationKey2
.compareTo(collationKey1
); // Secondary
104 report( subs
, subt
, result
, revResult
);
106 myCollator
->setStrength(Collator::PRIMARY
);
107 myCollator
->getCollationKey(subs
, collationKey1
, status1
);
108 myCollator
->getCollationKey(subt
, collationKey2
, status2
);
109 result
= collationKey1
.compareTo(collationKey2
); // Primary
110 revResult
= collationKey2
.compareTo(collationKey1
); // Primary
111 report(subs
, subt
, result
, revResult
);
114 UnicodeString
addOne(subs
);
115 addOne
+= (UChar32
)0xE000;
117 myCollator
->getCollationKey(subs
, collationKey1
, status1
);
118 myCollator
->getCollationKey(addOne
, collationKey2
, status2
);
119 result
= collationKey1
.compareTo(collationKey2
);
122 msg
+= "CollationKey(";
124 msg
+= ") .LT. CollationKey(";
131 result
= collationKey2
.compareTo(collationKey1
);
134 msg
+= "CollationKey(";
136 msg
+= ") .GT. CollationKey(";
144 CollationMonkeyTest::TestCompare(/* char* par */)
146 if(source
.length() == 0) {
147 errln(UNICODE_STRING("CollationMonkeyTest::TestCompare(): source is empty - ICU_DATA not set or data missing?", 87));
151 /* Seed the random-number generator with current time so that
152 * the numbers will be different every time we run.
154 srand( (unsigned)time( NULL
) );
155 int32_t s
= checkValue(rand() % source
.length());
156 int32_t t
= checkValue(rand() % source
.length());
157 int32_t slen
= checkValue((rand() - source
.length()) % source
.length());
158 int32_t tlen
= checkValue((rand() - source
.length()) % source
.length());
159 UnicodeString subs
, subt
;
161 source
.extract(MIN(s
, slen
), MAX(s
, slen
), subs
);
162 source
.extract(MIN(t
, tlen
), MAX(t
, tlen
), subt
);
164 myCollator
->setStrength(Collator::TERTIARY
);
165 int32_t result
= myCollator
->compare(subs
, subt
); // Tertiary
166 int32_t revResult
= myCollator
->compare(subt
, subs
); // Tertiary
167 report(subs
, subt
, result
, revResult
);
169 myCollator
->setStrength(Collator::SECONDARY
);
170 result
= myCollator
->compare(subs
, subt
); // Secondary
171 revResult
= myCollator
->compare(subt
, subs
); // Secondary
172 report(subs
, subt
, result
, revResult
);
174 myCollator
->setStrength(Collator::PRIMARY
);
175 result
= myCollator
->compare(subs
, subt
); // Primary
176 revResult
= myCollator
->compare(subt
, subs
); // Primary
177 report(subs
, subt
, result
, revResult
);
180 UnicodeString
addOne(subs
);
181 addOne
+= (UChar32
)0xE000;
183 result
= myCollator
->compare(subs
, addOne
);
195 result
= myCollator
->compare(addOne
, subs
);
206 void CollationMonkeyTest::TestRules(/* char* par */){
207 UChar testSourceCases
[][10] = {
208 {0x0061, 0x0062, 0x007a, 0},
209 {0x0061, 0x0062, 0x007a, 0},
212 UChar testTargetCases
[][10] = {
213 {0x0061, 0x0062, 0x00e4, 0},
214 {0x0061, 0x0062, 0x0061, 0x0308, 0},
218 logln("Demo Test 1 : Create a new table collation with rules \"& z < 0x00e4\"");
219 UErrorCode status
= U_ZERO_ERROR
;
220 Collator
*col
= Collator::createInstance("en_US", status
);
221 const UnicodeString baseRules
= ((RuleBasedCollator
*)col
)->getRules();
222 UnicodeString
newRules(" & z < ");
223 newRules
.append((UChar
)0x00e4);
224 newRules
.insert(0, baseRules
);
225 RuleBasedCollator
*myCollation
= new RuleBasedCollator(newRules
, status
);
226 if (U_FAILURE(status
)) {
227 errln( "Demo Test 1 Table Collation object creation failed.");
231 doTest(myCollation
, testSourceCases
[i
], testTargetCases
[i
], Collator::LESS
);
235 logln("Demo Test 2 : Create a new table collation with rules \"& z < a 0x0308\"");
237 newRules
.append(" & z < a");
238 newRules
.append((UChar
)0x0308);
239 newRules
.insert(0, baseRules
);
240 myCollation
= new RuleBasedCollator(newRules
, status
);
241 if (U_FAILURE(status
)) {
242 errln( "Demo Test 1 Table Collation object creation failed.");
246 doTest(myCollation
, testSourceCases
[i
], testTargetCases
[i
], Collator::LESS
);
253 void CollationMonkeyTest::runIndexedTest( int32_t index
, UBool exec
, const char* &name
, char* /*par*/ )
255 if (exec
) logln("TestSuite CollationMonkeyTest: ");
258 case 0: name
= "TestCompare"; if (exec
) TestCompare(/* par */); break;
259 case 1: name
= "TestCollationKey"; if (exec
) TestCollationKey(/* par */); break;
260 case 2: name
= "TestRules"; if (exec
) TestRules(/* par */); break;
261 default: name
= ""; break;
264 dataerrln("Class collator not instantiated");
269 #endif /* #if !UCONFIG_NO_COLLATION */