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