< prev index next >

test/jdk/java/text/Collator/MonkeyTest.java

Print this page

  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 {
 52     public static void main(String[] args) throws Exception {
 53         new MonkeyTest().run(args);
 54     }
 55 




 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 

 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     }
113     private static int checkValue(int value)
114     {
115         value *= (value > 0) ? 1 : -1;
116         return value;
117     }

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 }

  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 }
< prev index next >