1/*
2 *******************************************************************************
3 * Copyright (C) 2002-2014, International Business Machines Corporation and
4 * others. All Rights Reserved.
5 *******************************************************************************
6 */
7
8/**
9 * Port From:   ICU4C v2.1 : Collate/CollationFrenchTest
10 * Source File: $ICU4CRoot/source/test/intltest/frcoll.cpp
11 **/
12
13package com.ibm.icu.dev.test.collator;
14
15import java.util.Locale;
16
17import com.ibm.icu.dev.test.TestFmwk;
18import com.ibm.icu.text.CollationKey;
19import com.ibm.icu.text.Collator;
20import com.ibm.icu.text.RuleBasedCollator;
21
22public class CollationFrenchTest extends TestFmwk{
23    public static void main(String[] args) throws Exception {
24        new CollationFrenchTest().run(args);
25    }
26
27    private static char[][] testSourceCases = {
28        {0x0061/*'a'*/, 0x0062/*'b'*/, 0x0063/*'c'*/},
29        {0x0043/*'C'*/, 0x004f/*'O'*/, 0x0054/*'T'*/, 0x0045/*'E'*/},
30        {0x0063/*'c'*/, 0x006f/*'o'*/, 0x002d/*'-'*/, 0x006f/*'o'*/, 0x0070/*'p'*/},
31        {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/},
32        {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/},
33        {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/},
34        {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/},
35        {0x0048/*'H'*/, 0x0065/*'e'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x006f/*'o'*/},
36        {0x01f1},
37        {0xfb00},
38        {0x01fa},
39        {0x0101}
40    };
41
42    private static char[][] testTargetCases = {
43        {0x0041/*'A'*/, 0x0042/*'B'*/, 0x0043/*'C'*/},
44        {0x0063/*'c'*/, 0x00f4, 0x0074/*'t'*/, 0x0065/*'e'*/},
45        {0x0043/*'C'*/, 0x004f/*'O'*/, 0x004f/*'O'*/, 0x0050/*'P'*/},
46        {0x0070/*'p'*/, 0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x00E9},
47        {0x0070/*'p'*/,  0x00E9, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x00E9},
48        {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/},
49        {0x0070/*'p'*/, 0x00EA, 0x0063/*'c'*/, 0x0068/*'h'*/, 0x0065/*'e'*/, 0x0072/*'r'*/},
50        {0x0068/*'h'*/, 0x0065/*'e'*/, 0x006c/*'l'*/, 0x006c/*'l'*/, 0x004f/*'O'*/},
51        {0x01ee},
52        {0x25ca},
53        {0x00e0},
54        {0x01df}
55    };
56
57    private static int[] results = {
58        -1,
59        -1,
60        -1, /*Collator::GREATER,*/
61        -1,
62        1,
63        1,
64        -1,
65        1,
66       -1, /*Collator::GREATER,*/
67        1,
68        -1,
69        -1
70    };
71
72    // 0x0300 is grave, 0x0301 is acute
73    // the order of elements in this array must be different than the order in CollationEnglishTest
74    private static char[][] testAcute = {
75    /*00*/    {0x0065/*'e'*/, 0x0065/*'e'*/},
76    /*01*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/},
77    /*02*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/},
78    /*03*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/},
79    /*04*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/},
80    /*05*/    {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0301},
81    /*06*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0301},
82    /*07*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0301},
83    /*08*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0301},
84    /*09*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0301},
85    /*0a*/    {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0300, 0x0301},
86    /*0b*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0301},
87    /*0c*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0300, 0x0301},
88    /*0d*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0301},
89    /*0e*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0300, 0x0301},
90    /*0f*/    {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0300},
91    /*10*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0300},
92    /*11*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0300},
93    /*12*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0300},
94    /*13*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0300},
95    /*14*/    {0x0065/*'e'*/, 0x0065/*'e'*/, 0x0301, 0x0300},
96    /*15*/    {0x0065/*'e'*/, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0300},
97    /*16*/    {0x0065/*'e'*/, 0x0300, 0x0301, 0x0065/*'e'*/, 0x0301, 0x0300},
98    /*17*/    {0x0065/*'e'*/, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0300},
99    /*18*/    {0x0065/*'e'*/, 0x0301, 0x0300, 0x0065/*'e'*/, 0x0301, 0x0300}
100    };
101
102    private static char[][] testBugs = {
103        {0x0061/*'a'*/},
104        {0x0041/*'A'*/},
105        {0x0065/*'e'*/},
106        {0x0045/*'E'*/},
107        {0x00e9},
108        {0x00e8},
109        {0x00ea},
110        {0x00eb},
111        {0x0065/*'e'*/, 0x0061/*'a'*/},
112        {0x0078/*'x'*/}
113    };
114
115
116    private Collator myCollation = null;
117
118    public CollationFrenchTest() {
119
120    }
121    protected void init()throws Exception{
122        myCollation = Collator.getInstance(Locale.CANADA_FRENCH);
123    }
124
125    // perform tests with strength TERTIARY
126    public void TestTertiary() {
127        int i = 0;
128        myCollation.setStrength(Collator.TERTIARY);
129
130        for (i = 0; i < 12 ; i++) {
131            doTest(testSourceCases[i], testTargetCases[i], results[i]);
132        }
133    }
134
135    // perform tests with strength SECONDARY
136    public void TestSecondary() {
137        //test acute and grave ordering
138        int i = 0;
139        int j;
140        int expected;
141
142        myCollation.setStrength(Collator.SECONDARY);
143
144        for (i = 0; i < testAcute.length; i++) {
145            for (j = 0; j < testAcute.length; j++) {
146                if (i <  j) {
147                    expected = -1;
148                } else if (i == j) {
149                    expected = 0;
150                } else {
151                    expected = 1;
152                }
153                doTest(testAcute[i], testAcute[j], expected );
154            }
155        }
156    }
157
158    // perform extra tests
159    public void TestExtra() {
160        int i, j;
161        myCollation.setStrength(Collator.TERTIARY);
162        for (i = 0; i < 9 ; i++) {
163            for (j = i + 1; j < 10; j += 1) {
164                doTest(testBugs[i], testBugs[j], -1);
165            }
166        }
167    }
168
169    public void TestContinuationReordering()
170    {
171        String rule = "&0x2f00 << 0x2f01";
172        try {
173            RuleBasedCollator collator = new RuleBasedCollator(rule);
174            collator.setFrenchCollation(true);
175            CollationKey key1
176                        = collator.getCollationKey("a\u0325\u2f00\u2f01b\u0325");
177            CollationKey key2
178                        = collator.getCollationKey("a\u0325\u2f01\u2f01b\u0325");
179            if (key1.compareTo(key2) >= 0) {
180                errln("Error comparing continuation strings");
181            }
182        } catch (Exception e) {
183            errln(e.toString());
184        }
185    }
186
187    // main test routine, test rules specific to the french locale
188    private void doTest(char[] source, char[] target, int result) {
189        String s = new String(source);
190        String t = new String(target);
191        int compareResult = myCollation.compare(s, t);
192        CollationKey sortKey1, sortKey2;
193        sortKey1 = myCollation.getCollationKey(s);
194        sortKey2 = myCollation.getCollationKey(t);
195        int keyResult = sortKey1.compareTo(sortKey2);
196        reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
197    }
198
199    private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,
200                                int compareResult, int keyResult, int incResult, int expectedResult ) {
201        if (expectedResult < -1 || expectedResult > 1) {
202            errln("***** invalid call to reportCResult ****");
203            return;
204        }
205
206        boolean ok1 = (compareResult == expectedResult);
207        boolean ok2 = (keyResult == expectedResult);
208        boolean ok3 = (incResult == expectedResult);
209
210        if (ok1 && ok2 && ok3 && !isVerbose()) {
211            return;
212        } else {
213            String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
214            String msg2 = "\", \"";
215            String msg3 = "\") returned ";
216            String msg4 = "; expected ";
217
218            String sExpect = new String("");
219            String sResult = new String("");
220            sResult = CollationTest.appendCompareResult(compareResult, sResult);
221            sExpect = CollationTest.appendCompareResult(expectedResult, sExpect);
222            if (ok1) {
223                logln(msg1 + source + msg2 + target + msg3 + sResult);
224            } else {
225                errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
226            }
227
228            msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
229            msg2 = "\").compareTo(key(\"";
230            msg3 = "\")) returned ";
231            sResult = CollationTest.appendCompareResult(keyResult, sResult);
232            if (ok2) {
233                logln(msg1 + source + msg2 + target + msg3 + sResult);
234            } else {
235                errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
236                msg1 = "  ";
237                msg2 = " vs. ";
238                errln(msg1 + CollationTest.prettify(sourceKey) + msg2 + CollationTest.prettify(targetKey));
239            }
240
241            msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
242            msg2 = "\", \"";
243            msg3 = "\") returned ";
244
245            sResult = CollationTest.appendCompareResult(incResult, sResult);
246
247            if (ok3) {
248                logln(msg1 + source + msg2 + target + msg3 + sResult);
249            } else {
250                errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
251            }
252        }
253    }
254}
255