2 *******************************************************************************
3 * Copyright (C) 2002-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 * Port From: ICU4C v2.1 : Collate/CollationTurkishTest
10 * Source File: $ICU4CRoot/source/test/intltest/trcoll.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 CollationTurkishTest extends TestFmwk{
22 public static void main(String[] args) throws Exception{
23 new CollationTurkishTest().run(args);
26 private static char[][] testSourceCases = {
30 {0x00FC, 0x6f, 0x69, 0x64},
31 {0x68, 0x011E, 0x61, 0x6c, 0x74},
32 {0x73, 0x74, 0x72, 0x65, 0x73, 0x015E},
33 {0x76, 0x6f, 0x0131, 0x64},
34 {0x69, 0x64, 0x65, 0x61},
35 {0x00FC, 0x6f, 0x69, 0x64},
36 {0x76, 0x6f, 0x0131, 0x64},
37 {0x69, 0x64, 0x65, 0x61}
40 private static char[][] testTargetCases = {
44 {0x76, 0x6f, 0x69, 0x64},
45 {0x68, 0x61, 0x6c, 0x74},
46 {0x015E, 0x74, 0x72, 0x65, 0x015E, 0x73},
47 {0x76, 0x6f, 0x69, 0x64},
48 {0x49, 0x64, 0x65, 0x61},
49 {0x76, 0x6f, 0x69, 0x64},
50 {0x76, 0x6f, 0x69, 0x64},
51 {0x49, 0x64, 0x65, 0x61}
54 private static int[] results = {
69 private Collator myCollation = null;
71 public CollationTurkishTest() {
74 protected void init()throws Exception{
75 myCollation = Collator.getInstance(new Locale("tr", ""));
78 public void TestTertiary() {
80 myCollation.setStrength(Collator.TERTIARY);
81 for (i = 0; i < 8 ; i++) {
82 doTest(testSourceCases[i], testTargetCases[i], results[i]);
86 public void TestPrimary() {
88 myCollation.setStrength(Collator.PRIMARY);
89 for (i = 8; i < 11; i++) {
90 doTest(testSourceCases[i], testTargetCases[i], results[i]);
95 // main test routine, tests rules specific to turkish locale
96 private void doTest(char[] source, char[] target, int result) {
97 String s = new String(source);
98 String t = new String(target);
99 int compareResult = myCollation.compare(s, t);
100 CollationKey sortKey1, sortKey2;
101 sortKey1 = myCollation.getCollationKey(s);
102 sortKey2 = myCollation.getCollationKey(t);
103 int keyResult = sortKey1.compareTo(sortKey2);
104 reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
108 private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,
109 int compareResult, int keyResult, int incResult, int expectedResult ) {
110 if (expectedResult < -1 || expectedResult > 1) {
111 errln("***** invalid call to reportCResult ****");
115 boolean ok1 = (compareResult == expectedResult);
116 boolean ok2 = (keyResult == expectedResult);
117 boolean ok3 = (incResult == expectedResult);
119 if (ok1 && ok2 && ok3 && !isVerbose()) {
122 String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
123 String msg2 = "\", \"";
124 String msg3 = "\") returned ";
125 String msg4 = "; expected ";
127 String sExpect = new String("");
128 String sResult = new String("");
129 sResult = appendCompareResult(compareResult, sResult);
130 sExpect = appendCompareResult(expectedResult, sExpect);
132 logln(msg1 + source + msg2 + target + msg3 + sResult);
134 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
137 msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
138 msg2 = "\").compareTo(key(\"";
139 msg3 = "\")) returned ";
140 sResult = appendCompareResult(keyResult, sResult);
142 logln(msg1 + source + msg2 + target + msg3 + sResult);
144 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
147 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));
150 msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
152 msg3 = "\") returned ";
154 sResult = appendCompareResult(incResult, sResult);
157 logln(msg1 + source + msg2 + target + msg3 + sResult);
159 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
164 private String appendCompareResult(int result, String target) {
167 } else if (result == 0) {
169 } else if (result == 1) {
173 target += huh + result;
178 String prettify(CollationKey sourceKey) {
180 byte[] bytes= sourceKey.toByteArray();
183 for (i = 0; i < bytes.length; i++) {
184 target += Integer.toHexString(bytes[i]);