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