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