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 1.1 02/09/12
  26  * @bug 4176141 4655819
  27  * @summary Regression tests for Korean Collation
  28  */
  29 
  30 import java.text.*;
  31 import java.util.*;
  32 
  33 public class KoreanTest {
  34 
  35     // NOTE:
  36     //   Golden data in this test case is locale data dependent and
  37     //   may need to be changed if the Korean locale collation rules
  38     //   are changed.
  39 
  40     // And, CollationDecomp has been set to 0(NO_DECOMPOSITION) in
  41     // LocaleElements_ko.java.
  42     // This is very important to consider what is correct behavior in
  43     // Korean Collator. Sometimes different from other locales.
  44 
  45     /*
  46      * TERTIARY(default): s1 < s2,  SECONDARY: s1 < s2,  PRIMARY: s1 < s2
  47      */
  48     static final String[][] compData1 = {
  49         /*
  50          * Data to verify '<' relationship in LocaleElements_ja.java
  51          */
  52         {"\uACE0\uC591\uC774", "\u732B",
  53          "Hangul \"Cat\"(0xACE0 0xC591 0xC774) <---> Chinese Kanji \"Cat\"(0x732B)"},
  54         {"\u30FB", "\u2025",
  55          "Katakana middle dot(0x30FB) <---> Two dot leader(0x2025)"},
  56 
  57         {"\u00B1", "\u2260",
  58          "Plus-Minus Sign(0x00B1) <---> Not Equal To(0x2260)"},
  59         {"\u3011", "\u2260",
  60          "Right Black Lenticular Bracket(0x3011) <---> Not Equal To(0x2260)"},
  61         {"\u2260", "\u2103",
  62          "Not Equal To(0x2260) <---> Degree Celsius(0x2103)"},
  63         {"\u2260", "\u2606",
  64          "Not Equal To(0x2260) <---> White Star(0x2606)"},
  65 
  66         // Unlike other locales' Collator, compare() returns -1 because of
  67         // NO_DECOMPOSITION.
  68         /* above "assumption" is no longer true, now we do normalize ("decomposition")
  69            for the pattern in ko locale, but exclude those hangul syllables, so the
  70            test case below need to be excluded from tiger/1.5
  71         {"\u003D\u0338", "\u2260",
  72          "Equal(0x003D)Combining Long Solidus Overlay(0x0338) <---> Not Equal To(0x2260)"},
  73          */
  74     };
  75 
  76     /*
  77      * TERTIARY(default): s1 = s2,  SECONDARY: s1 = s2,  PRIMARY: s1 = s2
  78      */
  79     static final String[][] compData2 = {
  80         // Verify a character which has been added since Unicode 2.1.X.
  81         {"\u798F", "\uFA1B",
  82          "CJK Unified Ideograph \"FUKU\"(0x798F) <---> CJK Compatibility Ideograph \"FUKU\"(0xFA1B)"},
  83 
  84     };
  85 
  86     Collator col = Collator.getInstance(Locale.KOREA);
  87     int result = 0;
  88 
  89     public static void main(String[] args) throws Exception {
  90         new KoreanTest().run();
  91     }
  92 
  93     public void run() {
  94         //
  95         // Test for TERTIARY(default)
  96         //
  97         doCompare(compData1);
  98         doEquals(compData2);
  99 
 100         //
 101         // Test for SECONDARY
 102         //
 103         col.setStrength(Collator.SECONDARY);
 104         doCompare(compData1);
 105         doEquals(compData2);
 106 
 107         //
 108         // Test for PRIMARY
 109         //
 110         col.setStrength(Collator.PRIMARY);
 111         doCompare(compData1);
 112         doEquals(compData2);
 113 
 114         if (result !=0) {
 115             throw new RuntimeException("Unexpected results on Korean collation.");
 116         }
 117     }
 118 
 119     /* compare() should return -1 for each combination. */
 120     void doCompare(String[][] s) {
 121         int value;
 122         for (int i=0; i < s.length; i++) {
 123             if ((value = col.compare(s[i][0], s[i][1])) > -1) {
 124                 result++;
 125                 System.err.println("TERTIARY: The first string should be less than the second string:  " +
 126                     s[i][2] + "  compare() returned " + value + ".");
 127             }
 128         }
 129     }
 130 
 131     /* equals() should return true for each combination. */
 132     void doEquals(String[][] s) {
 133         for (int i=0; i < s.length; i++) {
 134             if (!col.equals(s[i][0], s[i][1])) {
 135                 result++;
 136                 System.err.println("TERTIARY: The first string should be equals to the second string:  " +
 137                     s[i][2] + "  compare() returned " +
 138                     col.compare(s[i][0], s[i][1] + "."));
 139             }
 140         }
 141     }
 142 }