Path: blob/master/test/jdk/java/text/Collator/CollatorTest.java
41149 views
/*1* Copyright (c) 1998, 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*/2223import java.lang.reflect.*;24import java.util.Hashtable;25import java.util.Enumeration;26import java.util.Vector;27import java.io.*;28import java.text.*;2930/**31* CollatorTest is a base class for tests that can be run conveniently from32* the command line as well as under the Java test harness.33* <p>34* Sub-classes implement a set of methods named Test<something>. Each35* of these methods performs some test. Test methods should indicate36* errors by calling either err or errln. This will increment the37* errorCount field and may optionally print a message to the log.38* Debugging information may also be added to the log via the log39* and logln methods. These methods will add their arguments to the40* log only if the test is being run in verbose mode.41*/42public abstract class CollatorTest extends IntlTest {4344//------------------------------------------------------------------------45// These methods are utilities specific to the Collation tests..46//------------------------------------------------------------------------4748protected void assertEqual(CollationElementIterator i1, CollationElementIterator i2) {49int c1, c2, count = 0;50do {51c1 = i1.next();52c2 = i2.next();53if (c1 != c2) {54errln(" " + count + ": " + c1 + " != " + c2);55break;56}57count++;58} while (c1 != CollationElementIterator.NULLORDER);59}6061// Replace nonprintable characters with unicode escapes62static protected String prettify(String str) {63StringBuffer result = new StringBuffer();6465String zero = "0000";6667for (int i = 0; i < str.length(); i++) {68char ch = str.charAt(i);69if (ch < 0x09 || (ch > 0x0A && ch < 0x20)|| (ch > 0x7E && ch < 0xA0) || ch > 0x100) {70String hex = Integer.toString((int)ch,16);7172result.append("\\u" + zero.substring(0, 4 - hex.length()) + hex);73} else {74result.append(ch);75}76}77return result.toString();78}7980// Produce a printable representation of a CollationKey81static protected String prettify(CollationKey key) {82StringBuffer result = new StringBuffer();83byte[] bytes = key.toByteArray();8485for (int i = 0; i < bytes.length; i += 2) {86int val = (bytes[i] << 8) + bytes[i+1];87result.append(Integer.toString(val, 16) + " ");88}89return result.toString();90}9192//------------------------------------------------------------------------93// Everything below here is boilerplate code that makes it possible94// to add a new test by simply adding a function to an existing class95//------------------------------------------------------------------------9697protected void doTest(Collator col, int strength,98String[] source, String[] target, int[] result) {99if (source.length != target.length) {100errln("Data size mismatch: source = " +101source.length + ", target = " + target.length);102103return; // Return if "-nothrow" is specified.104}105if (source.length != result.length) {106errln("Data size mismatch: source & target = " +107source.length + ", result = " + result.length);108109return; // Return if "-nothrow" is specified.110}111112col.setStrength(strength);113for (int i = 0; i < source.length ; i++) {114doTest(col, source[i], target[i], result[i]);115}116}117118protected void doTest(Collator col,119String source, String target, int result) {120char relation = '=';121if (result <= -1) {122relation = '<';123} else if (result >= 1) {124relation = '>';125}126127int compareResult = col.compare(source, target);128CollationKey sortKey1 = col.getCollationKey(source);129CollationKey sortKey2 = col.getCollationKey(target);130int keyResult = sortKey1.compareTo(sortKey2);131if (compareResult != keyResult) {132errln("Compare and Collation Key results are different! Source = " +133source + " Target = " + target);134}135if (keyResult != result) {136errln("Collation Test failed! Source = " + source + " Target = " +137target + " result should be " + relation);138}139}140}141142143