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/LotusCollationKoreanTest
010: * Source File: $ICU4CRoot/source/test/intltest/lcukocol.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 LotusCollationKoreanTest extends TestFmwk {
019: public static void main(String[] args) throws Exception {
020: new LotusCollationKoreanTest().run(args);
021: }
022:
023: private static char[][] testSourceCases = { { 0xac00 } };
024:
025: private static char[][] testTargetCases = { { 0xac01 } };
026:
027: private static int[] results = { -1 };
028:
029: private Collator myCollation;
030:
031: public LotusCollationKoreanTest() {
032: }
033:
034: protected void init() throws Exception {
035: myCollation = Collator.getInstance(Locale.KOREAN);
036: myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
037: }
038:
039: // performs test with strength TERIARY
040: public void TestTertiary() {
041: int i = 0;
042: myCollation.setStrength(Collator.TERTIARY);
043: for (i = 0; i < 1; i++) {
044: doTest(testSourceCases[i], testTargetCases[i], results[i]);
045: }
046: }
047:
048: // main test routine, tests rules specific to "Korean" locale
049: private void doTest(char[] source, char[] target, int result) {
050: String s = new String(source);
051: String t = new String(target);
052: int compareResult = myCollation.compare(s, t);
053: CollationKey sortKey1, sortKey2;
054: sortKey1 = myCollation.getCollationKey(s);
055: sortKey2 = myCollation.getCollationKey(t);
056: int keyResult = sortKey1.compareTo(sortKey2);
057: reportCResult(s, t, sortKey1, sortKey2, compareResult,
058: keyResult, compareResult, result);
059: }
060:
061: private void reportCResult(String source, String target,
062: CollationKey sourceKey, CollationKey targetKey,
063: int compareResult, int keyResult, int incResult,
064: int expectedResult) {
065: if (expectedResult < -1 || expectedResult > 1) {
066: errln("***** invalid call to reportCResult ****");
067: return;
068: }
069:
070: boolean ok1 = (compareResult == expectedResult);
071: boolean ok2 = (keyResult == expectedResult);
072: boolean ok3 = (incResult == expectedResult);
073:
074: if (ok1 && ok2 && ok3 && !isVerbose()) {
075: return;
076: } else {
077: String msg1 = ok1 ? "Ok: compare(\"" : "FAIL: compare(\"";
078: String msg2 = "\", \"";
079: String msg3 = "\") returned ";
080: String msg4 = "; expected ";
081:
082: String sExpect = new String("");
083: String sResult = new String("");
084: sResult = appendCompareResult(compareResult, sResult);
085: sExpect = appendCompareResult(expectedResult, sExpect);
086: if (ok1) {
087: logln(msg1 + source + msg2 + target + msg3 + sResult);
088: } else {
089: errln(msg1 + source + msg2 + target + msg3 + sResult
090: + msg4 + sExpect);
091: }
092:
093: msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
094: msg2 = "\").compareTo(key(\"";
095: msg3 = "\")) returned ";
096: sResult = appendCompareResult(keyResult, sResult);
097: if (ok2) {
098: logln(msg1 + source + msg2 + target + msg3 + sResult);
099: } else {
100: errln(msg1 + source + msg2 + target + msg3 + sResult
101: + msg4 + sExpect);
102: msg1 = " ";
103: msg2 = " vs. ";
104: errln(msg1 + prettify(sourceKey) + msg2
105: + prettify(targetKey));
106: }
107:
108: msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
109: msg2 = "\", \"";
110: msg3 = "\") returned ";
111:
112: sResult = appendCompareResult(incResult, sResult);
113:
114: if (ok3) {
115: logln(msg1 + source + msg2 + target + msg3 + sResult);
116: } else {
117: errln(msg1 + source + msg2 + target + msg3 + sResult
118: + msg4 + sExpect);
119: }
120: }
121: }
122:
123: private String appendCompareResult(int result, String target) {
124: if (result == -1) {
125: target += "LESS";
126: } else if (result == 0) {
127: target += "EQUAL";
128: } else if (result == 1) {
129: target += "GREATER";
130: } else {
131: String huh = "?";
132: target += huh + result;
133: }
134: return target;
135: }
136:
137: String prettify(CollationKey sourceKey) {
138: int i;
139: byte[] bytes = sourceKey.toByteArray();
140: String target = "[";
141:
142: for (i = 0; i < bytes.length; i++) {
143: target += Integer.toHexString(bytes[i]);
144: target += " ";
145: }
146: target += "]";
147: return target;
148: }
149: }
|