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