Path: blob/master/test/jdk/java/text/CharacterIterator/CharacterIteratorTest.java
41152 views
/*1* Copyright (c) 1997, 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* @library /java/text/testlib26* @summary test for Character Iterator27*/2829/*30*31*32* (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved33* (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved34*35* Portions copyright (c) 2007 Sun Microsystems, Inc.36* All Rights Reserved.37*38* The original version of this source code and documentation39* is copyrighted and owned by Taligent, Inc., a wholly-owned40* subsidiary of IBM. These materials are provided under terms41* of a License Agreement between Taligent and Sun. This technology42* is protected by multiple US and International patents.43*44* This notice and attribution to Taligent may not be removed.45* Taligent is a registered trademark of Taligent, Inc.46*47* Permission to use, copy, modify, and distribute this software48* and its documentation for NON-COMMERCIAL purposes and without49* fee is hereby granted provided that this copyright notice50* appears in all copies. Please refer to the file "copyright.html"51* for further important copyright and licensing information.52*53* SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF54* THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED55* TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A56* PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR57* ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR58* DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.59*60*/6162import java.text.*;6364public class CharacterIteratorTest extends IntlTest {65public static void main(String[] args) throws Exception {66new CharacterIteratorTest().run(args);67}6869public CharacterIteratorTest() {70}7172public void TestConstructionAndEquality() {73String testText = "Now is the time for all good men to come to the aid of their country.";74String testText2 = "Don't bother using this string.";7576CharacterIterator test1 = new StringCharacterIterator(testText);77CharacterIterator test2 = new StringCharacterIterator(testText, 5);78CharacterIterator test3 = new StringCharacterIterator(testText, 2, 20, 5);79CharacterIterator test4 = new StringCharacterIterator(testText2);80CharacterIterator test5 = (CharacterIterator)test1.clone();8182if (test1.equals(test2) || test1.equals(test3) || test1.equals(test4))83errln("Construation or equals() failed: Two unequal iterators tested equal");8485if (!test1.equals(test5))86errln("clone() or equals() failed: Two clones tested unequal");8788if (test1.hashCode() == test2.hashCode() || test1.hashCode() == test3.hashCode()89|| test1.hashCode() == test4.hashCode())90errln("hash() failed: different objects have same hash code");9192if (test1.hashCode() != test5.hashCode())93errln("hash() failed: identical objects have different hash codes");9495test1.setIndex(5);96if (!test1.equals(test2) || test1.equals(test5))97errln("setIndex() failed");98}99100public void TestIteration() {101String text = "Now is the time for all good men to come to the aid of their country.";102103CharacterIterator iter = new StringCharacterIterator(text, 5);104105if (iter.current() != text.charAt(5))106errln("Iterator didn't start out in the right place.");107108char c = iter.first();109int i = 0;110111if (iter.getBeginIndex() != 0 || iter.getEndIndex() != text.length())112errln("getBeginIndex() or getEndIndex() failed");113114logln("Testing forward iteration...");115do {116if (c == CharacterIterator.DONE && i != text.length())117errln("Iterator reached end prematurely");118else if (c != text.charAt(i))119errln("Character mismatch at position " + i + ", iterator has " + c +120", string has " + text.charAt(c));121122if (iter.current() != c)123errln("current() isn't working right");124if (iter.getIndex() != i)125errln("getIndex() isn't working right");126127if (c != CharacterIterator.DONE) {128c = iter.next();129i++;130}131} while (c != CharacterIterator.DONE);132133c = iter.last();134i = text.length() - 1;135136logln("Testing backward iteration...");137do {138if (c == CharacterIterator.DONE && i >= 0)139errln("Iterator reached end prematurely");140else if (c != text.charAt(i))141errln("Character mismatch at position " + i + ", iterator has " + c +142", string has " + text.charAt(c));143144if (iter.current() != c)145errln("current() isn't working right");146if (iter.getIndex() != i)147errln("getIndex() isn't working right");148149if (c != CharacterIterator.DONE) {150c = iter.previous();151i--;152}153} while (c != CharacterIterator.DONE);154155iter = new StringCharacterIterator(text, 5, 15, 10);156if (iter.getBeginIndex() != 5 || iter.getEndIndex() != 15)157errln("creation of a restricted-range iterator failed");158159if (iter.getIndex() != 10 || iter.current() != text.charAt(10))160errln("starting the iterator in the middle didn't work");161162c = iter.first();163i = 5;164165logln("Testing forward iteration over a range...");166do {167if (c == CharacterIterator.DONE && i != 15)168errln("Iterator reached end prematurely");169else if (c != text.charAt(i))170errln("Character mismatch at position " + i + ", iterator has " + c +171", string has " + text.charAt(c));172173if (iter.current() != c)174errln("current() isn't working right");175if (iter.getIndex() != i)176errln("getIndex() isn't working right");177178if (c != CharacterIterator.DONE) {179c = iter.next();180i++;181}182} while (c != CharacterIterator.DONE);183184c = iter.last();185i = 14;186187logln("Testing backward iteration over a range...");188do {189if (c == CharacterIterator.DONE && i >= 5)190errln("Iterator reached end prematurely");191else if (c != text.charAt(i))192errln("Character mismatch at position " + i + ", iterator has " + c +193", string has " + text.charAt(c));194195if (iter.current() != c)196errln("current() isn't working right");197if (iter.getIndex() != i)198errln("getIndex() isn't working right");199200if (c != CharacterIterator.DONE) {201c = iter.previous();202i--;203}204} while (c != CharacterIterator.DONE);205}206207/**208* @bug 4082050 4078261 4078255209*/210public void TestPathologicalCases() {211String text = "This is only a test.";212213/*214This test is commented out until API-change approval for bug #4082050 goes through.215// test for bug #4082050 (don't get an error if begin == end, even though all216// operations on the iterator will cause exceptions)217// [I actually fixed this so that you CAN create an iterator with begin == end,218// but all operations on it return DONE.]219CharacterIterator iter = new StringCharacterIterator(text, 5, 5, 5);220if (iter.first() != CharacterIterator.DONE221|| iter.next() != CharacterIterator.DONE222|| iter.last() != CharacterIterator.DONE223|| iter.previous() != CharacterIterator.DONE224|| iter.current() != CharacterIterator.DONE225|| iter.getIndex() != 5)226errln("Got something other than DONE when performing operations on an empty StringCharacterIterator");227*/228CharacterIterator iter = null;229230// if we try to construct a StringCharacterIterator with an endIndex that's off231// the end of the String under iterator, we're supposed to get an232// IllegalArgumentException233boolean gotException = false;234try {235iter = new StringCharacterIterator(text, 5, 100, 5);236}237catch (IllegalArgumentException e) {238gotException = true;239}240if (!gotException)241errln("StringCharacterIterator didn't throw an exception when given an invalid substring range.");242243// test for bug #4078255 (getting wrong value from next() when we're at the end244// of the string)245iter = new StringCharacterIterator(text);246int expectedIndex = iter.getEndIndex();247int actualIndex;248249iter.last();250actualIndex = iter.getIndex();251if (actualIndex != expectedIndex - 1)252errln("last() failed: expected " + (expectedIndex - 1) + ", got " + actualIndex);253254iter.next();255actualIndex = iter.getIndex();256if (actualIndex != expectedIndex)257errln("next() after last() failed: expected " + expectedIndex + ", got " + actualIndex);258259iter.next();260actualIndex = iter.getIndex();261if (actualIndex != expectedIndex)262errln("second next() after last() failed: expected " + expectedIndex + ", got " + actualIndex);263}264265/*266* @bug 4123771 4051073267* #4123771 is actually a duplicate of bug #4051073, which was fixed some time ago, but268* no one ever added a regression test for it.269*/270public void TestBug4123771() {271String text = "Some string for testing";272StringCharacterIterator iter = new StringCharacterIterator(text);273int index = iter.getEndIndex();274try {275char c = iter.setIndex(index);276}277catch (Exception e) {278System.out.println("method setIndex(int position) throws unexpected exception " + e);279System.out.println(" position: " + index);280System.out.println(" getEndIndex(): " + iter.getEndIndex());281System.out.println(" text.length(): " + text.length());282errln(""); // re-throw the exception through our test framework283}284}285}286287288