LinkedListTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
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 tests.api.java.util; 19 20import dalvik.annotation.TestTarget; 21import dalvik.annotation.TestInfo; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTargetClass; 24 25import java.util.ArrayList; 26import java.util.Arrays; 27import java.util.Collection; 28import java.util.Iterator; 29import java.util.LinkedList; 30import java.util.List; 31import java.util.ListIterator; 32import java.util.NoSuchElementException; 33 34 35import tests.support.Support_ListTest; 36 37@TestTargetClass(LinkedList.class) 38public class LinkedListTest extends junit.framework.TestCase { 39 40 LinkedList ll; 41 42 static Object[] objArray; 43 { 44 objArray = new Object[100]; 45 for (int i = 0; i < objArray.length; i++) 46 objArray[i] = new Integer(i); 47 } 48 49 /** 50 * @tests java.util.LinkedList#LinkedList() 51 */ 52 @TestInfo( 53 level = TestLevel.COMPLETE, 54 purpose = "", 55 targets = { 56 @TestTarget( 57 methodName = "LinkedList", 58 methodArgs = {} 59 ) 60 }) 61 public void test_Constructor() { 62 // Test for method java.util.LinkedList() 63 new Support_ListTest("", ll).runTest(); 64 65 LinkedList subList = new LinkedList(); 66 for (int i = -50; i < 150; i++) 67 subList.add(new Integer(i)); 68 new Support_ListTest("", subList.subList(50, 150)).runTest(); 69 } 70 71 /** 72 * @tests java.util.LinkedList#LinkedList(java.util.Collection) 73 */ 74 @TestInfo( 75 level = TestLevel.PARTIAL, 76 purpose = "Doesn't verify NullPointerException.", 77 targets = { 78 @TestTarget( 79 methodName = "LinkedList", 80 methodArgs = {java.util.Collection.class} 81 ) 82 }) 83 public void test_ConstructorLjava_util_Collection() { 84 // Test for method java.util.LinkedList(java.util.Collection) 85 assertTrue("Incorrect LinkedList constructed", new LinkedList(ll) 86 .equals(ll)); 87 } 88 89 /** 90 * @tests java.util.LinkedList#add(int, java.lang.Object) 91 */ 92 @TestInfo( 93 level = TestLevel.COMPLETE, 94 purpose = "", 95 targets = { 96 @TestTarget( 97 methodName = "add", 98 methodArgs = {int.class, Object.class} 99 ) 100 }) 101 public void test_addILjava_lang_Object() { 102 // Test for method void java.util.LinkedList.add(int, java.lang.Object) 103 Object o; 104 ll.add(50, o = "Test"); 105 assertTrue("Failed to add Object>: " + ll.get(50).toString(), ll 106 .get(50) == o); 107 assertTrue("Failed to fix up list after insert", 108 ll.get(51) == objArray[50] && (ll.get(52) == objArray[51])); 109 ll.add(50, null); 110 assertNull("Did not add null correctly", ll.get(50)); 111 } 112 113 /** 114 * @tests java.util.LinkedList#add(java.lang.Object) 115 */ 116 @TestInfo( 117 level = TestLevel.COMPLETE, 118 purpose = "", 119 targets = { 120 @TestTarget( 121 methodName = "add", 122 methodArgs = {Object.class} 123 ) 124 }) 125 public void test_addLjava_lang_Object() { 126 // Test for method boolean java.util.LinkedList.add(java.lang.Object) 127 Object o; 128 ll.add(o = new Object()); 129 assertTrue("Failed to add Object", ll.getLast() == o); 130 ll.add(null); 131 assertNull("Did not add null correctly", ll.get(ll.size() - 1)); 132 } 133 134 /** 135 * @tests java.util.LinkedList#addAll(int, java.util.Collection) 136 */ 137 @TestInfo( 138 level = TestLevel.PARTIAL, 139 purpose = "Doesn't verify IndexOutOfBoundsException, " + 140 "NullPointerException.", 141 targets = { 142 @TestTarget( 143 methodName = "addAll", 144 methodArgs = {int.class, java.util.Collection.class} 145 ) 146 }) 147 public void test_addAllILjava_util_Collection() { 148 // Test for method boolean java.util.LinkedList.addAll(int, 149 // java.util.Collection) 150 ll.addAll(50, (Collection) ll.clone()); 151 assertEquals("Returned incorrect size after adding to existing list", 200, ll 152 .size()); 153 for (int i = 0; i < 50; i++) 154 assertTrue("Manipulated elements < index", ll.get(i) == objArray[i]); 155 for (int i = 0; i >= 50 && (i < 150); i++) 156 assertTrue("Failed to ad elements properly", 157 ll.get(i) == objArray[i - 50]); 158 for (int i = 0; i >= 150 && (i < 200); i++) 159 assertTrue("Failed to ad elements properly", 160 ll.get(i) == objArray[i - 100]); 161 List myList = new LinkedList(); 162 myList.add(null); 163 myList.add("Blah"); 164 myList.add(null); 165 myList.add("Booga"); 166 myList.add(null); 167 ll.addAll(50, myList); 168 assertNull("a) List w/nulls not added correctly", ll.get(50)); 169 assertEquals("b) List w/nulls not added correctly", 170 "Blah", ll.get(51)); 171 assertNull("c) List w/nulls not added correctly", ll.get(52)); 172 assertEquals("d) List w/nulls not added correctly", 173 "Booga", ll.get(53)); 174 assertNull("e) List w/nulls not added correctly", ll.get(54)); 175 } 176 177 /** 178 * @tests java.util.LinkedList#addAll(int, java.util.Collection) 179 */ 180 @TestInfo( 181 level = TestLevel.PARTIAL, 182 purpose = "Verifies IndexOutOfBoundsException.", 183 targets = { 184 @TestTarget( 185 methodName = "addAll", 186 methodArgs = {int.class, java.util.Collection.class} 187 ) 188 }) 189 public void test_addAllILjava_util_Collection_2() { 190 // Regression for HARMONY-467 191 LinkedList obj = new LinkedList(); 192 try { 193 obj.addAll(-1, (Collection) null); 194 fail("IndexOutOfBoundsException expected"); 195 } catch (IndexOutOfBoundsException e) { 196 } 197 } 198 199 /** 200 * @tests java.util.LinkedList#addAll(java.util.Collection) 201 */ 202 @TestInfo( 203 level = TestLevel.PARTIAL, 204 purpose = "", 205 targets = { 206 @TestTarget( 207 methodName = "addAll", 208 methodArgs = {java.util.Collection.class} 209 ) 210 }) 211 public void test_addAllLjava_util_Collection() { 212 // Test for method boolean 213 // java.util.LinkedList.addAll(java.util.Collection) 214 List l = new ArrayList(); 215 l.addAll((Collection) ll.clone()); 216 for (int i = 0; i < ll.size(); i++) 217 assertTrue("Failed to add elements properly", l.get(i).equals( 218 ll.get(i))); 219 ll.addAll((Collection) ll.clone()); 220 assertEquals("Returned incorrect siZe after adding to existing list", 200, ll 221 .size()); 222 for (int i = 0; i < 100; i++) { 223 assertTrue("Added to list in incorrect order", ll.get(i).equals( 224 l.get(i))); 225 assertTrue("Failed to add to existing list", ll.get(i + 100) 226 .equals(l.get(i))); 227 } 228 List myList = new LinkedList(); 229 myList.add(null); 230 myList.add("Blah"); 231 myList.add(null); 232 myList.add("Booga"); 233 myList.add(null); 234 ll.addAll(myList); 235 assertNull("a) List w/nulls not added correctly", ll.get(200)); 236 assertEquals("b) List w/nulls not added correctly", 237 "Blah", ll.get(201)); 238 assertNull("c) List w/nulls not added correctly", ll.get(202)); 239 assertEquals("d) List w/nulls not added correctly", 240 "Booga", ll.get(203)); 241 assertNull("e) List w/nulls not added correctly", ll.get(204)); 242 } 243 244 /** 245 * @tests java.util.LinkedList#addFirst(java.lang.Object) 246 */ 247 @TestInfo( 248 level = TestLevel.COMPLETE, 249 purpose = "", 250 targets = { 251 @TestTarget( 252 methodName = "addFirst", 253 methodArgs = {Object.class} 254 ) 255 }) 256 public void test_addFirstLjava_lang_Object() { 257 // Test for method void java.util.LinkedList.addFirst(java.lang.Object) 258 Object o; 259 ll.addFirst(o = new Object()); 260 assertTrue("Failed to add Object", ll.getFirst() == o); 261 ll.addFirst(null); 262 assertNull("Failed to add null", ll.getFirst()); 263 } 264 265 /** 266 * @tests java.util.LinkedList#addLast(java.lang.Object) 267 */ 268 @TestInfo( 269 level = TestLevel.COMPLETE, 270 purpose = "", 271 targets = { 272 @TestTarget( 273 methodName = "addLast", 274 methodArgs = {Object.class} 275 ) 276 }) 277 public void test_addLastLjava_lang_Object() { 278 // Test for method void java.util.LinkedList.addLast(java.lang.Object) 279 Object o; 280 ll.addLast(o = new Object()); 281 assertTrue("Failed to add Object", ll.getLast() == o); 282 ll.addLast(null); 283 assertNull("Failed to add null", ll.getLast()); 284 } 285 286 /** 287 * @tests java.util.LinkedList#clear() 288 */ 289 @TestInfo( 290 level = TestLevel.COMPLETE, 291 purpose = "", 292 targets = { 293 @TestTarget( 294 methodName = "clear", 295 methodArgs = {} 296 ) 297 }) 298 public void test_clear() { 299 // Test for method void java.util.LinkedList.clear() 300 ll.clear(); 301 for (int i = 0; i < ll.size(); i++) 302 assertNull("Failed to clear list", ll.get(i)); 303 } 304 305 /** 306 * @tests java.util.LinkedList#clone() 307 */ 308 @TestInfo( 309 level = TestLevel.COMPLETE, 310 purpose = "", 311 targets = { 312 @TestTarget( 313 methodName = "clone", 314 methodArgs = {} 315 ) 316 }) 317 public void test_clone() { 318 // Test for method java.lang.Object java.util.LinkedList.clone() 319 Object x = ll.clone(); 320 assertTrue("Cloned list was inequal to cloned", x.equals(ll)); 321 for (int i = 0; i < ll.size(); i++) 322 assertTrue("Cloned list contains incorrect elements", ll.get(i) 323 .equals(((LinkedList) x).get(i))); 324 ll.addFirst(null); 325 x = ll.clone(); 326 assertTrue("List with a null did not clone properly", ll.equals(x)); 327 } 328 329 /** 330 * @tests java.util.LinkedList#contains(java.lang.Object) 331 */ 332 @TestInfo( 333 level = TestLevel.COMPLETE, 334 purpose = "", 335 targets = { 336 @TestTarget( 337 methodName = "contains", 338 methodArgs = {java.lang.Object.class} 339 ) 340 }) 341 public void test_containsLjava_lang_Object() { 342 // Test for method boolean 343 // java.util.LinkedList.contains(java.lang.Object) 344 assertTrue("Returned false for valid element", ll 345 .contains(objArray[99])); 346 assertTrue("Returned false for equal element", ll.contains(new Integer( 347 8))); 348 assertTrue("Returned true for invalid element", !ll 349 .contains(new Object())); 350 assertTrue("Should not contain null", !ll.contains(null)); 351 ll.add(25, null); 352 assertTrue("Should contain null", ll.contains(null)); 353 } 354 355 /** 356 * @tests java.util.LinkedList#get(int) 357 */ 358 @TestInfo( 359 level = TestLevel.COMPLETE, 360 purpose = "", 361 targets = { 362 @TestTarget( 363 methodName = "get", 364 methodArgs = {int.class} 365 ) 366 }) 367 public void test_getI() { 368 // Test for method java.lang.Object java.util.LinkedList.get(int) 369 assertTrue("Returned incorrect element", ll.get(22) == objArray[22]); 370 try { 371 ll.get(8765); 372 fail("Failed to throw expected exception for index > size"); 373 } catch (IndexOutOfBoundsException e) { 374 } 375 } 376 377 /** 378 * @tests java.util.LinkedList#getFirst() 379 */ 380 @TestInfo( 381 level = TestLevel.PARTIAL, 382 purpose = "Doesn't verify NoSuchElementException.", 383 targets = { 384 @TestTarget( 385 methodName = "getFirst", 386 methodArgs = {} 387 ) 388 }) 389 public void test_getFirst() { 390 // Test for method java.lang.Object java.util.LinkedList.getFirst() 391 assertTrue("Returned incorrect first element", ll.getFirst().equals( 392 objArray[0])); 393 } 394 395 /** 396 * @tests java.util.LinkedList#getLast() 397 */ 398 @TestInfo( 399 level = TestLevel.PARTIAL, 400 purpose = "Doesn't verify NoSuchElementException.", 401 targets = { 402 @TestTarget( 403 methodName = "getLast", 404 methodArgs = {} 405 ) 406 }) 407 public void test_getLast() { 408 // Test for method java.lang.Object java.util.LinkedList.getLast() 409 assertTrue("Returned incorrect first element", ll.getLast().equals( 410 objArray[objArray.length - 1])); 411 } 412 413 /** 414 * @tests java.util.LinkedList#indexOf(java.lang.Object) 415 */ 416 @TestInfo( 417 level = TestLevel.COMPLETE, 418 purpose = "", 419 targets = { 420 @TestTarget( 421 methodName = "indexOf", 422 methodArgs = {java.lang.Object.class} 423 ) 424 }) 425 public void test_indexOfLjava_lang_Object() { 426 // Test for method int java.util.LinkedList.indexOf(java.lang.Object) 427 assertEquals("Returned incorrect index", 87, ll.indexOf(objArray[87])); 428 assertEquals("Returned index for invalid Object", -1, ll 429 .indexOf(new Object())); 430 ll.add(20, null); 431 ll.add(24, null); 432 assertTrue("Index of null should be 20, but got: " + ll.indexOf(null), 433 ll.indexOf(null) == 20); 434 } 435 436 /** 437 * @tests java.util.LinkedList#lastIndexOf(java.lang.Object) 438 */ 439 @TestInfo( 440 level = TestLevel.COMPLETE, 441 purpose = "", 442 targets = { 443 @TestTarget( 444 methodName = "lastIndexOf", 445 methodArgs = {java.lang.Object.class} 446 ) 447 }) 448 public void test_lastIndexOfLjava_lang_Object() { 449 // Test for method int 450 // java.util.LinkedList.lastIndexOf(java.lang.Object) 451 ll.add(new Integer(99)); 452 assertEquals("Returned incorrect index", 453 100, ll.lastIndexOf(objArray[99])); 454 assertEquals("Returned index for invalid Object", -1, ll 455 .lastIndexOf(new Object())); 456 ll.add(20, null); 457 ll.add(24, null); 458 assertTrue("Last index of null should be 20, but got: " 459 + ll.lastIndexOf(null), ll.lastIndexOf(null) == 24); 460 } 461 462 /** 463 * @tests java.util.LinkedList#listIterator(int) 464 */ 465 @TestInfo( 466 level = TestLevel.PARTIAL, 467 purpose = "Doesn't verify IndexOutOfBoundsException.", 468 targets = { 469 @TestTarget( 470 methodName = "listIterator", 471 methodArgs = {int.class} 472 ) 473 }) 474 public void test_listIteratorI() { 475 // Test for method java.util.ListIterator 476 // java.util.LinkedList.listIterator(int) 477 ListIterator i = ll.listIterator(); 478 Object elm; 479 int n = 0; 480 while (i.hasNext()) { 481 if (n == 0 || n == objArray.length - 1) { 482 if (n == 0) 483 assertTrue("First element claimed to have a previous", !i 484 .hasPrevious()); 485 if (n == objArray.length) 486 assertTrue("Last element claimed to have next", !i 487 .hasNext()); 488 } 489 elm = i.next(); 490 assertTrue("Iterator returned elements in wrong order", 491 elm == objArray[n]); 492 if (n > 0 && n < objArray.length - 1) { 493 assertTrue("Next index returned incorrect value", 494 i.nextIndex() == n + 1); 495 assertTrue("previousIndex returned incorrect value : " 496 + i.previousIndex() + ", n val: " + n, i 497 .previousIndex() == n); 498 } 499 ++n; 500 } 501 List myList = new LinkedList(); 502 myList.add(null); 503 myList.add("Blah"); 504 myList.add(null); 505 myList.add("Booga"); 506 myList.add(null); 507 ListIterator li = myList.listIterator(); 508 assertTrue("li.hasPrevious() should be false", !li.hasPrevious()); 509 assertNull("li.next() should be null", li.next()); 510 assertTrue("li.hasPrevious() should be true", li.hasPrevious()); 511 assertNull("li.prev() should be null", li.previous()); 512 assertNull("li.next() should be null", li.next()); 513 assertEquals("li.next() should be Blah", "Blah", li.next()); 514 assertNull("li.next() should be null", li.next()); 515 assertEquals("li.next() should be Booga", "Booga", li.next()); 516 assertTrue("li.hasNext() should be true", li.hasNext()); 517 assertNull("li.next() should be null", li.next()); 518 assertTrue("li.hasNext() should be false", !li.hasNext()); 519 } 520 521 /** 522 * @tests java.util.LinkedList#remove(int) 523 */ 524 @TestInfo( 525 level = TestLevel.COMPLETE, 526 purpose = "", 527 targets = { 528 @TestTarget( 529 methodName = "remove", 530 methodArgs = {int.class} 531 ) 532 }) 533 public void test_removeI() { 534 // Test for method java.lang.Object java.util.LinkedList.remove(int) 535 ll.remove(10); 536 assertEquals("Failed to remove element", -1, ll.indexOf(objArray[10])); 537 try { 538 ll.remove(999); 539 fail("Failed to throw expected exception when index out of range"); 540 } catch (IndexOutOfBoundsException e) { 541 // Correct 542 } 543 544 ll.add(20, null); 545 ll.remove(20); 546 assertNotNull("Should have removed null", ll.get(20)); 547 } 548 549 /** 550 * @tests java.util.LinkedList#remove(java.lang.Object) 551 */ 552 @TestInfo( 553 level = TestLevel.COMPLETE, 554 purpose = "", 555 targets = { 556 @TestTarget( 557 methodName = "remove", 558 methodArgs = {java.lang.Object.class} 559 ) 560 }) 561 public void test_removeLjava_lang_Object() { 562 // Test for method boolean java.util.LinkedList.remove(java.lang.Object) 563 assertTrue("Failed to remove valid Object", ll.remove(objArray[87])); 564 assertTrue("Removed invalid object", !ll.remove(new Object())); 565 assertEquals("Found Object after removal", -1, ll.indexOf(objArray[87])); 566 ll.add(null); 567 ll.remove(null); 568 assertTrue("Should not contain null afrer removal", !ll.contains(null)); 569 } 570 571 /** 572 * @tests java.util.LinkedList#removeFirst() 573 */ 574 @TestInfo( 575 level = TestLevel.PARTIAL, 576 purpose = "Doesn't verify NoSuchElementException.", 577 targets = { 578 @TestTarget( 579 methodName = "removeFirst", 580 methodArgs = {} 581 ) 582 }) 583 public void test_removeFirst() { 584 // Test for method java.lang.Object java.util.LinkedList.removeFirst() 585 ll.removeFirst(); 586 assertTrue("Failed to remove first element", 587 ll.getFirst() != objArray[0]); 588 } 589 590 /** 591 * @tests java.util.LinkedList#removeLast() 592 */ 593 @TestInfo( 594 level = TestLevel.PARTIAL, 595 purpose = "Doesn't verify NoSuchElementException.", 596 targets = { 597 @TestTarget( 598 methodName = "removeLast", 599 methodArgs = {} 600 ) 601 }) 602 public void test_removeLast() { 603 // Test for method java.lang.Object java.util.LinkedList.removeLast() 604 ll.removeLast(); 605 assertTrue("Failed to remove last element", 606 ll.getLast() != objArray[objArray.length - 1]); 607 } 608 609 /** 610 * @tests java.util.LinkedList#set(int, java.lang.Object) 611 */ 612 @TestInfo( 613 level = TestLevel.PARTIAL, 614 purpose = "Doesn't verify IndexOutOfBoundsException.", 615 targets = { 616 @TestTarget( 617 methodName = "set", 618 methodArgs = {int.class, Object.class} 619 ) 620 }) 621 public void test_setILjava_lang_Object() { 622 // Test for method java.lang.Object java.util.LinkedList.set(int, 623 // java.lang.Object) 624 Object obj; 625 ll.set(65, obj = new Object()); 626 assertTrue("Failed to set object", ll.get(65) == obj); 627 } 628 629 /** 630 * @tests java.util.LinkedList#size() 631 */ 632 @TestInfo( 633 level = TestLevel.COMPLETE, 634 purpose = "", 635 targets = { 636 @TestTarget( 637 methodName = "size", 638 methodArgs = {} 639 ) 640 }) 641 public void test_size() { 642 // Test for method int java.util.LinkedList.size() 643 assertTrue("Returned incorrect size", ll.size() == objArray.length); 644 ll.removeFirst(); 645 assertTrue("Returned incorrect size", ll.size() == objArray.length - 1); 646 } 647 648 /** 649 * @tests java.util.LinkedList#toArray() 650 */ 651 @TestInfo( 652 level = TestLevel.COMPLETE, 653 purpose = "", 654 targets = { 655 @TestTarget( 656 methodName = "toArray", 657 methodArgs = {} 658 ) 659 }) 660 public void test_toArray() { 661 // Test for method java.lang.Object [] java.util.LinkedList.toArray() 662 ll.add(null); 663 Object[] obj = ll.toArray(); 664 assertEquals("Returned array of incorrect size", objArray.length + 1, obj.length); 665 666 for (int i = 0; i < obj.length - 1; i++) 667 assertTrue("Returned incorrect array: " + i, obj[i] == objArray[i]); 668 assertNull("Returned incorrect array--end isn't null", 669 obj[obj.length - 1]); 670 } 671 672 /** 673 * @tests java.util.LinkedList#toArray(java.lang.Object[]) 674 */ 675 @TestInfo( 676 level = TestLevel.PARTIAL, 677 purpose = "Doesn't verify ArrayStoreException, NullPointerException.", 678 targets = { 679 @TestTarget( 680 methodName = "toArray", 681 methodArgs = {Object[].class} 682 ) 683 }) 684 public void test_toArray$Ljava_lang_Object() { 685 // Test for method java.lang.Object [] 686 // java.util.LinkedList.toArray(java.lang.Object []) 687 Integer[] argArray = new Integer[100]; 688 Object[] retArray; 689 retArray = ll.toArray(argArray); 690 assertTrue("Returned different array than passed", retArray == argArray); 691 List retList = new LinkedList(Arrays.asList(retArray)); 692 Iterator li = ll.iterator(); 693 Iterator ri = retList.iterator(); 694 while (li.hasNext()) 695 assertTrue("Lists are not equal", li.next() == ri.next()); 696 argArray = new Integer[1000]; 697 retArray = ll.toArray(argArray); 698 assertNull("Failed to set first extra element to null", argArray[ll 699 .size()]); 700 for (int i = 0; i < ll.size(); i++) 701 assertTrue("Returned incorrect array: " + i, 702 retArray[i] == objArray[i]); 703 ll.add(50, null); 704 argArray = new Integer[101]; 705 retArray = ll.toArray(argArray); 706 assertTrue("Returned different array than passed", retArray == argArray); 707 retArray = ll.toArray(argArray); 708 assertTrue("Returned different array than passed", retArray == argArray); 709 retList = new LinkedList(Arrays.asList(retArray)); 710 li = ll.iterator(); 711 ri = retList.iterator(); 712 while (li.hasNext()) 713 assertTrue("Lists are not equal", li.next() == ri.next()); 714 } 715 @TestInfo( 716 level = TestLevel.COMPLETE, 717 purpose = "", 718 targets = { 719 @TestTarget( 720 methodName = "offer", 721 methodArgs = {Object.class} 722 ) 723 }) 724 public void test_offer() { 725 int origSize = ll.size(); 726 assertTrue("offer() should return true'", ll.offer(objArray[0])); 727 assertEquals("offer() should add an element as the last one", origSize, ll.lastIndexOf(objArray[0])); 728 } 729 @TestInfo( 730 level = TestLevel.PARTIAL, 731 purpose = "Doesn't verify that poll method removes the first element " + 732 "of the list.", 733 targets = { 734 @TestTarget( 735 methodName = "poll", 736 methodArgs = {} 737 ) 738 }) 739 public void test_poll() { 740 assertEquals("should return the head", objArray[0], ll.poll()); 741 LinkedList list = new LinkedList(); 742 assertNull("should return 'null' if list is empty", list.poll()); 743 } 744 @TestInfo( 745 level = TestLevel.COMPLETE, 746 purpose = "", 747 targets = { 748 @TestTarget( 749 methodName = "remove", 750 methodArgs = {} 751 ) 752 }) 753 public void test_remove() { 754 for (int i = 0; i < objArray.length; i++) { 755 assertEquals("should remove the head", objArray[i], ll.remove()); 756 } 757 assertEquals("should be empty", 0, ll.size()); 758 try { 759 ll.remove(); 760 fail("NoSuchElementException is expected when removing from the empty list"); 761 } catch (NoSuchElementException e) { 762 //-- expected 763 } 764 } 765 @TestInfo( 766 level = TestLevel.COMPLETE, 767 purpose = "", 768 targets = { 769 @TestTarget( 770 methodName = "element", 771 methodArgs = {} 772 ) 773 }) 774 public void test_element() { 775 assertEquals("should return the head", objArray[0], ll.element()); 776 assertEquals("element() should remove nothing", objArray.length, ll.size()); 777 try { 778 new LinkedList().remove(); 779 fail("NoSuchElementException is expected when the list is empty"); 780 } catch (NoSuchElementException e) { 781 //-- expected 782 } 783 } 784 785 /** 786 * Sets up the fixture, for example, open a network connection. This method 787 * is called before a test is executed. 788 */ 789 protected void setUp() throws Exception { 790 super.setUp(); 791 ll = new LinkedList(); 792 for (int i = 0; i < objArray.length; i++) { 793 ll.add(objArray[i]); 794 } 795 } 796} 797