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