2 *******************************************************************************
\r
3 * Copyright (C) 2002-2010, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
9 * Port From: ICU4C v2.1 : Collate/CollationGermanTest
\r
10 * Source File: $ICU4CRoot/source/test/intltest/decoll.cpp
\r
13 package com.ibm.icu.dev.test.collator;
\r
15 import java.util.Locale;
\r
17 import com.ibm.icu.dev.test.TestFmwk;
\r
18 import com.ibm.icu.text.CollationKey;
\r
19 import com.ibm.icu.text.Collator;
\r
21 public class CollationGermanTest extends TestFmwk{
\r
22 public static void main(String[] args) throws Exception{
\r
23 new CollationGermanTest().run(args);
\r
26 private static char[][] testSourceCases = {
\r
27 {0x47, 0x72, 0x00F6, 0x00DF, 0x65},
\r
29 {0x54, 0x00F6, 0x6e, 0x65},
\r
30 {0x54, 0x00F6, 0x6e, 0x65},
\r
31 {0x54, 0x00F6, 0x6e, 0x65},
\r
32 {0x61, 0x0308, 0x62, 0x63},
\r
33 {0x00E4, 0x62, 0x63},
\r
34 {0x00E4, 0x62, 0x63},
\r
35 {0x53, 0x74, 0x72, 0x61, 0x00DF, 0x65},
\r
37 {0x00E4, 0x62, 0x63},
\r
38 {0x53, 0x74, 0x72, 0x61, 0x00DF, 0x65}
\r
41 private static char[][] testTargetCases = {
\r
42 {0x47, 0x72, 0x6f, 0x73, 0x73, 0x69, 0x73, 0x74},
\r
43 {0x61, 0x0308, 0x62, 0x63},
\r
46 {0x54, 0x6f, 0x66, 0x75},
\r
47 {0x41, 0x0308, 0x62, 0x63},
\r
48 {0x61, 0x0308, 0x62, 0x63},
\r
49 {0x61, 0x65, 0x62, 0x63},
\r
50 {0x53, 0x74, 0x72, 0x61, 0x73, 0x73, 0x65},
\r
52 {0x61, 0x65, 0x62, 0x63},
\r
53 {0x53, 0x74, 0x72, 0x61, 0x73, 0x73, 0x65}
\r
56 private static int results[][] =
\r
73 private Collator myCollation = null;
\r
75 public CollationGermanTest() {
\r
78 protected void init() throws Exception{
\r
79 myCollation = Collator.getInstance(Locale.GERMAN);
\r
80 if(myCollation == null) {
\r
81 errln("ERROR: in creation of collator of GERMAN locale");
\r
84 // perform test with strength TERTIARY
\r
85 public void TestTertiary(){
\r
86 if(myCollation == null ) {
\r
87 errln("decoll: cannot start test, collator is null\n");
\r
92 myCollation.setStrength(Collator.TERTIARY);
\r
93 myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
\r
94 for (i = 0; i < 12 ; i++)
\r
96 doTest(testSourceCases[i], testTargetCases[i], results[i][1]);
\r
100 // perform test with strength SECONDARY
\r
101 //This method in icu4c has no implementation.
\r
102 public void TestSecondary(){
\r
105 // perform test with strength PRIMARY
\r
106 public void TestPrimary(){
\r
107 if(myCollation == null ) {
\r
108 errln("decoll: cannot start test, collator is null\n");
\r
112 myCollation.setStrength(Collator.PRIMARY);
\r
113 myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
\r
114 for (i = 0; i < 12 ; i++)
\r
116 doTest(testSourceCases[i], testTargetCases[i], results[i][0]);
\r
121 //main test routine, tests rules specific to germa locale
\r
122 private void doTest(char[] source, char[] target, int result){
\r
123 String s = new String(source);
\r
124 String t = new String(target);
\r
125 int compareResult = myCollation.compare(s, t);
\r
126 CollationKey sortKey1, sortKey2;
\r
127 sortKey1 = myCollation.getCollationKey(s);
\r
128 sortKey2 = myCollation.getCollationKey(t);
\r
129 int keyResult = sortKey1.compareTo(sortKey2);
\r
130 reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
\r
134 private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,
\r
135 int compareResult, int keyResult, int incResult, int expectedResult ){
\r
136 if (expectedResult < -1 || expectedResult > 1)
\r
138 errln("***** invalid call to reportCResult ****");
\r
142 boolean ok1 = (compareResult == expectedResult);
\r
143 boolean ok2 = (keyResult == expectedResult);
\r
144 boolean ok3 = (incResult == expectedResult);
\r
146 if (ok1 && ok2 && ok3 && !isVerbose()){
\r
149 String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
\r
150 String msg2 = "\", \"";
\r
151 String msg3 = "\") returned ";
\r
152 String msg4 = "; expected ";
\r
154 String sExpect = new String("");
\r
155 String sResult = new String("");
\r
156 sResult = appendCompareResult(compareResult, sResult);
\r
157 sExpect = appendCompareResult(expectedResult, sExpect);
\r
159 logln(msg1 + source + msg2 + target + msg3 + sResult);
\r
161 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
164 msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
\r
165 msg2 = "\").compareTo(key(\"";
\r
166 msg3 = "\")) returned ";
\r
167 sResult = appendCompareResult(keyResult, sResult);
\r
169 logln(msg1 + source + msg2 + target + msg3 + sResult);
\r
171 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
174 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));
\r
177 msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
\r
179 msg3 = "\") returned ";
\r
181 sResult = appendCompareResult(incResult, sResult);
\r
184 logln(msg1 + source + msg2 + target + msg3 + sResult);
\r
186 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
191 private String appendCompareResult(int result, String target){
\r
192 if (result == -1) //LESS
\r
196 else if (result == 0) //EQUAL
\r
200 else if (result == 1) //GREATER
\r
202 target += "GREATER";
\r
208 target += huh + result;
\r
213 String prettify(CollationKey sourceKey) {
\r
215 byte[] bytes= sourceKey.toByteArray();
\r
216 String target = "[";
\r
218 for (i = 0; i < bytes.length; i++) {
\r
219 target += Integer.toHexString(bytes[i]);
\r