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