]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/collate/src/com/ibm/icu/dev/test/collator/LotusCollationKoreanTest.java
Clean up imports.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / collate / src / com / ibm / icu / dev / test / collator / LotusCollationKoreanTest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2002-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7
8 /** 
9  * Port From:   ICU4C v2.1 : Collate/LotusCollationKoreanTest
10  * Source File: $ICU4CRoot/source/test/intltest/lcukocol.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  
21 public class LotusCollationKoreanTest extends TestFmwk{
22     public static void main(String[] args) throws Exception{
23         new LotusCollationKoreanTest().run(args);
24     }
25     
26     private static char[][] testSourceCases = {
27         {0xac00}
28     };
29     
30     private static char[][] testTargetCases = {
31         {0xac01}
32     };
33     
34     private static int[] results = {
35         -1
36     };
37     
38     private Collator myCollation;
39     
40     public LotusCollationKoreanTest() {
41     }
42     protected void init()throws Exception{
43         myCollation = Collator.getInstance(Locale.KOREAN);
44         myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
45     }
46     // performs test with strength TERIARY
47     public void TestTertiary() {
48         int i = 0;
49         myCollation.setStrength(Collator.TERTIARY);
50         for (i = 0; i < 1; i++) {
51             doTest(testSourceCases[i], testTargetCases[i], results[i]);
52             }
53         }
54     
55     // main test routine, tests rules specific to "Korean" locale
56     private void doTest( char[] source, char[] target, int result) {
57         String s = new String(source);
58         String t = new String(target);
59         int compareResult = myCollation.compare(s, t);
60         CollationKey sortKey1, sortKey2;
61         sortKey1 = myCollation.getCollationKey(s);
62         sortKey2 = myCollation.getCollationKey(t);
63         int keyResult = sortKey1.compareTo(sortKey2);
64         reportCResult( s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result ); 
65     }
66
67     private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,
68                                 int compareResult, int keyResult, int incResult, int expectedResult ){
69         if (expectedResult < -1 || expectedResult > 1) {
70             errln("***** invalid call to reportCResult ****");
71             return;
72         }
73
74         boolean ok1 = (compareResult == expectedResult);
75         boolean ok2 = (keyResult == expectedResult);
76         boolean ok3 = (incResult == expectedResult);
77
78         if (ok1 && ok2 && ok3 && !isVerbose()) {
79             return;    
80         } else {
81             String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
82             String msg2 = "\", \"";
83             String msg3 = "\") returned ";
84             String msg4 = "; expected ";
85             
86             String sExpect = new String("");
87             String sResult = new String("");
88             sResult = appendCompareResult(compareResult, sResult);
89             sExpect = appendCompareResult(expectedResult, sExpect);
90             if (ok1) {
91                 logln(msg1 + source + msg2 + target + msg3 + sResult);
92             } else {
93                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
94             }
95             
96             msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
97             msg2 = "\").compareTo(key(\"";
98             msg3 = "\")) returned ";
99             sResult = appendCompareResult(keyResult, sResult);
100             if (ok2) {
101                 logln(msg1 + source + msg2 + target + msg3 + sResult);
102             } else {
103                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
104                 msg1 = "  ";
105                 msg2 = " vs. ";
106                 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));
107             }
108             
109             msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
110             msg2 = "\", \"";
111             msg3 = "\") returned ";
112
113             sResult = appendCompareResult(incResult, sResult);
114
115             if (ok3) {
116                 logln(msg1 + source + msg2 + target + msg3 + sResult);
117             } else {
118                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
119             }                
120         }
121     }
122     
123     private String appendCompareResult(int result, String target){
124         if (result == -1) {
125             target += "LESS";
126         } else if (result == 0) {
127             target += "EQUAL";
128         } else if (result == 1) {
129             target += "GREATER";
130         } else {
131             String huh = "?";
132             target += huh + result;
133         }
134         return target;
135     }
136     
137     String prettify(CollationKey sourceKey) {
138         int i;
139         byte[] bytes= sourceKey.toByteArray();
140         String target = "[";
141     
142         for (i = 0; i < bytes.length; i++) {
143             target += Integer.toHexString(bytes[i]);
144             target += " ";
145         }
146         target += "]";
147         return target;
148     }
149 }