< prev index next >

test/jdk/java/text/CharacterIterator/CharacterIteratorTest.java

Print this page

  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 }

  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 }
< prev index next >