001: /*
002: *******************************************************************************
003: * Copyright (C) 2002-2005, International Business Machines Corporation and *
004: * others. All Rights Reserved. *
005: *******************************************************************************
006: */
007:
008: /**
009: * Port From: ICU4C v2.1 : Collate/CollationSpanishTest
010: * Source File: $ICU4CRoot/source/test/intltest/escoll.cpp
011: **/package com.ibm.icu.dev.test.collator;
012:
013: import com.ibm.icu.dev.test.*;
014: import com.ibm.icu.text.*;
015: import java.util.Locale;
016:
017: public class CollationSpanishTest extends TestFmwk {
018: public static void main(String[] args) throws Exception {
019: new CollationSpanishTest().run(args);
020: }
021:
022: private static char[][] testSourceCases = {
023: { 0x61, 0x6c, 0x69, 0x61, 0x73 },
024: { 0x45, 0x6c, 0x6c, 0x69, 0x6f, 0x74 },
025: { 0x48, 0x65, 0x6c, 0x6c, 0x6f },
026: { 0x61, 0x63, 0x48, 0x63 }, { 0x61, 0x63, 0x63 },
027: { 0x61, 0x6c, 0x69, 0x61, 0x73 },
028: { 0x61, 0x63, 0x48, 0x63 }, { 0x61, 0x63, 0x63 },
029: { 0x48, 0x65, 0x6c, 0x6c, 0x6f }, };
030:
031: private static char[][] testTargetCases = {
032: { 0x61, 0x6c, 0x6c, 0x69, 0x61, 0x73 },
033: { 0x45, 0x6d, 0x69, 0x6f, 0x74 },
034: { 0x68, 0x65, 0x6c, 0x6c, 0x4f },
035: { 0x61, 0x43, 0x48, 0x63 }, { 0x61, 0x43, 0x48, 0x63 },
036: { 0x61, 0x6c, 0x6c, 0x69, 0x61, 0x73 },
037: { 0x61, 0x43, 0x48, 0x63 }, { 0x61, 0x43, 0x48, 0x63 },
038: { 0x68, 0x65, 0x6c, 0x6c, 0x4f }, };
039:
040: private static int[] results = { -1, -1, 1, -1, -1,
041: // test primary > 5
042: -1, 0, -1, 0 };
043:
044: //static public Collator myCollation = Collator.getInstance(new Locale("es", "ES"));
045:
046: private Collator myCollation = null;
047:
048: public CollationSpanishTest() {
049:
050: }
051:
052: protected void init() throws Exception {
053: myCollation = Collator.getInstance(new Locale("es", "ES"));
054: }
055:
056: public void TestTertiary() {
057: int i = 0;
058: myCollation.setStrength(Collator.TERTIARY);
059: for (i = 0; i < 5; i++) {
060: doTest(testSourceCases[i], testTargetCases[i], results[i]);
061: }
062: }
063:
064: public void TestPrimary() {
065: int i;
066: myCollation.setStrength(Collator.PRIMARY);
067: for (i = 5; i < 9; i++) {
068: doTest(testSourceCases[i], testTargetCases[i], results[i]);
069: }
070: }
071:
072: // amin test routine, tests rules specific to the spanish locale
073: private void doTest(char[] source, char[] target, int result) {
074: String s = new String(source);
075: String t = new String(target);
076: int compareResult = myCollation.compare(s, t);
077: CollationKey sortKey1, sortKey2;
078: sortKey1 = myCollation.getCollationKey(s);
079: sortKey2 = myCollation.getCollationKey(t);
080: int keyResult = sortKey1.compareTo(sortKey2);
081: reportCResult(s, t, sortKey1, sortKey2, compareResult,
082: keyResult, compareResult, result);
083: }
084:
085: private void reportCResult(String source, String target,
086: CollationKey sourceKey, CollationKey targetKey,
087: int compareResult, int keyResult, int incResult,
088: int expectedResult) {
089: if (expectedResult < -1 || expectedResult > 1) {
090: errln("***** invalid call to reportCResult ****");
091: return;
092: }
093:
094: boolean ok1 = (compareResult == expectedResult);
095: boolean ok2 = (keyResult == expectedResult);
096: boolean ok3 = (incResult == expectedResult);
097:
098: if (ok1 && ok2 && ok3 && !isVerbose()) {
099: return;
100: } else {
101: String msg1 = ok1 ? "Ok: compare(\"" : "FAIL: compare(\"";
102: String msg2 = "\", \"";
103: String msg3 = "\") returned ";
104: String msg4 = "; expected ";
105:
106: String sExpect = new String("");
107: String sResult = new String("");
108: sResult = appendCompareResult(compareResult, sResult);
109: sExpect = appendCompareResult(expectedResult, sExpect);
110: if (ok1) {
111: logln(msg1 + source + msg2 + target + msg3 + sResult);
112: } else {
113: errln(msg1 + source + msg2 + target + msg3 + sResult
114: + msg4 + sExpect);
115: }
116:
117: msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
118: msg2 = "\").compareTo(key(\"";
119: msg3 = "\")) returned ";
120: sResult = appendCompareResult(keyResult, sResult);
121: if (ok2) {
122: logln(msg1 + source + msg2 + target + msg3 + sResult);
123: } else {
124: errln(msg1 + source + msg2 + target + msg3 + sResult
125: + msg4 + sExpect);
126: msg1 = " ";
127: msg2 = " vs. ";
128: errln(msg1 + prettify(sourceKey) + msg2
129: + prettify(targetKey));
130: }
131:
132: msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
133: msg2 = "\", \"";
134: msg3 = "\") returned ";
135:
136: sResult = appendCompareResult(incResult, sResult);
137:
138: if (ok3) {
139: logln(msg1 + source + msg2 + target + msg3 + sResult);
140: } else {
141: errln(msg1 + source + msg2 + target + msg3 + sResult
142: + msg4 + sExpect);
143: }
144: }
145: }
146:
147: private String appendCompareResult(int result, String target) {
148: if (result == -1) {
149: target += "LESS";
150: } else if (result == 0) {
151: target += "EQUAL";
152: } else if (result == 1) {
153: target += "GREATER";
154: } else {
155: String huh = "?";
156: target += huh + result;
157: }
158: return target;
159: }
160:
161: String prettify(CollationKey sourceKey) {
162: int i;
163: byte[] bytes = sourceKey.toByteArray();
164: String target = "[";
165:
166: for (i = 0; i < bytes.length; i++) {
167: target += Integer.toHexString(bytes[i]);
168: target += " ";
169: }
170: target += "]";
171: return target;
172: }
173: }
|