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