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 /*
25  * @test
26  * @bug 4062985 4108758 4108762 4157299
27  * @library /java/text/testlib
28  * @summary Test CollationElementIterator, particularly the new methods in 1.2
29  */
30 /*
31  * (C) Copyright IBM Corp. 1998 - All Rights Reserved
32  *
33  * The original version of this source code and documentation is copyrighted
34  * and owned by IBM, Inc. These materials are provided under terms of a
35  * License Agreement between IBM and Sun. This technology is protected by
36  * multiple US and International patents. This notice and attribution to IBM
37  * may not be removed.
38  */
39 
40 import java.util.Locale;
41 import java.text.*;
42 
43 public class IteratorTest extends CollatorTest {
44     // TODO:
45     //  - Test previous() with contracting character sequences, which don't work
46     //      at the moment.
47     //  - Test edge cases on setOffset(), e.g. offset > length, etc.
48     //
main(String[] args)49     public static void main(String[] args) throws Exception {
50         new IteratorTest().run(args);
51     }
52 
53     /**
54      * Test for CollationElementIterator.previous()
55      *
56      * @bug 4108758 - Make sure it works with contracting characters
57      *
58      */
TestPrevious()59     public void TestPrevious() throws ParseException {
60         // A basic test to see if it's working at all
61         backAndForth(en_us.getCollationElementIterator(test1));
62 
63         // Test with a contracting character sequence
64         RuleBasedCollator c1 = new RuleBasedCollator(
65             "< a,A < b,B < c,C, d,D < z,Z < ch,cH,Ch,CH" );
66 
67         backAndForth(c1.getCollationElementIterator("abchdcba"));
68 
69         // Test with an expanding character sequence
70         RuleBasedCollator c2 = new RuleBasedCollator(
71             "< a < b < c/abd < d" );
72 
73         backAndForth(c2.getCollationElementIterator("abcd"));
74 
75         // Now try both
76         RuleBasedCollator c3 = new RuleBasedCollator(
77             "< a < b < c/aba < d < z < ch" );
78 
79         backAndForth(c3.getCollationElementIterator("abcdbchdc"));
80     }
81 
82     /**
83      * Test for getOffset() and setOffset()
84      */
TestOffset()85     public void TestOffset() {
86         CollationElementIterator iter = en_us.getCollationElementIterator(test1);
87 
88         // Run all the way through the iterator, then get the offset
89         int orders[] = getOrders(iter);
90 
91         int offset = iter.getOffset();
92         if (offset != test1.length()) {
93             System.out.println("offset at end != length: "
94                                 + offset + " vs " + test1.length());
95         }
96 
97         // Now set the offset back to the beginning and see if it works
98         iter.setOffset(0);
99         assertEqual(iter, en_us.getCollationElementIterator(test1));
100 
101         // TODO: try iterating halfway through a messy string.
102     }
103 
104     /**
105      * Test for setText()
106      */
TestSetText()107     public void TestSetText() {
108         CollationElementIterator iter1 = en_us.getCollationElementIterator(test1);
109         CollationElementIterator iter2 = en_us.getCollationElementIterator(test2);
110 
111         // Run through the second iterator just to exercise it
112         int c = iter2.next();
113         int i = 0;
114         while ( ++i < 10 && c != CollationElementIterator.NULLORDER) {
115             c = iter2.next();
116         }
117 
118         // Now set it to point to the same string as the first iterator
119         iter2.setText(test1);
120         assertEqual(iter1, iter2);
121     }
122 
123     /** @bug 4108762
124      * Test for getMaxExpansion()
125      */
TestMaxExpansion()126     public void TestMaxExpansion() throws ParseException {
127         // Try a simple one first:
128         // The only expansion ends with 'e' and has length 2
129         String[][] test1 = {
130             { "< a & ae = \u00e4 < b < e", "" },
131             { "a",  "1" },
132             { "b",  "1" },
133             { "e",  "2" },
134         };
135         verifyExpansion(test1);
136 
137         // Now a more complicated one:
138         //   "a1" --> "ae"
139         //   "z" --> "aeef"
140         //
141         String[][] test2 = {
142             { "< a & ae = a1 & aeef = z < b < e < f", "" },
143             { "a",  "1" },
144             { "b",  "1" },
145             { "e",  "2" },
146             { "f",  "4" },
147         };
148         verifyExpansion(test2);
149     }
150 
151     /*
152      * @bug 4157299
153      */
TestClearBuffers()154     public void TestClearBuffers() throws ParseException {
155         RuleBasedCollator c = new RuleBasedCollator("< a < b < c & ab = d");
156         CollationElementIterator i = c.getCollationElementIterator("abcd");
157         int e0 = i.next();   // save the first collation element
158         i.setOffset(3);      // go to the expanding character
159         i.next();            // but only use up half of it
160         i.setOffset(0);      // go back to the beginning
161         int e = i.next();    // and get this one again
162         if (e != e0) {
163            errln("got " + Integer.toString(e, 16) + ", expected " +
164                        Integer.toString(e0, 16));
165         }
166     }
167 
168     //------------------------------------------------------------------------
169     // Internal utilities
170     //
171 
backAndForth(CollationElementIterator iter)172     private void backAndForth(CollationElementIterator iter) {
173         // Run through the iterator forwards and stick it into an array
174         int [] orders = getOrders(iter);
175 
176         // Now go through it backwards and make sure we get the same values
177         int index = orders.length;
178         int o;
179 
180         while ((o = iter.previous()) != CollationElementIterator.NULLORDER) {
181             if (o != orders[--index]) {
182                 errln("Mismatch at index " + index + ": "
183                         + orders[index] + " vs " + o);
184                 break;
185             }
186         }
187         if (index != 0) {
188             errln("Didn't get back to beginning - index is " + index);
189 
190             iter.reset();
191             err("next: ");
192             while ((o = iter.next()) != NULLORDER) {
193                 err( Integer.toHexString(o) + " ");
194             }
195             errln("");
196 
197             err("prev: ");
198             while ((o = iter.previous()) != NULLORDER) {
199                  err( Integer.toHexString(o) + " ");
200             }
201             errln("");
202         }
203     }
204 
205     /**
206      * Verify that getMaxExpansion works on a given set of collation rules
207      *
208      * The first row of the "tests" array contains the collation rules
209      * at index 0, and the string at index 1 is ignored.
210      *
211      * Subsequent rows of the array contain a character and a number, both
212      * represented as strings.  The character's collation order is determined,
213      * and getMaxExpansion is called for that character.  If its value is
214      * not equal to the specified number, an error results.
215      */
verifyExpansion(String[][] tests)216     private void verifyExpansion(String[][] tests) throws ParseException
217     {
218         RuleBasedCollator coll = new RuleBasedCollator(tests[0][0]);
219         CollationElementIterator iter = coll.getCollationElementIterator("");
220 
221         for (int i = 1; i < tests.length; i++) {
222             // First get the collation key that the test string expands to
223             iter.setText(tests[i][0]);
224 
225             int order = iter.next();
226 
227             if (order == NULLORDER || iter.next() != NULLORDER) {
228                 iter.reset();
229                 errln("verifyExpansion: '" + tests[i][0] +
230                     "' has multiple orders:" + orderString(iter));
231             }
232 
233             int expansion = iter.getMaxExpansion(order);
234             int expect = new Integer(tests[i][1]).intValue();
235 
236             if (expansion != expect) {
237                 errln("expansion for '" + tests[i][0] + "' is wrong: " +
238                     "expected " + expect + ", got " + expansion);
239             }
240         }
241     }
242 
243     /**
244      * Return an integer array containing all of the collation orders
245      * returned by calls to next on the specified iterator
246      */
getOrders(CollationElementIterator iter)247     private int[] getOrders(CollationElementIterator iter)
248     {
249         int maxSize = 100;
250         int size = 0;
251         int[] orders = new int[maxSize];
252 
253         int order;
254         while ((order = iter.next()) != NULLORDER) {
255             if (size == maxSize) {
256                 maxSize *= 2;
257                 int[] temp = new int[maxSize];
258                 System.arraycopy(orders, 0, temp, 0, size);
259                 orders = temp;
260             }
261             orders[size++] = order;
262         }
263 
264         if (orders.length > size) {
265             int[] temp = new int[size];
266             System.arraycopy(orders, 0, temp, 0, size);
267             orders = temp;
268         }
269         return orders;
270     };
271 
272     /**
273      * Return a string containing all of the collation orders
274      * returned by calls to next on the specified iterator
275      */
orderString(CollationElementIterator iter)276     private String orderString(CollationElementIterator iter) {
277         StringBuffer buf = new StringBuffer();
278 
279         int order;
280         while ((order = iter.next()) != NULLORDER) {
281             buf.append( Integer.toHexString(order) + " ");
282         }
283         return buf.toString();
284     }
285 
286     static final private int NULLORDER = CollationElementIterator.NULLORDER;
287     RuleBasedCollator en_us = (RuleBasedCollator)Collator.getInstance(Locale.US);
288 
289     String test1 = "What subset of all possible test cases?";
290     String test2 = "has the highest probability of detecting";
291 }
292