1 /*
  2  * Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.
  3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  4  *
  5  * This code is free software; you can redistribute it and/or modify it
  6  * under the terms of the GNU General Public License version 2 only, as
  7  * published by the Free Software Foundation.
  8  *
  9  * This code is distributed in the hope that it will be useful, but WITHOUT
 10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 12  * version 2 for more details (a copy is included in the LICENSE file that
 13  * accompanied this code).
 14  *
 15  * You should have received a copy of the GNU General Public License version
 16  * 2 along with this work; if not, write to the Free Software Foundation,
 17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 18  *
 19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 20  * or visit www.oracle.com if you need additional information or have any
 21  * questions.
 22  */
 23 
 24 /*
 25  * @test
 26  * @library /java/text/testlib
 27  * @summary test G7 Collation
 28  * @modules jdk.localedata
 29  */
 30 /*
 31  *
 32  *
 33  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
 34  * (C) Copyright IBM Corp. 1996, 1997 - All Rights Reserved
 35  *
 36  * Portions copyright (c) 2007 Sun Microsystems, Inc. All Rights Reserved.
 37  *
 38  *   The original version of this source code and documentation is copyrighted
 39  * and owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These
 40  * materials are provided under terms of a License Agreement between Taligent
 41  * and Sun. This technology is protected by multiple US and International
 42  * patents. This notice and attribution to Taligent may not be removed.
 43  *   Taligent is a registered trademark of Taligent, Inc.
 44  *
 45 */
 46 
 47 /**
 48  * G7 Test cases
 49  *
 50  * @author     Helena Shih
 51  */
 52 
 53 import java.text.Collator;
 54 import java.text.RuleBasedCollator;
 55 import java.util.Locale;
 56 
 57 // G7 test program for printing out test results
 58 
 59 public class G7Test extends CollatorTest {
 60 
 61     public static void main(String[] args) throws Exception {
 62         new G7Test().run(args);
 63     }
 64 
 65     private static final String testCases[] = {
 66         "black-birds",              // 0
 67         "Pat",                      // 1
 68         "p\u00E9ch\u00E9",          // 2
 69         "p\u00EAche",               // 3
 70         "p\u00E9cher",              // 4
 71         "p\u00EAcher",              // 5
 72         "Tod",                      // 6
 73         "T\u00F6ne",                // 7
 74         "Tofu",                     // 8
 75         "blackbirds",               // 9
 76         "Ton",                      // 10
 77         "PAT",                      // 11
 78         "blackbird",                // 12
 79         "black-bird",               // 13
 80         "pat",                      // 14
 81         // Additional tests
 82         "czar",                     // 15
 83         "churo",                    // 16
 84         "cat",                      // 17
 85         "darn",                     // 18
 86         "?",                        // 19
 87         "quick",                    // 20
 88         "#",                        // 21
 89         "&",                        // 22
 90         "aardvark",                 // 23
 91         "a-rdvark",                 // 24
 92         "abbot",                    // 25
 93         "coop",                     // 26
 94         "co-p",                     // 27
 95         "cop",                      // 28
 96         "zebra"                     // 29
 97     };
 98 
 99     // loop to TOTALTESTSET
100     private static final int[][] G7Results = {
101         { 12, 13,  9,  0, 14,  1, 11,  2,  3,  4,
102            5,  6,  8, 10,  7, 31, 31, 31, 31, 31,
103           31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, // en_US
104         { 12, 13,  9,  0, 14,  1, 11,  2,  3,  4,
105            5,  6,  8, 10,  7, 31, 31, 31, 31, 31,
106           31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, // en_GB
107         { 12, 13,  9,  0, 14,  1, 11,  2,  3,  4,
108            5,  6,  8, 10,  7, 31, 31, 31, 31, 31,
109           31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, // en_CA
110         { 12, 13,  9,  0, 14,  1, 11,  3,  2,  4,
111            5,  6,  8, 10,  7, 31, 31, 31, 31, 31,
112           31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, // fr_FR
113         { 12, 13,  9,  0, 14,  1, 11,  3,  2,  4,
114            5,  6,  8, 10,  7, 31, 31, 31, 31, 31,
115           31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, // fr_CA
116         { 12, 13,  9,  0, 14,  1, 11,  2,  3,  4,
117            5,  6,  8, 10,  7, 31, 31, 31, 31, 31,
118           31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, // de_DE
119         { 12, 13,  9,  0, 14,  1, 11,  2,  3,  4,
120            5,  6,  8, 10,  7, 31, 31, 31, 31, 31,
121           31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, // it_IT
122         { 12, 13,  9,  0, 14,  1, 11,  2,  3,  4,
123            5,  6,  8, 10,  7, 31, 31, 31, 31, 31,
124           31, 31, 31, 31, 31, 31, 31, 31, 31, 31 }, // ja_JP
125     };
126 
127     // new table collation with rules "& Z < p, P"
128     // loop to FIXEDTESTSET
129     private static final int[] Test1Results = {
130         12, 13,  9,  0,  6,  8, 10,  7, 14,  1,
131         11,  2,  3,  4,  5, 31, 31, 31, 31, 31,
132         31, 31, 31, 31, 31, 31, 31, 31, 31, 31
133     };
134 
135     // new table collation with rules "& C < ch , cH, Ch, CH "
136     // loop to TOTALTESTSET
137     private static final int[] Test2Results = {
138         19, 22, 21, 23, 25, 24, 12, 13,  9,  0,
139         17, 26, 28, 27, 15, 16, 18, 14,  1, 11,
140          2,  3,  4,  5, 20,  6,  8, 10,  7, 29
141     };
142 
143     // new table collation with rules
144     //     "& Question-mark ; ? & Hash-mark ; # & Ampersand ; '&'  "
145     // loop to TOTALTESTSET
146     private static final int[] Test3Results = {
147         23, 25, 22, 24, 12, 13,  9,  0, 17, 16,
148         26, 28, 27, 15, 18, 21, 14,  1, 11,  2,
149          3,  4,  5, 19, 20,  6,  8, 10,  7, 29
150     };
151 
152     // analogous to Japanese rules
153     //     " & aa ; a- & ee ; e- & ii ; i- & oo ; o- & uu ; u- "
154     // loop to TOTALTESTSET
155     private static final int[] Test4Results = {
156         19, 22, 21, 23, 24, 25, 12, 13,  9,  0,
157         17, 16, 26, 27, 28, 15, 18, 14,  1, 11,
158          2,  3,  4,  5, 20,  6,  8, 10,  7, 29
159     };
160 
161     public void TestG7Data() {
162         for (int i = 0; i < locales.length; i++) {
163             Collator myCollation= null;
164             RuleBasedCollator tblColl1 = null;
165 
166             try {
167                 myCollation = Collator.getInstance(locales[i]);
168                 tblColl1 = new RuleBasedCollator(((RuleBasedCollator)myCollation).getRules());
169             } catch (Exception foo) {
170                 errln("Exception: " + foo.getMessage() +
171                       " Locale : " + locales[i].getDisplayName() +
172                       " getRules failed\n");
173                 continue;
174             }
175             for (int j = 0; j < FIXEDTESTSET; j++) {
176                 for (int n = j+1; n < FIXEDTESTSET; n++) {
177                     doTest(tblColl1, testCases[G7Results[i][j]],
178                            testCases[G7Results[i][n]], -1);
179                 }
180             }
181             myCollation = null;
182         }
183     }
184 
185     /*
186      * Demo Test 1 : Create a new table collation with rules "& Z < p, P"
187      */
188     public void TestDemoTest1() {
189         int j = 0;
190         final Collator myCollation = Collator.getInstance(Locale.US);
191         final String defRules = ((RuleBasedCollator)myCollation).getRules();
192         RuleBasedCollator tblColl = null;
193         String newRules = defRules + " & Z < p, P";
194 
195         try {
196             tblColl = new RuleBasedCollator(newRules);
197             for (j = 0; j < FIXEDTESTSET; j++) {
198                 for (int n = j+1; n < FIXEDTESTSET; n++) {
199                     doTest(tblColl, testCases[Test1Results[j]],
200                            testCases[Test1Results[n]], -1);
201                 }
202             }
203             tblColl = null;
204         } catch (Exception foo) {
205             errln("Exception: " + foo.getMessage() +
206                   "\nDemo Test 1 Table Collation object creation failed.");
207         }
208     }
209 
210     /*
211      * Demo Test 2 : Create a new table collation with rules
212      *     "& C < ch , cH, Ch, CH"
213      */
214     public void TestDemoTest2() {
215         final Collator myCollation = Collator.getInstance(Locale.US);
216         final String defRules = ((RuleBasedCollator)myCollation).getRules();
217         String newRules = defRules + "& C < ch , cH, Ch, CH";
218 
219         try {
220             RuleBasedCollator tblColl = new RuleBasedCollator(newRules);
221             for (int j = 0; j < TOTALTESTSET; j++) {
222                 for (int n = j+1; n < TOTALTESTSET; n++) {
223                     doTest(tblColl, testCases[Test2Results[j]],
224                            testCases[Test2Results[n]], -1);
225                 }
226             }
227         } catch (Exception foo) {
228             errln("Exception: " + foo.getMessage() +
229                   "\nDemo Test 2 Table Collation object creation failed.\n");
230         }
231     }
232 
233     /*
234      * Demo Test 3 : Create a new table collation with rules
235      *     "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&'"
236      */
237     public void TestDemoTest3() {
238         final Collator myCollation = Collator.getInstance(Locale.US);
239         final String defRules = ((RuleBasedCollator)myCollation).getRules();
240         RuleBasedCollator tblColl = null;
241         String newRules = defRules + "& Question'-'mark ; '?' & Hash'-'mark ; '#' & Ampersand ; '&";
242 
243         try {
244             tblColl = new RuleBasedCollator(newRules);
245             for (int j = 0; j < TOTALTESTSET; j++) {
246                 for (int n = j+1; n < TOTALTESTSET; n++) {
247                     doTest(tblColl, testCases[Test3Results[j]],
248                            testCases[Test3Results[n]], -1);
249                 }
250             }
251         } catch (Exception foo) {
252             errln("Exception: " + foo.getMessage() +
253                   "\nDemo Test 3 Table Collation object creation failed.");
254         }
255     }
256 
257     /*
258      * Demo Test 4 : Create a new table collation with rules
259      *     " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' "
260      */
261     public void TestDemoTest4() {
262         final Collator myCollation = Collator.getInstance(Locale.US);
263         final String defRules = ((RuleBasedCollator)myCollation).getRules();
264         RuleBasedCollator tblColl = null;
265         String newRules = defRules + " & aa ; a'-' & ee ; e'-' & ii ; i'-' & oo ; o'-' & uu ; u'-' ";
266 
267         try {
268             tblColl = new RuleBasedCollator(newRules);
269             for (int j = 0; j < TOTALTESTSET; j++) {
270                 for (int n = j+1; n < TOTALTESTSET; n++) {
271                     doTest(tblColl, testCases[Test4Results[j]],
272                            testCases[Test4Results[n]], -1);
273                 }
274             }
275         } catch (Exception foo) {
276             errln("Exception: " + foo.getMessage() +
277                   "\nDemo Test 4 Table Collation object creation failed.");
278         }
279         tblColl = null;
280     }
281 
282     private static final int FIXEDTESTSET = 15;
283     private static final int TOTALTESTSET = 30;
284 
285     private static final Locale locales[] = {
286         Locale.US,
287         Locale.UK,
288         Locale.CANADA,
289         Locale.FRANCE,
290         Locale.CANADA_FRENCH,
291         Locale.GERMANY,
292         Locale.JAPAN,
293         Locale.ITALY
294     };
295 }