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