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.tests.java.util; 19 20import libcore.java.util.ForEachRemainingTester; 21import libcore.java.util.SpliteratorTester; 22import tests.support.Support_ListTest; 23 24import java.util.ArrayList; 25import java.util.Arrays; 26import java.util.Collection; 27import java.util.ConcurrentModificationException; 28import java.util.Enumeration; 29import java.util.HashSet; 30import java.util.Iterator; 31import java.util.LinkedList; 32import java.util.List; 33import java.util.NoSuchElementException; 34import java.util.Spliterator; 35import java.util.Vector; 36 37import static libcore.java.util.RemoveIfTester.*; 38 39public class VectorTest extends junit.framework.TestCase { 40 41 private Vector tVector = new Vector(); 42 43 Object[] objArray; 44 45 private String vString = "[Test 0, Test 1, Test 2, Test 3, Test 4, Test 5, Test 6, Test 7, Test 8, Test 9, Test 10, Test 11, Test 12, Test 13, Test 14, Test 15, Test 16, Test 17, Test 18, Test 19, Test 20, Test 21, Test 22, Test 23, Test 24, Test 25, Test 26, Test 27, Test 28, Test 29, Test 30, Test 31, Test 32, Test 33, Test 34, Test 35, Test 36, Test 37, Test 38, Test 39, Test 40, Test 41, Test 42, Test 43, Test 44, Test 45, Test 46, Test 47, Test 48, Test 49, Test 50, Test 51, Test 52, Test 53, Test 54, Test 55, Test 56, Test 57, Test 58, Test 59, Test 60, Test 61, Test 62, Test 63, Test 64, Test 65, Test 66, Test 67, Test 68, Test 69, Test 70, Test 71, Test 72, Test 73, Test 74, Test 75, Test 76, Test 77, Test 78, Test 79, Test 80, Test 81, Test 82, Test 83, Test 84, Test 85, Test 86, Test 87, Test 88, Test 89, Test 90, Test 91, Test 92, Test 93, Test 94, Test 95, Test 96, Test 97, Test 98, Test 99]"; 46 47 /** 48 * java.util.Vector#Vector() 49 */ 50 public void test_Constructor() { 51 // Test for method java.util.Vector() 52 53 Vector tv = new Vector(100); 54 for (int i = 0; i < 100; i++) 55 tv.addElement(new Integer(i)); 56 new Support_ListTest("", tv).runTest(); 57 58 tv = new Vector(200); 59 for (int i = -50; i < 150; i++) 60 tv.addElement(new Integer(i)); 61 new Support_ListTest("", tv.subList(50, 150)).runTest(); 62 63 Vector v = new Vector(); 64 assertEquals("Vector creation failed", 0, v.size()); 65 assertEquals("Wrong capacity", 10, v.capacity()); 66 } 67 68 /** 69 * java.util.Vector#Vector(int) 70 */ 71 public void test_ConstructorI() { 72 // Test for method java.util.Vector(int) 73 74 Vector v = new Vector(100); 75 assertEquals("Vector creation failed", 0, v.size()); 76 assertEquals("Wrong capacity", 100, v.capacity()); 77 78 try { 79 new Vector(-1); 80 fail("IllegalArgumentException expected"); 81 } catch (IllegalArgumentException e) { 82 //expected 83 } 84 } 85 86 /** 87 * java.util.Vector#Vector(int, int) 88 */ 89 public void test_ConstructorII() { 90 // Test for method java.util.Vector(int, int) 91 92 Vector v = new Vector(2, 10); 93 v.addElement(new Object()); 94 v.addElement(new Object()); 95 v.addElement(new Object()); 96 97 assertEquals("Failed to inc capacity by proper amount", 98 12, v.capacity()); 99 100 Vector grow = new Vector(3, -1); 101 grow.addElement("one"); 102 grow.addElement("two"); 103 grow.addElement("three"); 104 grow.addElement("four"); 105 assertEquals("Wrong size", 4, grow.size()); 106 assertEquals("Wrong capacity", 6, grow.capacity()); 107 108 Vector emptyVector = new Vector(0, 0); 109 emptyVector.addElement("one"); 110 assertEquals("Wrong size", 1, emptyVector.size()); 111 emptyVector.addElement("two"); 112 emptyVector.addElement("three"); 113 assertEquals("Wrong size", 3, emptyVector.size()); 114 115 try { 116 Vector negativeVector = new Vector(-1, 0); 117 fail("Should throw IllegalArgumentException"); 118 } catch (IllegalArgumentException e) { 119 // Excepted 120 } 121 } 122 123 /** 124 * java.util.Vector#Vector(java.util.Collection) 125 */ 126 public void test_ConstructorLjava_util_Collection() { 127 // Test for method java.util.Vector(java.util.Collection) 128 Collection l = new LinkedList(); 129 for (int i = 0; i < 100; i++) 130 l.add("Test " + i); 131 Vector myVector = new Vector(l); 132 assertTrue("Vector is not correct size", 133 myVector.size() == objArray.length); 134 for (int counter = 0; counter < objArray.length; counter++) 135 assertTrue("Vector does not contain correct elements", myVector 136 .contains(((List) l).get(counter))); 137 138 try { 139 new Vector(null); 140 fail("NullPointerException expected"); 141 } catch (NullPointerException e) { 142 //expected 143 } 144 } 145 146 /** 147 * java.util.Vector#add(int, java.lang.Object) 148 */ 149 public void test_addILjava_lang_Object() { 150 // Test for method void java.util.Vector.add(int, java.lang.Object) 151 Object o = new Object(); 152 Object prev = tVector.get(45); 153 tVector.add(45, o); 154 assertTrue("Failed to add Object", tVector.get(45) == o); 155 assertTrue("Failed to fix-up existing indices", tVector.get(46) == prev); 156 assertEquals("Wrong size after add", 101, tVector.size()); 157 158 prev = tVector.get(50); 159 tVector.add(50, null); 160 assertNull("Failed to add null", tVector.get(50)); 161 assertTrue("Failed to fix-up existing indices after adding null", 162 tVector.get(51) == prev); 163 assertEquals("Wrong size after add", 102, tVector.size()); 164 165 try { 166 tVector.add(-5, null); 167 fail("ArrayIndexOutOfBoundsException expected"); 168 } catch(ArrayIndexOutOfBoundsException e) { 169 //expected 170 } 171 172 try { 173 tVector.add(tVector.size() + 1, null); 174 fail("ArrayIndexOutOfBoundsException expected"); 175 } catch(ArrayIndexOutOfBoundsException e) { 176 //expected 177 } 178 } 179 180 /** 181 * java.util.Vector#add(java.lang.Object) 182 */ 183 public void test_addLjava_lang_Object() { 184 // Test for method boolean java.util.Vector.add(java.lang.Object) 185 Object o = new Object(); 186 tVector.add(o); 187 assertTrue("Failed to add Object", tVector.lastElement() == o); 188 assertEquals("Wrong size after add", 101, tVector.size()); 189 190 tVector.add(null); 191 assertNull("Failed to add null", tVector.lastElement()); 192 assertEquals("Wrong size after add", 102, tVector.size()); 193 } 194 195 /** 196 * java.util.Vector#addAll(int, java.util.Collection) 197 */ 198 public void test_addAllILjava_util_Collection() { 199 // Test for method boolean java.util.Vector.addAll(int, 200 // java.util.Collection) 201 Collection l = new LinkedList(); 202 for (int i = 0; i < 100; i++) 203 l.add("Test " + i); 204 Vector v = new Vector(); 205 tVector.addAll(50, l); 206 for (int i = 50; i < 100; i++) 207 assertTrue("Failed to add all elements", 208 tVector.get(i) == ((List) l).get(i - 50)); 209 v = new Vector(); 210 v.add("one"); 211 int r = 0; 212 try { 213 v.addAll(3, Arrays.asList(new String[] { "two", "three" })); 214 } catch (ArrayIndexOutOfBoundsException e) { 215 r = 1; 216 } catch (IndexOutOfBoundsException e) { 217 r = 2; 218 } 219 assertTrue("Invalid add: " + r, r == 1); 220 l = new LinkedList(); 221 l.add(null); 222 l.add("gah"); 223 l.add(null); 224 tVector.addAll(50, l); 225 assertNull("Wrong element at position 50--wanted null", 226 tVector.get(50)); 227 assertEquals("Wrong element at position 51--wanted 'gah'", "gah", tVector 228 .get(51)); 229 assertNull("Wrong element at position 52--wanted null", 230 tVector.get(52)); 231 232 try { 233 tVector.addAll(-5, Arrays.asList(new String[] { "two", "three" })); 234 fail("ArrayIndexOutOfBoundsException expected"); 235 } catch(ArrayIndexOutOfBoundsException e) { 236 //expected 237 } 238 239 try { 240 tVector.addAll(tVector.size() + 1, Arrays.asList(new String[] { "two", "three" })); 241 fail("ArrayIndexOutOfBoundsException expected"); 242 } catch(ArrayIndexOutOfBoundsException e) { 243 //expected 244 } 245 246 try { 247 tVector.addAll(tVector.size() / 2, null); 248 fail("NullPointerException expected"); 249 } catch(NullPointerException e) { 250 //expected 251 } 252 } 253 254 /** 255 * java.util.Vector#addAll(java.util.Collection) 256 */ 257 public void test_addAllLjava_util_Collection() { 258 // Test for method boolean java.util.Vector.addAll(java.util.Collection) 259 Vector v = new Vector(); 260 Collection l = new LinkedList(); 261 for (int i = 0; i < 100; i++) 262 l.add("Test " + i); 263 v.addAll(l); 264 assertTrue("Failed to add all elements", tVector.equals(v)); 265 266 v.addAll(l); 267 int vSize = tVector.size(); 268 for (int counter = vSize - 1; counter >= 0; counter--) 269 assertTrue("Failed to add elements correctly", v.get(counter) == v 270 .get(counter + vSize)); 271 272 l = new LinkedList(); 273 l.add(null); 274 l.add("gah"); 275 l.add(null); 276 tVector.addAll(l); 277 assertNull("Wrong element at 3rd last position--wanted null", tVector 278 .get(vSize)); 279 assertEquals("Wrong element at 2nd last position--wanted 'gah'", "gah", tVector 280 .get(vSize + 1)); 281 assertNull("Wrong element at last position--wanted null", tVector 282 .get(vSize + 2)); 283 284 try { 285 tVector.addAll(tVector.size() / 2, null); 286 fail("NullPointerException expected"); 287 } catch(NullPointerException e) { 288 //expected 289 } 290 } 291 292 /** 293 * java.util.Vector#addElement(java.lang.Object) 294 */ 295 public void test_addElementLjava_lang_Object() { 296 // Test for method void java.util.Vector.addElement(java.lang.Object) 297 Vector v = vectorClone(tVector); 298 v.addElement("Added Element"); 299 assertTrue("Failed to add element", v.contains("Added Element")); 300 assertEquals("Added Element to wrong slot", "Added Element", ((String) v.elementAt(100)) 301 ); 302 v.addElement(null); 303 assertTrue("Failed to add null", v.contains(null)); 304 assertNull("Added null to wrong slot", v.elementAt(101)); 305 } 306 307 /** 308 * java.util.Vector#addElement(java.lang.Object) 309 */ 310 public void test_addElementLjava_lang_Object_subtest0() { 311 // Test for method void java.util.Vector.addElement(java.lang.Object) 312 Vector v = vectorClone(tVector); 313 v.addElement("Added Element"); 314 assertTrue("Failed to add element", v.contains("Added Element")); 315 assertEquals("Added Element to wrong slot", "Added Element", ((String) v.elementAt(100)) 316 ); 317 v.addElement(null); 318 assertTrue("Failed to add null", v.contains(null)); 319 assertNull("Added null to wrong slot", v.elementAt(101)); 320 } 321 322 /** 323 * java.util.Vector#capacity() 324 */ 325 public void test_capacity() { 326 // Test for method int java.util.Vector.capacity() 327 328 Vector v = new Vector(9); 329 assertEquals("Incorrect capacity returned", 9, v.capacity()); 330 } 331 332 /** 333 * java.util.Vector#clear() 334 */ 335 public void test_clear() { 336 // Test for method void java.util.Vector.clear() 337 Vector orgVector = vectorClone(tVector); 338 tVector.clear(); 339 assertEquals("a) Cleared Vector has non-zero size", 0, tVector.size()); 340 Enumeration e = orgVector.elements(); 341 while (e.hasMoreElements()) 342 assertTrue("a) Cleared vector contained elements", !tVector 343 .contains(e.nextElement())); 344 345 tVector.add(null); 346 tVector.clear(); 347 assertEquals("b) Cleared Vector has non-zero size", 0, tVector.size()); 348 e = orgVector.elements(); 349 while (e.hasMoreElements()) 350 assertTrue("b) Cleared vector contained elements", !tVector 351 .contains(e.nextElement())); 352 } 353 354 /** 355 * java.util.Vector#clone() 356 */ 357 public void test_clone() { 358 // Test for method java.lang.Object java.util.Vector.clone() 359 tVector.add(25, null); 360 tVector.add(75, null); 361 Vector v = (Vector) tVector.clone(); 362 Enumeration orgNum = tVector.elements(); 363 Enumeration cnum = v.elements(); 364 365 while (orgNum.hasMoreElements()) { 366 assertTrue("Not enough elements copied", cnum.hasMoreElements()); 367 assertTrue("Vector cloned improperly, elements do not match", 368 orgNum.nextElement() == cnum.nextElement()); 369 } 370 assertTrue("Not enough elements copied", !cnum.hasMoreElements()); 371 372 } 373 374 /** 375 * java.util.Vector#contains(java.lang.Object) 376 */ 377 public void test_containsLjava_lang_Object() { 378 // Test for method boolean java.util.Vector.contains(java.lang.Object) 379 assertTrue("Did not find element", tVector.contains("Test 42")); 380 assertTrue("Found bogus element", !tVector.contains("Hello")); 381 assertTrue( 382 "Returned true looking for null in vector without null element", 383 !tVector.contains(null)); 384 tVector.insertElementAt(null, 20); 385 assertTrue( 386 "Returned false looking for null in vector with null element", 387 tVector.contains(null)); 388 } 389 390 /** 391 * java.util.Vector#containsAll(java.util.Collection) 392 */ 393 public void test_containsAllLjava_util_Collection() { 394 // Test for method boolean 395 // java.util.Vector.containsAll(java.util.Collection) 396 Collection s = new HashSet(); 397 for (int i = 0; i < 100; i++) 398 s.add("Test " + i); 399 400 assertTrue("Returned false for valid collection", tVector 401 .containsAll(s)); 402 s.add(null); 403 assertTrue("Returned true for invlaid collection containing null", 404 !tVector.containsAll(s)); 405 tVector.add(25, null); 406 assertTrue("Returned false for valid collection containing null", 407 tVector.containsAll(s)); 408 s = new HashSet(); 409 s.add(new Object()); 410 assertTrue("Returned true for invalid collection", !tVector 411 .containsAll(s)); 412 413 try { 414 tVector.containsAll(null); 415 fail("NullPointerException expected"); 416 } catch (NullPointerException e) { 417 //expected 418 } 419 } 420 421 /** 422 * java.util.Vector#copyInto(java.lang.Object[]) 423 */ 424 public void test_copyInto$Ljava_lang_Object() { 425 // Test for method void java.util.Vector.copyInto(java.lang.Object []) 426 427 Object[] a = new Object[100]; 428 tVector.setElementAt(null, 20); 429 tVector.copyInto(a); 430 431 for (int i = 0; i < 100; i++) 432 assertTrue("copyInto failed", a[i] == tVector.elementAt(i)); 433 434 try { 435 tVector.copyInto(null); 436 fail("NullPointerException expected"); 437 } catch (NullPointerException e) { 438 //expected 439 } 440 } 441 442 /** 443 * java.util.Vector#elementAt(int) 444 */ 445 public void test_elementAtI() { 446 // Test for method java.lang.Object java.util.Vector.elementAt(int) 447 assertEquals("Incorrect element returned", "Test 18", ((String) tVector 448 .elementAt(18))); 449 tVector.setElementAt(null, 20); 450 assertNull("Incorrect element returned--wanted null", tVector 451 .elementAt(20)); 452 453 try { 454 tVector.elementAt(-5); 455 fail("ArrayIndexOutOfBoundsException expected"); 456 } catch(ArrayIndexOutOfBoundsException e) { 457 //expected 458 } 459 460 try { 461 tVector.elementAt(tVector.size() + 1); 462 fail("ArrayIndexOutOfBoundsException expected"); 463 } catch(ArrayIndexOutOfBoundsException e) { 464 //expected 465 } 466 } 467 468 /** 469 * java.util.Vector#elements() 470 */ 471 public void test_elements() { 472 // Test for method java.util.Enumeration java.util.Vector.elements() 473 tVector.insertElementAt(null, 20); 474 Enumeration e = tVector.elements(); 475 int i = 0; 476 while (e.hasMoreElements()) { 477 assertTrue("Enumeration returned incorrect element at pos: " + i, e 478 .nextElement() == tVector.elementAt(i)); 479 i++; 480 } 481 assertTrue("Invalid enumeration", i == tVector.size()); 482 } 483 484 /** 485 * java.util.Vector#elements() 486 */ 487 public void test_elements_subtest0() { 488 final int iterations = 10000; 489 final Vector v = new Vector(); 490 Thread t1 = new Thread() { 491 public void run() { 492 for (int i = 0; i < iterations; i++) { 493 synchronized (v) { 494 v.addElement(String.valueOf(i)); 495 v.removeElementAt(0); 496 } 497 } 498 } 499 }; 500 t1.start(); 501 for (int i = 0; i < iterations; i++) { 502 Enumeration en = v.elements(); 503 try { 504 while (true) { 505 Object result = en.nextElement(); 506 if (result == null) { 507 fail("Null result: " + i); 508 } 509 } 510 } catch (NoSuchElementException e) { 511 } 512 } 513 } 514 515 /** 516 * java.util.Vector#ensureCapacity(int) 517 */ 518 public void test_ensureCapacityI() { 519 // Test for method void java.util.Vector.ensureCapacity(int) 520 521 Vector v = new Vector(9); 522 v.ensureCapacity(20); 523 assertEquals("ensureCapacity failed to set correct capacity", 20, v 524 .capacity()); 525 v = new Vector(100); 526 assertEquals("ensureCapacity reduced capacity", 100, v.capacity()); 527 528 v.ensureCapacity(150); 529 assertEquals( 530 "ensuieCapacity failed to set to be twice the old capacity", 531 200, v.capacity()); 532 533 v = new Vector(9, -1); 534 v.ensureCapacity(20); 535 assertEquals("ensureCapacity failed to set to be minCapacity", 20, v 536 .capacity()); 537 v.ensureCapacity(15); 538 assertEquals("ensureCapacity reduced capacity", 20, v.capacity()); 539 v.ensureCapacity(35); 540 assertEquals( 541 "ensuieCapacity failed to set to be twice the old capacity", 542 40, v.capacity()); 543 544 v = new Vector(9, 4); 545 v.ensureCapacity(11); 546 assertEquals("ensureCapacity failed to set correct capacity", 13, v 547 .capacity()); 548 v.ensureCapacity(5); 549 assertEquals("ensureCapacity reduced capacity", 13, v.capacity()); 550 v.ensureCapacity(20); 551 assertEquals( 552 "ensureCapacity failed to set to be twice the old capacity", 553 20, v.capacity()); 554 } 555 556 /** 557 * java.util.Vector#equals(java.lang.Object) 558 */ 559 public void test_equalsLjava_lang_Object() { 560 // Test for method boolean java.util.Vector.equals(java.lang.Object) 561 Vector v = new Vector(); 562 for (int i = 0; i < 100; i++) 563 v.addElement("Test " + i); 564 assertTrue("a) Equal vectors returned false", tVector.equals(v)); 565 v.addElement(null); 566 assertTrue("b) UnEqual vectors returned true", !tVector.equals(v)); 567 tVector.addElement(null); 568 assertTrue("c) Equal vectors returned false", tVector.equals(v)); 569 tVector.removeElementAt(22); 570 assertTrue("d) UnEqual vectors returned true", !tVector.equals(v)); 571 assertTrue("e) Equal vectors returned false", tVector.equals(tVector)); 572 assertFalse("f) UnEqual vectors returned true", tVector 573 .equals(new Object())); 574 assertFalse("g) Unequal vectors returned true", tVector.equals(null)); 575 } 576 577 /** 578 * java.util.Vector#firstElement() 579 */ 580 public void test_firstElement() { 581 // Test for method java.lang.Object java.util.Vector.firstElement() 582 assertEquals("Returned incorrect firstElement", "Test 0", tVector.firstElement() 583 ); 584 tVector.insertElementAt(null, 0); 585 assertNull("Returned incorrect firstElement--wanted null", tVector 586 .firstElement()); 587 588 Vector v = new Vector(10); 589 try { 590 v.firstElement(); 591 fail("Should throw NoSuchElementException"); 592 } catch (NoSuchElementException e) { 593 // Excepted 594 } 595 } 596 597 /** 598 * java.util.Vector#get(int) 599 */ 600 public void test_getI() { 601 // Test for method java.lang.Object java.util.Vector.get(int) 602 assertEquals("Get returned incorrect object", 603 "Test 80", tVector.get(80)); 604 tVector.add(25, null); 605 assertNull("Returned incorrect element--wanted null", 606 tVector.get(25)); 607 608 try { 609 tVector.get(-5); 610 fail("ArrayIndexOutOfBoundsException expected"); 611 } catch(ArrayIndexOutOfBoundsException e) { 612 //expected 613 } 614 615 try { 616 tVector.get(tVector.size() + 1); 617 fail("ArrayIndexOutOfBoundsException expected"); 618 } catch(ArrayIndexOutOfBoundsException e) { 619 //expected 620 } 621 } 622 623 /** 624 * java.util.Vector#hashCode() 625 */ 626 public void test_hashCode() { 627 // Test for method int java.util.Vector.hashCode() 628 int hashCode = 1; // one 629 tVector.insertElementAt(null, 20); 630 for (int i = 0; i < tVector.size(); i++) { 631 Object obj = tVector.elementAt(i); 632 hashCode = 31 * hashCode + (obj == null ? 0 : obj.hashCode()); 633 } 634 assertTrue("Incorrect hashCode returned. Wanted: " + hashCode 635 + " got: " + tVector.hashCode(), tVector.hashCode() == hashCode); 636 } 637 638 /** 639 * java.util.Vector#indexOf(java.lang.Object) 640 */ 641 public void test_indexOfLjava_lang_Object() { 642 // Test for method int java.util.Vector.indexOf(java.lang.Object) 643 assertEquals("Incorrect index returned", 10, tVector.indexOf("Test 10")); 644 assertEquals("Index returned for invalid Object", -1, tVector 645 .indexOf("XXXXXXXXXXX")); 646 tVector.setElementAt(null, 20); 647 tVector.setElementAt(null, 40); 648 assertTrue("Incorrect indexOf returned for null: " 649 + tVector.indexOf(null), tVector.indexOf(null) == 20); 650 } 651 652 /** 653 * java.util.Vector#indexOf(java.lang.Object, int) 654 */ 655 public void test_indexOfLjava_lang_ObjectI() { 656 // Test for method int java.util.Vector.indexOf(java.lang.Object, int) 657 assertEquals("Failed to find correct index", tVector.indexOf("Test 98", 658 50), 98); 659 assertTrue("Found index of bogus element", (tVector.indexOf( 660 "Test 1001", 50) == -1)); 661 tVector.setElementAt(null, 20); 662 tVector.setElementAt(null, 40); 663 tVector.setElementAt(null, 60); 664 assertTrue("a) Incorrect indexOf returned for null: " 665 + tVector.indexOf(null, 25), tVector.indexOf(null, 25) == 40); 666 assertTrue("b) Incorrect indexOf returned for null: " 667 + tVector.indexOf(null, 20), tVector.indexOf(null, 20) == 20); 668 try { 669 tVector.indexOf("Test 98", -1); 670 fail("should throw ArrayIndexOutOfBoundsException"); 671 } catch (ArrayIndexOutOfBoundsException e) { 672 673 } 674 assertEquals(-1, tVector.indexOf("Test 98", 1000)); 675 assertEquals(-1, tVector.indexOf("Test 98", Integer.MAX_VALUE)); 676 assertEquals(-1, tVector.indexOf("Test 98", tVector.size())); 677 assertEquals(98, tVector.indexOf("Test 98", 0)); 678 try { 679 tVector.indexOf("Test 98", Integer.MIN_VALUE); 680 fail("should throw ArrayIndexOutOfBoundsException"); 681 } catch (ArrayIndexOutOfBoundsException e) { 682 683 } 684 } 685 686 /** 687 * java.util.Vector#insertElementAt(java.lang.Object, int) 688 */ 689 public void test_insertElementAtLjava_lang_ObjectI() { 690 // Test for method void 691 // java.util.Vector.insertElementAt(java.lang.Object, int) 692 Vector v = vectorClone(tVector); 693 String prevElement = (String) v.elementAt(99); 694 v.insertElementAt("Inserted Element", 99); 695 assertEquals("Element not inserted", "Inserted Element", ((String) v.elementAt(99)) 696 ); 697 assertTrue("Elements shifted incorrectly", ((String) v.elementAt(100)) 698 .equals(prevElement)); 699 v.insertElementAt(null, 20); 700 assertNull("null not inserted", v.elementAt(20)); 701 702 try { 703 tVector.insertElementAt("Inserted Element", -1); 704 fail("Should throw ArrayIndexOutOfBoundsException"); 705 } catch (ArrayIndexOutOfBoundsException e) { 706 // Excepted 707 } 708 709 try { 710 tVector.insertElementAt(null, -1); 711 fail("Should throw ArrayIndexOutOfBoundsException"); 712 } catch (ArrayIndexOutOfBoundsException e) { 713 // Excepted 714 } 715 716 try { 717 tVector.insertElementAt("Inserted Element", tVector.size() + 1); 718 fail("Should throw ArrayIndexOutOfBoundsException"); 719 } catch (ArrayIndexOutOfBoundsException e) { 720 // Excepted 721 } 722 723 try { 724 tVector.insertElementAt(null, tVector.size() + 1); 725 fail("Should throw ArrayIndexOutOfBoundsException"); 726 } catch (ArrayIndexOutOfBoundsException e) { 727 // Excepted 728 } 729 } 730 731 /** 732 * java.util.Vector#isEmpty() 733 */ 734 public void test_isEmpty() { 735 // Test for method boolean java.util.Vector.isEmpty()Vector 736 Vector v = new java.util.Vector(); 737 assertTrue("Empty vector returned false", v.isEmpty()); 738 v.addElement(new Object()); 739 assertTrue("non-Empty vector returned true", !v.isEmpty()); 740 } 741 742 /** 743 * java.util.Vector#isEmpty() 744 */ 745 public void test_isEmpty_subtest0() { 746 final Vector v = new Vector(); 747 v.addElement("initial"); 748 Thread t1 = new Thread() { 749 public void run() { 750 while (!v.isEmpty()) 751 ; 752 v.addElement("final"); 753 } 754 }; 755 t1.start(); 756 for (int i = 0; i < 10000; i++) { 757 synchronized (v) { 758 v.removeElementAt(0); 759 v.addElement(String.valueOf(i)); 760 } 761 int size; 762 if ((size = v.size()) != 1) { 763 String result = "Size is not 1: " + size + " " + v; 764 // terminate the thread 765 v.removeAllElements(); 766 fail(result); 767 } 768 } 769 // terminate the thread 770 v.removeElementAt(0); 771 } 772 773 /** 774 * java.util.Vector#lastElement() 775 */ 776 public void test_lastElement() { 777 // Test for method java.lang.Object java.util.Vector.lastElement() 778 assertEquals("Incorrect last element returned", "Test 99", tVector.lastElement() 779 ); 780 tVector.addElement(null); 781 assertNull("Incorrect last element returned--wanted null", tVector 782 .lastElement()); 783 784 Vector vector = new Vector(); 785 try { 786 vector.lastElement(); 787 fail("Should throw NoSuchElementException"); 788 } catch (NoSuchElementException e) { 789 // Excepted 790 } 791 } 792 793 /** 794 * java.util.Vector#lastIndexOf(java.lang.Object) 795 */ 796 public void test_lastIndexOfLjava_lang_Object() { 797 // Test for method int java.util.Vector.lastIndexOf(java.lang.Object) 798 Vector v = new Vector(9); 799 for (int i = 0; i < 9; i++) 800 v.addElement("Test"); 801 v.addElement("z"); 802 assertEquals("Failed to return correct index", 8, v.lastIndexOf("Test")); 803 tVector.setElementAt(null, 20); 804 tVector.setElementAt(null, 40); 805 assertTrue("Incorrect lastIndexOf returned for null: " 806 + tVector.lastIndexOf(null), tVector.lastIndexOf(null) == 40); 807 } 808 809 /** 810 * java.util.Vector#lastIndexOf(java.lang.Object, int) 811 */ 812 public void test_lastIndexOfLjava_lang_ObjectI() { 813 // Test for method int java.util.Vector.lastIndexOf(java.lang.Object, 814 // int) 815 assertEquals("Failed to find object", 816 0, tVector.lastIndexOf("Test 0", 0)); 817 assertTrue("Found Object outside of index", (tVector.lastIndexOf( 818 "Test 0", 10) > -1)); 819 tVector.setElementAt(null, 20); 820 tVector.setElementAt(null, 40); 821 tVector.setElementAt(null, 60); 822 assertTrue("Incorrect lastIndexOf returned for null: " 823 + tVector.lastIndexOf(null, 15), 824 tVector.lastIndexOf(null, 15) == -1); 825 assertTrue("Incorrect lastIndexOf returned for null: " 826 + tVector.lastIndexOf(null, 45), 827 tVector.lastIndexOf(null, 45) == 40); 828 829 assertEquals(-1, tVector.lastIndexOf("Test 98", -1)); 830 assertEquals(-1, tVector.lastIndexOf("Test 98", 0)); 831 try { 832 assertEquals(-1, tVector.lastIndexOf("Test 98", 1000)); 833 fail("should throw IndexOutOfBoundsException"); 834 } catch (IndexOutOfBoundsException e) { 835 } 836 try { 837 assertEquals(-1, tVector.lastIndexOf("Test 98", Integer.MAX_VALUE)); 838 fail("should throw IndexOutOfBoundsException"); 839 } catch (IndexOutOfBoundsException e) { 840 } 841 try { 842 tVector.lastIndexOf("Test 98", tVector.size()); 843 fail("should throw IndexOutOfBoundsException"); 844 } catch (IndexOutOfBoundsException e) { 845 } 846 try { 847 tVector.indexOf("Test 98", Integer.MIN_VALUE); 848 fail("should throw ArrayIndexOutOfBoundsException"); 849 } catch (ArrayIndexOutOfBoundsException e) { 850 } 851 } 852 853 /** 854 * java.util.Vector#remove(int) 855 */ 856 public void test_removeI() { 857 // Test for method java.lang.Object java.util.Vector.remove(int) 858 Object removeElement = tVector.get(36); 859 Object result = tVector.remove(36); 860 assertFalse("Contained element after remove", tVector 861 .contains("Test 36")); 862 assertEquals("Should return the element that was removed", 863 removeElement, result); 864 assertEquals("Failed to decrement size after remove", 865 99, tVector.size()); 866 tVector.add(20, null); 867 removeElement = tVector.get(19); 868 result = tVector.remove(19); 869 assertNull("Didn't move null element over", tVector.get(19)); 870 assertEquals("Should return the element that was removed", 871 removeElement, result); 872 removeElement = tVector.get(19); 873 result = tVector.remove(19); 874 assertNotNull("Didn't remove null element", tVector.get(19)); 875 assertEquals("Should return the element that was removed", 876 removeElement, result); 877 assertEquals("Failed to decrement size after removing null", 98, tVector 878 .size()); 879 880 try { 881 tVector.remove(-1); 882 fail("Should throw ArrayIndexOutOfBoundsException"); 883 } catch (ArrayIndexOutOfBoundsException e) { 884 // Excepted 885 } 886 887 try { 888 tVector.remove(tVector.size()); 889 fail("Should throw ArrayIndexOutOfBoundsException"); 890 } catch (ArrayIndexOutOfBoundsException e) { 891 // Excepted 892 } 893 } 894 895 /** 896 * java.util.Vector#remove(java.lang.Object) 897 */ 898 public void test_removeLjava_lang_Object() { 899 // Test for method boolean java.util.Vector.remove(java.lang.Object) 900 tVector.remove("Test 0"); 901 assertTrue("Contained element after remove", !tVector 902 .contains("Test 0")); 903 assertEquals("Failed to decrement size after remove", 904 99, tVector.size()); 905 tVector.add(null); 906 tVector.remove(null); 907 assertTrue("Contained null after remove", !tVector.contains(null)); 908 assertEquals("Failed to decrement size after removing null", 99, tVector 909 .size()); 910 } 911 912 /** 913 * java.util.Vector#removeAll(java.util.Collection) 914 */ 915 public void test_removeAllLjava_util_Collection() { 916 // Test for method boolean 917 // java.util.Vector.removeAll(java.util.Collection) 918 Vector v = new Vector(); 919 Collection l = new LinkedList(); 920 for (int i = 0; i < 5; i++) 921 l.add("Test " + i); 922 v.addElement(l); 923 924 Collection s = new HashSet(); 925 Object o; 926 s.add(o = v.firstElement()); 927 v.removeAll(s); 928 assertTrue("Failed to remove items in collection", !v.contains(o)); 929 v.removeAll(l); 930 assertTrue("Failed to remove all elements", v.isEmpty()); 931 932 v.add(null); 933 v.add(null); 934 v.add("Boom"); 935 v.removeAll(s); 936 assertEquals("Should not have removed any elements", 3, v.size()); 937 l = new LinkedList(); 938 l.add(null); 939 v.removeAll(l); 940 assertEquals("Should only have one element", 1, v.size()); 941 assertEquals("Element should be 'Boom'", "Boom", v.firstElement()); 942 943 try { 944 v.removeAll(null); 945 fail("NullPointerException expected"); 946 } catch (NullPointerException e) { 947 //expected 948 } 949 } 950 951 /** 952 * java.util.Vector#removeAllElements() 953 */ 954 public void test_removeAllElements() { 955 // Test for method void java.util.Vector.removeAllElements() 956 Vector v = vectorClone(tVector); 957 v.removeAllElements(); 958 assertEquals("Failed to remove all elements", 0, v.size()); 959 } 960 961 /** 962 * java.util.Vector#removeElement(java.lang.Object) 963 */ 964 public void test_removeElementLjava_lang_Object() { 965 // Test for method boolean 966 // java.util.Vector.removeElement(java.lang.Object) 967 Vector v = vectorClone(tVector); 968 v.removeElement("Test 98"); 969 assertEquals("Element not removed", "Test 99", ((String) v.elementAt(98)) 970 ); 971 assertTrue("Vector is wrong size after removal: " + v.size(), 972 v.size() == 99); 973 tVector.addElement(null); 974 v.removeElement(null); 975 assertTrue("Vector is wrong size after removing null: " + v.size(), v 976 .size() == 99); 977 } 978 979 /** 980 * java.util.Vector#removeElementAt(int) 981 */ 982 public void test_removeElementAtI() { 983 // Test for method void java.util.Vector.removeElementAt(int) 984 Vector v = vectorClone(tVector); 985 int size = v.size(); 986 v.removeElementAt(50); 987 assertEquals("Failed to remove element", -1, v.indexOf("Test 50", 0)); 988 assertEquals("Test 51", v.get(50)); 989 assertEquals(size - 1, v.size()); 990 991 tVector.insertElementAt(null, 60); 992 assertNull(tVector.get(60)); 993 size = tVector.size(); 994 tVector.removeElementAt(60); 995 assertNotNull("Element at 60 should not be null after removal", tVector 996 .elementAt(60)); 997 assertEquals(size - 1, tVector.size()); 998 999 try { 1000 tVector.removeElementAt(-1); 1001 fail("Should throw ArrayIndexOutOfBoundsException"); 1002 } catch (ArrayIndexOutOfBoundsException e) { 1003 // Excepted 1004 } 1005 1006 try { 1007 tVector.removeElementAt(tVector.size()); 1008 fail("Should throw ArrayIndexOutOfBoundsException"); 1009 } catch (ArrayIndexOutOfBoundsException e) { 1010 // Excepted 1011 } 1012 } 1013 1014 /** 1015 * {@link java.util.Vector#removeRange(int, int)} 1016 */ 1017 public void test_removeRange() { 1018 MockVector myVector = new MockVector(); 1019 myVector.removeRange(0, 0); 1020 1021 try { 1022 myVector.removeRange(0, 1); 1023 fail("Should throw IndexOutOfBoundsException"); 1024 } catch (IndexOutOfBoundsException e) { 1025 // Excepted 1026 } 1027 1028 int[] data = { 1, 2, 3, 4 }; 1029 for (int i = 0; i < data.length; i++) { 1030 myVector.add(i, data[i]); 1031 } 1032 1033 myVector.removeRange(0, 2); 1034 assertEquals(data[2], myVector.get(0)); 1035 assertEquals(data[3], myVector.get(1)); 1036 1037 try { 1038 myVector.removeRange(-1, 1); 1039 fail("Should throw IndexOutOfBoundsException"); 1040 } catch (IndexOutOfBoundsException e) { 1041 // Excepted 1042 } 1043 1044 try { 1045 myVector.removeRange(0, -1); 1046 fail("Should throw IndexOutOfBoundsException"); 1047 } catch (IndexOutOfBoundsException e) { 1048 // Excepted 1049 } 1050 1051 try { 1052 myVector.removeRange(1, 0); 1053 fail("Should throw IndexOutOfBoundsException"); 1054 } catch (IndexOutOfBoundsException e) { 1055 // Excepted 1056 } 1057 1058 try { 1059 myVector.removeRange(2, 1); 1060 fail("Should throw IndexOutOfBoundsException"); 1061 } catch (IndexOutOfBoundsException e) { 1062 // Excepted 1063 } 1064 } 1065 1066 /** 1067 * java.util.Vector#retainAll(java.util.Collection) 1068 */ 1069 public void test_retainAllLjava_util_Collection() { 1070 // Test for method boolean 1071 // java.util.Vector.retainAll(java.util.Collection) 1072 Object o = tVector.firstElement(); 1073 tVector.add(null); 1074 Collection s = new HashSet(); 1075 s.add(o); 1076 s.add(null); 1077 tVector.retainAll(s); 1078 assertTrue("Retained items other than specified", tVector.size() == 2 1079 && tVector.contains(o) && tVector.contains(null)); 1080 } 1081 1082 /** 1083 * java.util.Vector#set(int, java.lang.Object) 1084 */ 1085 public void test_setILjava_lang_Object() { 1086 // Test for method java.lang.Object java.util.Vector.set(int, 1087 // java.lang.Object) 1088 Object o = new Object(); 1089 Object previous = tVector.get(23); 1090 Object result = tVector.set(23, o); 1091 assertEquals( 1092 "Should return the element previously at the specified position", 1093 previous, result); 1094 assertTrue("Failed to set Object", tVector.get(23) == o); 1095 1096 previous = tVector.get(0); 1097 result = tVector.set(0, null); 1098 assertEquals( 1099 "Should return the element previously at the specified position", 1100 previous, result); 1101 assertNull("Failed to set Object", tVector.get(0)); 1102 1103 try { 1104 tVector.set(-1, o); 1105 fail("Should throw ArrayIndexOutOfBoundsException"); 1106 } catch (ArrayIndexOutOfBoundsException e) { 1107 // Excepted 1108 } 1109 1110 try { 1111 tVector.set(-1, null); 1112 fail("Should throw ArrayIndexOutOfBoundsException"); 1113 } catch (ArrayIndexOutOfBoundsException e) { 1114 // Excepted 1115 } 1116 1117 try { 1118 tVector.set(tVector.size(), o); 1119 fail("Should throw ArrayIndexOutOfBoundsException"); 1120 } catch (ArrayIndexOutOfBoundsException e) { 1121 // Excepted 1122 } 1123 1124 try { 1125 tVector.set(tVector.size(), null); 1126 fail("Should throw ArrayIndexOutOfBoundsException"); 1127 } catch (ArrayIndexOutOfBoundsException e) { 1128 // Excepted 1129 } 1130 } 1131 1132 /** 1133 * java.util.Vector#setElementAt(java.lang.Object, int) 1134 */ 1135 public void test_setElementAtLjava_lang_ObjectI() { 1136 // Test for method void java.util.Vector.setElementAt(java.lang.Object, 1137 // int) 1138 Vector v = vectorClone(tVector); 1139 v.setElementAt("Inserted Element", 99); 1140 assertEquals("Element not set", "Inserted Element", ((String) v.elementAt(99)) 1141 ); 1142 1143 v.setElementAt(null, 0); 1144 assertNull("Null element not set", v.elementAt(0)); 1145 1146 try { 1147 v.setElementAt("Inserted Element", -1); 1148 fail("Should throw ArrayIndexOutOfBoundsException"); 1149 } catch (ArrayIndexOutOfBoundsException e) { 1150 // Excepted 1151 } 1152 1153 try { 1154 v.setElementAt(null, -1); 1155 fail("Should throw ArrayIndexOutOfBoundsException"); 1156 } catch (ArrayIndexOutOfBoundsException e) { 1157 // Excepted 1158 } 1159 1160 try { 1161 v.setElementAt("Inserted Element", v.size()); 1162 fail("Should throw ArrayIndexOutOfBoundsException"); 1163 } catch (ArrayIndexOutOfBoundsException e) { 1164 // Excepted 1165 } 1166 1167 try { 1168 v.setElementAt(null, v.size()); 1169 fail("Should throw ArrayIndexOutOfBoundsException"); 1170 } catch (ArrayIndexOutOfBoundsException e) { 1171 // Excepted 1172 } 1173 } 1174 1175 /** 1176 * java.util.Vector#setSize(int) 1177 */ 1178 public void test_setSizeI() { 1179 // Test for method void java.util.Vector.setSize(int) 1180 Vector v = vectorClone(tVector); 1181 int oldSize = v.size(); 1182 Object preElement = v.get(10); 1183 v.setSize(10); 1184 assertEquals("Failed to set size", 10, v.size()); 1185 assertEquals( 1186 "All components at index newSize and greater should be discarded", 1187 -1, v.indexOf(preElement)); 1188 try { 1189 v.get(oldSize - 1); 1190 } catch (ArrayIndexOutOfBoundsException e) { 1191 // Excepted; 1192 } 1193 1194 oldSize = v.size(); 1195 v.setSize(20); 1196 assertEquals("Failed to set size", 20, v.size()); 1197 for (int i = oldSize; i < v.size(); i++) { 1198 assertNull(v.get(i)); 1199 } 1200 1201 try { 1202 v.setSize(-1); 1203 fail("Should throw ArrayIndexOutOfBoundsException"); 1204 } catch (ArrayIndexOutOfBoundsException e) { 1205 // Excepted 1206 } 1207 } 1208 1209 /** 1210 * java.util.Vector#size() 1211 */ 1212 public void test_size() { 1213 // Test for method int java.util.Vector.size() 1214 assertEquals("Returned incorrect size", 100, tVector.size()); 1215 1216 final Vector v = new Vector(); 1217 v.addElement("initial"); 1218 Thread t1 = new Thread() { 1219 public void run() { 1220 while (v.size() > 0) 1221 ; 1222 v.addElement("final"); 1223 } 1224 }; 1225 t1.start(); 1226 for (int i = 0; i < 10000; i++) { 1227 synchronized (v) { 1228 v.removeElementAt(0); 1229 v.addElement(String.valueOf(i)); 1230 } 1231 int size; 1232 if ((size = v.size()) != 1) { 1233 String result = "Size is not 1: " + size + " " + v; 1234 // terminate the thread 1235 v.removeAllElements(); 1236 fail(result); 1237 } 1238 } 1239 // terminate the thread 1240 v.removeElementAt(0); 1241 } 1242 1243 /** 1244 * java.util.Vector#subList(int, int) 1245 */ 1246 public void test_subListII() { 1247 // Test for method java.util.List java.util.Vector.subList(int, int) 1248 List sl = tVector.subList(10, 25); 1249 assertEquals("Returned sublist of incorrect size", 15, sl.size()); 1250 for (int i = 10; i < 25; i++) 1251 assertTrue("Returned incorrect sublist", sl 1252 .contains(tVector.get(i))); 1253 1254 assertEquals("Not synchronized random access", "java.util.Collections$SynchronizedRandomAccessList", sl.getClass().getName() 1255 ); 1256 1257 } 1258 1259 /** 1260 * java.util.Vector#toArray() 1261 */ 1262 public void test_toArray() { 1263 // Test for method java.lang.Object [] java.util.Vector.toArray() 1264 assertTrue("Returned incorrect array", Arrays.equals(objArray, tVector 1265 .toArray())); 1266 } 1267 1268 /** 1269 * java.util.Vector#toArray(java.lang.Object[]) 1270 */ 1271 public void test_toArray$Ljava_lang_Object() { 1272 // Test for method java.lang.Object [] 1273 // java.util.Vector.toArray(java.lang.Object []) 1274 Object[] o = new Object[1000]; 1275 Object f = new Object(); 1276 for (int i = 0; i < o.length; i++) 1277 o[i] = f; 1278 tVector.toArray(o); 1279 assertNull("Failed to set slot to null", o[100]); 1280 for (int i = 0; i < tVector.size(); i++) 1281 assertTrue("Returned incorrect array", tVector.elementAt(i) == o[i]); 1282 } 1283 1284 1285 class SubVector<E> extends Vector<E> { 1286 1287 private static final long serialVersionUID = 1L; 1288 1289 public SubVector() { 1290 super(); 1291 } 1292 1293 public synchronized boolean add(E obj) { 1294 super.addElement(obj); 1295 return true; 1296 } 1297 1298 public synchronized void addElement(E obj) { 1299 super.add(obj); 1300 } 1301 1302 /** 1303 * java.util.Vector#add(Object) 1304 */ 1305 @SuppressWarnings("nls") 1306 public void test_add() { 1307 SubVector<String> subvector = new SubVector<String>(); 1308 subvector.add("foo"); 1309 subvector.addElement("bar"); 1310 assertEquals("Expected two elements in vector", 2, subvector.size()); 1311 } 1312 1313 } 1314 1315 /** 1316 * java.util.Vector#toString() 1317 */ 1318 public void test_toString() { 1319 // Ensure toString works with self-referencing elements. 1320 Vector<Object> vec = new Vector<Object>(3); 1321 vec.add(null); 1322 vec.add(new Object()); 1323 vec.add(vec); 1324 assertNotNull(vec.toString()); 1325 1326 // Test for method java.lang.String java.util.Vector.toString() 1327 assertTrue("Incorrect String returned", tVector.toString().equals( 1328 vString)); 1329 1330 Vector v = new Vector(); 1331 v.addElement("one"); 1332 v.addElement(v); 1333 v.addElement("3"); 1334 // test last element 1335 v.addElement(v); 1336 String result = v.toString(); 1337 assertTrue("should contain self ref", result.indexOf("(this") > -1); 1338 } 1339 1340 public void test_override_size() throws Exception { 1341 Vector v = new Vector(); 1342 Vector testv = new MockVector(); 1343 // though size is overriden, it should passed without exception 1344 testv.add(1); 1345 testv.add(2); 1346 testv.clear(); 1347 1348 testv.add(1); 1349 testv.add(2); 1350 v.add(1); 1351 v.add(2); 1352 // RI's bug here 1353 assertTrue(testv.equals(v)); 1354 } 1355 1356 /** 1357 * java.util.Vector#trimToSize() 1358 */ 1359 public void test_trimToSize() { 1360 // Test for method void java.util.Vector.trimToSize() 1361 Vector v = new Vector(10); 1362 v.addElement(new Object()); 1363 v.trimToSize(); 1364 assertEquals("Failed to trim capacity", 1, v.capacity()); 1365 } 1366 1367 public void test_removeRangeII() { 1368 MockVector mv = new MockVector(); 1369 mv.add("First"); 1370 mv.add("Second"); 1371 mv.add("One more"); 1372 mv.add("Last"); 1373 mv.removeRange(1, 3); 1374 assertTrue(mv.contains("First")); 1375 assertFalse(mv.contains("Second")); 1376 assertFalse(mv.contains("One more")); 1377 assertTrue(mv.contains("Last")); 1378 } 1379 1380 protected Vector vectorClone(Vector s) { 1381 return (Vector) s.clone(); 1382 } 1383 1384 public class MockVector extends Vector { 1385 @Override 1386 public synchronized int size() { 1387 return 0; 1388 } 1389 1390 public void removeRange(int start, int end) { 1391 super.removeRange(start, end); 1392 } 1393 } 1394 1395 public void test_forEach() throws Exception { 1396 Vector<Integer> vector = new Vector<Integer>(); 1397 vector.add(0); 1398 vector.add(1); 1399 vector.add(2); 1400 1401 Vector<Integer> output = new Vector<Integer>(); 1402 vector.forEach ( k -> output.add(k) ); 1403 1404 assertEquals(vector, output); 1405 } 1406 1407 1408 public void test_forEach_NPE() throws Exception { 1409 Vector<Integer> vector = new Vector<>(); 1410 try { 1411 vector.forEach(null); 1412 fail(); 1413 } catch(NullPointerException expected) {} 1414 } 1415 1416 public void test_forEach_CME() throws Exception { 1417 Vector<Integer> vector = new Vector<>(); 1418 vector.add(1); 1419 vector.add(2); 1420 try { 1421 vector.forEach(new java.util.function.Consumer<Integer>() { 1422 @Override 1423 public void accept(Integer t) {vector.add(t);} 1424 }); 1425 fail(); 1426 } catch(ConcurrentModificationException expected) {} 1427 } 1428 1429 public void test_forEachRemaining_iterator() throws Exception { 1430 ForEachRemainingTester.runTests(Vector.class, new String[] { "foo", "bar", "baz" }); 1431 ForEachRemainingTester.runTests(Vector.class, new String[] { "foo" }); 1432 } 1433 1434 public void test_spliterator() throws Exception { 1435 ArrayList<Integer> testElements = new ArrayList<>( 1436 Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)); 1437 Vector<Integer> list = new Vector<>(); 1438 list.addAll(testElements); 1439 1440 SpliteratorTester.runBasicIterationTests(list.spliterator(), testElements); 1441 SpliteratorTester.runBasicSplitTests(list, testElements); 1442 SpliteratorTester.testSpliteratorNPE(list.spliterator()); 1443 1444 assertTrue(list.spliterator().hasCharacteristics( 1445 Spliterator.ORDERED | Spliterator.SIZED | Spliterator.SUBSIZED)); 1446 1447 SpliteratorTester.runOrderedTests(list); 1448 SpliteratorTester.runSizedTests(list, 16 /* expected size */); 1449 SpliteratorTester.runSubSizedTests(list, 16 /* expected size */); 1450 } 1451 1452 public void test_spliterator_CME() throws Exception { 1453 Vector<Integer> list = new Vector<>(); 1454 list.add(52); 1455 1456 Spliterator<Integer> sp = list.spliterator(); 1457 try { 1458 sp.tryAdvance(value -> list.add(value)); 1459 fail(); 1460 } catch (ConcurrentModificationException expected) { 1461 } 1462 1463 try { 1464 sp.forEachRemaining(value -> list.add(value)); 1465 fail(); 1466 } catch (ConcurrentModificationException expected) { 1467 } 1468 } 1469 1470 public void test_removeIf() { 1471 runBasicRemoveIfTests(Vector<Integer>::new); 1472 runBasicRemoveIfTestsUnordered(Vector<Integer>::new); 1473 runRemoveIfOnEmpty(Vector<Integer>::new); 1474 testRemoveIfNPE(Vector<Integer>::new); 1475 testRemoveIfCME(Vector<Integer>::new); 1476 } 1477 1478 // http://b/25867131 et al. 1479 public void testIteratorAddAfterCompleteIteration() { 1480 Vector<String> strings = new Vector<>(); 1481 strings.add("string1"); 1482 Iterator<String> it = strings.iterator(); 1483 assertTrue(it.hasNext()); 1484 assertEquals("string1", it.next()); 1485 assertFalse(it.hasNext()); 1486 strings.add("string2"); 1487 // The value of hasNext() must not flap between true and false. If we returned "true" 1488 // here, we'd fail with a CME on the next call to next() anyway. 1489 assertFalse(it.hasNext()); 1490 } 1491 1492 public void testHasNextAfterRemoval() { 1493 Vector<String> strings = new Vector<>(); 1494 strings.add("string1"); 1495 Iterator<String> it = strings.iterator(); 1496 it.next(); 1497 it.remove(); 1498 assertFalse(it.hasNext()); 1499 1500 strings = new Vector<>(); 1501 strings.add("string1"); 1502 strings.add("string2"); 1503 it = strings.iterator(); 1504 it.next(); 1505 it.remove(); 1506 assertTrue(it.hasNext()); 1507 assertEquals("string2", it.next()); 1508 } 1509 1510 // http://b/27430229 1511 public void testRemoveAllDuringIteration() { 1512 Vector<String> vector = new Vector<>(); 1513 vector.add("food"); 1514 Iterator<String> vectorIterator = vector.iterator(); 1515 vectorIterator.next(); 1516 vector.removeAllElements(); 1517 assertFalse(vectorIterator.hasNext()); 1518 } 1519 1520 /** 1521 * Sets up the fixture, for example, open a network connection. This method 1522 * is called before a test is executed. 1523 */ 1524 protected void setUp() { 1525 for (int i = 0; i < 100; i++) { 1526 tVector.addElement("Test " + i); 1527 } 1528 objArray = new Object[100]; 1529 for (int i = 0; i < 100; i++) { 1530 objArray[i] = "Test " + i; 1531 } 1532 } 1533 1534 /** 1535 * Tears down the fixture, for example, close a network connection. This 1536 * method is called after a test is executed. 1537 */ 1538 protected void tearDown() { 1539 } 1540} 1541