001: /*
002: *******************************************************************************
003: * Copyright (C) 2002-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: /**
009: * Port From: ICU4C v2.1 : Collate/CollationDummyTest
010: * Source File: $ICU4CRoot/source/test/intltest/allcoll.cpp
011: * $ICU4CRoot/source/test/cintltst/callcoll.c
012: **/package com.ibm.icu.dev.test.collator;
013:
014: import com.ibm.icu.dev.test.*;
015: import com.ibm.icu.text.*;
016:
017: import java.util.Locale;
018:
019: public class CollationDummyTest extends TestFmwk {
020: public static void main(String[] args) throws Exception {
021: new CollationDummyTest().run(args);
022: // new CollationDummyTest().TestVariableTop();
023: }
024:
025: //testSourceCases[][] and testTargetCases[][], testCases[][] are ported from the file callcoll.c in icu4c
026: private static char[][] testSourceCases = {
027: { 0x61, 0x62, 0x27, 0x63 },
028: { 0x63, 0x6f, 0x2d, 0x6f, 0x70 },
029: { 0x61, 0x62 },
030: { 0x61, 0x6d, 0x70, 0x65, 0x72, 0x73, 0x61, 0x64 },
031: { 0x61, 0x6c, 0x6c },
032: { 0x66, 0x6f, 0x75, 0x72 },
033: { 0x66, 0x69, 0x76, 0x65 },
034: { 0x31 },
035: { 0x31 },
036: { 0x31 }, // 10
037: { 0x32 },
038: { 0x32 },
039: { 0x48, 0x65, 0x6c, 0x6c, 0x6f },
040: { 0x61, 0x3c, 0x62 },
041: { 0x61, 0x3c, 0x62 },
042: { 0x61, 0x63, 0x63 },
043: { 0x61, 0x63, 0x48, 0x63 }, // simple test
044: { 0x70, 0x00EA, 0x63, 0x68, 0x65 },
045: { 0x61, 0x62, 0x63 },
046: { 0x61, 0x62, 0x63 }, // 20
047: { 0x61, 0x62, 0x63 },
048: { 0x61, 0x62, 0x63 },
049: { 0x61, 0x62, 0x63 },
050: { 0x61, 0x00E6, 0x63 },
051: { 0x61, 0x63, 0x48, 0x63 }, // primary test
052: { 0x62, 0x6c, 0x61, 0x63, 0x6b },
053: { 0x66, 0x6f, 0x75, 0x72 },
054: { 0x66, 0x69, 0x76, 0x65 },
055: { 0x31 },
056: { 0x61, 0x62, 0x63 }, // 30
057: { 0x61, 0x62, 0x63 }, { 0x61, 0x62, 0x63, 0x48 },
058: { 0x61, 0x62, 0x63 },
059: { 0x61, 0x63, 0x48, 0x63 }, // 34
060: { 0x61, 0x63, 0x65, 0x30 }, { 0x31, 0x30 },
061: { 0x70, 0x00EA, 0x30 } // 37
062: };
063:
064: private static char[][] testTargetCases = {
065: { 0x61, 0x62, 0x63, 0x27 },
066: { 0x43, 0x4f, 0x4f, 0x50 },
067: { 0x61, 0x62, 0x63 },
068: { 0x26 },
069: { 0x26 },
070: { 0x34 },
071: { 0x35 },
072: { 0x6f, 0x6e, 0x65 },
073: { 0x6e, 0x6e, 0x65 },
074: { 0x70, 0x6e, 0x65 }, // 10
075: { 0x74, 0x77, 0x6f },
076: { 0x75, 0x77, 0x6f },
077: { 0x68, 0x65, 0x6c, 0x6c, 0x4f },
078: { 0x61, 0x3c, 0x3d, 0x62 },
079: { 0x61, 0x62, 0x63 },
080: { 0x61, 0x43, 0x48, 0x63 },
081: { 0x61, 0x43, 0x48, 0x63 }, // simple test
082: { 0x70, 0x00E9, 0x63, 0x68, 0x00E9 },
083: { 0x61, 0x62, 0x63 },
084: { 0x61, 0x42, 0x43 }, // 20
085: { 0x61, 0x62, 0x63, 0x68 },
086: { 0x61, 0x62, 0x64 },
087: { 0x00E4, 0x62, 0x63 },
088: { 0x61, 0x00C6, 0x63 },
089: { 0x61, 0x43, 0x48, 0x63 }, // primary test
090: { 0x62, 0x6c, 0x61, 0x63, 0x6b, 0x2d, 0x62, 0x69, 0x72,
091: 0x64 }, { 0x34 }, { 0x35 },
092: { 0x6f, 0x6e, 0x65 },
093: { 0x61, 0x62, 0x63 },
094: { 0x61, 0x42, 0x63 }, // 30
095: { 0x61, 0x62, 0x63, 0x68 },
096: { 0x61, 0x62, 0x64 },
097: { 0x61, 0x43, 0x48, 0x63 }, // 34
098: { 0x61, 0x63, 0x65, 0x30 }, { 0x31, 0x30 },
099: { 0x70, 0x00EB, 0x30 } // 37
100: };
101:
102: private static char[][] testCases = { { 0x61 }, { 0x41 },
103: { 0x00e4 }, { 0x00c4 }, { 0x61, 0x65 }, { 0x61, 0x45 },
104: { 0x41, 0x65 }, { 0x41, 0x45 }, { 0x00e6 }, { 0x00c6 },
105: { 0x62 }, { 0x63 }, { 0x7a } };
106:
107: int[] results = { -1, -1, //Collator::GREATER,
108: -1, -1, -1, -1, -1, 1, 1, -1, // 10
109: 1, -1, 1, 1, -1, -1, -1,
110: // test primary > 17
111: 0, 0, 0, // 20
112: -1, -1, 0, 0, 0, -1,
113: // test secondary > 26
114: 0, 0, 0, 0, 0, // 30
115: 0, -1, 0, // 34
116: 0, 0, -1 };
117:
118: final int MAX_TOKEN_LEN = 16;
119:
120: public RuleBasedCollator myCollation;
121:
122: public CollationDummyTest() {
123: }
124:
125: protected void init() throws Exception {
126: String ruleset = "& C < ch, cH, Ch, CH & Five, 5 & Four, 4 & one, 1 & Ampersand; '&' & Two, 2 ";
127: // String ruleset = "& Four, 4";
128: myCollation = null;
129: myCollation = new RuleBasedCollator(ruleset);
130: }
131:
132: // perform test with strength tertiary
133: public void TestTertiary() {
134: int i = 0;
135: myCollation.setStrength(Collator.TERTIARY);
136: for (i = 0; i < 17; i++) {
137: doTest(myCollation, testSourceCases[i], testTargetCases[i],
138: results[i]);
139: }
140: }
141:
142: // perform test with strength PRIMARY
143: public void TestPrimary() {
144: // problem in strcollinc for unfinshed contractions
145: myCollation.setStrength(Collator.PRIMARY);
146: for (int i = 17; i < 26; i++) {
147: doTest(myCollation, testSourceCases[i], testTargetCases[i],
148: results[i]);
149: }
150: }
151:
152: //perform test with strength SECONDARY
153: public void TestSecondary() {
154: int i;
155: myCollation.setStrength(Collator.SECONDARY);
156: for (i = 26; i < 34; i++) {
157: doTest(myCollation, testSourceCases[i], testTargetCases[i],
158: results[i]);
159: }
160: }
161:
162: // perform extra tests
163: public void TestExtra() {
164: int i, j;
165: myCollation.setStrength(Collator.TERTIARY);
166: for (i = 0; i < testCases.length - 1; i++) {
167: for (j = i + 1; j < testCases.length; j += 1) {
168: doTest(myCollation, testCases[i], testCases[j], -1);
169: }
170: }
171: }
172:
173: public void TestIdentical() {
174: int i;
175: myCollation.setStrength(Collator.IDENTICAL);
176: for (i = 34; i < 37; i++) {
177: doTest(myCollation, testSourceCases[i], testTargetCases[i],
178: results[i]);
179: }
180: }
181:
182: public void TestJB581() {
183: String source = "THISISATEST.";
184: String target = "Thisisatest.";
185: Collator coll = null;
186: try {
187: coll = Collator.getInstance(Locale.ENGLISH);
188: } catch (Exception e) {
189: errln("ERROR: Failed to create the collator for : en_US\n");
190: return;
191: }
192:
193: int result = coll.compare(source, target);
194: // result is 1, secondary differences only for ignorable space characters
195: if (result != 1) {
196: errln("Comparing two strings with only secondary differences in C failed.\n");
197: return;
198: }
199:
200: // To compare them with just primary differences
201: coll.setStrength(Collator.PRIMARY);
202: result = coll.compare(source, target);
203: // result is 0
204: if (result != 0) {
205: errln("Comparing two strings with no differences in C failed.\n");
206: return;
207: }
208:
209: // Now, do the same comparison with keys
210: CollationKey sourceKeyOut, targetKeyOut;
211: sourceKeyOut = coll.getCollationKey(source);
212: targetKeyOut = coll.getCollationKey(target);
213: result = sourceKeyOut.compareTo(targetKeyOut);
214: if (result != 0) {
215: errln("Comparing two strings with sort keys in C failed.\n");
216: return;
217: }
218: }
219:
220: //TestSurrogates() is ported from cintltst/callcoll.c
221:
222: /**
223: * Tests surrogate support.
224: */
225: public void TestSurrogates() {
226: String rules = "&z<'\ud800\udc00'<'\ud800\udc0a\u0308'<A";
227: String source[] = { "z", "\uD800\uDC00", "\ud800\udc0a\u0308",
228: "\ud800\udc02" };
229:
230: String target[] = { "\uD800\uDC00", "\ud800\udc0a\u0308", "A",
231: "\ud800\udc03" };
232:
233: // this test is to verify the supplementary sort key order in the english
234: // collator
235: Collator enCollation;
236: try {
237: enCollation = Collator.getInstance(Locale.ENGLISH);
238: } catch (Exception e) {
239: errln("ERROR: Failed to create the collator for ENGLISH");
240: return;
241: }
242:
243: myCollation.setStrength(Collator.TERTIARY);
244: int count = 0;
245: // logln("start of english collation supplementary characters test\n");
246: while (count < 2) {
247: doTest(enCollation, source[count], target[count], -1);
248: count++;
249: }
250: doTest(enCollation, source[count], target[count], 1);
251:
252: // logln("start of tailored collation supplementary characters test\n");
253: count = 0;
254: Collator newCollation;
255: try {
256: newCollation = new RuleBasedCollator(rules);
257: } catch (Exception e) {
258: errln("ERROR: Failed to create the collator for rules");
259: return;
260: }
261:
262: // tests getting collation elements for surrogates for tailored rules
263: while (count < 4) {
264: doTest(newCollation, source[count], target[count], -1);
265: count++;
266: }
267:
268: // tests that \uD801\uDC01 still has the same value, not changed
269: CollationKey enKey = enCollation.getCollationKey(source[3]);
270: CollationKey newKey = newCollation.getCollationKey(source[3]);
271: int keyResult = enKey.compareTo(newKey);
272: if (keyResult != 0) {
273: errln("Failed : non-tailored supplementary characters should have the same value\n");
274: }
275: }
276:
277: //TestVariableTop() is ported from cintltst/callcoll.c
278: /**
279: * Tests the [variable top] tag in rule syntax. Since the default [alternate]
280: * tag has the value shifted, any codepoints before [variable top] should give
281: * a primary ce of 0.
282: */
283: public void TestVariableTop() {
284: String rule = "&z = [variable top]";
285: Collator myColl;
286: Collator enColl;
287: char[] source = new char[1];
288: char ch;
289: int expected[] = { 0 };
290:
291: try {
292: enColl = Collator.getInstance(Locale.ENGLISH);
293: } catch (Exception e) {
294: errln("ERROR: Failed to create the collator for ENGLISH");
295: return;
296: }
297:
298: try {
299: myColl = new RuleBasedCollator(rule);
300: } catch (Exception e) {
301: errln("Fail to create RuleBasedCollator with rules:" + rule);
302: return;
303: }
304: enColl.setStrength(Collator.PRIMARY);
305: myColl.setStrength(Collator.PRIMARY);
306:
307: ((RuleBasedCollator) enColl).setAlternateHandlingShifted(true);
308: ((RuleBasedCollator) myColl).setAlternateHandlingShifted(true);
309:
310: if (((RuleBasedCollator) enColl).isAlternateHandlingShifted() != true) {
311: errln("ERROR: ALTERNATE_HANDLING value can not be set to SHIFTED\n");
312: }
313:
314: // space is supposed to be a variable
315: CollationKey key = enColl.getCollationKey(" ");
316: byte[] result = key.toByteArray();
317:
318: for (int i = 0; i < result.length; i++) {
319: if (result[i] != expected[i]) {
320: errln("ERROR: SHIFTED alternate does not return 0 for primary of space\n");
321: break;
322: }
323: }
324:
325: ch = 'a';
326: while (ch < 'z') {
327: source[0] = ch;
328: key = myColl.getCollationKey(new String(source));
329: result = key.toByteArray();
330:
331: for (int i = 0; i < result.length; i++) {
332: if (result[i] != expected[i]) {
333: errln("ERROR: SHIFTED alternate does not return 0 for primary of space\n");
334: break;
335: }
336: }
337: ch++;
338: }
339: }
340:
341: public void TestJB1401() {
342: Collator myCollator = null;
343: char[] NFD_UnsafeStartChars = { 0x0f73, // Tibetan Vowel Sign II
344: 0x0f75, // Tibetan Vowel Sign UU
345: 0x0f81, // Tibetan Vowel Sign Reversed II
346: 0 };
347: int i;
348:
349: try {
350: myCollator = Collator.getInstance(Locale.ENGLISH);
351: } catch (Exception e) {
352: errln("ERROR: Failed to create the collator for ENGLISH");
353: return;
354: }
355: myCollator.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
356: for (i = 0;; i++) {
357: // Get the next funny character to be tested, and set up the
358: // three test strings X, Y, Z, consisting of an A-grave + test char,
359: // in original form, NFD, and then NFC form.
360: char c = NFD_UnsafeStartChars[i];
361: if (c == 0) {
362: break;
363: }
364:
365: String x = "\u00C0" + c; // \u00C0 is A Grave
366: String y;
367: String z;
368:
369: try {
370: y = Normalizer.decompose(x, false);
371: z = Normalizer.decompose(y, true);
372: } catch (Exception e) {
373: errln("ERROR: Failed to normalize test of character"
374: + c);
375: return;
376: }
377:
378: // Collation test. All three strings should be equal.
379: // doTest does both strcoll and sort keys, with params in both orders.
380: doTest(myCollator, x, y, 0);
381: doTest(myCollator, x, z, 0);
382: doTest(myCollator, y, z, 0);
383:
384: // Run collation element iterators over the three strings. Results should be same for each.
385:
386: {
387: CollationElementIterator ceiX, ceiY, ceiZ;
388: int ceX, ceY, ceZ;
389: int j;
390: try {
391: ceiX = ((RuleBasedCollator) myCollator)
392: .getCollationElementIterator(x);
393: ceiY = ((RuleBasedCollator) myCollator)
394: .getCollationElementIterator(y);
395: ceiZ = ((RuleBasedCollator) myCollator)
396: .getCollationElementIterator(z);
397: } catch (Exception e) {
398: errln("ERROR: getCollationElementIterator failed");
399: return;
400: }
401:
402: for (j = 0;; j++) {
403: try {
404: ceX = ceiX.next();
405: ceY = ceiY.next();
406: ceZ = ceiZ.next();
407: } catch (Exception e) {
408: errln("ERROR: CollationElementIterator.next failed for iteration "
409: + j);
410: break;
411: }
412:
413: if (ceX != ceY || ceY != ceZ) {
414: errln("ERROR: ucol_next failed for iteration "
415: + j);
416: break;
417: }
418: if (ceX == CollationElementIterator.NULLORDER) {
419: break;
420: }
421: }
422: }
423: }
424: }
425:
426: // main test method called with different strengths,
427: // tests comparison of custum collation with different strengths
428:
429: private void doTest(Collator collation, char[] source,
430: char[] target, int result) {
431: String s = new String(source);
432: String t = new String(target);
433: doTestVariant(collation, s, t, result);
434: if (result == -1) {
435: doTestVariant(collation, t, s, 1);
436: } else if (result == 1) {
437: doTestVariant(collation, t, s, -1);
438: } else {
439: doTestVariant(collation, t, s, 0);
440: }
441: }
442:
443: // main test method called with different strengths,
444: // tests comparison of custum collation with different strengths
445:
446: private void doTest(Collator collation, String s, String t,
447: int result) {
448: doTestVariant(collation, s, t, result);
449: if (result == -1) {
450: doTestVariant(collation, t, s, 1);
451: } else if (result == 1) {
452: doTestVariant(collation, t, s, -1);
453: } else {
454: doTestVariant(collation, t, s, 0);
455: }
456: }
457:
458: private void doTestVariant(Collator collation, String source,
459: String target, int result) {
460: int compareResult = collation.compare(source, target);
461: CollationKey srckey, tgtkey;
462: srckey = collation.getCollationKey(source);
463: tgtkey = collation.getCollationKey(target);
464: int keyResult = srckey.compareTo(tgtkey);
465: if (compareResult != result) {
466: errln("String comparison failed in variant test\n");
467: }
468: if (keyResult != result) {
469: errln("Collation key comparison failed in variant test\n");
470: }
471: }
472: }
|