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 Collation, Monkey style
 28  * @run junit MonkeyTest
 29  */
 30 /*
 31 (C) Copyright Taligent, Inc. 1996 - All Rights Reserved
 32 (C) Copyright IBM Corp. 1996 - All Rights Reserved
 33 
 34   The original version of this source code and documentation is copyrighted and
 35 owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are
 36 provided under terms of a License Agreement between Taligent and Sun. This
 37 technology is protected by multiple US and International patents. This notice and
 38 attribution to Taligent may not be removed.
 39   Taligent is a registered trademark of Taligent, Inc.
 40 */
 41 
 42 import java.io.IOException;
 43 import java.util.Random;
 44 import java.io.OutputStream;
 45 import java.io.PrintStream;
 46 import java.util.Locale;
 47 import java.text.Collator;
 48 import java.text.RuleBasedCollator;
 49 import java.text.CollationKey;
 50 
 51 import org.junit.jupiter.api.Test;
 52 
 53 import static org.junit.jupiter.api.Assertions.fail;
 54 
 55 public class MonkeyTest
 56 {
 57     public void report(String s, String t, int result, int revResult)
 58     {
 59         if (result == -1)
 60         {
 61             if (revResult != 1)
 62                 fail(" --> Test Failed");
 63         }
 64         else if (result == 1)
 65         {
 66             if (revResult != -1)
 67                 fail(" --> Test Failed");
 68         }
 69         else if (result == 0)
 70         {
 71             if (revResult != 0)
 72                 fail(" --> Test Failed");
 73         }
 74     }
 75 
 76     @Test
 77     public void TestCollationKey()
 78     {
 79         String source = "-abcdefghijklmnopqrstuvwxyz#&^$@";
 80         Random r = new Random(3);
 81         int s = checkValue(r.nextInt() % source.length());
 82         int t = checkValue(r.nextInt() % source.length());
 83         int slen = checkValue((r.nextInt() - source.length()) % source.length());
 84         int tlen = checkValue((r.nextInt() - source.length()) % source.length());
 85         String subs = source.substring((s > slen ? slen : s), (s >= slen ? s : slen));
 86         String subt = source.substring((t > tlen ? tlen : t), (t >= tlen ? t : tlen));
 87         myCollator.setStrength(Collator.TERTIARY);
 88         CollationKey CollationKey1 = myCollator.getCollationKey(subs);
 89         CollationKey CollationKey2 = myCollator.getCollationKey(subt);
 90         int result = CollationKey1.compareTo(CollationKey2);  // Tertiary
 91         int revResult = CollationKey2.compareTo(CollationKey1);  // Tertiary
 92         report(("CollationKey(" + subs + ")"), ("CollationKey(" + subt + ")"), result, revResult);
 93         myCollator.setStrength(Collator.SECONDARY);
 94         CollationKey1 = myCollator.getCollationKey(subs);
 95         CollationKey2 = myCollator.getCollationKey(subt);
 96         result = CollationKey1.compareTo(CollationKey2);  // Secondary
 97         revResult = CollationKey2.compareTo(CollationKey1);   // Secondary
 98         report(("CollationKey(" + subs + ")") , ("CollationKey(" + subt + ")"), result, revResult);
 99         myCollator.setStrength(Collator.PRIMARY);
100         CollationKey1 = myCollator.getCollationKey(subs);
101         CollationKey2 = myCollator.getCollationKey(subt);
102         result = CollationKey1.compareTo(CollationKey2);  // Primary
103         revResult = CollationKey2.compareTo(CollationKey1);   // Primary
104         report(("CollationKey(" + subs + ")"), ("CollationKey(" + subt + ")"), result, revResult);
105         String addOne = subs + "\uE000";
106         CollationKey1 = myCollator.getCollationKey(subs);
107         CollationKey2 = myCollator.getCollationKey(addOne);
108         result = CollationKey1.compareTo(CollationKey2);
109         if (result != -1)
110             fail("CollationKey(" + subs + ")" + ".LT." + "CollationKey(" + addOne + ") Failed.");
111         result = CollationKey2.compareTo(CollationKey1);
112         if (result != 1)
113             fail("CollationKey(" + addOne + ")" + ".GT." + "CollationKey(" + subs + ") Failed.");
114     }
115     private static int checkValue(int value)
116     {
117         value *= (value > 0) ? 1 : -1;
118         return value;
119     }
120     @Test
121     public void TestCompare()
122     {
123         String source = "-abcdefghijklmnopqrstuvwxyz#&^$@";
124         Random r = new Random(3);
125         int s = checkValue(r.nextInt() % source.length());
126         int t = checkValue(r.nextInt() % source.length());
127         int slen = checkValue((r.nextInt() - source.length()) % source.length());
128         int tlen = checkValue((r.nextInt() - source.length()) % source.length());
129         String subs = source.substring((s > slen ? slen : s), (s >= slen ? s : slen));
130         String subt = source.substring((t > tlen ? tlen : t), (t >= tlen ? t : tlen));
131         myCollator.setStrength(Collator.TERTIARY);
132         int result = myCollator.compare(subs, subt);  // Tertiary
133         int revResult = myCollator.compare(subt, subs);  // Tertiary
134         report(subs, subt, result, revResult);
135         myCollator.setStrength(Collator.SECONDARY);
136         result = myCollator.compare(subs, subt);  // Secondary
137         revResult = myCollator.compare(subt, subs);  // Secondary
138         report(subs, subt, result, revResult);
139         myCollator.setStrength(Collator.PRIMARY);
140         result = myCollator.compare(subs, subt);  // Primary
141         revResult = myCollator.compare(subt, subs);  // Primary
142         report(subs, subt, result, revResult);
143         String addOne = subs + "\uE000";
144         result = myCollator.compare(subs, addOne);
145         if (result != -1)
146             fail("Test : " + subs + " .LT. " + addOne + " Failed.");
147         result = myCollator.compare(addOne, subs);
148         if (result != 1)
149             fail("Test : " + addOne + " .GE. " + subs + " Failed.");
150     }
151     private static Collator myCollator = Collator.getInstance();
152 }