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/CollationFinnishTest
\r
10 * Source File: $ICU4CRoot/source/test/intltest/ficoll.cpp
\r
13 package com.ibm.icu.dev.test.collator;
\r
15 import com.ibm.icu.dev.test.TestFmwk;
\r
16 import com.ibm.icu.text.CollationKey;
\r
17 import com.ibm.icu.text.Collator;
\r
18 import com.ibm.icu.util.ULocale;
\r
20 public class CollationFinnishTest extends TestFmwk {
\r
21 public static void main(String[] args) throws Exception {
\r
22 new CollationFinnishTest().run(args);
\r
25 private static char[][] testSourceCases = {
\r
28 {0x61, 0x00FC, 0x62, 0x65, 0x63, 0x6b},
\r
29 {0x4c, 0x00E5, 0x76, 0x69},
\r
33 private static char[][] testTargetCases = {
\r
36 {0x61, 0x78, 0x62, 0x65, 0x63, 0x6b},
\r
37 {0x4c, 0x00E4, 0x77, 0x65},
\r
41 private static int[] results = {
\r
47 0, //collation rules for finnish have changed!
\r
50 private Collator myCollation = null;
\r
52 public CollationFinnishTest() {
\r
54 protected void init()throws Exception{
\r
55 myCollation = Collator.getInstance(new ULocale("fi_FI@collation=standard"));
\r
59 // perform tests with strength PRIMARY
\r
60 public void TestPrimary() {
\r
62 myCollation.setStrength(Collator.PRIMARY);
\r
63 for(i = 4; i < 5; i++) {
\r
64 doTest(testSourceCases[i], testTargetCases[i], results[i]);
\r
68 // perform test with strength TERTIARY
\r
69 public void TestTertiary() {
\r
71 myCollation.setStrength(Collator.TERTIARY);
\r
72 for(i = 0; i < 4; i++ ) {
\r
73 doTest(testSourceCases[i], testTargetCases[i], results[i]);
\r
77 // main test routine, tests rules specific to the finish locale
\r
78 private void doTest(char[] source, char[] target, int result) {
\r
79 String s = new String(source);
\r
80 String t = new String(target);
\r
81 int compareResult = myCollation.compare(s, t);
\r
82 CollationKey sortKey1, sortKey2;
\r
83 sortKey1 = myCollation.getCollationKey(s);
\r
84 sortKey2 = myCollation.getCollationKey(t);
\r
85 int keyResult = sortKey1.compareTo(sortKey2);
\r
86 reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
\r
89 private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,
\r
90 int compareResult, int keyResult, int incResult, int expectedResult ) {
\r
91 if (expectedResult < -1 || expectedResult > 1) {
\r
92 errln("***** invalid call to reportCResult ****");
\r
96 boolean ok1 = (compareResult == expectedResult);
\r
97 boolean ok2 = (keyResult == expectedResult);
\r
98 boolean ok3 = (incResult == expectedResult);
\r
100 if (ok1 && ok2 && ok3 && !isVerbose()) {
\r
103 String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
\r
104 String msg2 = "\", \"";
\r
105 String msg3 = "\") returned ";
\r
106 String msg4 = "; expected ";
\r
108 String sExpect = new String("");
\r
109 String sResult = new String("");
\r
110 sResult = appendCompareResult(compareResult, sResult);
\r
111 sExpect = appendCompareResult(expectedResult, sExpect);
\r
113 logln(msg1 + source + msg2 + target + msg3 + sResult);
\r
115 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
118 msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
\r
119 msg2 = "\").compareTo(key(\"";
\r
120 msg3 = "\")) returned ";
\r
121 sResult = appendCompareResult(keyResult, sResult);
\r
123 logln(msg1 + source + msg2 + target + msg3 + sResult);
\r
125 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
128 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));
\r
131 msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
\r
133 msg3 = "\") returned ";
\r
135 sResult = appendCompareResult(incResult, sResult);
\r
138 logln(msg1 + source + msg2 + target + msg3 + sResult);
\r
140 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
145 private String appendCompareResult(int result, String target) {
\r
146 if (result == -1) {
\r
148 } else if (result == 0) {
\r
150 } else if (result == 1) {
\r
151 target += "GREATER";
\r
154 target += huh + result;
\r
159 String prettify(CollationKey sourceKey) {
\r
161 byte[] bytes= sourceKey.toByteArray();
\r
162 String target = "[";
\r
164 for (i = 0; i < bytes.length; i++) {
\r
165 target += Integer.toHexString(bytes[i]);
\r