1 /********************************************************************
3 * Copyright (c) 2002-2004, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 ********************************************************************
7 * @author Mark E. Davis
8 * @author Vladimir Weinstein
11 #include "unicode/utypes.h"
13 #if !UCONFIG_NO_NORMALIZATION
18 #include "unicode/caniter.h"
19 #include "unicode/normlzr.h"
20 #include "unicode/uchar.h"
23 #define ARRAY_LENGTH(array) ((int32_t)(sizeof (array) / sizeof (*array)))
25 #define CASE(id,test) case id: \
29 logln((UnicodeString)""); \
34 void CanonicalIteratorTest::runIndexedTest(int32_t index
, UBool exec
,
35 const char* &name
, char* /*par*/) {
38 CASE(1, TestExhaustive
);
40 default: name
= ""; break;
45 * Convert Java-style strings with \u Unicode escapes into UnicodeString objects
46 static UnicodeString str(const char *input)
48 UnicodeString str(input, ""); // Invariant conversion
49 return str.unescape();
54 CanonicalIteratorTest::CanonicalIteratorTest() :
55 nameTrans(NULL
), hexTrans(NULL
)
59 CanonicalIteratorTest::~CanonicalIteratorTest()
61 #if !UCONFIG_NO_TRANSLITERATION
62 if(nameTrans
!= NULL
) {
65 if(hexTrans
!= NULL
) {
71 void CanonicalIteratorTest::TestExhaustive() {
72 UErrorCode status
= U_ZERO_ERROR
;
73 CanonicalIterator
it("", status
);
75 UnicodeString s
, decomp
, comp
;
76 // Test static and dynamic class IDs
77 if(it
.getDynamicClassID() != CanonicalIterator::getStaticClassID()){
78 errln("CanonicalIterator::getStaticClassId ! = CanonicalIterator.getDynamicClassID");
80 for (i
= 0; i
< 0x10FFFF; quick
?i
+=0x10:++i
) {
81 //for (i = 0xae00; i < 0xaf00; ++i) {
83 if ((i
% 0x100) == 0) {
84 logln("Testing U+%06X", i
);
87 // skip characters we know don't have decomps
88 int8_t type
= u_charType(i
);
89 if (type
== U_UNASSIGNED
|| type
== U_PRIVATE_USE_CHAR
90 || type
== U_SURROGATE
) continue;
93 s
+= (UChar32
)0x0345; //"\\u0345";
95 Normalizer::decompose(s
, FALSE
, 0, decomp
, status
);
96 Normalizer::compose(s
, FALSE
, 0, comp
, status
);
98 // skip characters that don't have either decomp.
99 // need quick test for this!
100 if (s
== decomp
&& s
== comp
) {
104 it
.setSource(s
, status
);
105 UBool gotDecomp
= FALSE
;
106 UBool gotComp
= FALSE
;
107 UBool gotSource
= FALSE
;
110 UnicodeString item
= it
.next();
111 if (item
.isBogus()) break;
112 if (item
== s
) gotSource
= TRUE
;
113 if (item
== decomp
) gotDecomp
= TRUE
;
114 if (item
== comp
) gotComp
= TRUE
;
117 if (!gotSource
|| !gotDecomp
|| !gotComp
) {
118 errln("FAIL CanonicalIterator: " + s
+ (int)i
);
123 void CanonicalIteratorTest::TestBasic() {
125 UErrorCode status
= U_ZERO_ERROR
;
127 static const char * const testArray
[][2] = {
128 {"\\u00C5d\\u0307\\u0327", "A\\u030Ad\\u0307\\u0327, A\\u030Ad\\u0327\\u0307, A\\u030A\\u1E0B\\u0327, "
129 "A\\u030A\\u1E11\\u0307, \\u00C5d\\u0307\\u0327, \\u00C5d\\u0327\\u0307, "
130 "\\u00C5\\u1E0B\\u0327, \\u00C5\\u1E11\\u0307, \\u212Bd\\u0307\\u0327, "
131 "\\u212Bd\\u0327\\u0307, \\u212B\\u1E0B\\u0327, \\u212B\\u1E11\\u0307"},
132 {"\\u010d\\u017E", "c\\u030Cz\\u030C, c\\u030C\\u017E, \\u010Dz\\u030C, \\u010D\\u017E"},
133 {"x\\u0307\\u0327", "x\\u0307\\u0327, x\\u0327\\u0307, \\u1E8B\\u0327"},
137 // This is not interesting for C/C++ as the data is already built beforehand
139 UnicodeSet ss
= CanonicalIterator
.getSafeStart();
140 logln("Safe Start: " + ss
.toPattern(true));
141 ss
= CanonicalIterator
.getStarts('a');
142 expectEqual("Characters with 'a' at the start of their decomposition: ", "", CanonicalIterator
.getStarts('a'),
143 new UnicodeSet("[\u00E0-\u00E5\u0101\u0103\u0105\u01CE\u01DF\u01E1\u01FB"
144 + "\u0201\u0203\u0227\u1E01\u1EA1\u1EA3\u1EA5\u1EA7\u1EA9\u1EAB\u1EAD\u1EAF\u1EB1\u1EB3\u1EB5\u1EB7]")
149 // NOTE: we use a TreeSet below to sort the output, which is not guaranteed to be sorted!
151 Hashtable
*permutations
= new Hashtable(FALSE
, status
);
152 permutations
->setValueDeleter(uhash_deleteUnicodeString
);
153 UnicodeString
toPermute("ABC");
155 CanonicalIterator::permute(toPermute
, FALSE
, permutations
, status
);
157 logln("testing permutation");
159 expectEqual("Simple permutation ", "", collectionToString(permutations
), "ABC, ACB, BAC, BCA, CAB, CBA");
164 logln("testing samples");
165 Hashtable
*set
= new Hashtable(FALSE
, status
);
166 set
->setValueDeleter(uhash_deleteUnicodeString
);
168 CanonicalIterator
it("", status
);
169 if(U_SUCCESS(status
)) {
170 for (i
= 0; i
< ARRAY_LENGTH(testArray
); ++i
) {
171 //logln("Results for: " + name.transliterate(testArray[i]));
172 UnicodeString testStr
= CharsToUnicodeString(testArray
[i
][0]);
173 it
.setSource(testStr
, status
);
176 //UnicodeString *result = new UnicodeString(it.next());
177 UnicodeString
result(it
.next());
178 if (result
.isBogus()) {
181 set
->put(result
, new UnicodeString(result
), status
); // Add result to the table
182 //logln(++counter + ": " + hex.transliterate(result));
183 //logln(" = " + name.transliterate(result));
185 expectEqual(i
+ ": ", testStr
, collectionToString(set
), CharsToUnicodeString(testArray
[i
][1]));
189 errln("Couldn't instantiate canonical iterator. Error: %s", u_errorName(status
));
194 void CanonicalIteratorTest::expectEqual(const UnicodeString
&message
, const UnicodeString
&item
, const UnicodeString
&a
, const UnicodeString
&b
) {
196 errln("FAIL: " + message
+ getReadable(item
));
197 errln("\t" + getReadable(a
));
198 errln("\t" + getReadable(b
));
200 logln("Checked: " + message
+ getReadable(item
));
201 logln("\t" + getReadable(a
));
202 logln("\t" + getReadable(b
));
206 UnicodeString
CanonicalIteratorTest::getReadable(const UnicodeString
&s
) {
207 UErrorCode status
= U_ZERO_ERROR
;
208 UnicodeString result
= "[";
209 if (s
.length() == 0) return "";
210 // set up for readable display
211 #if !UCONFIG_NO_TRANSLITERATION
213 if (nameTrans
== NULL
)
214 nameTrans
= Transliterator::createInstance("[^\\ -\\u007F] name", UTRANS_FORWARD
, status
);
215 UnicodeString sName
= s
;
216 nameTrans
->transliterate(sName
);
220 if (hexTrans
== NULL
)
221 hexTrans
= Transliterator::createInstance("[^\\ -\\u007F] hex", UTRANS_FORWARD
, status
);
223 UnicodeString sHex
= s
;
224 #if !UCONFIG_NO_TRANSLITERATION
225 if(hexTrans
) { // maybe there is no data and transliterator cannot be instantiated
226 hexTrans
->transliterate(sHex
);
232 //return "[" + (verbose ? name->transliterate(s) + "; " : "") + hex->transliterate(s) + "]";
235 U_CFUNC
int U_CALLCONV
236 compareUnicodeStrings(const void *s1
, const void *s2
) {
237 UnicodeString
**st1
= (UnicodeString
**)s1
;
238 UnicodeString
**st2
= (UnicodeString
**)s2
;
240 return (*st1
)->compare(**st2
);
244 UnicodeString
CanonicalIteratorTest::collectionToString(Hashtable
*col
) {
245 UnicodeString result
;
247 // Iterate over the Hashtable, then qsort.
249 UnicodeString
**resArray
= new UnicodeString
*[col
->count()];
252 const UHashElement
*ne
= NULL
;
254 //Iterator it = basic.iterator();
255 ne
= col
->nextElement(el
);
256 //while (it.hasNext())
258 //String item = (String) it.next();
259 UnicodeString
*item
= (UnicodeString
*)(ne
->value
.pointer
);
260 resArray
[i
++] = item
;
261 ne
= col
->nextElement(el
);
264 for(i
= 0; i
<col
->count(); ++i
) {
268 qsort(resArray
, col
->count(), sizeof(UnicodeString
*), compareUnicodeStrings
);
270 result
= *resArray
[0];
272 for(i
= 1; i
<col
->count(); ++i
) {
274 result
+= *resArray
[i
];
278 Iterator it = col.iterator();
279 while (it.hasNext()) {
280 if (result.length() != 0) result.append(", ");
281 result.append(it.next().toString());
290 void CanonicalIteratorTest::TestAPI() {
291 UErrorCode status
= U_ZERO_ERROR
;
292 // Test reset and getSource
293 UnicodeString
start("ljubav");
294 logln("Testing CanonicalIterator::getSource");
295 logln("Instantiating canonical iterator with string "+start
);
296 CanonicalIterator
can(start
, status
);
297 UnicodeString source
= can
.getSource();
298 logln("CanonicalIterator::getSource returned "+source
);
299 if(start
!= source
) {
300 errln("CanonicalIterator.getSource() didn't return the starting string. Expected "+start
+", got "+source
);
302 logln("Testing CanonicalIterator::reset");
303 UnicodeString next
= can
.next();
304 logln("CanonicalIterator::next returned "+next
);
308 UnicodeString afterReset
= can
.next();
309 logln("After reset, CanonicalIterator::next returned "+afterReset
);
311 if(next
!= afterReset
) {
312 errln("Next after instantiation ("+next
+") is different from next after reset ("+afterReset
+").");
315 logln("Testing getStaticClassID and getDynamicClassID");
316 if(can
.getDynamicClassID() != CanonicalIterator::getStaticClassID()){
317 errln("RTTI failed for CanonicalIterator getDynamicClassID != getStaticClassID");
321 #endif /* #if !UCONFIG_NO_NORMALIZATION */