]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-52_1/main/tests/collate/src/com/ibm/icu/dev/test/collator/CollationSpanishTest.java
Upgrade ICU4J.
[Dictionary.git] / jars / icu4j-52_1 / main / tests / collate / src / com / ibm / icu / dev / test / collator / CollationSpanishTest.java
1 /*
2  *******************************************************************************
3  * Copyright (C) 2002-2010, International Business Machines Corporation and    *
4  * others. All Rights Reserved.                                                *
5  *******************************************************************************
6  */
7
8 /** 
9  * Port From:   ICU4C v2.1 : Collate/CollationSpanishTest
10  * Source File: $ICU4CRoot/source/test/intltest/escoll.cpp
11  **/
12  
13  package com.ibm.icu.dev.test.collator;
14  
15  import java.util.Locale;
16
17 import com.ibm.icu.dev.test.TestFmwk;
18 import com.ibm.icu.text.CollationKey;
19 import com.ibm.icu.text.Collator;
20  
21  public class CollationSpanishTest extends TestFmwk {
22     public static void main(String[] args) throws Exception {
23         new CollationSpanishTest().run(args);
24     }
25     
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},
31         {0x61, 0x63, 0x63},
32         {0x61, 0x6c, 0x69, 0x61, 0x73},
33         {0x61, 0x63, 0x48, 0x63},
34         {0x61, 0x63, 0x63},
35         {0x48, 0x65, 0x6c, 0x6c, 0x6f},
36     };
37
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},
48     };
49
50     private static int[] results = {
51         -1,
52         -1,
53         1,
54         -1,
55         -1,
56         // test primary > 5
57         -1,
58         0,
59         -1,
60         0
61     };
62     
63     //static public Collator myCollation = Collator.getInstance(new Locale("es", "ES"));
64     
65     private Collator myCollation = null;
66     
67     public CollationSpanishTest() {
68
69     }
70     protected void init()throws Exception{
71         myCollation = Collator.getInstance(new Locale("es", "ES"));
72     }
73     public void TestTertiary(){
74         int i = 0;
75         myCollation.setStrength(Collator.TERTIARY);
76         for (i = 0; i < 5 ; i++) {
77             doTest(testSourceCases[i], testTargetCases[i], results[i]);
78         }
79     }
80     
81     public void TestPrimary(){
82         int i;
83         myCollation.setStrength(Collator.PRIMARY);
84         for (i = 5; i < 9; i++) {
85             doTest(testSourceCases[i], testTargetCases[i], results[i]);
86         }
87     }
88     
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);
99     }
100     
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 ****");
105             return;
106         }
107
108         boolean ok1 = (compareResult == expectedResult);
109         boolean ok2 = (keyResult == expectedResult);
110         boolean ok3 = (incResult == expectedResult);
111
112         if (ok1 && ok2 && ok3 && !isVerbose()) {
113             return;    
114         } else {
115             String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
116             String msg2 = "\", \"";
117             String msg3 = "\") returned ";
118             String msg4 = "; expected ";
119             
120             String sExpect = new String("");
121             String sResult = new String("");
122             sResult = appendCompareResult(compareResult, sResult);
123             sExpect = appendCompareResult(expectedResult, sExpect);
124             if (ok1) {
125                 logln(msg1 + source + msg2 + target + msg3 + sResult);
126             } else {
127                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
128             }
129             
130             msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
131             msg2 = "\").compareTo(key(\"";
132             msg3 = "\")) returned ";
133             sResult = appendCompareResult(keyResult, sResult);
134             if (ok2) {
135                 logln(msg1 + source + msg2 + target + msg3 + sResult);
136             } else {
137                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
138                 msg1 = "  ";
139                 msg2 = " vs. ";
140                 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));
141             }
142             
143             msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
144             msg2 = "\", \"";
145             msg3 = "\") returned ";
146
147             sResult = appendCompareResult(incResult, sResult);
148
149             if (ok3) {
150                 logln(msg1 + source + msg2 + target + msg3 + sResult);
151             } else {
152                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
153             }                
154         }
155     }
156     
157     private String appendCompareResult(int result, String target){
158         if (result == -1) {
159             target += "LESS";
160         } else if (result == 0) {
161             target += "EQUAL";
162         } else if (result == 1) {
163             target += "GREATER";
164         } else {
165             String huh = "?";
166             target += huh + result;
167         }
168         return target;
169     }
170     
171     String prettify(CollationKey sourceKey) {
172         int i;
173         byte[] bytes= sourceKey.toByteArray();
174         String target = "[";
175     
176         for (i = 0; i < bytes.length; i++) {
177             target += Integer.toHexString(bytes[i]);
178             target += " ";
179         }
180         target += "]";
181         return target;
182     }
183 }