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