1 /********************************************************************
3 * Copyright (c) 2002-2010, 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
);
74 if (U_FAILURE(status
)) {
75 dataerrln("Error creating CanonicalIterator: %s", u_errorName(status
));
80 // Test static and dynamic class IDs
81 if(it
.getDynamicClassID() != CanonicalIterator::getStaticClassID()){
82 errln("CanonicalIterator::getStaticClassId ! = CanonicalIterator.getDynamicClassID");
84 for (i
= 0; i
< 0x10FFFF; quick
?i
+=0x10:++i
) {
85 //for (i = 0xae00; i < 0xaf00; ++i) {
87 if ((i
% 0x100) == 0) {
88 logln("Testing U+%06X", i
);
91 // skip characters we know don't have decomps
92 int8_t type
= u_charType(i
);
93 if (type
== U_UNASSIGNED
|| type
== U_PRIVATE_USE_CHAR
94 || type
== U_SURROGATE
) continue;
97 characterTest(s
, i
, it
);
99 s
+= (UChar32
)0x0345; //"\\u0345";
100 characterTest(s
, i
, it
);
104 void CanonicalIteratorTest::TestBasic() {
106 UErrorCode status
= U_ZERO_ERROR
;
108 static const char * const testArray
[][2] = {
109 {"\\u00C5d\\u0307\\u0327", "A\\u030Ad\\u0307\\u0327, A\\u030Ad\\u0327\\u0307, A\\u030A\\u1E0B\\u0327, "
110 "A\\u030A\\u1E11\\u0307, \\u00C5d\\u0307\\u0327, \\u00C5d\\u0327\\u0307, "
111 "\\u00C5\\u1E0B\\u0327, \\u00C5\\u1E11\\u0307, \\u212Bd\\u0307\\u0327, "
112 "\\u212Bd\\u0327\\u0307, \\u212B\\u1E0B\\u0327, \\u212B\\u1E11\\u0307"},
113 {"\\u010d\\u017E", "c\\u030Cz\\u030C, c\\u030C\\u017E, \\u010Dz\\u030C, \\u010D\\u017E"},
114 {"x\\u0307\\u0327", "x\\u0307\\u0327, x\\u0327\\u0307, \\u1E8B\\u0327"},
118 // This is not interesting for C/C++ as the data is already built beforehand
120 UnicodeSet ss
= CanonicalIterator
.getSafeStart();
121 logln("Safe Start: " + ss
.toPattern(true));
122 ss
= CanonicalIterator
.getStarts('a');
123 expectEqual("Characters with 'a' at the start of their decomposition: ", "", CanonicalIterator
.getStarts('a'),
124 new UnicodeSet("[\u00E0-\u00E5\u0101\u0103\u0105\u01CE\u01DF\u01E1\u01FB"
125 + "\u0201\u0203\u0227\u1E01\u1EA1\u1EA3\u1EA5\u1EA7\u1EA9\u1EAB\u1EAD\u1EAF\u1EB1\u1EB3\u1EB5\u1EB7]")
130 // NOTE: we use a TreeSet below to sort the output, which is not guaranteed to be sorted!
132 Hashtable
*permutations
= new Hashtable(FALSE
, status
);
133 permutations
->setValueDeleter(uhash_deleteUnicodeString
);
134 UnicodeString
toPermute("ABC");
136 CanonicalIterator::permute(toPermute
, FALSE
, permutations
, status
);
138 logln("testing permutation");
140 expectEqual("Simple permutation ", "", collectionToString(permutations
), "ABC, ACB, BAC, BCA, CAB, CBA");
145 logln("testing samples");
146 Hashtable
*set
= new Hashtable(FALSE
, status
);
147 set
->setValueDeleter(uhash_deleteUnicodeString
);
149 CanonicalIterator
it("", status
);
150 if(U_SUCCESS(status
)) {
151 for (i
= 0; i
< ARRAY_LENGTH(testArray
); ++i
) {
152 //logln("Results for: " + name.transliterate(testArray[i]));
153 UnicodeString testStr
= CharsToUnicodeString(testArray
[i
][0]);
154 it
.setSource(testStr
, status
);
157 //UnicodeString *result = new UnicodeString(it.next());
158 UnicodeString
result(it
.next());
159 if (result
.isBogus()) {
162 set
->put(result
, new UnicodeString(result
), status
); // Add result to the table
163 //logln(++counter + ": " + hex.transliterate(result));
164 //logln(" = " + name.transliterate(result));
166 expectEqual(i
+ ": ", testStr
, collectionToString(set
), CharsToUnicodeString(testArray
[i
][1]));
170 dataerrln("Couldn't instantiate canonical iterator. Error: %s", u_errorName(status
));
175 void CanonicalIteratorTest::characterTest(UnicodeString
&s
, UChar32 ch
, CanonicalIterator
&it
)
177 UErrorCode status
= U_ZERO_ERROR
;
178 UnicodeString decomp
, comp
;
179 UBool gotDecomp
= FALSE
;
180 UBool gotComp
= FALSE
;
181 UBool gotSource
= FALSE
;
183 Normalizer::decompose(s
, FALSE
, 0, decomp
, status
);
184 Normalizer::compose(s
, FALSE
, 0, comp
, status
);
186 // skip characters that don't have either decomp.
187 // need quick test for this!
188 if (s
== decomp
&& s
== comp
) {
192 it
.setSource(s
, status
);
195 UnicodeString item
= it
.next();
196 if (item
.isBogus()) break;
197 if (item
== s
) gotSource
= TRUE
;
198 if (item
== decomp
) gotDecomp
= TRUE
;
199 if (item
== comp
) gotComp
= TRUE
;
202 if (!gotSource
|| !gotDecomp
|| !gotComp
) {
203 errln("FAIL CanonicalIterator: " + s
+ (int)ch
);
207 void CanonicalIteratorTest::expectEqual(const UnicodeString
&message
, const UnicodeString
&item
, const UnicodeString
&a
, const UnicodeString
&b
) {
209 errln("FAIL: " + message
+ getReadable(item
));
210 errln("\t" + getReadable(a
));
211 errln("\t" + getReadable(b
));
213 logln("Checked: " + message
+ getReadable(item
));
214 logln("\t" + getReadable(a
));
215 logln("\t" + getReadable(b
));
219 UnicodeString
CanonicalIteratorTest::getReadable(const UnicodeString
&s
) {
220 UErrorCode status
= U_ZERO_ERROR
;
221 UnicodeString result
= "[";
222 if (s
.length() == 0) return "";
223 // set up for readable display
224 #if !UCONFIG_NO_TRANSLITERATION
226 if (nameTrans
== NULL
)
227 nameTrans
= Transliterator::createInstance("[^\\ -\\u007F] name", UTRANS_FORWARD
, status
);
228 UnicodeString sName
= s
;
229 nameTrans
->transliterate(sName
);
233 if (hexTrans
== NULL
)
234 hexTrans
= Transliterator::createInstance("[^\\ -\\u007F] hex", UTRANS_FORWARD
, status
);
236 UnicodeString sHex
= s
;
237 #if !UCONFIG_NO_TRANSLITERATION
238 if(hexTrans
) { // maybe there is no data and transliterator cannot be instantiated
239 hexTrans
->transliterate(sHex
);
245 //return "[" + (verbose ? name->transliterate(s) + "; " : "") + hex->transliterate(s) + "]";
248 U_CFUNC
int U_CALLCONV
249 compareUnicodeStrings(const void *s1
, const void *s2
) {
250 UnicodeString
**st1
= (UnicodeString
**)s1
;
251 UnicodeString
**st2
= (UnicodeString
**)s2
;
253 return (*st1
)->compare(**st2
);
257 UnicodeString
CanonicalIteratorTest::collectionToString(Hashtable
*col
) {
258 UnicodeString result
;
260 // Iterate over the Hashtable, then qsort.
262 UnicodeString
**resArray
= new UnicodeString
*[col
->count()];
265 const UHashElement
*ne
= NULL
;
267 //Iterator it = basic.iterator();
268 ne
= col
->nextElement(el
);
269 //while (it.hasNext())
271 //String item = (String) it.next();
272 UnicodeString
*item
= (UnicodeString
*)(ne
->value
.pointer
);
273 resArray
[i
++] = item
;
274 ne
= col
->nextElement(el
);
277 for(i
= 0; i
<col
->count(); ++i
) {
281 qsort(resArray
, col
->count(), sizeof(UnicodeString
*), compareUnicodeStrings
);
283 result
= *resArray
[0];
285 for(i
= 1; i
<col
->count(); ++i
) {
287 result
+= *resArray
[i
];
291 Iterator it = col.iterator();
292 while (it.hasNext()) {
293 if (result.length() != 0) result.append(", ");
294 result.append(it.next().toString());
303 void CanonicalIteratorTest::TestAPI() {
304 UErrorCode status
= U_ZERO_ERROR
;
305 // Test reset and getSource
306 UnicodeString
start("ljubav");
307 logln("Testing CanonicalIterator::getSource");
308 logln("Instantiating canonical iterator with string "+start
);
309 CanonicalIterator
can(start
, status
);
310 if (U_FAILURE(status
)) {
311 dataerrln("Error creating CanonicalIterator: %s", u_errorName(status
));
314 UnicodeString source
= can
.getSource();
315 logln("CanonicalIterator::getSource returned "+source
);
316 if(start
!= source
) {
317 errln("CanonicalIterator.getSource() didn't return the starting string. Expected "+start
+", got "+source
);
319 logln("Testing CanonicalIterator::reset");
320 UnicodeString next
= can
.next();
321 logln("CanonicalIterator::next returned "+next
);
325 UnicodeString afterReset
= can
.next();
326 logln("After reset, CanonicalIterator::next returned "+afterReset
);
328 if(next
!= afterReset
) {
329 errln("Next after instantiation ("+next
+") is different from next after reset ("+afterReset
+").");
332 logln("Testing getStaticClassID and getDynamicClassID");
333 if(can
.getDynamicClassID() != CanonicalIterator::getStaticClassID()){
334 errln("RTTI failed for CanonicalIterator getDynamicClassID != getStaticClassID");
338 #endif /* #if !UCONFIG_NO_NORMALIZATION */