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/CollationFinnishTest
010: * Source File: $ICU4CRoot/source/test/intltest/ficoll.cpp
011: **/package com.ibm.icu.dev.test.collator;
012:
013: import com.ibm.icu.dev.test.*;
014: import com.ibm.icu.text.*;
015:
016: import java.util.Locale;
017:
018: public class CollationFinnishTest extends TestFmwk {
019: public static void main(String[] args) throws Exception {
020: new CollationFinnishTest().run(args);
021: }
022:
023: private static char[][] testSourceCases = { { 0x77, 0x61, 0x74 },
024: { 0x76, 0x61, 0x74 },
025: { 0x61, 0x00FC, 0x62, 0x65, 0x63, 0x6b },
026: { 0x4c, 0x00E5, 0x76, 0x69 }, { 0x77, 0x61, 0x74 } };
027:
028: private static char[][] testTargetCases = { { 0x76, 0x61, 0x74 },
029: { 0x77, 0x61, 0x79 },
030: { 0x61, 0x78, 0x62, 0x65, 0x63, 0x6b },
031: { 0x4c, 0x00E4, 0x77, 0x65 }, { 0x76, 0x61, 0x74 } };
032:
033: private static int[] results = { 1, -1, 1, -1,
034: // test primary > 4
035: 0, //collation rules for finnish have changed!
036: };
037:
038: private Collator myCollation = null;
039:
040: public CollationFinnishTest() {
041: }
042:
043: protected void init() throws Exception {
044: myCollation = Collator.getInstance(new Locale("fi", "FI"));
045: }
046:
047: // perform tests with strength PRIMARY
048: public void TestPrimary() {
049: int i = 0;
050: myCollation.setStrength(Collator.PRIMARY);
051: for (i = 4; i < 5; i++) {
052: doTest(testSourceCases[i], testTargetCases[i], results[i]);
053: }
054: }
055:
056: // perform test with strength TERTIARY
057: public void TestTertiary() {
058: int i = 0;
059: myCollation.setStrength(Collator.TERTIARY);
060: for (i = 0; i < 4; i++) {
061: doTest(testSourceCases[i], testTargetCases[i], results[i]);
062: }
063: }
064:
065: // main test routine, tests rules specific to the finish locale
066: private void doTest(char[] source, char[] target, int result) {
067: String s = new String(source);
068: String t = new String(target);
069: int compareResult = myCollation.compare(s, t);
070: CollationKey sortKey1, sortKey2;
071: sortKey1 = myCollation.getCollationKey(s);
072: sortKey2 = myCollation.getCollationKey(t);
073: int keyResult = sortKey1.compareTo(sortKey2);
074: reportCResult(s, t, sortKey1, sortKey2, compareResult,
075: keyResult, compareResult, result);
076: }
077:
078: private void reportCResult(String source, String target,
079: CollationKey sourceKey, CollationKey targetKey,
080: int compareResult, int keyResult, int incResult,
081: int expectedResult) {
082: if (expectedResult < -1 || expectedResult > 1) {
083: errln("***** invalid call to reportCResult ****");
084: return;
085: }
086:
087: boolean ok1 = (compareResult == expectedResult);
088: boolean ok2 = (keyResult == expectedResult);
089: boolean ok3 = (incResult == expectedResult);
090:
091: if (ok1 && ok2 && ok3 && !isVerbose()) {
092: return;
093: } else {
094: String msg1 = ok1 ? "Ok: compare(\"" : "FAIL: compare(\"";
095: String msg2 = "\", \"";
096: String msg3 = "\") returned ";
097: String msg4 = "; expected ";
098:
099: String sExpect = new String("");
100: String sResult = new String("");
101: sResult = appendCompareResult(compareResult, sResult);
102: sExpect = appendCompareResult(expectedResult, sExpect);
103: if (ok1) {
104: logln(msg1 + source + msg2 + target + msg3 + sResult);
105: } else {
106: errln(msg1 + source + msg2 + target + msg3 + sResult
107: + msg4 + sExpect);
108: }
109:
110: msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
111: msg2 = "\").compareTo(key(\"";
112: msg3 = "\")) returned ";
113: sResult = appendCompareResult(keyResult, sResult);
114: if (ok2) {
115: logln(msg1 + source + msg2 + target + msg3 + sResult);
116: } else {
117: errln(msg1 + source + msg2 + target + msg3 + sResult
118: + msg4 + sExpect);
119: msg1 = " ";
120: msg2 = " vs. ";
121: errln(msg1 + prettify(sourceKey) + msg2
122: + prettify(targetKey));
123: }
124:
125: msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
126: msg2 = "\", \"";
127: msg3 = "\") returned ";
128:
129: sResult = appendCompareResult(incResult, sResult);
130:
131: if (ok3) {
132: logln(msg1 + source + msg2 + target + msg3 + sResult);
133: } else {
134: errln(msg1 + source + msg2 + target + msg3 + sResult
135: + msg4 + sExpect);
136: }
137: }
138: }
139:
140: private String appendCompareResult(int result, String target) {
141: if (result == -1) {
142: target += "LESS";
143: } else if (result == 0) {
144: target += "EQUAL";
145: } else if (result == 1) {
146: target += "GREATER";
147: } else {
148: String huh = "?";
149: target += huh + result;
150: }
151: return target;
152: }
153:
154: String prettify(CollationKey sourceKey) {
155: int i;
156: byte[] bytes = sourceKey.toByteArray();
157: String target = "[";
158:
159: for (i = 0; i < bytes.length; i++) {
160: target += Integer.toHexString(bytes[i]);
161: target += " ";
162: }
163: target += "]";
164: return target;
165: }
166: }
|