1 /*
  2  * Copyright (c) 1998, 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 import java.lang.reflect.*;
 25 import java.util.Hashtable;
 26 import java.util.Enumeration;
 27 import java.util.Vector;
 28 import java.io.*;
 29 import java.text.*;
 30 
 31 /**
 32  * CollatorTest is a base class for tests that can be run conveniently from
 33  * the command line as well as under the Java test harness.
 34  * <p>
 35  * Sub-classes implement a set of methods named Test<something>. Each
 36  * of these methods performs some test. Test methods should indicate
 37  * errors by calling either err or errln.  This will increment the
 38  * errorCount field and may optionally print a message to the log.
 39  * Debugging information may also be added to the log via the log
 40  * and logln methods.  These methods will add their arguments to the
 41  * log only if the test is being run in verbose mode.
 42  */
 43 public abstract class CollatorTest extends IntlTest {
 44 
 45     //------------------------------------------------------------------------
 46     // These methods are utilities specific to the Collation tests..
 47     //------------------------------------------------------------------------
 48 
 49     protected void assertEqual(CollationElementIterator i1, CollationElementIterator i2) {
 50         int c1, c2, count = 0;
 51         do {
 52             c1 = i1.next();
 53             c2 = i2.next();
 54             if (c1 != c2) {
 55                 errln("    " + count + ": " + c1 + " != " + c2);
 56                 break;
 57             }
 58             count++;
 59         } while (c1 != CollationElementIterator.NULLORDER);
 60     }
 61 
 62     // Replace nonprintable characters with unicode escapes
 63     static protected String prettify(String str) {
 64         StringBuffer result = new StringBuffer();
 65 
 66         String zero = "0000";
 67 
 68         for (int i = 0; i < str.length(); i++) {
 69             char ch = str.charAt(i);
 70             if (ch < 0x09 || (ch > 0x0A && ch < 0x20)|| (ch > 0x7E && ch < 0xA0) || ch > 0x100) {
 71                 String hex = Integer.toString((int)ch,16);
 72 
 73                 result.append("\\u" + zero.substring(0, 4 - hex.length()) + hex);
 74             } else {
 75                 result.append(ch);
 76             }
 77         }
 78         return result.toString();
 79     }
 80 
 81     // Produce a printable representation of a CollationKey
 82     static protected String prettify(CollationKey key) {
 83         StringBuffer result = new StringBuffer();
 84         byte[] bytes = key.toByteArray();
 85 
 86         for (int i = 0; i < bytes.length; i += 2) {
 87             int val = (bytes[i] << 8) + bytes[i+1];
 88             result.append(Integer.toString(val, 16) + " ");
 89         }
 90         return result.toString();
 91     }
 92 
 93     //------------------------------------------------------------------------
 94     // Everything below here is boilerplate code that makes it possible
 95     // to add a new test by simply adding a function to an existing class
 96     //------------------------------------------------------------------------
 97 
 98     protected void doTest(Collator col, int strength,
 99                           String[] source, String[] target, int[] result) {
100         if (source.length != target.length) {
101             errln("Data size mismatch: source = " +
102                   source.length + ", target = " + target.length);
103 
104             return; // Return if "-nothrow" is specified.
105         }
106         if (source.length != result.length) {
107             errln("Data size mismatch: source & target = " +
108                   source.length + ", result = " + result.length);
109 
110             return; // Return if "-nothrow" is specified.
111         }
112 
113         col.setStrength(strength);
114         for (int i = 0; i < source.length ; i++) {
115             doTest(col, source[i], target[i], result[i]);
116         }
117     }
118 
119     protected void doTest(Collator col,
120                           String source, String target, int result) {
121         char relation = '=';
122         if (result <= -1) {
123             relation = '<';
124         } else if (result >= 1) {
125             relation = '>';
126         }
127 
128         int compareResult = col.compare(source, target);
129         CollationKey sortKey1 = col.getCollationKey(source);
130         CollationKey sortKey2 = col.getCollationKey(target);
131         int keyResult = sortKey1.compareTo(sortKey2);
132         if (compareResult != keyResult) {
133             errln("Compare and Collation Key results are different! Source = " +
134                   source + " Target = " + target);
135         }
136         if (keyResult != result) {
137             errln("Collation Test failed! Source = " + source + " Target = " +
138                   target + " result should be " + relation);
139         }
140     }
141 }