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