Path: blob/master/test/jdk/java/text/Collator/MonkeyTest.java
41149 views
/*1* Copyright (c) 1997, 2016, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @library /java/text/testlib26* @summary test Collation, Monkey style27*/28/*29(C) Copyright Taligent, Inc. 1996 - All Rights Reserved30(C) Copyright IBM Corp. 1996 - All Rights Reserved3132The original version of this source code and documentation is copyrighted and33owned by Taligent, Inc., a wholly-owned subsidiary of IBM. These materials are34provided under terms of a License Agreement between Taligent and Sun. This35technology is protected by multiple US and International patents. This notice and36attribution to Taligent may not be removed.37Taligent is a registered trademark of Taligent, Inc.38*/3940import java.io.IOException;41import java.util.Random;42import java.io.OutputStream;43import java.io.PrintStream;44import java.util.Locale;45import java.text.Collator;46import java.text.RuleBasedCollator;47import java.text.CollationKey;4849public class MonkeyTest extends CollatorTest50{51public static void main(String[] args) throws Exception {52new MonkeyTest().run(args);53}5455public void report(String s, String t, int result, int revResult)56{57if (result == -1)58{59if (revResult != 1)60errln(" --> Test Failed");61}62else if (result == 1)63{64if (revResult != -1)65errln(" --> Test Failed");66}67else if (result == 0)68{69if (revResult != 0)70errln(" --> Test Failed");71}72}7374public void TestCollationKey()75{76String source = "-abcdefghijklmnopqrstuvwxyz#&^$@";77Random r = new Random(3);78int s = checkValue(r.nextInt() % source.length());79int t = checkValue(r.nextInt() % source.length());80int slen = checkValue((r.nextInt() - source.length()) % source.length());81int tlen = checkValue((r.nextInt() - source.length()) % source.length());82String subs = source.substring((s > slen ? slen : s), (s >= slen ? s : slen));83String subt = source.substring((t > tlen ? tlen : t), (t >= tlen ? t : tlen));84myCollator.setStrength(Collator.TERTIARY);85CollationKey CollationKey1 = myCollator.getCollationKey(subs);86CollationKey CollationKey2 = myCollator.getCollationKey(subt);87int result = CollationKey1.compareTo(CollationKey2); // Tertiary88int revResult = CollationKey2.compareTo(CollationKey1); // Tertiary89report(("CollationKey(" + subs + ")"), ("CollationKey(" + subt + ")"), result, revResult);90myCollator.setStrength(Collator.SECONDARY);91CollationKey1 = myCollator.getCollationKey(subs);92CollationKey2 = myCollator.getCollationKey(subt);93result = CollationKey1.compareTo(CollationKey2); // Secondary94revResult = CollationKey2.compareTo(CollationKey1); // Secondary95report(("CollationKey(" + subs + ")") , ("CollationKey(" + subt + ")"), result, revResult);96myCollator.setStrength(Collator.PRIMARY);97CollationKey1 = myCollator.getCollationKey(subs);98CollationKey2 = myCollator.getCollationKey(subt);99result = CollationKey1.compareTo(CollationKey2); // Primary100revResult = CollationKey2.compareTo(CollationKey1); // Primary101report(("CollationKey(" + subs + ")"), ("CollationKey(" + subt + ")"), result, revResult);102String addOne = subs + "\uE000";103CollationKey1 = myCollator.getCollationKey(subs);104CollationKey2 = myCollator.getCollationKey(addOne);105result = CollationKey1.compareTo(CollationKey2);106if (result != -1)107errln("CollationKey(" + subs + ")" + ".LT." + "CollationKey(" + addOne + ") Failed.");108result = CollationKey2.compareTo(CollationKey1);109if (result != 1)110errln("CollationKey(" + addOne + ")" + ".GT." + "CollationKey(" + subs + ") Failed.");111}112private static int checkValue(int value)113{114value *= (value > 0) ? 1 : -1;115return value;116}117public void TestCompare()118{119String source = "-abcdefghijklmnopqrstuvwxyz#&^$@";120Random r = new Random(3);121int s = checkValue(r.nextInt() % source.length());122int t = checkValue(r.nextInt() % source.length());123int slen = checkValue((r.nextInt() - source.length()) % source.length());124int tlen = checkValue((r.nextInt() - source.length()) % source.length());125String subs = source.substring((s > slen ? slen : s), (s >= slen ? s : slen));126String subt = source.substring((t > tlen ? tlen : t), (t >= tlen ? t : tlen));127myCollator.setStrength(Collator.TERTIARY);128int result = myCollator.compare(subs, subt); // Tertiary129int revResult = myCollator.compare(subt, subs); // Tertiary130report(subs, subt, result, revResult);131myCollator.setStrength(Collator.SECONDARY);132result = myCollator.compare(subs, subt); // Secondary133revResult = myCollator.compare(subt, subs); // Secondary134report(subs, subt, result, revResult);135myCollator.setStrength(Collator.PRIMARY);136result = myCollator.compare(subs, subt); // Primary137revResult = myCollator.compare(subt, subs); // Primary138report(subs, subt, result, revResult);139String addOne = subs + "\uE000";140result = myCollator.compare(subs, addOne);141if (result != -1)142errln("Test : " + subs + " .LT. " + addOne + " Failed.");143result = myCollator.compare(addOne, subs);144if (result != 1)145errln("Test : " + addOne + " .GE. " + subs + " Failed.");146}147private static Collator myCollator = Collator.getInstance();148}149150151