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