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