2 *******************************************************************************
3 * Copyright (C) 2002-2012, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
9 * Port From: ICU4C v2.1 : Collate/CollationKanaTest
10 * Source File: $ICU4CRoot/source/test/intltest/jacoll.cpp
13 package com.ibm.icu.dev.test.collator;
15 import java.util.Locale;
17 import com.ibm.icu.dev.test.TestFmwk;
18 import com.ibm.icu.text.CollationKey;
19 import com.ibm.icu.text.Collator;
20 import com.ibm.icu.text.RuleBasedCollator;
21 import com.ibm.icu.util.ULocale;
23 public class CollationKanaTest extends TestFmwk{
24 public static void main(String[] args) throws Exception{
25 new CollationKanaTest().run(args);
28 private static char[][] testSourceCases = {
34 {0x30A2, 0x30FC, 0x30C8} /* 6 */
37 private static char[][] testTargetCases = {
42 {0x30A2, 0x30FC, 0x30C8},
43 {0x3042, 0x3042, 0x3068} /* 6 */
46 private static int[] results = {
48 0, //Collator::LESS, /* Katakanas and Hiraganas are equal on tertiary level(ICU 2.0)*/
50 1, // Collator::LESS, /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*/
52 -1, //Collator::GREATER /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*//* 6 */
55 private static char[][] testBaseCases = {
62 private static char[][] testPlainDakutenHandakutenCases = {
69 private static char[][] testSmallLargeCases = {
76 private static char[][] testKatakanaHiraganaCases = {
83 private static char[][] testChooonKigooCases = {
84 /*0*/ {0x30AB, 0x30FC, 0x3042},
85 /*1*/ {0x30AB, 0x30FC, 0x30A2},
86 /*2*/ {0x30AB, 0x30A4, 0x3042},
87 /*3*/ {0x30AB, 0x30A4, 0x30A2},
88 /*6*/ {0x30AD, 0x30FC, 0x3042}, /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*/
89 /*7*/ {0x30AD, 0x30FC, 0x30A2}, /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*/
90 /*4*/ {0x30AD, 0x30A4, 0x3042},
91 /*5*/ {0x30AD, 0x30A4, 0x30A2}
94 private Collator myCollation = null;
96 public CollationKanaTest() {
98 protected void init()throws Exception{
99 if(myCollation==null){
100 myCollation = Collator.getInstance(Locale.JAPANESE);
103 // performs test with strength TERIARY
104 public void TestTertiary() {
106 myCollation.setStrength(Collator.TERTIARY);
108 for (i = 0; i < 6; i++) {
109 doTest(testSourceCases[i], testTargetCases[i], results[i]);
113 /* Testing base letters */
114 public void TestBase() {
116 myCollation.setStrength(Collator.PRIMARY);
117 for (i = 0; i < 3 ; i++) {
118 doTest(testBaseCases[i], testBaseCases[i + 1], -1);
122 /* Testing plain, Daku-ten, Handaku-ten letters */
123 public void TestPlainDakutenHandakuten() {
125 myCollation.setStrength(Collator.SECONDARY);
126 for (i = 0; i < 3 ; i++) {
127 doTest(testPlainDakutenHandakutenCases[i], testPlainDakutenHandakutenCases[i + 1], -1);
132 * Test Small, Large letters
134 public void TestSmallLarge() {
136 myCollation.setStrength(Collator.TERTIARY);
138 for (i = 0; i < 3 ; i++) {
139 doTest(testSmallLargeCases[i], testSmallLargeCases[i + 1], -1);
144 * Test Katakana, Hiragana letters
146 public void TestKatakanaHiragana() {
148 myCollation.setStrength(Collator.QUATERNARY);
149 for (i = 0; i < 3 ; i++) {
150 doTest(testKatakanaHiraganaCases[i], testKatakanaHiraganaCases[i + 1], -1);
157 public void TestChooonKigoo() {
159 myCollation.setStrength(Collator.QUATERNARY);
160 for (i = 0; i < 7 ; i++) {
161 doTest(testChooonKigooCases[i], testChooonKigooCases[i + 1], -1);
166 * Test common Hiragana and Katakana characters (e.g. 0x3099) (ticket:6140)
168 public void TestCommonCharacters() {
169 char[] tmp1 = { 0x3058, 0x30B8 };
170 char[] tmp2 = { 0x3057, 0x3099, 0x30B7, 0x3099 };
171 CollationKey key1, key2;
173 String string1 = new String(tmp1);
174 String string2 = new String(tmp2);
175 RuleBasedCollator rb = (RuleBasedCollator)Collator.getInstance(ULocale.JAPANESE);
176 rb.setStrength(Collator.QUATERNARY);
177 rb.setAlternateHandlingShifted(false);
179 result = rb.compare(string1, string2);
181 key1 = rb.getCollationKey(string1);
182 key2 = rb.getCollationKey(string2);
184 if ( result != 0 || !key1.equals(key2)) {
185 errln("Failed Hiragana and Katakana common characters test. Expected results to be equal.");
189 // main test routine, tests rules specific to "Kana" locale
190 private void doTest(char[] source, char[] target, int result){
192 String s = new String(source);
193 String t = new String(target);
194 int compareResult = myCollation.compare(s, t);
195 CollationKey sortKey1, sortKey2;
196 sortKey1 = myCollation.getCollationKey(s);
197 sortKey2 = myCollation.getCollationKey(t);
198 int keyResult = sortKey1.compareTo(sortKey2);
199 reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
203 private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,
204 int compareResult, int keyResult, int incResult, int expectedResult ){
205 if (expectedResult < -1 || expectedResult > 1) {
206 errln("***** invalid call to reportCResult ****");
210 boolean ok1 = (compareResult == expectedResult);
211 boolean ok2 = (keyResult == expectedResult);
212 boolean ok3 = (incResult == expectedResult);
214 if (ok1 && ok2 && ok3 && !isVerbose()){
217 String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
218 String msg2 = "\", \"";
219 String msg3 = "\") returned ";
220 String msg4 = "; expected ";
222 String sExpect = new String("");
223 String sResult = new String("");
224 sResult = appendCompareResult(compareResult, sResult);
225 sExpect = appendCompareResult(expectedResult, sExpect);
227 logln(msg1 + source + msg2 + target + msg3 + sResult);
229 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
232 msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
233 msg2 = "\").compareTo(key(\"";
234 msg3 = "\")) returned ";
235 sResult = appendCompareResult(keyResult, sResult);
237 logln(msg1 + source + msg2 + target + msg3 + sResult);
239 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
242 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));
245 msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
247 msg3 = "\") returned ";
249 sResult = appendCompareResult(incResult, sResult);
252 logln(msg1 + source + msg2 + target + msg3 + sResult);
254 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
259 private String appendCompareResult(int result, String target){
262 } else if (result == 0) {
264 } else if (result == 1) {
268 target += huh + result;
273 String prettify(CollationKey sourceKey) {
275 byte[] bytes= sourceKey.toByteArray();
278 for (i = 0; i < bytes.length; i++) {
279 target += Integer.toHexString(bytes[i]);