CollationGermanTest.java revision 7935b1839a081ed19ae0d33029ad3c09632a2caa
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/CollationGermanTest
10 * Source File: $ICU4CRoot/source/test/intltest/decoll.cpp
11 **/
12
13 package com.ibm.icu.dev.test.collator;
14
15 import java.util.Locale;
16
17import com.ibm.icu.dev.test.TestFmwk;
18import com.ibm.icu.text.CollationKey;
19import com.ibm.icu.text.Collator;
20
21 public class CollationGermanTest extends TestFmwk{
22    public static void main(String[] args) throws Exception{
23        new CollationGermanTest().run(args);
24    }
25
26    private static char[][] testSourceCases = {
27        {0x47, 0x72, 0x00F6, 0x00DF, 0x65},
28        {0x61, 0x62, 0x63},
29        {0x54, 0x00F6, 0x6e, 0x65},
30        {0x54, 0x00F6, 0x6e, 0x65},
31        {0x54, 0x00F6, 0x6e, 0x65},
32        {0x61, 0x0308, 0x62, 0x63},
33        {0x00E4, 0x62, 0x63},
34        {0x00E4, 0x62, 0x63},
35        {0x53, 0x74, 0x72, 0x61, 0x00DF, 0x65},
36        {0x65, 0x66, 0x67},
37        {0x00E4, 0x62, 0x63},
38        {0x53, 0x74, 0x72, 0x61, 0x00DF, 0x65}
39    };
40
41    private static char[][] testTargetCases = {
42        {0x47, 0x72, 0x6f, 0x73, 0x73, 0x69, 0x73, 0x74},
43        {0x61, 0x0308, 0x62, 0x63},
44        {0x54, 0x6f, 0x6e},
45        {0x54, 0x6f, 0x64},
46        {0x54, 0x6f, 0x66, 0x75},
47        {0x41, 0x0308, 0x62, 0x63},
48        {0x61, 0x0308, 0x62, 0x63},
49        {0x61, 0x65, 0x62, 0x63},
50        {0x53, 0x74, 0x72, 0x61, 0x73, 0x73, 0x65},
51        {0x65, 0x66, 0x67},
52        {0x61, 0x65, 0x62, 0x63},
53        {0x53, 0x74, 0x72, 0x61, 0x73, 0x73, 0x65}
54    };
55
56    private static int results[][] =
57    {
58        //  Primary  Tertiary
59        { -1,        -1 },
60        { 0,         -1 },
61        { 1,          1 },
62        { 1,          1 },
63        { 1,          1 },
64        { 0,         -1 },
65        { 0,          0 },
66        { -1,        -1 },
67        { 0,          1 },
68        { 0,          0 },
69        { -1,        -1 },
70        { 0,          1 }
71    };
72
73    private Collator myCollation = null;
74
75    public CollationGermanTest() {
76
77    }
78    protected void init() throws Exception{
79        myCollation = Collator.getInstance(Locale.GERMAN);
80        if(myCollation == null) {
81            errln("ERROR: in creation of collator of GERMAN locale");
82        }
83    }
84    // perform test with strength TERTIARY
85    public void TestTertiary(){
86        if(myCollation == null ) {
87            errln("decoll: cannot start test, collator is null\n");
88            return;
89        }
90
91        int i = 0;
92        myCollation.setStrength(Collator.TERTIARY);
93        myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
94        for (i = 0; i < 12 ; i++)
95        {
96            doTest(testSourceCases[i], testTargetCases[i], results[i][1]);
97        }
98    }
99
100    // perform test with strength SECONDARY
101    //This method in icu4c has no implementation.
102    public void TestSecondary(){
103    }
104
105     // perform test with strength PRIMARY
106    public void TestPrimary(){
107        if(myCollation == null ) {
108            errln("decoll: cannot start test, collator is null\n");
109            return;
110        }
111        int i;
112        myCollation.setStrength(Collator.PRIMARY);
113        myCollation.setDecomposition(Collator.CANONICAL_DECOMPOSITION);
114        for (i = 0; i < 12 ; i++)
115        {
116            doTest(testSourceCases[i], testTargetCases[i], results[i][0]);
117        }
118    }
119
120
121    //main test routine, tests rules specific to germa locale
122    private void doTest(char[] source, char[] target, int result){
123        String s = new String(source);
124        String t = new String(target);
125        int compareResult = myCollation.compare(s, t);
126        CollationKey sortKey1, sortKey2;
127        sortKey1 = myCollation.getCollationKey(s);
128        sortKey2 = myCollation.getCollationKey(t);
129        int keyResult = sortKey1.compareTo(sortKey2);
130        reportCResult(s, t, sortKey1, sortKey2, compareResult, keyResult, compareResult, result);
131
132    }
133
134    private void reportCResult( String source, String target, CollationKey sourceKey, CollationKey targetKey,
135                                int compareResult, int keyResult, int incResult, int expectedResult ){
136        if (expectedResult < -1 || expectedResult > 1)
137        {
138            errln("***** invalid call to reportCResult ****");
139            return;
140        }
141
142        boolean ok1 = (compareResult == expectedResult);
143        boolean ok2 = (keyResult == expectedResult);
144        boolean ok3 = (incResult == expectedResult);
145
146        if (ok1 && ok2 && ok3 && !isVerbose()){
147            return;
148        }else{
149            String msg1 = ok1? "Ok: compare(\"" : "FAIL: compare(\"";
150            String msg2 = "\", \"";
151            String msg3 = "\") returned ";
152            String msg4 = "; expected ";
153
154            String sExpect = new String("");
155            String sResult = new String("");
156            sResult = CollationTest.appendCompareResult(compareResult, sResult);
157            sExpect = CollationTest.appendCompareResult(expectedResult, sExpect);
158            if (ok1) {
159                logln(msg1 + source + msg2 + target + msg3 + sResult);
160            } else {
161                errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
162            }
163
164            msg1 = ok2 ? "Ok: key(\"" : "FAIL: key(\"";
165            msg2 = "\").compareTo(key(\"";
166            msg3 = "\")) returned ";
167            sResult = CollationTest.appendCompareResult(keyResult, sResult);
168            if (ok2) {
169                logln(msg1 + source + msg2 + target + msg3 + sResult);
170            } else {
171                errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
172                msg1 = "  ";
173                msg2 = " vs. ";
174                errln(msg1 + CollationTest.prettify(sourceKey) + msg2 + CollationTest.prettify(targetKey));
175            }
176
177            msg1 = ok3 ? "Ok: incCompare(\"" : "FAIL: incCompare(\"";
178            msg2 = "\", \"";
179            msg3 = "\") returned ";
180
181            sResult = CollationTest.appendCompareResult(incResult, sResult);
182
183            if (ok3) {
184                logln(msg1 + source + msg2 + target + msg3 + sResult);
185            } else {
186                errln(msg1 + source + msg2 + target + msg3 + sResult + msg4 + sExpect);
187            }
188        }
189    }
190}
191