]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_8_1_1/main/tests/collate/src/com/ibm/icu/dev/test/collator/CollationTurkishTest.java
Added flags.
[Dictionary.git] / jars / icu4j-4_8_1_1 / main / tests / collate / src / com / ibm / icu / dev / test / collator / CollationTurkishTest.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/CollationTurkishTest
10  * Source File: $ICU4CRoot/source/test/intltest/trcoll.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  
21 public class CollationTurkishTest extends TestFmwk{
22     public static void main(String[] args) throws Exception{
23         new CollationTurkishTest().run(args);
24     }
25     
26     private static char[][] testSourceCases = {
27         {0x73, 0x0327},
28         {0x76, 0x00E4, 0x74},
29         {0x6f, 0x6c, 0x64},
30         {0x00FC, 0x6f, 0x69, 0x64},
31         {0x68, 0x011E, 0x61, 0x6c, 0x74},
32         {0x73, 0x74, 0x72, 0x65, 0x73, 0x015E},
33         {0x76, 0x6f, 0x0131, 0x64},
34         {0x69, 0x64, 0x65, 0x61},
35         {0x00FC, 0x6f, 0x69, 0x64},
36         {0x76, 0x6f, 0x0131, 0x64},
37         {0x69, 0x64, 0x65, 0x61}
38     };
39
40     private static char[][] testTargetCases = {
41         {0x75, 0x0308},
42         {0x76, 0x62, 0x74},
43         {0x00D6, 0x61, 0x79},
44         {0x76, 0x6f, 0x69, 0x64},
45         {0x68, 0x61, 0x6c, 0x74},
46         {0x015E, 0x74, 0x72, 0x65, 0x015E, 0x73},
47         {0x76, 0x6f, 0x69, 0x64},
48         {0x49, 0x64, 0x65, 0x61},
49         {0x76, 0x6f, 0x69, 0x64},
50         {0x76, 0x6f, 0x69, 0x64},
51         {0x49, 0x64, 0x65, 0x61}
52     };
53
54     private static int[] results = {
55         -1,
56         -1,
57         -1,
58         -1,
59         1,
60         -1,
61         -1,
62         1,
63     // test priamry > 8
64         -1,
65         -1,
66         1
67     };
68     
69     private Collator myCollation = null;
70     
71     public CollationTurkishTest() {
72
73     }
74     protected void init()throws Exception{
75         myCollation = Collator.getInstance(new Locale("tr", ""));
76     }
77     
78     public void TestTertiary() {
79         int i = 0;
80         myCollation.setStrength(Collator.TERTIARY);
81         for (i = 0; i < 8 ; i++) {
82             doTest(testSourceCases[i], testTargetCases[i], results[i]);
83         }
84     }
85     
86     public void TestPrimary() {
87         int i;
88         myCollation.setStrength(Collator.PRIMARY);
89         for (i = 8; i < 11; i++) {
90             doTest(testSourceCases[i], testTargetCases[i], results[i]);
91         }
92     }
93     
94     
95     // main test routine, tests rules specific to turkish locale
96     private void doTest(char[] source, char[] target, int result) {
97         String s = new String(source);
98         String t = new String(target);
99         int compareResult = myCollation.compare(s, t);
100         CollationKey sortKey1, sortKey2;
101         sortKey1 = myCollation.getCollationKey(s);
102         sortKey2 = myCollation.getCollationKey(t);
103         int keyResult = sortKey1.compareTo(sortKey2);
104         reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
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 }