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