001: /*
002: *******************************************************************************
003: * Copyright (C) 2002-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: /**
009: * Port From: ICU4C v2.1 : Collate/CollationGermanTest
010: * Source File: $ICU4CRoot/source/test/intltest/decoll.cpp
011: **/package com.ibm.icu.dev.test.collator;
012:
013: import com.ibm.icu.dev.test.*;
014: import com.ibm.icu.text.*;
015: import java.util.Locale;
016:
017: public class CollationGermanTest extends TestFmwk {
018: public static void main(String[] args) throws Exception {
019: new CollationGermanTest().run(args);
020: }
021:
022: private static char[][] testSourceCases = {
023: { 0x47, 0x72, 0x00F6, 0x00DF, 0x65 }, { 0x61, 0x62, 0x63 },
024: { 0x54, 0x00F6, 0x6e, 0x65 }, { 0x54, 0x00F6, 0x6e, 0x65 },
025: { 0x54, 0x00F6, 0x6e, 0x65 }, { 0x61, 0x0308, 0x62, 0x63 },
026: { 0x00E4, 0x62, 0x63 }, { 0x00E4, 0x62, 0x63 },
027: { 0x53, 0x74, 0x72, 0x61, 0x00DF, 0x65 },
028: { 0x65, 0x66, 0x67 }, { 0x00E4, 0x62, 0x63 },
029: { 0x53, 0x74, 0x72, 0x61, 0x00DF, 0x65 } };
030:
031: private static char[][] testTargetCases = {
032: { 0x47, 0x72, 0x6f, 0x73, 0x73, 0x69, 0x73, 0x74 },
033: { 0x61, 0x0308, 0x62, 0x63 }, { 0x54, 0x6f, 0x6e },
034: { 0x54, 0x6f, 0x64 }, { 0x54, 0x6f, 0x66, 0x75 },
035: { 0x41, 0x0308, 0x62, 0x63 }, { 0x61, 0x0308, 0x62, 0x63 },
036: { 0x61, 0x65, 0x62, 0x63 },
037: { 0x53, 0x74, 0x72, 0x61, 0x73, 0x73, 0x65 },
038: { 0x65, 0x66, 0x67 }, { 0x61, 0x65, 0x62, 0x63 },
039: { 0x53, 0x74, 0x72, 0x61, 0x73, 0x73, 0x65 } };
040:
041: private static int results[][] = {
042: // Primary Tertiary
043: { -1, -1 }, { 0, -1 }, { 1, 1 }, { 1, 1 }, { 1, 1 },
044: { 0, -1 }, { 0, 0 }, { -1, -1 }, { 0, 1 }, { 0, 0 },
045: { -1, -1 }, { 0, 1 } };
046:
047: private Collator myCollation = null;
048:
049: public CollationGermanTest() {
050:
051: }
052:
053: protected void init() throws Exception {
054: myCollation = Collator.getInstance(Locale.GERMAN);
055: if (myCollation == null) {
056: errln("ERROR: in creation of collator of GERMAN locale");
057: }
058: }
059:
060: // perform test with strength TERTIARY
061: public void TestTertiary() {
062: if (myCollation == null) {
063: errln("decoll: cannot start test, collator is null\n");
064: return;
065: }
066:
067: int i = 0;
068: myCollation.setStrength(Collator.TERTIARY);
069: myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
070: for (i = 0; i < 12; i++) {
071: doTest(testSourceCases[i], testTargetCases[i],
072: results[i][1]);
073: }
074: }
075:
076: // perform test with strength SECONDARY
077: //This method in icu4c has no implementation.
078: public void TestSecondary() {
079: }
080:
081: // perform test with strength PRIMARY
082: public void TestPrimary() {
083: if (myCollation == null) {
084: errln("decoll: cannot start test, collator is null\n");
085: return;
086: }
087: int i;
088: myCollation.setStrength(Collator.PRIMARY);
089: myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
090: for (i = 0; i < 12; i++) {
091: doTest(testSourceCases[i], testTargetCases[i],
092: results[i][0]);
093: }
094: }
095:
096: //main test routine, tests rules specific to germa locale
097: private void doTest(char[] source, char[] target, int result) {
098: String s = new String(source);
099: String t = new String(target);
100: int compareResult = myCollation.compare(s, t);
101: CollationKey sortKey1, sortKey2;
102: sortKey1 = myCollation.getCollationKey(s);
103: sortKey2 = myCollation.getCollationKey(t);
104: int keyResult = sortKey1.compareTo(sortKey2);
105: reportCResult(s, t, sortKey1, sortKey2, compareResult,
106: keyResult, compareResult, result);
107:
108: }
109:
110: private void reportCResult(String source, String target,
111: CollationKey sourceKey, CollationKey targetKey,
112: int compareResult, int keyResult, int incResult,
113: int expectedResult) {
114: if (expectedResult < -1 || expectedResult > 1) {
115: errln("***** invalid call to reportCResult ****");
116: return;
117: }
118:
119: boolean ok1 = (compareResult == expectedResult);
120: boolean ok2 = (keyResult == expectedResult);
121: boolean ok3 = (incResult == expectedResult);
122:
123: if (ok1 && ok2 && ok3 && !isVerbose()) {
124: return;
125: } else {
126: String msg1 = ok1 ? "Ok: compare(\"" : "FAIL: compare(\"";
127: String msg2 = "\", \"";
128: String msg3 = "\") returned ";
129: String msg4 = "; expected ";
130:
131: String sExpect = new String("");
132: String sResult = new String("");
133: sResult = appendCompareResult(compareResult, sResult);
134: sExpect = appendCompareResult(expectedResult, sExpect);
135: if (ok1) {
136: logln(msg1 + source + msg2 + target + msg3 + sResult);
137: } else {
138: errln(msg1 + source + msg2 + target + msg3 + sResult
139: + msg4 + sExpect);
140: }
141:
142: msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
143: msg2 = "\").compareTo(key(\"";
144: msg3 = "\")) returned ";
145: sResult = appendCompareResult(keyResult, sResult);
146: if (ok2) {
147: logln(msg1 + source + msg2 + target + msg3 + sResult);
148: } else {
149: errln(msg1 + source + msg2 + target + msg3 + sResult
150: + msg4 + sExpect);
151: msg1 = " ";
152: msg2 = " vs. ";
153: errln(msg1 + prettify(sourceKey) + msg2
154: + prettify(targetKey));
155: }
156:
157: msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
158: msg2 = "\", \"";
159: msg3 = "\") returned ";
160:
161: sResult = appendCompareResult(incResult, sResult);
162:
163: if (ok3) {
164: logln(msg1 + source + msg2 + target + msg3 + sResult);
165: } else {
166: errln(msg1 + source + msg2 + target + msg3 + sResult
167: + msg4 + sExpect);
168: }
169: }
170: }
171:
172: private String appendCompareResult(int result, String target) {
173: if (result == -1) //LESS
174: {
175: target += "LESS";
176: } else if (result == 0) //EQUAL
177: {
178: target += "EQUAL";
179: } else if (result == 1) //GREATER
180: {
181: target += "GREATER";
182: } else {
183: String huh = "?";
184:
185: target += huh + result;
186: }
187: return target;
188: }
189:
190: String prettify(CollationKey sourceKey) {
191: int i;
192: byte[] bytes = sourceKey.toByteArray();
193: String target = "[";
194:
195: for (i = 0; i < bytes.length; i++) {
196: target += Integer.toHexString(bytes[i]);
197: target += " ";
198: }
199: target += "]";
200: return target;
201: }
202: }
|