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