]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/collator/CollationSpanishTest.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / collator / CollationSpanishTest.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2002-2005, 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 com.ibm.icu.dev.test.*;\r
16  import com.ibm.icu.text.*;\r
17  import java.util.Locale;\r
18  \r
19  public class CollationSpanishTest extends TestFmwk {\r
20     public static void main(String[] args) throws Exception {\r
21         new CollationSpanishTest().run(args);\r
22     }\r
23     \r
24     private static char[][] testSourceCases = {\r
25         {0x61, 0x6c, 0x69, 0x61, 0x73},\r
26         {0x45, 0x6c, 0x6c, 0x69, 0x6f, 0x74},\r
27         {0x48, 0x65, 0x6c, 0x6c, 0x6f},\r
28         {0x61, 0x63, 0x48, 0x63},\r
29         {0x61, 0x63, 0x63},\r
30         {0x61, 0x6c, 0x69, 0x61, 0x73},\r
31         {0x61, 0x63, 0x48, 0x63},\r
32         {0x61, 0x63, 0x63},\r
33         {0x48, 0x65, 0x6c, 0x6c, 0x6f},\r
34     };\r
35 \r
36     private static char[][] testTargetCases = {\r
37         {0x61, 0x6c, 0x6c, 0x69, 0x61, 0x73},\r
38         {0x45, 0x6d, 0x69, 0x6f, 0x74},\r
39         {0x68, 0x65, 0x6c, 0x6c, 0x4f},\r
40         {0x61, 0x43, 0x48, 0x63},\r
41         {0x61, 0x43, 0x48, 0x63},\r
42         {0x61, 0x6c, 0x6c, 0x69, 0x61, 0x73},\r
43         {0x61, 0x43, 0x48, 0x63},\r
44         {0x61, 0x43, 0x48, 0x63},\r
45         {0x68, 0x65, 0x6c, 0x6c, 0x4f},\r
46     };\r
47 \r
48     private static int[] results = {\r
49         -1,\r
50         -1,\r
51         1,\r
52         -1,\r
53         -1,\r
54         // test primary > 5\r
55         -1,\r
56         0,\r
57         -1,\r
58         0\r
59     };\r
60     \r
61     //static public Collator myCollation = Collator.getInstance(new Locale("es", "ES"));\r
62     \r
63     private Collator myCollation = null;\r
64     \r
65     public CollationSpanishTest() {\r
66 \r
67     }\r
68     protected void init()throws Exception{\r
69         myCollation = Collator.getInstance(new Locale("es", "ES"));\r
70     }\r
71     public void TestTertiary(){\r
72         int i = 0;\r
73         myCollation.setStrength(Collator.TERTIARY);\r
74         for (i = 0; i < 5 ; i++) {\r
75             doTest(testSourceCases[i], testTargetCases[i], results[i]);\r
76         }\r
77     }\r
78     \r
79     public void TestPrimary(){\r
80         int i;\r
81         myCollation.setStrength(Collator.PRIMARY);\r
82         for (i = 5; i < 9; i++) {\r
83             doTest(testSourceCases[i], testTargetCases[i], results[i]);\r
84         }\r
85     }\r
86     \r
87     // amin test routine, tests rules specific to the spanish locale\r
88     private void doTest(char[] source, char[] target, int result) {\r
89         String s = new String(source);\r
90         String t = new String(target);\r
91         int compareResult = myCollation.compare(s, t);\r
92         CollationKey sortKey1, sortKey2;\r
93         sortKey1 = myCollation.getCollationKey(s);\r
94         sortKey2 = myCollation.getCollationKey(t);\r
95         int keyResult = sortKey1.compareTo(sortKey2);\r
96         reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);\r
97     }\r
98     \r
99     private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,\r
100                                 int compareResult, int keyResult, int incResult, int expectedResult ) {\r
101         if (expectedResult < -1 || expectedResult > 1) {\r
102             errln("***** invalid call to reportCResult ****");\r
103             return;\r
104         }\r
105 \r
106         boolean ok1 = (compareResult == expectedResult);\r
107         boolean ok2 = (keyResult == expectedResult);\r
108         boolean ok3 = (incResult == expectedResult);\r
109 \r
110         if (ok1 && ok2 && ok3 && !isVerbose()) {\r
111             return;    \r
112         } else {\r
113             String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";\r
114             String msg2 = "\", \"";\r
115             String msg3 = "\") returned ";\r
116             String msg4 = "; expected ";\r
117             \r
118             String sExpect = new String("");\r
119             String sResult = new String("");\r
120             sResult = appendCompareResult(compareResult, sResult);\r
121             sExpect = appendCompareResult(expectedResult, sExpect);\r
122             if (ok1) {\r
123                 logln(msg1 + source + msg2 + target + msg3 + sResult);\r
124             } else {\r
125                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
126             }\r
127             \r
128             msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";\r
129             msg2 = "\").compareTo(key(\"";\r
130             msg3 = "\")) returned ";\r
131             sResult = appendCompareResult(keyResult, sResult);\r
132             if (ok2) {\r
133                 logln(msg1 + source + msg2 + target + msg3 + sResult);\r
134             } else {\r
135                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
136                 msg1 = "  ";\r
137                 msg2 = " vs. ";\r
138                 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));\r
139             }\r
140             \r
141             msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";\r
142             msg2 = "\", \"";\r
143             msg3 = "\") returned ";\r
144 \r
145             sResult = appendCompareResult(incResult, sResult);\r
146 \r
147             if (ok3) {\r
148                 logln(msg1 + source + msg2 + target + msg3 + sResult);\r
149             } else {\r
150                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
151             }                \r
152         }\r
153     }\r
154     \r
155     private String appendCompareResult(int result, String target){\r
156         if (result == -1) {\r
157             target += "LESS";\r
158         } else if (result == 0) {\r
159             target += "EQUAL";\r
160         } else if (result == 1) {\r
161             target += "GREATER";\r
162         } else {\r
163             String huh = "?";\r
164             target += huh + result;\r
165         }\r
166         return target;\r
167     }\r
168     \r
169     String prettify(CollationKey sourceKey) {\r
170         int i;\r
171         byte[] bytes= sourceKey.toByteArray();\r
172         String target = "[";\r
173     \r
174         for (i = 0; i < bytes.length; i++) {\r
175             target += Integer.toHexString(bytes[i]);\r
176             target += " ";\r
177         }\r
178         target += "]";\r
179         return target;\r
180     }\r
181 }\r