1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.text.tests.java.text; 19 20import dalvik.annotation.TestTargets; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTargetNew; 23import dalvik.annotation.TestTargetClass; 24 25import junit.framework.TestCase; 26 27import java.text.CharacterIterator; 28import java.text.StringCharacterIterator; 29 30@TestTargetClass(StringCharacterIterator.class) 31public class StringCharacterIteratorTest extends TestCase { 32 33 /** 34 * @tests java.text.StringCharacterIterator.StringCharacterIterator(String, 35 * int) 36 */ 37 @TestTargetNew( 38 level = TestLevel.COMPLETE, 39 notes = "", 40 method = "StringCharacterIterator", 41 args = {java.lang.String.class, int.class} 42 ) 43 public void test_ConstructorI() { 44 assertNotNull(new StringCharacterIterator("value", 0)); 45 assertNotNull(new StringCharacterIterator("value", "value".length())); 46 assertNotNull(new StringCharacterIterator("", 0)); 47 try { 48 new StringCharacterIterator(null, 0); 49 fail("Assert 0: no null pointer"); 50 } catch (NullPointerException e) { 51 // expected 52 } 53 54 try { 55 new StringCharacterIterator("value", -1); 56 fail("Assert 1: no illegal argument"); 57 } catch (IllegalArgumentException e) { 58 // expected 59 } 60 61 try { 62 new StringCharacterIterator("value", "value".length() + 1); 63 fail("Assert 2: no illegal argument"); 64 } catch (IllegalArgumentException e) { 65 // expected 66 } 67 } 68 69 /** 70 * @tests java.text.StringCharacterIterator(String, int, int, int) 71 */ 72 @TestTargetNew( 73 level = TestLevel.COMPLETE, 74 notes = "", 75 method = "StringCharacterIterator", 76 args = {java.lang.String.class, int.class, int.class, int.class} 77 ) 78 public void test_ConstructorIII() { 79 assertNotNull(new StringCharacterIterator("value", 0, "value".length(), 80 0)); 81 assertNotNull(new StringCharacterIterator("value", 0, "value".length(), 82 1)); 83 assertNotNull(new StringCharacterIterator("", 0, 0, 0)); 84 85 try { 86 new StringCharacterIterator(null, 0, 0, 0); 87 fail("no null pointer"); 88 } catch (NullPointerException e) { 89 // Expected 90 } 91 92 try { 93 new StringCharacterIterator("value", -1, "value".length(), 0); 94 fail("no illegal argument: invalid begin"); 95 } catch (IllegalArgumentException e) { 96 // Expected 97 } 98 99 try { 100 new StringCharacterIterator("value", 0, "value".length() + 1, 0); 101 fail("no illegal argument: invalid end"); 102 } catch (IllegalArgumentException e) { 103 // Expected 104 } 105 106 try { 107 new StringCharacterIterator("value", 2, 1, 0); 108 fail("no illegal argument: start greater than end"); 109 } catch (IllegalArgumentException e) { 110 // Expected 111 } 112 113 try { 114 new StringCharacterIterator("value", 2, 1, 2); 115 fail("no illegal argument: start greater than end"); 116 } catch (IllegalArgumentException e) { 117 // Expected 118 } 119 120 try { 121 new StringCharacterIterator("value", 2, 4, 1); 122 fail("no illegal argument: location greater than start"); 123 } catch (IllegalArgumentException e) { 124 // Expected 125 } 126 127 try { 128 new StringCharacterIterator("value", 0, 2, 3); 129 fail("no illegal argument: location greater than start"); 130 } catch (IllegalArgumentException e) { 131 // Expected 132 } 133 } 134 135 /** 136 * @tests java.text.StringCharacterIterator.equals(Object) 137 */ 138 @TestTargetNew( 139 level = TestLevel.COMPLETE, 140 notes = "", 141 method = "equals", 142 args = {java.lang.Object.class} 143 ) 144 public void test_equalsLjava_lang_Object() { 145 StringCharacterIterator sci0 = new StringCharacterIterator("fixture"); 146 assertEquals(sci0, sci0); 147 assertFalse(sci0.equals(null)); 148 assertFalse(sci0.equals("fixture")); 149 150 StringCharacterIterator sci1 = new StringCharacterIterator("fixture"); 151 assertEquals(sci0, sci1); 152 153 sci1.next(); 154 assertFalse(sci0.equals(sci1)); 155 sci0.next(); 156 assertEquals(sci0, sci1); 157 158 StringCharacterIterator it1 = new StringCharacterIterator("testing", 2, 159 6, 4); 160 StringCharacterIterator it2 = new StringCharacterIterator("xxstinx", 2, 161 6, 4); 162 assertTrue("Range is equal", !it1.equals(it2)); 163 StringCharacterIterator it3 = new StringCharacterIterator("testing", 2, 164 6, 2); 165 it3.setIndex(4); 166 assertTrue("Not equal", it1.equals(it3)); 167 } 168 169 /** 170 * @tests java.text.StringCharacterIterator.clone() 171 */ 172 @TestTargetNew( 173 level = TestLevel.COMPLETE, 174 notes = "", 175 method = "clone", 176 args = {} 177 ) 178 public void test_clone() { 179 StringCharacterIterator sci0 = new StringCharacterIterator("fixture"); 180 assertSame(sci0, sci0); 181 StringCharacterIterator sci1 = (StringCharacterIterator) sci0.clone(); 182 assertNotSame(sci0, sci1); 183 assertEquals(sci0, sci1); 184 185 StringCharacterIterator it = new StringCharacterIterator("testing", 2, 186 6, 4); 187 StringCharacterIterator clone = (StringCharacterIterator) it.clone(); 188 assertTrue("Clone not equal", it.equals(clone)); 189 } 190 191 /** 192 * @tests java.text.StringCharacterIterator.current() 193 */ 194 @TestTargetNew( 195 level = TestLevel.COMPLETE, 196 notes = "", 197 method = "current", 198 args = {} 199 ) 200 public void test_current() { 201 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 202 assertEquals('f', fixture.current()); 203 fixture.next(); 204 assertEquals('i', fixture.current()); 205 206 StringCharacterIterator it = new StringCharacterIterator("testing", 2, 207 6, 4); 208 assertEquals("Wrong current char", 'i', it.current()); 209 it.next(); 210 it.next(); 211 assertEquals("Doesn't return DONE", StringCharacterIterator.DONE, 212 it.current()); 213 it.next(); 214 assertEquals("Doesn't return DONE after next()", 215 StringCharacterIterator.DONE, 216 it.current()); 217 } 218 219 /** 220 * @tests java.text.StringCharacterIterator.first() 221 */ 222 @TestTargetNew( 223 level = TestLevel.COMPLETE, 224 notes = "", 225 method = "first", 226 args = {} 227 ) 228 public void test_first() { 229 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 230 assertEquals('f', fixture.first()); 231 fixture.next(); 232 assertEquals('f', fixture.first()); 233 fixture = new StringCharacterIterator("fixture", 1); 234 assertEquals('f', fixture.first()); 235 fixture = new StringCharacterIterator("fixture", 1, "fixture".length(), 236 2); 237 assertEquals('i', fixture.first()); 238 239 StringCharacterIterator it1 = new StringCharacterIterator("testing", 2, 240 6, 4); 241 assertEquals("Wrong first char", 's', it1.first()); 242 assertEquals("Wrong next char", 't', it1.next()); 243 it1 = new StringCharacterIterator("testing", 2, 2, 2); 244 assertTrue("Not DONE", it1.first() == CharacterIterator.DONE); 245 } 246 247 /** 248 * @tests java.text.StringCharacterIterator.getBeginIndex() 249 */ 250 @TestTargetNew( 251 level = TestLevel.COMPLETE, 252 notes = "", 253 method = "getBeginIndex", 254 args = {} 255 ) 256 public void test_getBeginIndex() { 257 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 258 assertEquals(0, fixture.getBeginIndex()); 259 fixture = new StringCharacterIterator("fixture", 1); 260 assertEquals(0, fixture.getBeginIndex()); 261 fixture = new StringCharacterIterator("fixture", 1, "fixture".length(), 262 2); 263 assertEquals(1, fixture.getBeginIndex()); 264 265 StringCharacterIterator it1 = new StringCharacterIterator("testing", 2, 266 6, 4); 267 assertEquals("Wrong begin index 2", 2, it1.getBeginIndex()); 268 } 269 270 /** 271 * @tests java.text.StringCharacterIterator.getEndIndex() 272 */ 273 @TestTargetNew( 274 level = TestLevel.COMPLETE, 275 notes = "", 276 method = "getEndIndex", 277 args = {} 278 ) 279 public void test_getEndIndex() { 280 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 281 assertEquals("fixture".length(), fixture.getEndIndex()); 282 fixture = new StringCharacterIterator("fixture", 1); 283 assertEquals("fixture".length(), fixture.getEndIndex()); 284 fixture = new StringCharacterIterator("fixture", 1, "fixture".length(), 285 2); 286 assertEquals("fixture".length(), fixture.getEndIndex()); 287 fixture = new StringCharacterIterator("fixture", 1, 4, 2); 288 assertEquals(4, fixture.getEndIndex()); 289 290 StringCharacterIterator it1 = new StringCharacterIterator("testing", 2, 291 6, 4); 292 assertEquals("Wrong end index 6", 6, it1.getEndIndex()); 293 } 294 295 /** 296 * @tests java.text.StringCharacterIterator.getIndex() 297 */ 298 @TestTargetNew( 299 level = TestLevel.COMPLETE, 300 notes = "", 301 method = "getIndex", 302 args = {} 303 ) 304 public void testGetIndex() { 305 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 306 assertEquals(0, fixture.getIndex()); 307 fixture = new StringCharacterIterator("fixture", 1); 308 assertEquals(1, fixture.getIndex()); 309 fixture = new StringCharacterIterator("fixture", 1, "fixture".length(), 310 2); 311 assertEquals(2, fixture.getIndex()); 312 fixture = new StringCharacterIterator("fixture", 1, 4, 2); 313 assertEquals(2, fixture.getIndex()); 314 } 315 316 /** 317 * @tests java.text.StringCharacterIterator.last() 318 */ 319 @TestTargetNew( 320 level = TestLevel.COMPLETE, 321 notes = "", 322 method = "last", 323 args = {} 324 ) 325 public void testLast() { 326 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 327 assertEquals('e', fixture.last()); 328 fixture.next(); 329 assertEquals('e', fixture.last()); 330 fixture = new StringCharacterIterator("fixture", 1); 331 assertEquals('e', fixture.last()); 332 fixture = new StringCharacterIterator("fixture", 1, "fixture".length(), 333 2); 334 assertEquals('e', fixture.last()); 335 fixture = new StringCharacterIterator("fixture", 1, 4, 2); 336 assertEquals('t', fixture.last()); 337 } 338 339 /** 340 * @tests java.text.StringCharacterIterator.next() 341 */ 342 @TestTargetNew( 343 level = TestLevel.COMPLETE, 344 notes = "", 345 method = "next", 346 args = {} 347 ) 348 public void test_next() { 349 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 350 assertEquals(0, fixture.getIndex()); 351 assertEquals('i', fixture.next()); 352 assertEquals(1, fixture.getIndex()); 353 assertEquals('x', fixture.next()); 354 assertEquals(2, fixture.getIndex()); 355 assertEquals('t', fixture.next()); 356 assertEquals(3, fixture.getIndex()); 357 assertEquals('u', fixture.next()); 358 assertEquals(4, fixture.getIndex()); 359 assertEquals('r', fixture.next()); 360 assertEquals(5, fixture.getIndex()); 361 assertEquals('e', fixture.next()); 362 assertEquals(6, fixture.getIndex()); 363 assertEquals(CharacterIterator.DONE, fixture.next()); 364 assertEquals(7, fixture.getIndex()); 365 assertEquals(CharacterIterator.DONE, fixture.next()); 366 assertEquals(7, fixture.getIndex()); 367 assertEquals(CharacterIterator.DONE, fixture.next()); 368 assertEquals(7, fixture.getIndex()); 369 370 StringCharacterIterator it1 = new StringCharacterIterator("testing", 2, 371 6, 3); 372 char result = it1.next(); 373 assertEquals("Wrong next char1", 'i', result); 374 assertEquals("Wrong next char2", 'n', it1.next()); 375 assertTrue("Wrong next char3", it1.next() == CharacterIterator.DONE); 376 assertTrue("Wrong next char4", it1.next() == CharacterIterator.DONE); 377 int index = it1.getIndex(); 378 assertEquals("Wrong index", 6, index); 379 assertTrue("Wrong current char", 380 it1.current() == CharacterIterator.DONE); 381 } 382 383 /** 384 * @tests java.text.StringCharacterIterator.previous() 385 */ 386 @TestTargetNew( 387 level = TestLevel.COMPLETE, 388 notes = "", 389 method = "previous", 390 args = {} 391 ) 392 public void test_previous() { 393 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 394 assertEquals(CharacterIterator.DONE, fixture.previous()); 395 assertEquals('i', fixture.next()); 396 assertEquals('x', fixture.next()); 397 assertEquals('t', fixture.next()); 398 assertEquals('u', fixture.next()); 399 assertEquals('r', fixture.next()); 400 assertEquals('e', fixture.next()); 401 assertEquals(CharacterIterator.DONE, fixture.next()); 402 assertEquals(CharacterIterator.DONE, fixture.next()); 403 assertEquals(CharacterIterator.DONE, fixture.next()); 404 assertEquals(7, fixture.getIndex()); 405 assertEquals('e', fixture.previous()); 406 assertEquals(6, fixture.getIndex()); 407 assertEquals('r', fixture.previous()); 408 assertEquals(5, fixture.getIndex()); 409 assertEquals('u', fixture.previous()); 410 assertEquals(4, fixture.getIndex()); 411 assertEquals('t', fixture.previous()); 412 assertEquals(3, fixture.getIndex()); 413 assertEquals('x', fixture.previous()); 414 assertEquals(2, fixture.getIndex()); 415 assertEquals('i', fixture.previous()); 416 assertEquals(1, fixture.getIndex()); 417 assertEquals('f', fixture.previous()); 418 assertEquals(0, fixture.getIndex()); 419 assertEquals(CharacterIterator.DONE, fixture.previous()); 420 assertEquals(0, fixture.getIndex()); 421 422 StringCharacterIterator it1 = new StringCharacterIterator("testing", 2, 423 6, 4); 424 assertEquals("Wrong previous char1", 't', it1.previous()); 425 assertEquals("Wrong previous char2", 's', it1.previous()); 426 assertTrue("Wrong previous char3", 427 it1.previous() == CharacterIterator.DONE); 428 assertTrue("Wrong previous char4", 429 it1.previous() == CharacterIterator.DONE); 430 assertEquals("Wrong index", 2, it1.getIndex()); 431 assertEquals("Wrong current char", 's', it1.current()); 432 } 433 434 /** 435 * @tests java.text.StringCharacterIterator.setIndex(int) 436 */ 437 @TestTargetNew( 438 level = TestLevel.COMPLETE, 439 notes = "", 440 method = "setIndex", 441 args = {int.class} 442 ) 443 public void test_setIndex() { 444 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 445 while (fixture.next() != CharacterIterator.DONE) { 446 // empty 447 } 448 assertEquals("fixture".length(), fixture.getIndex()); 449 fixture.setIndex(0); 450 assertEquals(0, fixture.getIndex()); 451 assertEquals('f', fixture.current()); 452 fixture.setIndex("fixture".length() - 1); 453 assertEquals('e', fixture.current()); 454 try { 455 fixture.setIndex(-1); 456 fail("no illegal argument"); 457 } catch (IllegalArgumentException e) { 458 // expected 459 } 460 461 try { 462 fixture.setIndex("fixture".length() + 1); 463 fail("no illegal argument"); 464 } catch (IllegalArgumentException e) { 465 // expected 466 } 467 } 468 469 /** 470 * @tests java.text.StringCharacterIterator.setText(String) 471 */ 472 @TestTargetNew( 473 level = TestLevel.COMPLETE, 474 notes = "", 475 method = "setText", 476 args = {java.lang.String.class} 477 ) 478 public void test_setText() { 479 StringCharacterIterator fixture = new StringCharacterIterator("fixture"); 480 fixture.setText("fix"); 481 assertEquals('f', fixture.current()); 482 assertEquals('x', fixture.last()); 483 484 try { 485 fixture.setText(null); 486 fail("no null pointer"); 487 } catch (NullPointerException e) { 488 // expected 489 } 490 } 491 492 /** 493 * @tests java.text.StringCharacterIterator#StringCharacterIterator(java.lang.String) 494 */ 495 @TestTargetNew( 496 level = TestLevel.COMPLETE, 497 notes = "", 498 method = "StringCharacterIterator", 499 args = {java.lang.String.class} 500 ) 501 public void test_ConstructorLjava_lang_String() { 502 assertNotNull(new StringCharacterIterator("value")); 503 assertNotNull(new StringCharacterIterator("")); 504 try { 505 new StringCharacterIterator(null); 506 fail("Assert 0: no null pointer"); 507 } catch (NullPointerException e) { 508 // expected 509 } 510 511 StringCharacterIterator it = new StringCharacterIterator("testing"); 512 assertEquals("Wrong begin index", 0, it.getBeginIndex()); 513 assertEquals("Wrong end index", 7, it.getEndIndex()); 514 assertEquals("Wrong current index", 0, it.getIndex()); 515 assertEquals("Wrong current char", 't', it.current()); 516 assertEquals("Wrong next char", 'e', it.next()); 517 } 518 519 /** 520 * @tests java.text.StringCharacterIterator#StringCharacterIterator(java.lang.String, 521 * int) 522 */ 523 @TestTargetNew( 524 level = TestLevel.COMPLETE, 525 notes = "", 526 method = "StringCharacterIterator", 527 args = {java.lang.String.class, int.class} 528 ) 529 public void test_ConstructorLjava_lang_StringI() { 530 StringCharacterIterator it = new StringCharacterIterator("testing", 3); 531 assertEquals("Wrong begin index", 0, it.getBeginIndex()); 532 assertEquals("Wrong end index", 7, it.getEndIndex()); 533 assertEquals("Wrong current index", 3, it.getIndex()); 534 assertEquals("Wrong current char", 't', it.current()); 535 assertEquals("Wrong next char", 'i', it.next()); 536 } 537 538 /** 539 * @tests java.text.StringCharacterIterator#StringCharacterIterator(java.lang.String, 540 * int, int, int) 541 */ 542 @TestTargetNew( 543 level = TestLevel.COMPLETE, 544 notes = "", 545 method = "StringCharacterIterator", 546 args = {java.lang.String.class, int.class, int.class, int.class} 547 ) 548 public void test_ConstructorLjava_lang_StringIII() { 549 StringCharacterIterator it = new StringCharacterIterator("testing", 2, 550 6, 4); 551 assertEquals("Wrong begin index", 2, it.getBeginIndex()); 552 assertEquals("Wrong end index", 6, it.getEndIndex()); 553 assertEquals("Wrong current index", 4, it.getIndex()); 554 assertEquals("Wrong current char", 'i', it.current()); 555 assertEquals("Wrong next char", 'n', it.next()); 556 } 557 558 /** 559 * @tests java.text.StringCharacterIterator#getIndex() 560 */ 561 @TestTargetNew( 562 level = TestLevel.COMPLETE, 563 notes = "", 564 method = "getIndex", 565 args = {} 566 ) 567 public void test_getIndex() { 568 StringCharacterIterator it1 = new StringCharacterIterator("testing", 2, 569 6, 4); 570 assertEquals("Wrong index 4", 4, it1.getIndex()); 571 it1.next(); 572 assertEquals("Wrong index 5", 5, it1.getIndex()); 573 it1.last(); 574 assertEquals("Wrong index 4/2", 5, it1.getIndex()); 575 } 576 577 /** 578 * @tests java.text.StringCharacterIterator#hashCode() 579 */ 580 @TestTargetNew( 581 level = TestLevel.COMPLETE, 582 notes = "", 583 method = "hashCode", 584 args = {} 585 ) 586 public void test_hashCode() { 587 StringCharacterIterator it1 = new StringCharacterIterator("testing", 2, 588 6, 4); 589 StringCharacterIterator it2 = new StringCharacterIterator("xxstinx", 2, 590 6, 4); 591 assertTrue("Hash is equal", it1.hashCode() != it2.hashCode()); 592 StringCharacterIterator it3 = new StringCharacterIterator("testing", 2, 593 6, 2); 594 assertTrue("Hash equal1", it1.hashCode() != it3.hashCode()); 595 it3 = new StringCharacterIterator("testing", 0, 6, 4); 596 assertTrue("Hash equal2", it1.hashCode() != it3.hashCode()); 597 it3 = new StringCharacterIterator("testing", 2, 5, 4); 598 assertTrue("Hash equal3", it1.hashCode() != it3.hashCode()); 599 it3 = new StringCharacterIterator("froging", 2, 6, 4); 600 assertTrue("Hash equal4", it1.hashCode() != it3.hashCode()); 601 602 StringCharacterIterator sci0 = new StringCharacterIterator("fixture"); 603 assertEquals(sci0.hashCode(), sci0.hashCode()); 604 605 StringCharacterIterator sci1 = new StringCharacterIterator("fixture"); 606 assertEquals(sci0.hashCode(), sci1.hashCode()); 607 608 sci1.next(); 609 sci0.next(); 610 assertEquals(sci0.hashCode(), sci1.hashCode()); 611 } 612 613 /** 614 * @tests java.text.StringCharacterIterator#last() 615 */ 616 @TestTargetNew( 617 level = TestLevel.COMPLETE, 618 notes = "", 619 method = "last", 620 args = {} 621 ) 622 public void test_last() { 623 StringCharacterIterator it1 = new StringCharacterIterator("testing", 2, 624 6, 3); 625 assertEquals("Wrong last char", 'n', it1.last()); 626 assertEquals("Wrong previous char", 'i', it1.previous()); 627 it1 = new StringCharacterIterator("testing", 2, 2, 2); 628 assertTrue("Not DONE", it1.last() == CharacterIterator.DONE); 629 } 630 631 /** 632 * @tests java.text.StringCharacterIterator#setIndex(int) 633 */ 634 @TestTargetNew( 635 level = TestLevel.COMPLETE, 636 notes = "", 637 method = "setIndex", 638 args = {int.class} 639 ) 640 public void test_setIndexI() { 641 StringCharacterIterator it1 = new StringCharacterIterator("testing", 2, 642 6, 4); 643 assertEquals("Wrong result1", 's', it1.setIndex(2)); 644 char result = it1.next(); 645 assertTrue("Wrong next char: " + result, result == 't'); 646 assertTrue("Wrong result2", it1.setIndex(6) == CharacterIterator.DONE); 647 assertEquals("Wrong previous char", 'n', it1.previous()); 648 } 649 650 /** 651 * @tests java.text.StringCharacterIterator#setText(java.lang.String) 652 */ 653 @TestTargetNew( 654 level = TestLevel.COMPLETE, 655 notes = "", 656 method = "setText", 657 args = {java.lang.String.class} 658 ) 659 public void test_setTextLjava_lang_String() { 660 StringCharacterIterator it1 = new StringCharacterIterator("testing", 2, 661 6, 4); 662 it1.setText("frog"); 663 assertEquals("Wrong begin index", 0, it1.getBeginIndex()); 664 assertEquals("Wrong end index", 4, it1.getEndIndex()); 665 assertEquals("Wrong current index", 0, it1.getIndex()); 666 } 667} 668