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