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 package test.java.text.Collator; 42 43 import java.io.IOException; 44 import java.util.Random; 45 import java.io.OutputStream; 46 import java.io.PrintStream; 47 import java.util.Locale; 48 import java.text.Collator; 49 import java.text.RuleBasedCollator; 50 import java.text.CollationKey; 51 52 public class MonkeyTest extends CollatorTest 53 { main(String[] args)54 public static void main(String[] args) throws Exception { 55 new MonkeyTest().run(args); 56 } 57 report(String s, String t, int result, int revResult)58 public void report(String s, String t, int result, int revResult) 59 { 60 if (result == -1) 61 { 62 if (revResult != 1) 63 errln(" --> Test Failed"); 64 } 65 else if (result == 1) 66 { 67 if (revResult != -1) 68 errln(" --> Test Failed"); 69 } 70 else if (result == 0) 71 { 72 if (revResult != 0) 73 errln(" --> Test Failed"); 74 } 75 } 76 TestCollationKey()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 errln("CollationKey(" + subs + ")" + ".LT." + "CollationKey(" + addOne + ") Failed."); 111 result = CollationKey2.compareTo(CollationKey1); 112 if (result != 1) 113 errln("CollationKey(" + addOne + ")" + ".GT." + "CollationKey(" + subs + ") Failed."); 114 } checkValue(int value)115 private static int checkValue(int value) 116 { 117 value *= (value > 0) ? 1 : -1; 118 return value; 119 } TestCompare()120 public void TestCompare() 121 { 122 String source = "-abcdefghijklmnopqrstuvwxyz#&^$@"; 123 Random r = new Random(3); 124 int s = checkValue(r.nextInt() % source.length()); 125 int t = checkValue(r.nextInt() % source.length()); 126 int slen = checkValue((r.nextInt() - source.length()) % source.length()); 127 int tlen = checkValue((r.nextInt() - source.length()) % source.length()); 128 String subs = source.substring((s > slen ? slen : s), (s >= slen ? s : slen)); 129 String subt = source.substring((t > tlen ? tlen : t), (t >= tlen ? t : tlen)); 130 myCollator.setStrength(Collator.TERTIARY); 131 int result = myCollator.compare(subs, subt); // Tertiary 132 int revResult = myCollator.compare(subt, subs); // Tertiary 133 report(subs, subt, result, revResult); 134 myCollator.setStrength(Collator.SECONDARY); 135 result = myCollator.compare(subs, subt); // Secondary 136 revResult = myCollator.compare(subt, subs); // Secondary 137 report(subs, subt, result, revResult); 138 myCollator.setStrength(Collator.PRIMARY); 139 result = myCollator.compare(subs, subt); // Primary 140 revResult = myCollator.compare(subt, subs); // Primary 141 report(subs, subt, result, revResult); 142 String addOne = subs + "\uE000"; 143 result = myCollator.compare(subs, addOne); 144 if (result != -1) 145 errln("Test : " + subs + " .LT. " + addOne + " Failed."); 146 result = myCollator.compare(addOne, subs); 147 if (result != 1) 148 errln("Test : " + addOne + " .GE. " + subs + " Failed."); 149 } 150 private static Collator myCollator = Collator.getInstance(); 151 } 152