2 *******************************************************************************
3 * Copyright (C) 2002-2010, International Business Machines Corporation and *
4 * others. All Rights Reserved. *
5 *******************************************************************************
9 * Port From: ICU4C v2.1 : Collate/CollationSpanishTest
10 * Source File: $ICU4CRoot/source/test/intltest/escoll.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 CollationSpanishTest extends TestFmwk {
22 public static void main(String[] args) throws Exception {
23 new CollationSpanishTest().run(args);
26 private static char[][] testSourceCases = {
27 {0x61, 0x6c, 0x69, 0x61, 0x73},
28 {0x45, 0x6c, 0x6c, 0x69, 0x6f, 0x74},
29 {0x48, 0x65, 0x6c, 0x6c, 0x6f},
30 {0x61, 0x63, 0x48, 0x63},
32 {0x61, 0x6c, 0x69, 0x61, 0x73},
33 {0x61, 0x63, 0x48, 0x63},
35 {0x48, 0x65, 0x6c, 0x6c, 0x6f},
38 private static char[][] testTargetCases = {
39 {0x61, 0x6c, 0x6c, 0x69, 0x61, 0x73},
40 {0x45, 0x6d, 0x69, 0x6f, 0x74},
41 {0x68, 0x65, 0x6c, 0x6c, 0x4f},
42 {0x61, 0x43, 0x48, 0x63},
43 {0x61, 0x43, 0x48, 0x63},
44 {0x61, 0x6c, 0x6c, 0x69, 0x61, 0x73},
45 {0x61, 0x43, 0x48, 0x63},
46 {0x61, 0x43, 0x48, 0x63},
47 {0x68, 0x65, 0x6c, 0x6c, 0x4f},
50 private static int[] results = {
63 //static public Collator myCollation = Collator.getInstance(new Locale("es", "ES"));
65 private Collator myCollation = null;
67 public CollationSpanishTest() {
70 protected void init()throws Exception{
71 myCollation = Collator.getInstance(new Locale("es", "ES"));
73 public void TestTertiary(){
75 myCollation.setStrength(Collator.TERTIARY);
76 for (i = 0; i < 5 ; i++) {
77 doTest(testSourceCases[i], testTargetCases[i], results[i]);
81 public void TestPrimary(){
83 myCollation.setStrength(Collator.PRIMARY);
84 for (i = 5; i < 9; i++) {
85 doTest(testSourceCases[i], testTargetCases[i], results[i]);
89 // amin test routine, tests rules specific to the spanish locale
90 private void doTest(char[] source, char[] target, int result) {
91 String s = new String(source);
92 String t = new String(target);
93 int compareResult = myCollation.compare(s, t);
94 CollationKey sortKey1, sortKey2;
95 sortKey1 = myCollation.getCollationKey(s);
96 sortKey2 = myCollation.getCollationKey(t);
97 int keyResult = sortKey1.compareTo(sortKey2);
98 reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
101 private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,
102 int compareResult, int keyResult, int incResult, int expectedResult ) {
103 if (expectedResult < -1 || expectedResult > 1) {
104 errln("***** invalid call to reportCResult ****");
108 boolean ok1 = (compareResult == expectedResult);
109 boolean ok2 = (keyResult == expectedResult);
110 boolean ok3 = (incResult == expectedResult);
112 if (ok1 && ok2 && ok3 && !isVerbose()) {
115 String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
116 String msg2 = "\", \"";
117 String msg3 = "\") returned ";
118 String msg4 = "; expected ";
120 String sExpect = new String("");
121 String sResult = new String("");
122 sResult = appendCompareResult(compareResult, sResult);
123 sExpect = appendCompareResult(expectedResult, sExpect);
125 logln(msg1 + source + msg2 + target + msg3 + sResult);
127 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
130 msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
131 msg2 = "\").compareTo(key(\"";
132 msg3 = "\")) returned ";
133 sResult = appendCompareResult(keyResult, sResult);
135 logln(msg1 + source + msg2 + target + msg3 + sResult);
137 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
140 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));
143 msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
145 msg3 = "\") returned ";
147 sResult = appendCompareResult(incResult, sResult);
150 logln(msg1 + source + msg2 + target + msg3 + sResult);
152 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
157 private String appendCompareResult(int result, String target){
160 } else if (result == 0) {
162 } else if (result == 1) {
166 target += huh + result;
171 String prettify(CollationKey sourceKey) {
173 byte[] bytes= sourceKey.toByteArray();
176 for (i = 0; i < bytes.length; i++) {
177 target += Integer.toHexString(bytes[i]);