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