2 *******************************************************************************
\r
3 * Copyright (C) 2002-2005, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
9 * Port From: ICU4C v2.1 : Collate/CollationSpanishTest
\r
10 * Source File: $ICU4CRoot/source/test/intltest/escoll.cpp
\r
13 package com.ibm.icu.dev.test.collator;
\r
15 import com.ibm.icu.dev.test.*;
\r
16 import com.ibm.icu.text.*;
\r
17 import java.util.Locale;
\r
19 public class CollationSpanishTest extends TestFmwk {
\r
20 public static void main(String[] args) throws Exception {
\r
21 new CollationSpanishTest().run(args);
\r
24 private static char[][] testSourceCases = {
\r
25 {0x61, 0x6c, 0x69, 0x61, 0x73},
\r
26 {0x45, 0x6c, 0x6c, 0x69, 0x6f, 0x74},
\r
27 {0x48, 0x65, 0x6c, 0x6c, 0x6f},
\r
28 {0x61, 0x63, 0x48, 0x63},
\r
30 {0x61, 0x6c, 0x69, 0x61, 0x73},
\r
31 {0x61, 0x63, 0x48, 0x63},
\r
33 {0x48, 0x65, 0x6c, 0x6c, 0x6f},
\r
36 private static char[][] testTargetCases = {
\r
37 {0x61, 0x6c, 0x6c, 0x69, 0x61, 0x73},
\r
38 {0x45, 0x6d, 0x69, 0x6f, 0x74},
\r
39 {0x68, 0x65, 0x6c, 0x6c, 0x4f},
\r
40 {0x61, 0x43, 0x48, 0x63},
\r
41 {0x61, 0x43, 0x48, 0x63},
\r
42 {0x61, 0x6c, 0x6c, 0x69, 0x61, 0x73},
\r
43 {0x61, 0x43, 0x48, 0x63},
\r
44 {0x61, 0x43, 0x48, 0x63},
\r
45 {0x68, 0x65, 0x6c, 0x6c, 0x4f},
\r
48 private static int[] results = {
\r
61 //static public Collator myCollation = Collator.getInstance(new Locale("es", "ES"));
\r
63 private Collator myCollation = null;
\r
65 public CollationSpanishTest() {
\r
68 protected void init()throws Exception{
\r
69 myCollation = Collator.getInstance(new Locale("es", "ES"));
\r
71 public void TestTertiary(){
\r
73 myCollation.setStrength(Collator.TERTIARY);
\r
74 for (i = 0; i < 5 ; i++) {
\r
75 doTest(testSourceCases[i], testTargetCases[i], results[i]);
\r
79 public void TestPrimary(){
\r
81 myCollation.setStrength(Collator.PRIMARY);
\r
82 for (i = 5; i < 9; i++) {
\r
83 doTest(testSourceCases[i], testTargetCases[i], results[i]);
\r
87 // amin test routine, tests rules specific to the spanish locale
\r
88 private void doTest(char[] source, char[] target, int result) {
\r
89 String s = new String(source);
\r
90 String t = new String(target);
\r
91 int compareResult = myCollation.compare(s, t);
\r
92 CollationKey sortKey1, sortKey2;
\r
93 sortKey1 = myCollation.getCollationKey(s);
\r
94 sortKey2 = myCollation.getCollationKey(t);
\r
95 int keyResult = sortKey1.compareTo(sortKey2);
\r
96 reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
\r
99 private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,
\r
100 int compareResult, int keyResult, int incResult, int expectedResult ) {
\r
101 if (expectedResult < -1 || expectedResult > 1) {
\r
102 errln("***** invalid call to reportCResult ****");
\r
106 boolean ok1 = (compareResult == expectedResult);
\r
107 boolean ok2 = (keyResult == expectedResult);
\r
108 boolean ok3 = (incResult == expectedResult);
\r
110 if (ok1 && ok2 && ok3 && !isVerbose()) {
\r
113 String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
\r
114 String msg2 = "\", \"";
\r
115 String msg3 = "\") returned ";
\r
116 String msg4 = "; expected ";
\r
118 String sExpect = new String("");
\r
119 String sResult = new String("");
\r
120 sResult = appendCompareResult(compareResult, sResult);
\r
121 sExpect = appendCompareResult(expectedResult, sExpect);
\r
123 logln(msg1 + source + msg2 + target + msg3 + sResult);
\r
125 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
128 msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
\r
129 msg2 = "\").compareTo(key(\"";
\r
130 msg3 = "\")) returned ";
\r
131 sResult = appendCompareResult(keyResult, sResult);
\r
133 logln(msg1 + source + msg2 + target + msg3 + sResult);
\r
135 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
138 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));
\r
141 msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
\r
143 msg3 = "\") returned ";
\r
145 sResult = appendCompareResult(incResult, sResult);
\r
148 logln(msg1 + source + msg2 + target + msg3 + sResult);
\r
150 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
\r
155 private String appendCompareResult(int result, String target){
\r
156 if (result == -1) {
\r
158 } else if (result == 0) {
\r
160 } else if (result == 1) {
\r
161 target += "GREATER";
\r
164 target += huh + result;
\r
169 String prettify(CollationKey sourceKey) {
\r
171 byte[] bytes= sourceKey.toByteArray();
\r
172 String target = "[";
\r
174 for (i = 0; i < bytes.length; i++) {
\r
175 target += Integer.toHexString(bytes[i]);
\r