1 /*
  2  * Copyright (c) 1997, 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  * @summary test for Character Iterator
 27  * @run junit CharacterIteratorTest
 28  */
 29 
 30 /*
 31  * This file is available under and governed by the GNU General Public
 32  * License version 2 only, as published by the Free Software Foundation.
 33  * However, the following notice accompanied the original version of this
 34  * file and, per its terms, should not be removed:
 35  *
 36  * (C) Copyright Taligent, Inc. 1996, 1997 - All Rights Reserved
 37  * (C) Copyright IBM Corp. 1996 - 1998 - All Rights Reserved
 38  *
 39  * Portions copyright (c) 2007 Sun Microsystems, Inc.
 40  * All Rights Reserved.
 41  *
 42  * The original version of this source code and documentation
 43  * is copyrighted and owned by Taligent, Inc., a wholly-owned
 44  * subsidiary of IBM. These materials are provided under terms
 45  * of a License Agreement between Taligent and Sun. This technology
 46  * is protected by multiple US and International patents.
 47  *
 48  * This notice and attribution to Taligent may not be removed.
 49  * Taligent is a registered trademark of Taligent, Inc.
 50  *
 51  * Permission to use, copy, modify, and distribute this software
 52  * and its documentation for NON-COMMERCIAL purposes and without
 53  * fee is hereby granted provided that this copyright notice
 54  * appears in all copies. Please refer to the file "copyright.html"
 55  * for further important copyright and licensing information.
 56  *
 57  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF
 58  * THE SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
 59  * TO THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
 60  * PARTICULAR PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR
 61  * ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 62  * DISTRIBUTING THIS SOFTWARE OR ITS DERIVATIVES.
 63  *
 64  */
 65 
 66 import java.text.*;
 67 
 68 import org.junit.jupiter.api.Test;
 69 
 70 import static org.junit.jupiter.api.Assertions.fail;
 71 
 72 public class CharacterIteratorTest {
 73     public CharacterIteratorTest() {
 74     }
 75 
 76     @Test
 77     public void TestConstructionAndEquality() {
 78         String  testText = "Now is the time for all good men to come to the aid of their country.";
 79         String  testText2 = "Don't bother using this string.";
 80 
 81         CharacterIterator test1 = new StringCharacterIterator(testText);
 82         CharacterIterator test2 = new StringCharacterIterator(testText, 5);
 83         CharacterIterator test3 = new StringCharacterIterator(testText, 2, 20, 5);
 84         CharacterIterator test4 = new StringCharacterIterator(testText2);
 85         CharacterIterator test5 = (CharacterIterator)test1.clone();
 86 
 87         if (test1.equals(test2) || test1.equals(test3) || test1.equals(test4))
 88             fail("Construation or equals() failed: Two unequal iterators tested equal");
 89 
 90         if (!test1.equals(test5))
 91             fail("clone() or equals() failed: Two clones tested unequal");
 92 
 93         if (test1.hashCode() == test2.hashCode() || test1.hashCode() == test3.hashCode()
 94                         || test1.hashCode() == test4.hashCode())
 95             fail("hash() failed:  different objects have same hash code");
 96 
 97         if (test1.hashCode() != test5.hashCode())
 98             fail("hash() failed:  identical objects have different hash codes");
 99 
100         test1.setIndex(5);
101         if (!test1.equals(test2) || test1.equals(test5))
102             fail("setIndex() failed");
103     }
104 
105     @Test
106     public void TestIteration() {
107         String text = "Now is the time for all good men to come to the aid of their country.";
108 
109         CharacterIterator   iter = new StringCharacterIterator(text, 5);
110 
111         if (iter.current() != text.charAt(5))
112             fail("Iterator didn't start out in the right place.");
113 
114         char c = iter.first();
115         int     i = 0;
116 
117         if (iter.getBeginIndex() != 0 || iter.getEndIndex() != text.length())
118             fail("getBeginIndex() or getEndIndex() failed");
119 
120         System.out.println("Testing forward iteration...");
121         do {
122             if (c == CharacterIterator.DONE && i != text.length())
123                 fail("Iterator reached end prematurely");
124             else if (c != text.charAt(i))
125                 fail("Character mismatch at position " + i + ", iterator has " + c +
126                                     ", string has " + text.charAt(c));
127 
128             if (iter.current() != c)
129                 fail("current() isn't working right");
130             if (iter.getIndex() != i)
131                 fail("getIndex() isn't working right");
132 
133             if (c != CharacterIterator.DONE) {
134                 c = iter.next();
135                 i++;
136             }
137         } while (c != CharacterIterator.DONE);
138 
139         c = iter.last();
140         i = text.length() - 1;
141 
142         System.out.println("Testing backward iteration...");
143         do {
144             if (c == CharacterIterator.DONE && i >= 0)
145                 fail("Iterator reached end prematurely");
146             else if (c != text.charAt(i))
147                 fail("Character mismatch at position " + i + ", iterator has " + c +
148                                     ", string has " + text.charAt(c));
149 
150             if (iter.current() != c)
151                 fail("current() isn't working right");
152             if (iter.getIndex() != i)
153                 fail("getIndex() isn't working right");
154 
155             if (c != CharacterIterator.DONE) {
156                 c = iter.previous();
157                 i--;
158             }
159         } while (c != CharacterIterator.DONE);
160 
161         iter = new StringCharacterIterator(text, 5, 15, 10);
162         if (iter.getBeginIndex() != 5 || iter.getEndIndex() != 15)
163             fail("creation of a restricted-range iterator failed");
164 
165         if (iter.getIndex() != 10 || iter.current() != text.charAt(10))
166             fail("starting the iterator in the middle didn't work");
167 
168         c = iter.first();
169         i = 5;
170 
171         System.out.println("Testing forward iteration over a range...");
172         do {
173             if (c == CharacterIterator.DONE && i != 15)
174                 fail("Iterator reached end prematurely");
175             else if (c != text.charAt(i))
176                 fail("Character mismatch at position " + i + ", iterator has " + c +
177                                     ", string has " + text.charAt(c));
178 
179             if (iter.current() != c)
180                 fail("current() isn't working right");
181             if (iter.getIndex() != i)
182                 fail("getIndex() isn't working right");
183 
184             if (c != CharacterIterator.DONE) {
185                 c = iter.next();
186                 i++;
187             }
188         } while (c != CharacterIterator.DONE);
189 
190         c = iter.last();
191         i = 14;
192 
193         System.out.println("Testing backward iteration over a range...");
194         do {
195             if (c == CharacterIterator.DONE && i >= 5)
196                 fail("Iterator reached end prematurely");
197             else if (c != text.charAt(i))
198                 fail("Character mismatch at position " + i + ", iterator has " + c +
199                                     ", string has " + text.charAt(c));
200 
201             if (iter.current() != c)
202                 fail("current() isn't working right");
203             if (iter.getIndex() != i)
204                 fail("getIndex() isn't working right");
205 
206             if (c != CharacterIterator.DONE) {
207                 c = iter.previous();
208                 i--;
209             }
210         } while (c != CharacterIterator.DONE);
211     }
212 
213     /**
214      * @bug 4082050 4078261 4078255
215      */
216     @Test
217     public void TestPathologicalCases() {
218         String text = "This is only a test.";
219 
220 /*
221 This test is commented out until API-change approval for bug #4082050 goes through.
222         // test for bug #4082050 (don't get an error if begin == end, even though all
223         // operations on the iterator will cause exceptions)
224         // [I actually fixed this so that you CAN create an iterator with begin == end,
225         // but all operations on it return DONE.]
226         CharacterIterator iter = new StringCharacterIterator(text, 5, 5, 5);
227         if (iter.first() != CharacterIterator.DONE
228             || iter.next() != CharacterIterator.DONE
229             || iter.last() != CharacterIterator.DONE
230             || iter.previous() != CharacterIterator.DONE
231             || iter.current() != CharacterIterator.DONE
232             || iter.getIndex() != 5)
233             fail("Got something other than DONE when performing operations on an empty StringCharacterIterator");
234 */
235 CharacterIterator iter = null;
236 
237         // if we try to construct a StringCharacterIterator with an endIndex that's off
238         // the end of the String under iterator, we're supposed to get an
239         // IllegalArgumentException
240         boolean gotException = false;
241         try {
242             iter = new StringCharacterIterator(text, 5, 100, 5);
243         }
244         catch (IllegalArgumentException e) {
245             gotException = true;
246         }
247         if (!gotException)
248             fail("StringCharacterIterator didn't throw an exception when given an invalid substring range.");
249 
250         // test for bug #4078255 (getting wrong value from next() when we're at the end
251         // of the string)
252         iter = new StringCharacterIterator(text);
253         int expectedIndex = iter.getEndIndex();
254         int actualIndex;
255 
256         iter.last();
257         actualIndex = iter.getIndex();
258         if (actualIndex != expectedIndex - 1)
259             fail("last() failed: expected " + (expectedIndex - 1) + ", got " + actualIndex);
260 
261         iter.next();
262         actualIndex = iter.getIndex();
263         if (actualIndex != expectedIndex)
264             fail("next() after last() failed: expected " + expectedIndex + ", got " + actualIndex);
265 
266         iter.next();
267         actualIndex = iter.getIndex();
268         if (actualIndex != expectedIndex)
269             fail("second next() after last() failed: expected " + expectedIndex + ", got " + actualIndex);
270     }
271 
272     /*
273      * @bug 4123771 4051073
274      * #4123771 is actually a duplicate of bug #4051073, which was fixed some time ago, but
275      * no one ever added a regression test for it.
276      */
277     @Test
278     public void TestBug4123771() {
279         String text = "Some string for testing";
280         StringCharacterIterator iter = new StringCharacterIterator(text);
281         int index = iter.getEndIndex();
282         try {
283             char c = iter.setIndex(index);
284         }
285         catch (Exception e) {
286             System.out.println("method setIndex(int position) throws unexpected exception " + e);
287             System.out.println(" position: " + index);
288             System.out.println(" getEndIndex(): " + iter.getEndIndex());
289             System.out.println(" text.length(): " + text.length());
290             fail(""); // re-throw the exception through our test framework
291         }
292     }
293 }