Path: blob/master/test/jdk/java/text/Collator/IteratorTest.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*/2223/*24* @test25* @bug 4062985 4108758 4108762 415729926* @library /java/text/testlib27* @summary Test CollationElementIterator, particularly the new methods in 1.228*/29/*30* (C) Copyright IBM Corp. 1998 - All Rights Reserved31*32* The original version of this source code and documentation is copyrighted33* and owned by IBM, Inc. These materials are provided under terms of a34* License Agreement between IBM and Sun. This technology is protected by35* multiple US and International patents. This notice and attribution to IBM36* may not be removed.37*/3839import java.util.Locale;40import java.text.*;4142public class IteratorTest extends CollatorTest {43// TODO:44// - Test previous() with contracting character sequences, which don't work45// at the moment.46// - Test edge cases on setOffset(), e.g. offset > length, etc.47//48public static void main(String[] args) throws Exception {49new IteratorTest().run(args);50}5152/**53* Test for CollationElementIterator.previous()54*55* @bug 4108758 - Make sure it works with contracting characters56*57*/58public void TestPrevious() throws ParseException {59// A basic test to see if it's working at all60backAndForth(en_us.getCollationElementIterator(test1));6162// Test with a contracting character sequence63RuleBasedCollator c1 = new RuleBasedCollator(64"< a,A < b,B < c,C, d,D < z,Z < ch,cH,Ch,CH" );6566backAndForth(c1.getCollationElementIterator("abchdcba"));6768// Test with an expanding character sequence69RuleBasedCollator c2 = new RuleBasedCollator(70"< a < b < c/abd < d" );7172backAndForth(c2.getCollationElementIterator("abcd"));7374// Now try both75RuleBasedCollator c3 = new RuleBasedCollator(76"< a < b < c/aba < d < z < ch" );7778backAndForth(c3.getCollationElementIterator("abcdbchdc"));79}8081/**82* Test for getOffset() and setOffset()83*/84public void TestOffset() {85CollationElementIterator iter = en_us.getCollationElementIterator(test1);8687// Run all the way through the iterator, then get the offset88int orders[] = getOrders(iter);8990int offset = iter.getOffset();91if (offset != test1.length()) {92System.out.println("offset at end != length: "93+ offset + " vs " + test1.length());94}9596// Now set the offset back to the beginning and see if it works97iter.setOffset(0);98assertEqual(iter, en_us.getCollationElementIterator(test1));99100// TODO: try iterating halfway through a messy string.101}102103/**104* Test for setText()105*/106public void TestSetText() {107CollationElementIterator iter1 = en_us.getCollationElementIterator(test1);108CollationElementIterator iter2 = en_us.getCollationElementIterator(test2);109110// Run through the second iterator just to exercise it111int c = iter2.next();112int i = 0;113while ( ++i < 10 && c != CollationElementIterator.NULLORDER) {114c = iter2.next();115}116117// Now set it to point to the same string as the first iterator118iter2.setText(test1);119assertEqual(iter1, iter2);120}121122/** @bug 4108762123* Test for getMaxExpansion()124*/125public void TestMaxExpansion() throws ParseException {126// Try a simple one first:127// The only expansion ends with 'e' and has length 2128String[][] test1 = {129{ "< a & ae = \u00e4 < b < e", "" },130{ "a", "1" },131{ "b", "1" },132{ "e", "2" },133};134verifyExpansion(test1);135136// Now a more complicated one:137// "a1" --> "ae"138// "z" --> "aeef"139//140String[][] test2 = {141{ "< a & ae = a1 & aeef = z < b < e < f", "" },142{ "a", "1" },143{ "b", "1" },144{ "e", "2" },145{ "f", "4" },146};147verifyExpansion(test2);148}149150/*151* @bug 4157299152*/153public void TestClearBuffers() throws ParseException {154RuleBasedCollator c = new RuleBasedCollator("< a < b < c & ab = d");155CollationElementIterator i = c.getCollationElementIterator("abcd");156int e0 = i.next(); // save the first collation element157i.setOffset(3); // go to the expanding character158i.next(); // but only use up half of it159i.setOffset(0); // go back to the beginning160int e = i.next(); // and get this one again161if (e != e0) {162errln("got " + Integer.toString(e, 16) + ", expected " +163Integer.toString(e0, 16));164}165}166167//------------------------------------------------------------------------168// Internal utilities169//170171private void backAndForth(CollationElementIterator iter) {172// Run through the iterator forwards and stick it into an array173int [] orders = getOrders(iter);174175// Now go through it backwards and make sure we get the same values176int index = orders.length;177int o;178179while ((o = iter.previous()) != CollationElementIterator.NULLORDER) {180if (o != orders[--index]) {181errln("Mismatch at index " + index + ": "182+ orders[index] + " vs " + o);183break;184}185}186if (index != 0) {187errln("Didn't get back to beginning - index is " + index);188189iter.reset();190err("next: ");191while ((o = iter.next()) != NULLORDER) {192err( Integer.toHexString(o) + " ");193}194errln("");195196err("prev: ");197while ((o = iter.previous()) != NULLORDER) {198err( Integer.toHexString(o) + " ");199}200errln("");201}202}203204/**205* Verify that getMaxExpansion works on a given set of collation rules206*207* The first row of the "tests" array contains the collation rules208* at index 0, and the string at index 1 is ignored.209*210* Subsequent rows of the array contain a character and a number, both211* represented as strings. The character's collation order is determined,212* and getMaxExpansion is called for that character. If its value is213* not equal to the specified number, an error results.214*/215private void verifyExpansion(String[][] tests) throws ParseException216{217RuleBasedCollator coll = new RuleBasedCollator(tests[0][0]);218CollationElementIterator iter = coll.getCollationElementIterator("");219220for (int i = 1; i < tests.length; i++) {221// First get the collation key that the test string expands to222iter.setText(tests[i][0]);223224int order = iter.next();225226if (order == NULLORDER || iter.next() != NULLORDER) {227iter.reset();228errln("verifyExpansion: '" + tests[i][0] +229"' has multiple orders:" + orderString(iter));230}231232int expansion = iter.getMaxExpansion(order);233int expect = new Integer(tests[i][1]).intValue();234235if (expansion != expect) {236errln("expansion for '" + tests[i][0] + "' is wrong: " +237"expected " + expect + ", got " + expansion);238}239}240}241242/**243* Return an integer array containing all of the collation orders244* returned by calls to next on the specified iterator245*/246private int[] getOrders(CollationElementIterator iter)247{248int maxSize = 100;249int size = 0;250int[] orders = new int[maxSize];251252int order;253while ((order = iter.next()) != NULLORDER) {254if (size == maxSize) {255maxSize *= 2;256int[] temp = new int[maxSize];257System.arraycopy(orders, 0, temp, 0, size);258orders = temp;259}260orders[size++] = order;261}262263if (orders.length > size) {264int[] temp = new int[size];265System.arraycopy(orders, 0, temp, 0, size);266orders = temp;267}268return orders;269};270271/**272* Return a string containing all of the collation orders273* returned by calls to next on the specified iterator274*/275private String orderString(CollationElementIterator iter) {276StringBuffer buf = new StringBuffer();277278int order;279while ((order = iter.next()) != NULLORDER) {280buf.append( Integer.toHexString(order) + " ");281}282return buf.toString();283}284285static final private int NULLORDER = CollationElementIterator.NULLORDER;286RuleBasedCollator en_us = (RuleBasedCollator)Collator.getInstance(Locale.US);287288String test1 = "What subset of all possible test cases?";289String test2 = "has the highest probability of detecting";290}291292293