]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/icu/dev/test/collator/CollationCurrencyTest.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / icu / dev / test / collator / CollationCurrencyTest.java
1 /*\r
2  *******************************************************************************\r
3  * Copyright (C) 2002-2004, 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/CollationCurrencyTest\r
10  * Source File: $ICU4CRoot/source/test/intltest/currcoll.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 CollationCurrencyTest extends TestFmwk {\r
20     public static void main(String[] args) throws Exception {\r
21         new CollationCurrencyTest().run(args);\r
22     }\r
23     \r
24     public void TestCurrency() {\r
25         // All the currency symbols, in collation order\r
26         char[][] currency = {\r
27             { 0x00A4 }, /*00A4; L; [14 36, 03, 03]    # [082B.0020.0002] # CURRENCY SIGN*/\r
28             { 0x00A2 }, /*00A2; L; [14 38, 03, 03]    # [082C.0020.0002] # CENT SIGN*/\r
29             { 0xFFE0 }, /*FFE0; L; [14 38, 03, 05]    # [082C.0020.0003] # FULLWIDTH CENT SIGN*/\r
30             { 0x0024 }, /*0024; L; [14 3A, 03, 03]    # [082D.0020.0002] # DOLLAR SIGN*/\r
31             { 0xFF04 }, /*FF04; L; [14 3A, 03, 05]    # [082D.0020.0003] # FULLWIDTH DOLLAR SIGN*/\r
32             { 0xFE69 }, /*FE69; L; [14 3A, 03, 1D]    # [082D.0020.000F] # SMALL DOLLAR SIGN*/\r
33             { 0x00A3 }, /*00A3; L; [14 3C, 03, 03]    # [082E.0020.0002] # POUND SIGN*/\r
34             { 0xFFE1 }, /*FFE1; L; [14 3C, 03, 05]    # [082E.0020.0003] # FULLWIDTH POUND SIGN*/\r
35             { 0x00A5 }, /*00A5; L; [14 3E, 03, 03]    # [082F.0020.0002] # YEN SIGN*/\r
36             { 0xFFE5 }, /*FFE5; L; [14 3E, 03, 05]    # [082F.0020.0003] # FULLWIDTH YEN SIGN*/\r
37             { 0x09F2 }, /*09F2; L; [14 40, 03, 03]    # [0830.0020.0002] # BENGALI RUPEE MARK*/\r
38             { 0x09F3 }, /*09F3; L; [14 42, 03, 03]    # [0831.0020.0002] # BENGALI RUPEE SIGN*/\r
39             { 0x0E3F }, /*0E3F; L; [14 44, 03, 03]    # [0832.0020.0002] # THAI CURRENCY SYMBOL BAHT*/\r
40             { 0x17DB }, /*17DB; L; [14 46, 03, 03]    # [0833.0020.0002] # KHMER CURRENCY SYMBOL RIEL*/\r
41             { 0x20A0 }, /*20A0; L; [14 48, 03, 03]    # [0834.0020.0002] # EURO-CURRENCY SIGN*/\r
42             { 0x20A1 }, /*20A1; L; [14 4A, 03, 03]    # [0835.0020.0002] # COLON SIGN*/\r
43             { 0x20A2 }, /*20A2; L; [14 4C, 03, 03]    # [0836.0020.0002] # CRUZEIRO SIGN*/\r
44             { 0x20A3 }, /*20A3; L; [14 4E, 03, 03]    # [0837.0020.0002] # FRENCH FRANC SIGN*/\r
45             { 0x20A4 }, /*20A4; L; [14 50, 03, 03]    # [0838.0020.0002] # LIRA SIGN*/\r
46             { 0x20A5 }, /*20A5; L; [14 52, 03, 03]    # [0839.0020.0002] # MILL SIGN*/\r
47             { 0x20A6 }, /*20A6; L; [14 54, 03, 03]    # [083A.0020.0002] # NAIRA SIGN*/\r
48             { 0x20A7 }, /*20A7; L; [14 56, 03, 03]    # [083B.0020.0002] # PESETA SIGN*/\r
49             { 0x20A9 }, /*20A9; L; [14 58, 03, 03]    # [083C.0020.0002] # WON SIGN*/\r
50             { 0xFFE6 }, /*FFE6; L; [14 58, 03, 05]    # [083C.0020.0003] # FULLWIDTH WON SIGN*/\r
51             { 0x20AA }, /*20AA; L; [14 5A, 03, 03]    # [083D.0020.0002] # NEW SHEQEL SIGN*/\r
52             { 0x20AB }, /*20AB; L; [14 5C, 03, 03]    # [083E.0020.0002] # DONG SIGN*/\r
53             { 0x20AC }, /*20AC; L; [14 5E, 03, 03]    # [083F.0020.0002] # EURO SIGN*/\r
54             { 0x20AD }, /*20AD; L; [14 60, 03, 03]    # [0840.0020.0002] # KIP SIGN*/\r
55             { 0x20AE }, /*20AE; L; [14 62, 03, 03]    # [0841.0020.0002] # TUGRIK SIGN*/\r
56             { 0x20AF } /*20AF; L; [14 64, 03, 03]    # [0842.0020.0002] # DRACHMA SIGN*/\r
57         };\r
58     \r
59         int i, j;\r
60         int expectedResult = 0;\r
61         RuleBasedCollator c = (RuleBasedCollator)Collator.getInstance(Locale.ENGLISH);\r
62         \r
63         // Compare each currency symbol against all the\r
64         // currency symbols, including itself\r
65         String source;\r
66         String target;\r
67         \r
68         for (i = 0; i < currency.length; i += 1) {\r
69             for (j = 0; j < currency.length; j += 1) {\r
70                 source = new String(currency[i]);\r
71                 target = new String(currency[j]);\r
72 \r
73                 if (i < j) {\r
74                     expectedResult = -1;\r
75                 } else if ( i == j) {\r
76                     expectedResult = 0;\r
77                 } else {\r
78                     expectedResult = 1;\r
79                 }\r
80 \r
81                 int compareResult = c.compare(source, target);\r
82                 CollationKey sourceKey = null;\r
83             \r
84                 sourceKey = c.getCollationKey(source);\r
85 \r
86                 if (sourceKey == null) {\r
87                     errln("Couldn't get collationKey for source");\r
88                     continue;\r
89                 }\r
90             \r
91                 CollationKey targetKey = null;\r
92                 targetKey = c.getCollationKey(target);\r
93                 if (targetKey == null) {\r
94                     errln("Couldn't get collationKey for source");\r
95                     continue;\r
96                 }\r
97 \r
98                 int keyResult = sourceKey.compareTo(targetKey);\r
99 \r
100                 reportCResult( source, target, sourceKey, targetKey, compareResult, keyResult, compareResult, expectedResult );\r
101             }\r
102         }\r
103     }\r
104     \r
105     private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,\r
106                                 int compareResult, int keyResult, int incResult, int expectedResult ) {\r
107         if (expectedResult < -1 || expectedResult > 1) {\r
108             errln("***** invalid call to reportCResult ****");\r
109             return;\r
110         }\r
111 \r
112         boolean ok1 = (compareResult == expectedResult);\r
113         boolean ok2 = (keyResult == expectedResult);\r
114         boolean ok3 = (incResult == expectedResult);\r
115 \r
116         if (ok1 && ok2 && ok3 && !isVerbose()) {\r
117             return;    \r
118         } else {\r
119             String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";\r
120             String msg2 = "\", \"";\r
121             String msg3 = "\") returned ";\r
122             String msg4 = "; expected ";\r
123             \r
124             String sExpect = new String("");\r
125             String sResult = new String("");\r
126             sResult = appendCompareResult(compareResult, sResult);\r
127             sExpect = appendCompareResult(expectedResult, sExpect);\r
128             if (ok1) {\r
129                 logln(msg1 + source + msg2 + target + msg3 + sResult);\r
130             } else {\r
131                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
132             }\r
133             \r
134             msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";\r
135             msg2 = "\").compareTo(key(\"";\r
136             msg3 = "\")) returned ";\r
137             sResult = appendCompareResult(keyResult, sResult);\r
138             if (ok2) {\r
139                 logln(msg1 + source + msg2 + target + msg3 + sResult);\r
140             } else {\r
141                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
142                 msg1 = "  ";\r
143                 msg2 = " vs. ";\r
144                 errln(msg1 + prettify(sourceKey) + msg2 + prettify(targetKey));\r
145             }\r
146             \r
147             msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";\r
148             msg2 = "\", \"";\r
149             msg3 = "\") returned ";\r
150 \r
151             sResult = appendCompareResult(incResult, sResult);\r
152 \r
153             if (ok3) {\r
154                 logln(msg1 + source + msg2 + target + msg3 + sResult);\r
155             } else {\r
156                 errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);\r
157             }                \r
158         }\r
159     }\r
160     \r
161     private String appendCompareResult(int result, String target){\r
162         if (result == -1) {\r
163             target += "LESS";\r
164         } else if (result == 0) {\r
165             target += "EQUAL";\r
166         } else if (result == 1) {\r
167             target += "GREATER";\r
168         } else {\r
169             String huh = "?";\r
170             target += huh + result;\r
171         }\r
172         return target;\r
173     }\r
174     \r
175     String prettify(CollationKey sourceKey) {\r
176         int i;\r
177         byte[] bytes= sourceKey.toByteArray();\r
178         String target = "[";\r
179     \r
180         for (i = 0; i < bytes.length; i++) {\r
181             target += Integer.toHexString(bytes[i]);\r
182             target += " ";\r
183         }\r
184         target += "]";\r
185         return target;\r
186     }\r
187 }