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