1// © 2016 and later: Unicode, Inc. and others. 2// License & terms of use: http://www.unicode.org/copyright.html#License 3/* 4 ******************************************************************************* 5 * Copyright (C) 2002-2014, International Business Machines Corporation and 6 * others. All Rights Reserved. 7 ******************************************************************************* 8 */ 9 10/** 11 * Port From: ICU4C v2.1 : Collate/CollationKanaTest 12 * Source File: $ICU4CRoot/source/test/intltest/jacoll.cpp 13 **/ 14 15package com.ibm.icu.dev.test.collator; 16 17import java.util.Locale; 18 19import org.junit.Before; 20import org.junit.Test; 21 22import com.ibm.icu.dev.test.TestFmwk; 23import com.ibm.icu.text.CollationKey; 24import com.ibm.icu.text.Collator; 25import com.ibm.icu.text.RuleBasedCollator; 26import com.ibm.icu.util.ULocale; 27 28public class CollationKanaTest extends TestFmwk{ 29 private static char[][] testSourceCases = { 30 {0xff9E}, 31 {0x3042}, 32 {0x30A2}, 33 {0x3042, 0x3042}, 34 {0x30A2, 0x30FC}, 35 {0x30A2, 0x30FC, 0x30C8} /* 6 */ 36 }; 37 38 private static char[][] testTargetCases = { 39 {0xFF9F}, 40 {0x30A2}, 41 {0x3042, 0x3042}, 42 {0x30A2, 0x30FC}, 43 {0x30A2, 0x30FC, 0x30C8}, 44 {0x3042, 0x3042, 0x3068} /* 6 */ 45 }; 46 47 private static int[] results = { 48 -1, 49 0, //Collator::LESS, /* Katakanas and Hiraganas are equal on tertiary level(ICU 2.0)*/ 50 -1, 51 1, // Collator::LESS, /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*/ 52 -1, 53 -1, //Collator::GREATER /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*//* 6 */ 54 }; 55 56 private static char[][] testBaseCases = { 57 {0x30AB}, 58 {0x30AB, 0x30AD}, 59 {0x30AD}, 60 {0x30AD, 0x30AD} 61 }; 62 63 private static char[][] testPlainDakutenHandakutenCases = { 64 {0x30CF, 0x30AB}, 65 {0x30D0, 0x30AB}, 66 {0x30CF, 0x30AD}, 67 {0x30D0, 0x30AD} 68 }; 69 70 private static char[][] testSmallLargeCases = { 71 {0x30C3, 0x30CF}, 72 {0x30C4, 0x30CF}, 73 {0x30C3, 0x30D0}, 74 {0x30C4, 0x30D0} 75 }; 76 77 private static char[][] testKatakanaHiraganaCases = { 78 {0x3042, 0x30C3}, 79 {0x30A2, 0x30C3}, 80 {0x3042, 0x30C4}, 81 {0x30A2, 0x30C4} 82 }; 83 84 private static char[][] testChooonKigooCases = { 85 /*0*/ {0x30AB, 0x30FC, 0x3042}, 86 /*1*/ {0x30AB, 0x30FC, 0x30A2}, 87 /*2*/ {0x30AB, 0x30A4, 0x3042}, 88 /*3*/ {0x30AB, 0x30A4, 0x30A2}, 89 /*6*/ {0x30AD, 0x30FC, 0x3042}, /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*/ 90 /*7*/ {0x30AD, 0x30FC, 0x30A2}, /* Prolonged sound mark sorts BEFORE equivalent vowel (ICU 2.0)*/ 91 /*4*/ {0x30AD, 0x30A4, 0x3042}, 92 /*5*/ {0x30AD, 0x30A4, 0x30A2} 93 }; 94 95 private Collator myCollation = null; 96 97 public CollationKanaTest() { 98 } 99 100 @Before 101 public void init()throws Exception { 102 if(myCollation==null){ 103 myCollation = Collator.getInstance(Locale.JAPANESE); 104 } 105 } 106 107 // performs test with strength TERIARY 108 @Test 109 public void TestTertiary() { 110 int i = 0; 111 myCollation.setStrength(Collator.TERTIARY); 112 113 for (i = 0; i < 6; i++) { 114 doTest(testSourceCases[i], testTargetCases[i], results[i]); 115 } 116 } 117 118 /* Testing base letters */ 119 @Test 120 public void TestBase() { 121 int i; 122 myCollation.setStrength(Collator.PRIMARY); 123 for (i = 0; i < 3 ; i++) { 124 doTest(testBaseCases[i], testBaseCases[i + 1], -1); 125 } 126 } 127 128 /* Testing plain, Daku-ten, Handaku-ten letters */ 129 @Test 130 public void TestPlainDakutenHandakuten() { 131 int i; 132 myCollation.setStrength(Collator.SECONDARY); 133 for (i = 0; i < 3 ; i++) { 134 doTest(testPlainDakutenHandakutenCases[i], testPlainDakutenHandakutenCases[i + 1], -1); 135 } 136 } 137 138 /* 139 * Test Small, Large letters 140 */ 141 @Test 142 public void TestSmallLarge() { 143 int i; 144 myCollation.setStrength(Collator.TERTIARY); 145 146 for (i = 0; i < 3 ; i++) { 147 doTest(testSmallLargeCases[i], testSmallLargeCases[i + 1], -1); 148 } 149 } 150 151 /* 152 * Test Katakana, Hiragana letters 153 */ 154 @Test 155 public void TestKatakanaHiragana() { 156 int i; 157 myCollation.setStrength(Collator.QUATERNARY); 158 for (i = 0; i < 3 ; i++) { 159 doTest(testKatakanaHiraganaCases[i], testKatakanaHiraganaCases[i + 1], -1); 160 } 161 } 162 163 /* 164 * Test Choo-on kigoo 165 */ 166 @Test 167 public void TestChooonKigoo() { 168 int i; 169 myCollation.setStrength(Collator.QUATERNARY); 170 for (i = 0; i < 7 ; i++) { 171 doTest(testChooonKigooCases[i], testChooonKigooCases[i + 1], -1); 172 } 173 } 174 175 /* 176 * Test common Hiragana and Katakana characters (e.g. 0x3099) (ticket:6140) 177 */ 178 @Test 179 public void TestCommonCharacters() { 180 char[] tmp1 = { 0x3058, 0x30B8 }; 181 char[] tmp2 = { 0x3057, 0x3099, 0x30B7, 0x3099 }; 182 CollationKey key1, key2; 183 int result; 184 String string1 = new String(tmp1); 185 String string2 = new String(tmp2); 186 RuleBasedCollator rb = (RuleBasedCollator)Collator.getInstance(ULocale.JAPANESE); 187 rb.setStrength(Collator.QUATERNARY); 188 rb.setAlternateHandlingShifted(false); 189 190 result = rb.compare(string1, string2); 191 192 key1 = rb.getCollationKey(string1); 193 key2 = rb.getCollationKey(string2); 194 195 if ( result != 0 || !key1.equals(key2)) { 196 errln("Failed Hiragana and Katakana common characters test. Expected results to be equal."); 197 } 198 199 } 200 // main test routine, tests rules specific to "Kana" locale 201 private void doTest(char[] source, char[] target, int result){ 202 203 String s = new String(source); 204 String t = new String(target); 205 int compareResult = myCollation.compare(s, t); 206 CollationKey sortKey1, sortKey2; 207 sortKey1 = myCollation.getCollationKey(s); 208 sortKey2 = myCollation.getCollationKey(t); 209 int keyResult = sortKey1.compareTo(sortKey2); 210 reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result); 211 212 } 213 214 private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey, 215 int compareResult, int keyResult, int incResult, int expectedResult ){ 216 if (expectedResult < -1 || expectedResult > 1) { 217 errln("***** invalid call to reportCResult ****"); 218 return; 219 } 220 221 boolean ok1 = (compareResult == expectedResult); 222 boolean ok2 = (keyResult == expectedResult); 223 boolean ok3 = (incResult == expectedResult); 224 225 if (ok1 && ok2 && ok3 && !isVerbose()){ 226 return; 227 } else { 228 String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\""; 229 String msg2 = "\", \""; 230 String msg3 = "\") returned "; 231 String msg4 = "; expected "; 232 233 String sExpect = new String(""); 234 String sResult = new String(""); 235 sResult = CollationTest.appendCompareResult(compareResult, sResult); 236 sExpect = CollationTest.appendCompareResult(expectedResult, sExpect); 237 if (ok1) { 238 logln(msg1 + source + msg2 + target + msg3 + sResult); 239 } else { 240 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect); 241 } 242 243 msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\""; 244 msg2 = "\").compareTo(key(\""; 245 msg3 = "\")) returned "; 246 sResult = CollationTest.appendCompareResult(keyResult, sResult); 247 if (ok2) { 248 logln(msg1 + source + msg2 + target + msg3 + sResult); 249 } else { 250 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect); 251 msg1 = " "; 252 msg2 = " vs. "; 253 errln(msg1 + CollationTest.prettify(sourceKey) + msg2 + CollationTest.prettify(targetKey)); 254 } 255 256 msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\""; 257 msg2 = "\", \""; 258 msg3 = "\") returned "; 259 260 sResult = CollationTest.appendCompareResult(incResult, sResult); 261 262 if (ok3) { 263 logln(msg1 + source + msg2 + target + msg3 + sResult); 264 } else { 265 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect); 266 } 267 } 268 } 269} 270