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