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