]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_4_2-src/main/tests/collate/src/com/ibm/icu/dev/test/collator/CollationFinnishTest.java
go
[Dictionary.git] / jars / icu4j-4_4_2-src / main / tests / collate / src / com / ibm / icu / dev / test / collator / CollationFinnishTest.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/CollationFinnishTest\r
10  * Source File: $ICU4CRoot/source/test/intltest/ficoll.cpp\r
11  **/\r
12  \r
13 package com.ibm.icu.dev.test.collator;\r
14  \r
15 import com.ibm.icu.dev.test.TestFmwk;\r
16 import com.ibm.icu.text.CollationKey;\r
17 import com.ibm.icu.text.Collator;\r
18 import com.ibm.icu.util.ULocale;\r
19  \r
20 public class CollationFinnishTest extends TestFmwk {\r
21     public static void main(String[] args) throws Exception {\r
22         new CollationFinnishTest().run(args);\r
23     }\r
24     \r
25     private static char[][] testSourceCases = {\r
26         {0x77, 0x61, 0x74},\r
27         {0x76, 0x61, 0x74},\r
28         {0x61, 0x00FC, 0x62, 0x65, 0x63, 0x6b},\r
29         {0x4c, 0x00E5, 0x76, 0x69},\r
30         {0x77, 0x61, 0x74}\r
31     };\r
32 \r
33     private static char[][] testTargetCases = {\r
34         {0x76, 0x61, 0x74},\r
35         {0x77, 0x61, 0x79},\r
36         {0x61, 0x78, 0x62, 0x65, 0x63, 0x6b},\r
37         {0x4c, 0x00E4, 0x77, 0x65},\r
38         {0x76, 0x61, 0x74}\r
39     };\r
40 \r
41     private static int[] results = {\r
42         1,\r
43         -1,\r
44         1,\r
45         -1,\r
46         // test primary > 4\r
47         0, //collation rules for finnish have changed!\r
48     };\r
49 \r
50     private Collator myCollation = null;\r
51     \r
52     public CollationFinnishTest() {\r
53     }\r
54     protected void init()throws Exception{\r
55         myCollation = Collator.getInstance(new ULocale("fi_FI@collation=standard"));\r
56     }\r
57      \r
58     \r
59     // perform tests with strength PRIMARY\r
60     public void TestPrimary() {\r
61         int i = 0;\r
62         myCollation.setStrength(Collator.PRIMARY);\r
63         for(i = 4; i < 5; i++) {\r
64             doTest(testSourceCases[i], testTargetCases[i], results[i]);    \r
65         }         \r
66     }\r
67     \r
68     // perform test with strength TERTIARY\r
69     public void TestTertiary() {\r
70         int i = 0;\r
71         myCollation.setStrength(Collator.TERTIARY);\r
72         for(i = 0; i < 4; i++ ) {\r
73             doTest(testSourceCases[i], testTargetCases[i], results[i]);\r
74         }    \r
75     }\r
76     \r
77     // main test routine, tests rules specific to the finish locale\r
78     private void doTest(char[] source, char[] target, int result) {\r
79         String s = new String(source);\r
80         String t = new String(target);\r
81         int compareResult = myCollation.compare(s, t);\r
82         CollationKey sortKey1, sortKey2;\r
83         sortKey1 = myCollation.getCollationKey(s);\r
84         sortKey2 = myCollation.getCollationKey(t);\r
85         int keyResult = sortKey1.compareTo(sortKey2);\r
86         reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);\r
87     }\r
88     \r
89     private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,\r
90                                 int compareResult, int keyResult, int incResult, int expectedResult ) {\r
91         if (expectedResult < -1 || expectedResult > 1) {\r
92             errln("***** invalid call to reportCResult ****");\r
93             return;\r
94         }\r
95 \r
96         boolean ok1 = (compareResult == expectedResult);\r
97         boolean ok2 = (keyResult == expectedResult);\r
98         boolean ok3 = (incResult == expectedResult);\r
99 \r
100         if (ok1 && ok2 && ok3 && !isVerbose()) {\r
101             return;    \r
102         } else {\r
103             String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";\r
104             String msg2 = "\", \"";\r
105             String msg3 = "\") returned ";\r
106             String msg4 = "; expected ";\r
107             \r
108             String sExpect = new String("");\r
109             String sResult = new String("");\r
110             sResult = appendCompareResult(compareResult, sResult);\r
111             sExpect = appendCompareResult(expectedResult, sExpect);\r
112             if (ok1) {\r
113                 logln(msg1 + source + msg2 + target + msg3 + sResult);\r
114             } else {\r
115                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
116             }\r
117             \r
118             msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";\r
119             msg2 = "\").compareTo(key(\"";\r
120             msg3 = "\")) returned ";\r
121             sResult = appendCompareResult(keyResult, sResult);\r
122             if (ok2) {\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                 msg1 = "  ";\r
127                 msg2 = " vs. ";\r
128                 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));\r
129             }\r
130             \r
131             msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";\r
132             msg2 = "\", \"";\r
133             msg3 = "\") returned ";\r
134 \r
135             sResult = appendCompareResult(incResult, sResult);\r
136 \r
137             if (ok3) {\r
138                 logln(msg1 + source + msg2 + target + msg3 + sResult);\r
139             } else {\r
140                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
141             }               \r
142         }\r
143     }\r
144     \r
145     private String appendCompareResult(int result, String target) {\r
146         if (result == -1) {\r
147             target += "LESS";\r
148         } else if (result == 0) {\r
149             target += "EQUAL";\r
150         } else if (result == 1) {\r
151             target += "GREATER";\r
152         } else {\r
153             String huh = "?";\r
154             target += huh + result;\r
155         }\r
156         return target;\r
157     }\r
158     \r
159     String prettify(CollationKey sourceKey) {\r
160         int i;\r
161         byte[] bytes= sourceKey.toByteArray();\r
162         String target = "[";\r
163     \r
164         for (i = 0; i < bytes.length; i++) {\r
165             target += Integer.toHexString(bytes[i]);\r
166             target += " ";\r
167         }\r
168         target += "]";\r
169         return target;\r
170     }\r
171 }\r
172     \r