]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/collator/CollationGermanTest.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / collator / CollationGermanTest.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2002-2005, International Business Machines Corporation and    *\r
4  * others. All Rights Reserved.                                                *\r
5  *******************************************************************************\r
6  */\r
7 \r
8 /** \r
9  * Port From:   ICU4C v2.1 : Collate/CollationGermanTest\r
10  * Source File: $ICU4CRoot/source/test/intltest/decoll.cpp\r
11  **/\r
12  \r
13  package com.ibm.icu.dev.test.collator;\r
14  \r
15  import com.ibm.icu.dev.test.*;\r
16  import com.ibm.icu.text.*;\r
17  import java.util.Locale;\r
18  \r
19  public class CollationGermanTest extends TestFmwk{\r
20     public static void main(String[] args) throws Exception{\r
21         new CollationGermanTest().run(args);\r
22     }\r
23     \r
24     private static char[][] testSourceCases = {\r
25         {0x47, 0x72, 0x00F6, 0x00DF, 0x65},\r
26         {0x61, 0x62, 0x63},\r
27         {0x54, 0x00F6, 0x6e, 0x65},\r
28         {0x54, 0x00F6, 0x6e, 0x65},\r
29         {0x54, 0x00F6, 0x6e, 0x65},\r
30         {0x61, 0x0308, 0x62, 0x63},\r
31         {0x00E4, 0x62, 0x63},\r
32         {0x00E4, 0x62, 0x63},\r
33         {0x53, 0x74, 0x72, 0x61, 0x00DF, 0x65},\r
34         {0x65, 0x66, 0x67},\r
35         {0x00E4, 0x62, 0x63},\r
36         {0x53, 0x74, 0x72, 0x61, 0x00DF, 0x65}\r
37     };\r
38 \r
39     private static char[][] testTargetCases = {\r
40         {0x47, 0x72, 0x6f, 0x73, 0x73, 0x69, 0x73, 0x74},\r
41         {0x61, 0x0308, 0x62, 0x63},\r
42         {0x54, 0x6f, 0x6e},\r
43         {0x54, 0x6f, 0x64},\r
44         {0x54, 0x6f, 0x66, 0x75},\r
45         {0x41, 0x0308, 0x62, 0x63},\r
46         {0x61, 0x0308, 0x62, 0x63},\r
47         {0x61, 0x65, 0x62, 0x63},\r
48         {0x53, 0x74, 0x72, 0x61, 0x73, 0x73, 0x65},\r
49         {0x65, 0x66, 0x67},\r
50         {0x61, 0x65, 0x62, 0x63},\r
51         {0x53, 0x74, 0x72, 0x61, 0x73, 0x73, 0x65}\r
52     };\r
53 \r
54     private static int results[][] =\r
55     {\r
56         //  Primary  Tertiary\r
57         { -1,        -1 },\r
58         { 0,         -1 },\r
59         { 1,          1 },\r
60         { 1,          1 },\r
61         { 1,          1 },\r
62         { 0,         -1 },\r
63         { 0,          0 },\r
64         { -1,        -1 },\r
65         { 0,          1 },\r
66         { 0,          0 },\r
67         { -1,        -1 },\r
68         { 0,          1 }\r
69     };\r
70     \r
71     private Collator myCollation = null;\r
72     \r
73     public CollationGermanTest() {\r
74 \r
75     }\r
76     protected void init() throws Exception{\r
77         myCollation = Collator.getInstance(Locale.GERMAN);\r
78         if(myCollation == null) {\r
79             errln("ERROR: in creation of collator of GERMAN locale");\r
80         }\r
81     }\r
82     // perform test with strength TERTIARY\r
83     public void TestTertiary(){\r
84         if(myCollation == null ) {\r
85             errln("decoll: cannot start test, collator is null\n");\r
86             return;\r
87         }\r
88 \r
89         int i = 0;\r
90         myCollation.setStrength(Collator.TERTIARY);\r
91         myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION);\r
92         for (i = 0; i < 12 ; i++)\r
93         {\r
94             doTest(testSourceCases[i], testTargetCases[i], results[i][1]);\r
95         }\r
96     }\r
97     \r
98     // perform test with strength SECONDARY\r
99     //This method in icu4c has no implementation.\r
100     public void TestSecondary(){\r
101     }\r
102     \r
103      // perform test with strength PRIMARY\r
104     public void TestPrimary(){\r
105         if(myCollation == null ) {\r
106             errln("decoll: cannot start test, collator is null\n");\r
107             return;\r
108         }\r
109         int i;\r
110         myCollation.setStrength(Collator.PRIMARY);\r
111         myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION);\r
112         for (i = 0; i < 12 ; i++)\r
113         {\r
114             doTest(testSourceCases[i], testTargetCases[i], results[i][0]);\r
115         }\r
116     }\r
117     \r
118     \r
119     //main test routine, tests rules specific to germa locale\r
120     private void doTest(char[] source, char[] target, int result){\r
121         String s = new String(source);\r
122         String t = new String(target);\r
123         int compareResult = myCollation.compare(s, t);\r
124         CollationKey sortKey1, sortKey2;\r
125         sortKey1 = myCollation.getCollationKey(s);\r
126         sortKey2 = myCollation.getCollationKey(t);\r
127         int keyResult = sortKey1.compareTo(sortKey2);\r
128         reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);\r
129         \r
130     }\r
131     \r
132     private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,\r
133                                 int compareResult, int keyResult, int incResult, int expectedResult ){\r
134         if (expectedResult < -1 || expectedResult > 1)\r
135         {\r
136             errln("***** invalid call to reportCResult ****");\r
137             return;\r
138         }\r
139 \r
140         boolean ok1 = (compareResult == expectedResult);\r
141         boolean ok2 = (keyResult == expectedResult);\r
142         boolean ok3 = (incResult == expectedResult);\r
143 \r
144         if (ok1 && ok2 && ok3 && !isVerbose()){\r
145             return;    \r
146         }else{\r
147             String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";\r
148             String msg2 = "\", \"";\r
149             String msg3 = "\") returned ";\r
150             String msg4 = "; expected ";\r
151             \r
152             String sExpect = new String("");\r
153             String sResult = new String("");\r
154             sResult = appendCompareResult(compareResult, sResult);\r
155             sExpect = appendCompareResult(expectedResult, sExpect);\r
156             if (ok1) {\r
157                 logln(msg1 + source + msg2 + target + msg3 + sResult);\r
158             } else {\r
159                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
160             }\r
161             \r
162             msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";\r
163             msg2 = "\").compareTo(key(\"";\r
164             msg3 = "\")) returned ";\r
165             sResult = appendCompareResult(keyResult, sResult);\r
166             if (ok2) {\r
167                 logln(msg1 + source + msg2 + target + msg3 + sResult);\r
168             } else {\r
169                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
170                 msg1 = "  ";\r
171                 msg2 = " vs. ";\r
172                 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));\r
173             }\r
174             \r
175             msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";\r
176             msg2 = "\", \"";\r
177             msg3 = "\") returned ";\r
178 \r
179             sResult = appendCompareResult(incResult, sResult);\r
180 \r
181             if (ok3) {\r
182                 logln(msg1 + source + msg2 + target + msg3 + sResult);\r
183             } else {\r
184                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
185             }                \r
186         }\r
187     }\r
188     \r
189     private String appendCompareResult(int result, String target){\r
190         if (result == -1)   //LESS\r
191         {\r
192             target += "LESS";\r
193         }\r
194         else if (result == 0)   //EQUAL\r
195         {\r
196             target += "EQUAL";\r
197         }\r
198         else if (result == 1)   //GREATER\r
199         {\r
200             target += "GREATER";\r
201         }\r
202         else\r
203         {\r
204             String huh = "?";\r
205 \r
206             target += huh + result;\r
207         }\r
208         return target;\r
209     }\r
210     \r
211     String prettify(CollationKey sourceKey) {\r
212         int i;\r
213         byte[] bytes= sourceKey.toByteArray();\r
214         String target = "[";\r
215     \r
216         for (i = 0; i < bytes.length; i++) {\r
217             target += Integer.toHexString(bytes[i]);\r
218             target += " ";\r
219         }\r
220         target += "]";\r
221         return target;\r
222     }\r
223 }