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/CollationKanaTest
010: * Source File: $ICU4CRoot/source/test/intltest/jacoll.cpp
011: **/package com.ibm.icu.dev.test.collator;
012:
013: import com.ibm.icu.dev.test.*;
014: import com.ibm.icu.text.*;
015: import java.util.Locale;
016:
017: public class CollationKanaTest extends TestFmwk {
018: public static void main(String[] args) throws Exception {
019: new CollationKanaTest().run(args);
020: }
021:
022: private static char[][] testSourceCases = { { 0xff9E }, { 0x3042 },
023: { 0x30A2 }, { 0x3042, 0x3042 }, { 0x30A2, 0x30FC },
024: { 0x30A2, 0x30FC, 0x30C8 } /* 6 */
025: };
026:
027: private static char[][] testTargetCases = { { 0xFF9F }, { 0x30A2 },
028: { 0x3042, 0x3042 }, { 0x30A2, 0x30FC },
029: { 0x30A2, 0x30FC, 0x30C8 }, { 0x3042, 0x3042, 0x3068 } /* 6 */
030: };
031:
032: private static int[] results = { -1, 0, //Collator::LESS, /* Katakanas and Hiraganas are equal on tertiary level(ICU 2.0)*/
033: -1, 1, // Collator::LESS, /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*/
034: -1, -1, //Collator::GREATER /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*//* 6 */
035: };
036:
037: private static char[][] testBaseCases = { { 0x30AB },
038: { 0x30AB, 0x30AD }, { 0x30AD }, { 0x30AD, 0x30AD } };
039:
040: private static char[][] testPlainDakutenHandakutenCases = {
041: { 0x30CF, 0x30AB }, { 0x30D0, 0x30AB }, { 0x30CF, 0x30AD },
042: { 0x30D0, 0x30AD } };
043:
044: private static char[][] testSmallLargeCases = { { 0x30C3, 0x30CF },
045: { 0x30C4, 0x30CF }, { 0x30C3, 0x30D0 }, { 0x30C4, 0x30D0 } };
046:
047: private static char[][] testKatakanaHiraganaCases = {
048: { 0x3042, 0x30C3 }, { 0x30A2, 0x30C3 }, { 0x3042, 0x30C4 },
049: { 0x30A2, 0x30C4 } };
050:
051: private static char[][] testChooonKigooCases = {
052: /*0*/{ 0x30AB, 0x30FC, 0x3042 },
053: /*1*/{ 0x30AB, 0x30FC, 0x30A2 },
054: /*2*/{ 0x30AB, 0x30A4, 0x3042 },
055: /*3*/{ 0x30AB, 0x30A4, 0x30A2 },
056: /*6*/{ 0x30AD, 0x30FC, 0x3042 }, /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*/
057: /*7*/{ 0x30AD, 0x30FC, 0x30A2 }, /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*/
058: /*4*/{ 0x30AD, 0x30A4, 0x3042 },
059: /*5*/{ 0x30AD, 0x30A4, 0x30A2 } };
060:
061: private Collator myCollation = null;
062:
063: public CollationKanaTest() {
064: }
065:
066: protected void init() throws Exception {
067: if (myCollation == null) {
068: myCollation = Collator.getInstance(Locale.JAPANESE);
069: }
070: }
071:
072: // performs test with strength TERIARY
073: public void TestTertiary() {
074: int i = 0;
075: myCollation.setStrength(Collator.TERTIARY);
076:
077: for (i = 0; i < 6; i++) {
078: doTest(testSourceCases[i], testTargetCases[i], results[i]);
079: }
080: }
081:
082: /* Testing base letters */
083: public void TestBase() {
084: int i;
085: myCollation.setStrength(Collator.PRIMARY);
086: for (i = 0; i < 3; i++) {
087: doTest(testBaseCases[i], testBaseCases[i + 1], -1);
088: }
089: }
090:
091: /* Testing plain, Daku-ten, Handaku-ten letters */
092: public void TestPlainDakutenHandakuten() {
093: int i;
094: myCollation.setStrength(Collator.SECONDARY);
095: for (i = 0; i < 3; i++) {
096: doTest(testPlainDakutenHandakutenCases[i],
097: testPlainDakutenHandakutenCases[i + 1], -1);
098: }
099: }
100:
101: /*
102: * Test Small, Large letters
103: */
104: public void TestSmallLarge() {
105: int i;
106: myCollation.setStrength(Collator.TERTIARY);
107:
108: for (i = 0; i < 3; i++) {
109: doTest(testSmallLargeCases[i], testSmallLargeCases[i + 1],
110: -1);
111: }
112: }
113:
114: /*
115: * Test Katakana, Hiragana letters
116: */
117: public void TestKatakanaHiragana() {
118: int i;
119: myCollation.setStrength(Collator.QUATERNARY);
120: for (i = 0; i < 3; i++) {
121: doTest(testKatakanaHiraganaCases[i],
122: testKatakanaHiraganaCases[i + 1], -1);
123: }
124: }
125:
126: /*
127: * Test Choo-on kigoo
128: */
129: public void TestChooonKigoo() {
130: int i;
131: myCollation.setStrength(Collator.QUATERNARY);
132: for (i = 0; i < 7; i++) {
133: doTest(testChooonKigooCases[i],
134: testChooonKigooCases[i + 1], -1);
135: }
136: }
137:
138: // main test routine, tests rules specific to "Kana" locale
139: private void doTest(char[] source, char[] target, int result) {
140:
141: String s = new String(source);
142: String t = new String(target);
143: int compareResult = myCollation.compare(s, t);
144: CollationKey sortKey1, sortKey2;
145: sortKey1 = myCollation.getCollationKey(s);
146: sortKey2 = myCollation.getCollationKey(t);
147: int keyResult = sortKey1.compareTo(sortKey2);
148: reportCResult(s, t, sortKey1, sortKey2, compareResult,
149: keyResult, compareResult, result);
150:
151: }
152:
153: private void reportCResult(String source, String target,
154: CollationKey sourceKey, CollationKey targetKey,
155: int compareResult, int keyResult, int incResult,
156: int expectedResult) {
157: if (expectedResult < -1 || expectedResult > 1) {
158: errln("***** invalid call to reportCResult ****");
159: return;
160: }
161:
162: boolean ok1 = (compareResult == expectedResult);
163: boolean ok2 = (keyResult == expectedResult);
164: boolean ok3 = (incResult == expectedResult);
165:
166: if (ok1 && ok2 && ok3 && !isVerbose()) {
167: return;
168: } else {
169: String msg1 = ok1 ? "Ok: compare(\"" : "FAIL: compare(\"";
170: String msg2 = "\", \"";
171: String msg3 = "\") returned ";
172: String msg4 = "; expected ";
173:
174: String sExpect = new String("");
175: String sResult = new String("");
176: sResult = appendCompareResult(compareResult, sResult);
177: sExpect = appendCompareResult(expectedResult, sExpect);
178: if (ok1) {
179: logln(msg1 + source + msg2 + target + msg3 + sResult);
180: } else {
181: errln(msg1 + source + msg2 + target + msg3 + sResult
182: + msg4 + sExpect);
183: }
184:
185: msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
186: msg2 = "\").compareTo(key(\"";
187: msg3 = "\")) returned ";
188: sResult = appendCompareResult(keyResult, sResult);
189: if (ok2) {
190: logln(msg1 + source + msg2 + target + msg3 + sResult);
191: } else {
192: errln(msg1 + source + msg2 + target + msg3 + sResult
193: + msg4 + sExpect);
194: msg1 = " ";
195: msg2 = " vs. ";
196: errln(msg1 + prettify(sourceKey) + msg2
197: + prettify(targetKey));
198: }
199:
200: msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
201: msg2 = "\", \"";
202: msg3 = "\") returned ";
203:
204: sResult = appendCompareResult(incResult, sResult);
205:
206: if (ok3) {
207: logln(msg1 + source + msg2 + target + msg3 + sResult);
208: } else {
209: errln(msg1 + source + msg2 + target + msg3 + sResult
210: + msg4 + sExpect);
211: }
212: }
213: }
214:
215: private String appendCompareResult(int result, String target) {
216: if (result == -1) {
217: target += "LESS";
218: } else if (result == 0) {
219: target += "EQUAL";
220: } else if (result == 1) {
221: target += "GREATER";
222: } else {
223: String huh = "?";
224: target += huh + result;
225: }
226: return target;
227: }
228:
229: String prettify(CollationKey sourceKey) {
230: int i;
231: byte[] bytes = sourceKey.toByteArray();
232: String target = "[";
233:
234: for (i = 0; i < bytes.length; i++) {
235: target += Integer.toHexString(bytes[i]);
236: target += " ";
237: }
238: target += "]";
239: return target;
240: }
241: }
|