1 /*
  2  * Copyright (c) 1997, 2022, 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  * @library /java/text/testlib
 27  * @summary test for Character Iterator
 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 public class CharacterIteratorTest extends IntlTest {
 69     public static void main(String[] args) throws Exception {
 70         new CharacterIteratorTest().run(args);
 71     }
 72 
 73     public CharacterIteratorTest() {
 74     }
 75 
 76     public void TestConstructionAndEquality() {
 77         String  testText = "Now is the time for all good men to come to the aid of their country.";
 78         String  testText2 = "Don't bother using this string.";
 79 
 80         CharacterIterator test1 = new StringCharacterIterator(testText);
 81         CharacterIterator test2 = new StringCharacterIterator(testText, 5);
 82         CharacterIterator test3 = new StringCharacterIterator(testText, 2, 20, 5);
 83         CharacterIterator test4 = new StringCharacterIterator(testText2);
 84         CharacterIterator test5 = (CharacterIterator)test1.clone();
 85 
 86         if (test1.equals(test2) || test1.equals(test3) || test1.equals(test4))
 87             errln("Construation or equals() failed: Two unequal iterators tested equal");
 88 
 89         if (!test1.equals(test5))
 90             errln("clone() or equals() failed: Two clones tested unequal");
 91 
 92         if (test1.hashCode() == test2.hashCode() || test1.hashCode() == test3.hashCode()
 93                         || test1.hashCode() == test4.hashCode())
 94             errln("hash() failed:  different objects have same hash code");
 95 
 96         if (test1.hashCode() != test5.hashCode())
 97             errln("hash() failed:  identical objects have different hash codes");
 98 
 99         test1.setIndex(5);
100         if (!test1.equals(test2) || test1.equals(test5))
101             errln("setIndex() failed");
102     }
103 
104     public void TestIteration() {
105         String text = "Now is the time for all good men to come to the aid of their country.";
106 
107         CharacterIterator   iter = new StringCharacterIterator(text, 5);
108 
109         if (iter.current() != text.charAt(5))
110             errln("Iterator didn't start out in the right place.");
111 
112         char c = iter.first();
113         int     i = 0;
114 
115         if (iter.getBeginIndex() != 0 || iter.getEndIndex() != text.length())
116             errln("getBeginIndex() or getEndIndex() failed");
117 
118         logln("Testing forward iteration...");
119         do {
120             if (c == CharacterIterator.DONE && i != text.length())
121                 errln("Iterator reached end prematurely");
122             else if (c != text.charAt(i))
123                 errln("Character mismatch at position " + i + ", iterator has " + c +
124                                     ", string has " + text.charAt(c));
125 
126             if (iter.current() != c)
127                 errln("current() isn't working right");
128             if (iter.getIndex() != i)
129                 errln("getIndex() isn't working right");
130 
131             if (c != CharacterIterator.DONE) {
132                 c = iter.next();
133                 i++;
134             }
135         } while (c != CharacterIterator.DONE);
136 
137         c = iter.last();
138         i = text.length() - 1;
139 
140         logln("Testing backward iteration...");
141         do {
142             if (c == CharacterIterator.DONE && i >= 0)
143                 errln("Iterator reached end prematurely");
144             else if (c != text.charAt(i))
145                 errln("Character mismatch at position " + i + ", iterator has " + c +
146                                     ", string has " + text.charAt(c));
147 
148             if (iter.current() != c)
149                 errln("current() isn't working right");
150             if (iter.getIndex() != i)
151                 errln("getIndex() isn't working right");
152 
153             if (c != CharacterIterator.DONE) {
154                 c = iter.previous();
155                 i--;
156             }
157         } while (c != CharacterIterator.DONE);
158 
159         iter = new StringCharacterIterator(text, 5, 15, 10);
160         if (iter.getBeginIndex() != 5 || iter.getEndIndex() != 15)
161             errln("creation of a restricted-range iterator failed");
162 
163         if (iter.getIndex() != 10 || iter.current() != text.charAt(10))
164             errln("starting the iterator in the middle didn't work");
165 
166         c = iter.first();
167         i = 5;
168 
169         logln("Testing forward iteration over a range...");
170         do {
171             if (c == CharacterIterator.DONE && i != 15)
172                 errln("Iterator reached end prematurely");
173             else if (c != text.charAt(i))
174                 errln("Character mismatch at position " + i + ", iterator has " + c +
175                                     ", string has " + text.charAt(c));
176 
177             if (iter.current() != c)
178                 errln("current() isn't working right");
179             if (iter.getIndex() != i)
180                 errln("getIndex() isn't working right");
181 
182             if (c != CharacterIterator.DONE) {
183                 c = iter.next();
184                 i++;
185             }
186         } while (c != CharacterIterator.DONE);
187 
188         c = iter.last();
189         i = 14;
190 
191         logln("Testing backward iteration over a range...");
192         do {
193             if (c == CharacterIterator.DONE && i >= 5)
194                 errln("Iterator reached end prematurely");
195             else if (c != text.charAt(i))
196                 errln("Character mismatch at position " + i + ", iterator has " + c +
197                                     ", string has " + text.charAt(c));
198 
199             if (iter.current() != c)
200                 errln("current() isn't working right");
201             if (iter.getIndex() != i)
202                 errln("getIndex() isn't working right");
203 
204             if (c != CharacterIterator.DONE) {
205                 c = iter.previous();
206                 i--;
207             }
208         } while (c != CharacterIterator.DONE);
209     }
210 
211     /**
212      * @bug 4082050 4078261 4078255
213      */
214     public void TestPathologicalCases() {
215         String text = "This is only a test.";
216 
217 /*
218 This test is commented out until API-change approval for bug #4082050 goes through.
219         // test for bug #4082050 (don't get an error if begin == end, even though all
220         // operations on the iterator will cause exceptions)
221         // [I actually fixed this so that you CAN create an iterator with begin == end,
222         // but all operations on it return DONE.]
223         CharacterIterator iter = new StringCharacterIterator(text, 5, 5, 5);
224         if (iter.first() != CharacterIterator.DONE
225             || iter.next() != CharacterIterator.DONE
226             || iter.last() != CharacterIterator.DONE
227             || iter.previous() != CharacterIterator.DONE
228             || iter.current() != CharacterIterator.DONE
229             || iter.getIndex() != 5)
230             errln("Got something other than DONE when performing operations on an empty StringCharacterIterator");
231 */
232 CharacterIterator iter = null;
233 
234         // if we try to construct a StringCharacterIterator with an endIndex that's off
235         // the end of the String under iterator, we're supposed to get an
236         // IllegalArgumentException
237         boolean gotException = false;
238         try {
239             iter = new StringCharacterIterator(text, 5, 100, 5);
240         }
241         catch (IllegalArgumentException e) {
242             gotException = true;
243         }
244         if (!gotException)
245             errln("StringCharacterIterator didn't throw an exception when given an invalid substring range.");
246 
247         // test for bug #4078255 (getting wrong value from next() when we're at the end
248         // of the string)
249         iter = new StringCharacterIterator(text);
250         int expectedIndex = iter.getEndIndex();
251         int actualIndex;
252 
253         iter.last();
254         actualIndex = iter.getIndex();
255         if (actualIndex != expectedIndex - 1)
256             errln("last() failed: expected " + (expectedIndex - 1) + ", got " + actualIndex);
257 
258         iter.next();
259         actualIndex = iter.getIndex();
260         if (actualIndex != expectedIndex)
261             errln("next() after last() failed: expected " + expectedIndex + ", got " + actualIndex);
262 
263         iter.next();
264         actualIndex = iter.getIndex();
265         if (actualIndex != expectedIndex)
266             errln("second next() after last() failed: expected " + expectedIndex + ", got " + actualIndex);
267     }
268 
269     /*
270      * @bug 4123771 4051073
271      * #4123771 is actually a duplicate of bug #4051073, which was fixed some time ago, but
272      * no one ever added a regression test for it.
273      */
274     public void TestBug4123771() {
275         String text = "Some string for testing";
276         StringCharacterIterator iter = new StringCharacterIterator(text);
277         int index = iter.getEndIndex();
278         try {
279             char c = iter.setIndex(index);
280         }
281         catch (Exception e) {
282             System.out.println("method setIndex(int position) throws unexpected exception " + e);
283             System.out.println(" position: " + index);
284             System.out.println(" getEndIndex(): " + iter.getEndIndex());
285             System.out.println(" text.length(): " + text.length());
286             errln(""); // re-throw the exception through our test framework
287         }
288     }
289 }