1 /* 2 * Copyright (C) 2007 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.google.common.collect; 18 19 import static com.google.common.collect.Iterators.advance; 20 import static com.google.common.collect.Iterators.get; 21 import static com.google.common.collect.Iterators.getLast; 22 import static com.google.common.collect.Lists.newArrayList; 23 import static com.google.common.truth.Truth.assertThat; 24 import static java.util.Arrays.asList; 25 26 import com.google.common.annotations.GwtCompatible; 27 import com.google.common.base.Function; 28 import com.google.common.base.Predicate; 29 import com.google.common.base.Predicates; 30 31 import junit.framework.AssertionFailedError; 32 import junit.framework.TestCase; 33 34 import java.util.Collections; 35 import java.util.Enumeration; 36 import java.util.Iterator; 37 import java.util.List; 38 import java.util.ListIterator; 39 import java.util.NoSuchElementException; 40 import java.util.RandomAccess; 41 import java.util.Set; 42 import java.util.Vector; 43 44 /** 45 * Unit test for {@code Iterators}. 46 * 47 * @author Kevin Bourrillion 48 */ 49 @GwtCompatible(emulated = true) 50 public class IteratorsTest extends TestCase { 51 52 public void testEmptyIterator() { 53 Iterator<String> iterator = Iterators.emptyIterator(); 54 assertFalse(iterator.hasNext()); 55 try { 56 iterator.next(); 57 fail("no exception thrown"); 58 } catch (NoSuchElementException expected) { 59 } 60 try { 61 iterator.remove(); 62 fail("no exception thrown"); 63 } catch (UnsupportedOperationException expected) { 64 } 65 } 66 67 public void testEmptyListIterator() { 68 ListIterator<String> iterator = Iterators.emptyListIterator(); 69 assertFalse(iterator.hasNext()); 70 assertFalse(iterator.hasPrevious()); 71 assertEquals(0, iterator.nextIndex()); 72 assertEquals(-1, iterator.previousIndex()); 73 try { 74 iterator.next(); 75 fail("no exception thrown"); 76 } catch (NoSuchElementException expected) { 77 } 78 try { 79 iterator.previous(); 80 fail("no exception thrown"); 81 } catch (NoSuchElementException expected) { 82 } 83 try { 84 iterator.remove(); 85 fail("no exception thrown"); 86 } catch (UnsupportedOperationException expected) { 87 } 88 try { 89 iterator.set("a"); 90 fail("no exception thrown"); 91 } catch (UnsupportedOperationException expected) { 92 } 93 try { 94 iterator.add("a"); 95 fail("no exception thrown"); 96 } catch (UnsupportedOperationException expected) { 97 } 98 } 99 100 public void testEmptyModifiableIterator() { 101 Iterator<String> iterator = Iterators.emptyModifiableIterator(); 102 assertFalse(iterator.hasNext()); 103 try { 104 iterator.next(); 105 fail("Expected NoSuchElementException"); 106 } catch (NoSuchElementException expected) { 107 } 108 try { 109 iterator.remove(); 110 fail("Expected IllegalStateException"); 111 } catch (IllegalStateException expected) { 112 } 113 } 114 115 public void testSize0() { 116 Iterator<String> iterator = Iterators.emptyIterator(); 117 assertEquals(0, Iterators.size(iterator)); 118 } 119 120 public void testSize1() { 121 Iterator<Integer> iterator = Collections.singleton(0).iterator(); 122 assertEquals(1, Iterators.size(iterator)); 123 } 124 125 public void testSize_partiallyConsumed() { 126 Iterator<Integer> iterator = asList(1, 2, 3, 4, 5).iterator(); 127 iterator.next(); 128 iterator.next(); 129 assertEquals(3, Iterators.size(iterator)); 130 } 131 132 public void test_contains_nonnull_yes() { 133 Iterator<String> set = asList("a", null, "b").iterator(); 134 assertTrue(Iterators.contains(set, "b")); 135 } 136 137 public void test_contains_nonnull_no() { 138 Iterator<String> set = asList("a", "b").iterator(); 139 assertFalse(Iterators.contains(set, "c")); 140 } 141 142 public void test_contains_null_yes() { 143 Iterator<String> set = asList("a", null, "b").iterator(); 144 assertTrue(Iterators.contains(set, null)); 145 } 146 147 public void test_contains_null_no() { 148 Iterator<String> set = asList("a", "b").iterator(); 149 assertFalse(Iterators.contains(set, null)); 150 } 151 152 public void testGetOnlyElement_noDefault_valid() { 153 Iterator<String> iterator = Collections.singletonList("foo").iterator(); 154 assertEquals("foo", Iterators.getOnlyElement(iterator)); 155 } 156 157 public void testGetOnlyElement_noDefault_empty() { 158 Iterator<String> iterator = Iterators.emptyIterator(); 159 try { 160 Iterators.getOnlyElement(iterator); 161 fail(); 162 } catch (NoSuchElementException expected) { 163 } 164 } 165 166 public void testGetOnlyElement_noDefault_moreThanOneLessThanFiveElements() { 167 Iterator<String> iterator = asList("one", "two").iterator(); 168 try { 169 Iterators.getOnlyElement(iterator); 170 fail(); 171 } catch (IllegalArgumentException expected) { 172 assertEquals("expected one element but was: <one, two>", 173 expected.getMessage()); 174 } 175 } 176 177 public void testGetOnlyElement_noDefault_fiveElements() { 178 Iterator<String> iterator = 179 asList("one", "two", "three", "four", "five").iterator(); 180 try { 181 Iterators.getOnlyElement(iterator); 182 fail(); 183 } catch (IllegalArgumentException expected) { 184 assertEquals("expected one element but was: " 185 + "<one, two, three, four, five>", 186 expected.getMessage()); 187 } 188 } 189 190 public void testGetOnlyElement_noDefault_moreThanFiveElements() { 191 Iterator<String> iterator = 192 asList("one", "two", "three", "four", "five", "six").iterator(); 193 try { 194 Iterators.getOnlyElement(iterator); 195 fail(); 196 } catch (IllegalArgumentException expected) { 197 assertEquals("expected one element but was: " 198 + "<one, two, three, four, five, ...>", 199 expected.getMessage()); 200 } 201 } 202 203 public void testGetOnlyElement_withDefault_singleton() { 204 Iterator<String> iterator = Collections.singletonList("foo").iterator(); 205 assertEquals("foo", Iterators.getOnlyElement(iterator, "bar")); 206 } 207 208 public void testGetOnlyElement_withDefault_empty() { 209 Iterator<String> iterator = Iterators.emptyIterator(); 210 assertEquals("bar", Iterators.getOnlyElement(iterator, "bar")); 211 } 212 213 public void testGetOnlyElement_withDefault_empty_null() { 214 Iterator<String> iterator = Iterators.emptyIterator(); 215 assertNull(Iterators.getOnlyElement(iterator, null)); 216 } 217 218 public void testGetOnlyElement_withDefault_two() { 219 Iterator<String> iterator = asList("foo", "bar").iterator(); 220 try { 221 Iterators.getOnlyElement(iterator, "x"); 222 fail(); 223 } catch (IllegalArgumentException expected) { 224 assertEquals("expected one element but was: <foo, bar>", 225 expected.getMessage()); 226 } 227 } 228 229 public void testFilterSimple() { 230 Iterator<String> unfiltered = Lists.newArrayList("foo", "bar").iterator(); 231 Iterator<String> filtered = Iterators.filter(unfiltered, 232 Predicates.equalTo("foo")); 233 List<String> expected = Collections.singletonList("foo"); 234 List<String> actual = Lists.newArrayList(filtered); 235 assertEquals(expected, actual); 236 } 237 238 public void testFilterNoMatch() { 239 Iterator<String> unfiltered = Lists.newArrayList("foo", "bar").iterator(); 240 Iterator<String> filtered = Iterators.filter(unfiltered, 241 Predicates.alwaysFalse()); 242 List<String> expected = Collections.emptyList(); 243 List<String> actual = Lists.newArrayList(filtered); 244 assertEquals(expected, actual); 245 } 246 247 public void testFilterMatchAll() { 248 Iterator<String> unfiltered = Lists.newArrayList("foo", "bar").iterator(); 249 Iterator<String> filtered = Iterators.filter(unfiltered, 250 Predicates.alwaysTrue()); 251 List<String> expected = Lists.newArrayList("foo", "bar"); 252 List<String> actual = Lists.newArrayList(filtered); 253 assertEquals(expected, actual); 254 } 255 256 public void testFilterNothing() { 257 Iterator<String> unfiltered = Collections.<String>emptyList().iterator(); 258 Iterator<String> filtered = Iterators.filter(unfiltered, 259 new Predicate<String>() { 260 @Override 261 public boolean apply(String s) { 262 throw new AssertionFailedError("Should never be evaluated"); 263 } 264 }); 265 266 List<String> expected = Collections.emptyList(); 267 List<String> actual = Lists.newArrayList(filtered); 268 assertEquals(expected, actual); 269 } 270 271 public void testAny() { 272 List<String> list = Lists.newArrayList(); 273 Predicate<String> predicate = Predicates.equalTo("pants"); 274 275 assertFalse(Iterators.any(list.iterator(), predicate)); 276 list.add("cool"); 277 assertFalse(Iterators.any(list.iterator(), predicate)); 278 list.add("pants"); 279 assertTrue(Iterators.any(list.iterator(), predicate)); 280 } 281 282 public void testAll() { 283 List<String> list = Lists.newArrayList(); 284 Predicate<String> predicate = Predicates.equalTo("cool"); 285 286 assertTrue(Iterators.all(list.iterator(), predicate)); 287 list.add("cool"); 288 assertTrue(Iterators.all(list.iterator(), predicate)); 289 list.add("pants"); 290 assertFalse(Iterators.all(list.iterator(), predicate)); 291 } 292 293 public void testFind_firstElement() { 294 Iterable<String> list = Lists.newArrayList("cool", "pants"); 295 Iterator<String> iterator = list.iterator(); 296 assertEquals("cool", Iterators.find(iterator, Predicates.equalTo("cool"))); 297 assertEquals("pants", iterator.next()); 298 } 299 300 public void testFind_lastElement() { 301 Iterable<String> list = Lists.newArrayList("cool", "pants"); 302 Iterator<String> iterator = list.iterator(); 303 assertEquals("pants", Iterators.find(iterator, 304 Predicates.equalTo("pants"))); 305 assertFalse(iterator.hasNext()); 306 } 307 308 public void testFind_notPresent() { 309 Iterable<String> list = Lists.newArrayList("cool", "pants"); 310 Iterator<String> iterator = list.iterator(); 311 try { 312 Iterators.find(iterator, Predicates.alwaysFalse()); 313 fail(); 314 } catch (NoSuchElementException e) { 315 } 316 assertFalse(iterator.hasNext()); 317 } 318 319 public void testFind_matchAlways() { 320 Iterable<String> list = Lists.newArrayList("cool", "pants"); 321 Iterator<String> iterator = list.iterator(); 322 assertEquals("cool", Iterators.find(iterator, Predicates.alwaysTrue())); 323 } 324 325 public void testFind_withDefault_first() { 326 Iterable<String> list = Lists.newArrayList("cool", "pants"); 327 Iterator<String> iterator = list.iterator(); 328 assertEquals("cool", 329 Iterators.find(iterator, Predicates.equalTo("cool"), "woot")); 330 assertEquals("pants", iterator.next()); 331 } 332 333 public void testFind_withDefault_last() { 334 Iterable<String> list = Lists.newArrayList("cool", "pants"); 335 Iterator<String> iterator = list.iterator(); 336 assertEquals("pants", 337 Iterators.find(iterator, Predicates.equalTo("pants"), "woot")); 338 assertFalse(iterator.hasNext()); 339 } 340 341 public void testFind_withDefault_notPresent() { 342 Iterable<String> list = Lists.newArrayList("cool", "pants"); 343 Iterator<String> iterator = list.iterator(); 344 assertEquals("woot", 345 Iterators.find(iterator, Predicates.alwaysFalse(), "woot")); 346 assertFalse(iterator.hasNext()); 347 } 348 349 public void testFind_withDefault_notPresent_nullReturn() { 350 Iterable<String> list = Lists.newArrayList("cool", "pants"); 351 Iterator<String> iterator = list.iterator(); 352 assertNull( 353 Iterators.find(iterator, Predicates.alwaysFalse(), null)); 354 assertFalse(iterator.hasNext()); 355 } 356 357 public void testFind_withDefault_matchAlways() { 358 Iterable<String> list = Lists.newArrayList("cool", "pants"); 359 Iterator<String> iterator = list.iterator(); 360 assertEquals("cool", 361 Iterators.find(iterator, Predicates.alwaysTrue(), "woot")); 362 assertEquals("pants", iterator.next()); 363 } 364 365 public void testTryFind_firstElement() { 366 Iterable<String> list = Lists.newArrayList("cool", "pants"); 367 Iterator<String> iterator = list.iterator(); 368 assertEquals("cool", 369 Iterators.tryFind(iterator, Predicates.equalTo("cool")).get()); 370 } 371 372 public void testTryFind_lastElement() { 373 Iterable<String> list = Lists.newArrayList("cool", "pants"); 374 Iterator<String> iterator = list.iterator(); 375 assertEquals("pants", 376 Iterators.tryFind(iterator, Predicates.equalTo("pants")).get()); 377 } 378 379 public void testTryFind_alwaysTrue() { 380 Iterable<String> list = Lists.newArrayList("cool", "pants"); 381 Iterator<String> iterator = list.iterator(); 382 assertEquals("cool", 383 Iterators.tryFind(iterator, Predicates.alwaysTrue()).get()); 384 } 385 386 public void testTryFind_alwaysFalse_orDefault() { 387 Iterable<String> list = Lists.newArrayList("cool", "pants"); 388 Iterator<String> iterator = list.iterator(); 389 assertEquals("woot", 390 Iterators.tryFind(iterator, Predicates.alwaysFalse()).or("woot")); 391 assertFalse(iterator.hasNext()); 392 } 393 394 public void testTryFind_alwaysFalse_isPresent() { 395 Iterable<String> list = Lists.newArrayList("cool", "pants"); 396 Iterator<String> iterator = list.iterator(); 397 assertFalse( 398 Iterators.tryFind(iterator, Predicates.alwaysFalse()).isPresent()); 399 assertFalse(iterator.hasNext()); 400 } 401 402 public void testTransform() { 403 Iterator<String> input = asList("1", "2", "3").iterator(); 404 Iterator<Integer> result = Iterators.transform(input, 405 new Function<String, Integer>() { 406 @Override 407 public Integer apply(String from) { 408 return Integer.valueOf(from); 409 } 410 }); 411 412 List<Integer> actual = Lists.newArrayList(result); 413 List<Integer> expected = asList(1, 2, 3); 414 assertEquals(expected, actual); 415 } 416 417 public void testTransformRemove() { 418 List<String> list = Lists.newArrayList("1", "2", "3"); 419 Iterator<String> input = list.iterator(); 420 Iterator<Integer> iterator = Iterators.transform(input, 421 new Function<String, Integer>() { 422 @Override 423 public Integer apply(String from) { 424 return Integer.valueOf(from); 425 } 426 }); 427 428 assertEquals(Integer.valueOf(1), iterator.next()); 429 assertEquals(Integer.valueOf(2), iterator.next()); 430 iterator.remove(); 431 assertEquals(asList("1", "3"), list); 432 } 433 434 public void testPoorlyBehavedTransform() { 435 Iterator<String> input = asList("1", null, "3").iterator(); 436 Iterator<Integer> result = Iterators.transform(input, 437 new Function<String, Integer>() { 438 @Override 439 public Integer apply(String from) { 440 return Integer.valueOf(from); 441 } 442 }); 443 444 result.next(); 445 try { 446 result.next(); 447 fail("Expected NFE"); 448 } catch (NumberFormatException nfe) { 449 // Expected to fail. 450 } 451 } 452 453 public void testNullFriendlyTransform() { 454 Iterator<Integer> input = asList(1, 2, null, 3).iterator(); 455 Iterator<String> result = Iterators.transform(input, 456 new Function<Integer, String>() { 457 @Override 458 public String apply(Integer from) { 459 return String.valueOf(from); 460 } 461 }); 462 463 List<String> actual = Lists.newArrayList(result); 464 List<String> expected = asList("1", "2", "null", "3"); 465 assertEquals(expected, actual); 466 } 467 468 public void testCycleOfEmpty() { 469 // "<String>" for javac 1.5. 470 Iterator<String> cycle = Iterators.<String>cycle(); 471 assertFalse(cycle.hasNext()); 472 } 473 474 public void testCycleOfOne() { 475 Iterator<String> cycle = Iterators.cycle("a"); 476 for (int i = 0; i < 3; i++) { 477 assertTrue(cycle.hasNext()); 478 assertEquals("a", cycle.next()); 479 } 480 } 481 482 public void testCycleOfOneWithRemove() { 483 Iterable<String> iterable = Lists.newArrayList("a"); 484 Iterator<String> cycle = Iterators.cycle(iterable); 485 assertTrue(cycle.hasNext()); 486 assertEquals("a", cycle.next()); 487 cycle.remove(); 488 assertEquals(Collections.emptyList(), iterable); 489 assertFalse(cycle.hasNext()); 490 } 491 492 public void testCycleOfTwo() { 493 Iterator<String> cycle = Iterators.cycle("a", "b"); 494 for (int i = 0; i < 3; i++) { 495 assertTrue(cycle.hasNext()); 496 assertEquals("a", cycle.next()); 497 assertTrue(cycle.hasNext()); 498 assertEquals("b", cycle.next()); 499 } 500 } 501 502 public void testCycleOfTwoWithRemove() { 503 Iterable<String> iterable = Lists.newArrayList("a", "b"); 504 Iterator<String> cycle = Iterators.cycle(iterable); 505 assertTrue(cycle.hasNext()); 506 assertEquals("a", cycle.next()); 507 assertTrue(cycle.hasNext()); 508 assertEquals("b", cycle.next()); 509 assertTrue(cycle.hasNext()); 510 assertEquals("a", cycle.next()); 511 cycle.remove(); 512 assertEquals(Collections.singletonList("b"), iterable); 513 assertTrue(cycle.hasNext()); 514 assertEquals("b", cycle.next()); 515 assertTrue(cycle.hasNext()); 516 assertEquals("b", cycle.next()); 517 cycle.remove(); 518 assertEquals(Collections.emptyList(), iterable); 519 assertFalse(cycle.hasNext()); 520 } 521 522 public void testCycleRemoveWithoutNext() { 523 Iterator<String> cycle = Iterators.cycle("a", "b"); 524 assertTrue(cycle.hasNext()); 525 try { 526 cycle.remove(); 527 fail("no exception thrown"); 528 } catch (IllegalStateException expected) { 529 } 530 } 531 532 public void testCycleRemoveSameElementTwice() { 533 Iterator<String> cycle = Iterators.cycle("a", "b"); 534 cycle.next(); 535 cycle.remove(); 536 try { 537 cycle.remove(); 538 fail("no exception thrown"); 539 } catch (IllegalStateException expected) { 540 } 541 } 542 543 public void testCycleWhenRemoveIsNotSupported() { 544 Iterable<String> iterable = asList("a", "b"); 545 Iterator<String> cycle = Iterators.cycle(iterable); 546 cycle.next(); 547 try { 548 cycle.remove(); 549 fail("no exception thrown"); 550 } catch (UnsupportedOperationException expected) { 551 } 552 } 553 554 public void testCycleRemoveAfterHasNext() { 555 Iterable<String> iterable = Lists.newArrayList("a"); 556 Iterator<String> cycle = Iterators.cycle(iterable); 557 assertTrue(cycle.hasNext()); 558 assertEquals("a", cycle.next()); 559 assertTrue(cycle.hasNext()); 560 cycle.remove(); 561 assertEquals(Collections.emptyList(), iterable); 562 assertFalse(cycle.hasNext()); 563 } 564 565 public void testCycleNoSuchElementException() { 566 Iterable<String> iterable = Lists.newArrayList("a"); 567 Iterator<String> cycle = Iterators.cycle(iterable); 568 assertTrue(cycle.hasNext()); 569 assertEquals("a", cycle.next()); 570 cycle.remove(); 571 assertFalse(cycle.hasNext()); 572 try { 573 cycle.next(); 574 fail(); 575 } catch (NoSuchElementException expected) {} 576 } 577 578 /** 579 * Illustrates the somewhat bizarre behavior when a null is passed in. 580 */ 581 public void testConcatContainingNull() { 582 @SuppressWarnings("unchecked") 583 Iterator<Iterator<Integer>> input 584 = asList(iterateOver(1, 2), null, iterateOver(3)).iterator(); 585 Iterator<Integer> result = Iterators.concat(input); 586 assertEquals(1, (int) result.next()); 587 assertEquals(2, (int) result.next()); 588 try { 589 result.hasNext(); 590 fail("no exception thrown"); 591 } catch (NullPointerException e) { 592 } 593 try { 594 result.next(); 595 fail("no exception thrown"); 596 } catch (NullPointerException e) { 597 } 598 // There is no way to get "through" to the 3. Buh-bye 599 } 600 601 @SuppressWarnings("unchecked") 602 public void testConcatVarArgsContainingNull() { 603 try { 604 Iterators.concat(iterateOver(1, 2), null, iterateOver(3), iterateOver(4), 605 iterateOver(5)); 606 fail("no exception thrown"); 607 } catch (NullPointerException e) { 608 } 609 } 610 611 public void testAddAllWithEmptyIterator() { 612 List<String> alreadyThere = Lists.newArrayList("already", "there"); 613 614 boolean changed = Iterators.addAll(alreadyThere, 615 Iterators.<String>emptyIterator()); 616 assertThat(alreadyThere).has().exactly("already", "there").inOrder(); 617 assertFalse(changed); 618 } 619 620 public void testAddAllToList() { 621 List<String> alreadyThere = Lists.newArrayList("already", "there"); 622 List<String> freshlyAdded = Lists.newArrayList("freshly", "added"); 623 624 boolean changed = Iterators.addAll(alreadyThere, freshlyAdded.iterator()); 625 626 assertThat(alreadyThere).has().exactly("already", "there", "freshly", "added"); 627 assertTrue(changed); 628 } 629 630 public void testAddAllToSet() { 631 Set<String> alreadyThere 632 = Sets.newLinkedHashSet(asList("already", "there")); 633 List<String> oneMore = Lists.newArrayList("there"); 634 635 boolean changed = Iterators.addAll(alreadyThere, oneMore.iterator()); 636 assertThat(alreadyThere).has().exactly("already", "there").inOrder(); 637 assertFalse(changed); 638 } 639 640 private static Iterator<Integer> iterateOver(final Integer... values) { 641 return newArrayList(values).iterator(); 642 } 643 644 public void testElementsEqual() { 645 Iterable<?> a; 646 Iterable<?> b; 647 648 // Base case. 649 a = Lists.newArrayList(); 650 b = Collections.emptySet(); 651 assertTrue(Iterators.elementsEqual(a.iterator(), b.iterator())); 652 653 // A few elements. 654 a = asList(4, 8, 15, 16, 23, 42); 655 b = asList(4, 8, 15, 16, 23, 42); 656 assertTrue(Iterators.elementsEqual(a.iterator(), b.iterator())); 657 658 // The same, but with nulls. 659 a = asList(4, 8, null, 16, 23, 42); 660 b = asList(4, 8, null, 16, 23, 42); 661 assertTrue(Iterators.elementsEqual(a.iterator(), b.iterator())); 662 663 // Different Iterable types (still equal elements, though). 664 a = ImmutableList.of(4, 8, 15, 16, 23, 42); 665 b = asList(4, 8, 15, 16, 23, 42); 666 assertTrue(Iterators.elementsEqual(a.iterator(), b.iterator())); 667 668 // An element differs. 669 a = asList(4, 8, 15, 12, 23, 42); 670 b = asList(4, 8, 15, 16, 23, 42); 671 assertFalse(Iterators.elementsEqual(a.iterator(), b.iterator())); 672 673 // null versus non-null. 674 a = asList(4, 8, 15, null, 23, 42); 675 b = asList(4, 8, 15, 16, 23, 42); 676 assertFalse(Iterators.elementsEqual(a.iterator(), b.iterator())); 677 assertFalse(Iterators.elementsEqual(b.iterator(), a.iterator())); 678 679 // Different lengths. 680 a = asList(4, 8, 15, 16, 23); 681 b = asList(4, 8, 15, 16, 23, 42); 682 assertFalse(Iterators.elementsEqual(a.iterator(), b.iterator())); 683 assertFalse(Iterators.elementsEqual(b.iterator(), a.iterator())); 684 685 // Different lengths, one is empty. 686 a = Collections.emptySet(); 687 b = asList(4, 8, 15, 16, 23, 42); 688 assertFalse(Iterators.elementsEqual(a.iterator(), b.iterator())); 689 assertFalse(Iterators.elementsEqual(b.iterator(), a.iterator())); 690 } 691 692 public void testPartition_badSize() { 693 Iterator<Integer> source = Iterators.singletonIterator(1); 694 try { 695 Iterators.partition(source, 0); 696 fail(); 697 } catch (IllegalArgumentException expected) { 698 } 699 } 700 701 public void testPartition_empty() { 702 Iterator<Integer> source = Iterators.emptyIterator(); 703 Iterator<List<Integer>> partitions = Iterators.partition(source, 1); 704 assertFalse(partitions.hasNext()); 705 } 706 707 public void testPartition_singleton1() { 708 Iterator<Integer> source = Iterators.singletonIterator(1); 709 Iterator<List<Integer>> partitions = Iterators.partition(source, 1); 710 assertTrue(partitions.hasNext()); 711 assertTrue(partitions.hasNext()); 712 assertEquals(ImmutableList.of(1), partitions.next()); 713 assertFalse(partitions.hasNext()); 714 } 715 716 public void testPartition_singleton2() { 717 Iterator<Integer> source = Iterators.singletonIterator(1); 718 Iterator<List<Integer>> partitions = Iterators.partition(source, 2); 719 assertTrue(partitions.hasNext()); 720 assertTrue(partitions.hasNext()); 721 assertEquals(ImmutableList.of(1), partitions.next()); 722 assertFalse(partitions.hasNext()); 723 } 724 725 public void testPartition_view() { 726 List<Integer> list = asList(1, 2); 727 Iterator<List<Integer>> partitions 728 = Iterators.partition(list.iterator(), 1); 729 730 // Changes before the partition is retrieved are reflected 731 list.set(0, 3); 732 List<Integer> first = partitions.next(); 733 734 // Changes after are not 735 list.set(0, 4); 736 737 assertEquals(ImmutableList.of(3), first); 738 } 739 740 public void testPaddedPartition_badSize() { 741 Iterator<Integer> source = Iterators.singletonIterator(1); 742 try { 743 Iterators.paddedPartition(source, 0); 744 fail(); 745 } catch (IllegalArgumentException expected) { 746 } 747 } 748 749 public void testPaddedPartition_empty() { 750 Iterator<Integer> source = Iterators.emptyIterator(); 751 Iterator<List<Integer>> partitions = Iterators.paddedPartition(source, 1); 752 assertFalse(partitions.hasNext()); 753 } 754 755 public void testPaddedPartition_singleton1() { 756 Iterator<Integer> source = Iterators.singletonIterator(1); 757 Iterator<List<Integer>> partitions = Iterators.paddedPartition(source, 1); 758 assertTrue(partitions.hasNext()); 759 assertTrue(partitions.hasNext()); 760 assertEquals(ImmutableList.of(1), partitions.next()); 761 assertFalse(partitions.hasNext()); 762 } 763 764 public void testPaddedPartition_singleton2() { 765 Iterator<Integer> source = Iterators.singletonIterator(1); 766 Iterator<List<Integer>> partitions = Iterators.paddedPartition(source, 2); 767 assertTrue(partitions.hasNext()); 768 assertTrue(partitions.hasNext()); 769 assertEquals(asList(1, null), partitions.next()); 770 assertFalse(partitions.hasNext()); 771 } 772 773 public void testPaddedPartition_view() { 774 List<Integer> list = asList(1, 2); 775 Iterator<List<Integer>> partitions 776 = Iterators.paddedPartition(list.iterator(), 1); 777 778 // Changes before the PaddedPartition is retrieved are reflected 779 list.set(0, 3); 780 List<Integer> first = partitions.next(); 781 782 // Changes after are not 783 list.set(0, 4); 784 785 assertEquals(ImmutableList.of(3), first); 786 } 787 788 public void testPaddedPartitionRandomAccess() { 789 Iterator<Integer> source = asList(1, 2, 3).iterator(); 790 Iterator<List<Integer>> partitions = Iterators.paddedPartition(source, 2); 791 assertTrue(partitions.next() instanceof RandomAccess); 792 assertTrue(partitions.next() instanceof RandomAccess); 793 } 794 795 public void testForArrayEmpty() { 796 String[] array = new String[0]; 797 Iterator<String> iterator = Iterators.forArray(array); 798 assertFalse(iterator.hasNext()); 799 try { 800 iterator.next(); 801 fail(); 802 } catch (NoSuchElementException expected) {} 803 } 804 805 public void testForArrayTypical() { 806 String[] array = {"foo", "bar"}; 807 Iterator<String> iterator = Iterators.forArray(array); 808 assertTrue(iterator.hasNext()); 809 assertEquals("foo", iterator.next()); 810 assertTrue(iterator.hasNext()); 811 try { 812 iterator.remove(); 813 fail(); 814 } catch (UnsupportedOperationException expected) {} 815 assertEquals("bar", iterator.next()); 816 assertFalse(iterator.hasNext()); 817 try { 818 iterator.next(); 819 fail(); 820 } catch (NoSuchElementException expected) {} 821 } 822 823 public void testForArrayOffset() { 824 String[] array = {"foo", "bar", "cat", "dog"}; 825 Iterator<String> iterator = Iterators.forArray(array, 1, 2, 0); 826 assertTrue(iterator.hasNext()); 827 assertEquals("bar", iterator.next()); 828 assertTrue(iterator.hasNext()); 829 assertEquals("cat", iterator.next()); 830 assertFalse(iterator.hasNext()); 831 try { 832 Iterators.forArray(array, 2, 3, 0); 833 fail(); 834 } catch (IndexOutOfBoundsException expected) {} 835 } 836 837 public void testForArrayLength0() { 838 String[] array = {"foo", "bar"}; 839 assertFalse(Iterators.forArray(array, 0, 0, 0).hasNext()); 840 assertFalse(Iterators.forArray(array, 1, 0, 0).hasNext()); 841 assertFalse(Iterators.forArray(array, 2, 0, 0).hasNext()); 842 try { 843 Iterators.forArray(array, -1, 0, 0); 844 fail(); 845 } catch (IndexOutOfBoundsException expected) {} 846 try { 847 Iterators.forArray(array, 3, 0, 0); 848 fail(); 849 } catch (IndexOutOfBoundsException expected) {} 850 } 851 852 public void testForEnumerationEmpty() { 853 Enumeration<Integer> enumer = enumerate(); 854 Iterator<Integer> iter = Iterators.forEnumeration(enumer); 855 856 assertFalse(iter.hasNext()); 857 try { 858 iter.next(); 859 fail(); 860 } catch (NoSuchElementException expected) { 861 } 862 } 863 864 public void testForEnumerationSingleton() { 865 Enumeration<Integer> enumer = enumerate(1); 866 Iterator<Integer> iter = Iterators.forEnumeration(enumer); 867 868 assertTrue(iter.hasNext()); 869 assertTrue(iter.hasNext()); 870 assertEquals(1, (int) iter.next()); 871 try { 872 iter.remove(); 873 fail(); 874 } catch (UnsupportedOperationException expected) { 875 } 876 assertFalse(iter.hasNext()); 877 try { 878 iter.next(); 879 fail(); 880 } catch (NoSuchElementException expected) { 881 } 882 } 883 884 public void testForEnumerationTypical() { 885 Enumeration<Integer> enumer = enumerate(1, 2, 3); 886 Iterator<Integer> iter = Iterators.forEnumeration(enumer); 887 888 assertTrue(iter.hasNext()); 889 assertEquals(1, (int) iter.next()); 890 assertTrue(iter.hasNext()); 891 assertEquals(2, (int) iter.next()); 892 assertTrue(iter.hasNext()); 893 assertEquals(3, (int) iter.next()); 894 assertFalse(iter.hasNext()); 895 } 896 897 public void testAsEnumerationEmpty() { 898 Iterator<Integer> iter = Iterators.emptyIterator(); 899 Enumeration<Integer> enumer = Iterators.asEnumeration(iter); 900 901 assertFalse(enumer.hasMoreElements()); 902 try { 903 enumer.nextElement(); 904 fail(); 905 } catch (NoSuchElementException expected) { 906 } 907 } 908 909 public void testAsEnumerationSingleton() { 910 Iterator<Integer> iter = ImmutableList.of(1).iterator(); 911 Enumeration<Integer> enumer = Iterators.asEnumeration(iter); 912 913 assertTrue(enumer.hasMoreElements()); 914 assertTrue(enumer.hasMoreElements()); 915 assertEquals(1, (int) enumer.nextElement()); 916 assertFalse(enumer.hasMoreElements()); 917 try { 918 enumer.nextElement(); 919 fail(); 920 } catch (NoSuchElementException expected) { 921 } 922 } 923 924 public void testAsEnumerationTypical() { 925 Iterator<Integer> iter = ImmutableList.of(1, 2, 3).iterator(); 926 Enumeration<Integer> enumer = Iterators.asEnumeration(iter); 927 928 assertTrue(enumer.hasMoreElements()); 929 assertEquals(1, (int) enumer.nextElement()); 930 assertTrue(enumer.hasMoreElements()); 931 assertEquals(2, (int) enumer.nextElement()); 932 assertTrue(enumer.hasMoreElements()); 933 assertEquals(3, (int) enumer.nextElement()); 934 assertFalse(enumer.hasMoreElements()); 935 } 936 937 private static Enumeration<Integer> enumerate(Integer... ints) { 938 Vector<Integer> vector = new Vector<Integer>(); 939 vector.addAll(asList(ints)); 940 return vector.elements(); 941 } 942 943 public void testToString() { 944 Iterator<String> iterator = Lists.newArrayList("yam", "bam", "jam", "ham").iterator(); 945 assertEquals("[yam, bam, jam, ham]", Iterators.toString(iterator)); 946 } 947 948 public void testToStringWithNull() { 949 Iterator<String> iterator = Lists.newArrayList("hello", null, "world").iterator(); 950 assertEquals("[hello, null, world]", Iterators.toString(iterator)); 951 } 952 953 public void testToStringEmptyIterator() { 954 Iterator<String> iterator = Collections.<String>emptyList().iterator(); 955 assertEquals("[]", Iterators.toString(iterator)); 956 } 957 958 public void testLimit() { 959 List<String> list = newArrayList(); 960 try { 961 Iterators.limit(list.iterator(), -1); 962 fail("expected exception"); 963 } catch (IllegalArgumentException expected) { 964 // expected 965 } 966 967 assertFalse(Iterators.limit(list.iterator(), 0).hasNext()); 968 assertFalse(Iterators.limit(list.iterator(), 1).hasNext()); 969 970 list.add("cool"); 971 assertFalse(Iterators.limit(list.iterator(), 0).hasNext()); 972 assertEquals(list, newArrayList(Iterators.limit(list.iterator(), 1))); 973 assertEquals(list, newArrayList(Iterators.limit(list.iterator(), 2))); 974 975 list.add("pants"); 976 assertFalse(Iterators.limit(list.iterator(), 0).hasNext()); 977 assertEquals(ImmutableList.of("cool"), 978 newArrayList(Iterators.limit(list.iterator(), 1))); 979 assertEquals(list, newArrayList(Iterators.limit(list.iterator(), 2))); 980 assertEquals(list, newArrayList(Iterators.limit(list.iterator(), 3))); 981 } 982 983 public void testLimitRemove() { 984 List<String> list = newArrayList(); 985 list.add("cool"); 986 list.add("pants"); 987 Iterator<String> iterator = Iterators.limit(list.iterator(), 1); 988 iterator.next(); 989 iterator.remove(); 990 assertFalse(iterator.hasNext()); 991 assertEquals(1, list.size()); 992 assertEquals("pants", list.get(0)); 993 } 994 995 public void testGetNext_withDefault_singleton() { 996 Iterator<String> iterator = Collections.singletonList("foo").iterator(); 997 assertEquals("foo", Iterators.getNext(iterator, "bar")); 998 } 999 1000 public void testGetNext_withDefault_empty() { 1001 Iterator<String> iterator = Iterators.emptyIterator(); 1002 assertEquals("bar", Iterators.getNext(iterator, "bar")); 1003 } 1004 1005 public void testGetNext_withDefault_empty_null() { 1006 Iterator<String> iterator = Iterators.emptyIterator(); 1007 assertNull(Iterators.getNext(iterator, null)); 1008 } 1009 1010 public void testGetNext_withDefault_two() { 1011 Iterator<String> iterator = asList("foo", "bar").iterator(); 1012 assertEquals("foo", Iterators.getNext(iterator, "x")); 1013 } 1014 1015 public void testGetLast_basic() { 1016 List<String> list = newArrayList(); 1017 list.add("a"); 1018 list.add("b"); 1019 assertEquals("b", getLast(list.iterator())); 1020 } 1021 1022 public void testGetLast_exception() { 1023 List<String> list = newArrayList(); 1024 try { 1025 getLast(list.iterator()); 1026 fail(); 1027 } catch (NoSuchElementException expected) { 1028 } 1029 } 1030 1031 public void testGetLast_withDefault_singleton() { 1032 Iterator<String> iterator = Collections.singletonList("foo").iterator(); 1033 assertEquals("foo", Iterators.getLast(iterator, "bar")); 1034 } 1035 1036 public void testGetLast_withDefault_empty() { 1037 Iterator<String> iterator = Iterators.emptyIterator(); 1038 assertEquals("bar", Iterators.getLast(iterator, "bar")); 1039 } 1040 1041 public void testGetLast_withDefault_empty_null() { 1042 Iterator<String> iterator = Iterators.emptyIterator(); 1043 assertNull(Iterators.getLast(iterator, null)); 1044 } 1045 1046 public void testGetLast_withDefault_two() { 1047 Iterator<String> iterator = asList("foo", "bar").iterator(); 1048 assertEquals("bar", Iterators.getLast(iterator, "x")); 1049 } 1050 1051 public void testGet_basic() { 1052 List<String> list = newArrayList(); 1053 list.add("a"); 1054 list.add("b"); 1055 Iterator<String> iterator = list.iterator(); 1056 assertEquals("b", get(iterator, 1)); 1057 assertFalse(iterator.hasNext()); 1058 } 1059 1060 public void testGet_atSize() { 1061 List<String> list = newArrayList(); 1062 list.add("a"); 1063 list.add("b"); 1064 Iterator<String> iterator = list.iterator(); 1065 try { 1066 get(iterator, 2); 1067 fail(); 1068 } catch (IndexOutOfBoundsException expected) {} 1069 assertFalse(iterator.hasNext()); 1070 } 1071 1072 public void testGet_pastEnd() { 1073 List<String> list = newArrayList(); 1074 list.add("a"); 1075 list.add("b"); 1076 Iterator<String> iterator = list.iterator(); 1077 try { 1078 get(iterator, 5); 1079 fail(); 1080 } catch (IndexOutOfBoundsException expected) {} 1081 assertFalse(iterator.hasNext()); 1082 } 1083 1084 public void testGet_empty() { 1085 List<String> list = newArrayList(); 1086 Iterator<String> iterator = list.iterator(); 1087 try { 1088 get(iterator, 0); 1089 fail(); 1090 } catch (IndexOutOfBoundsException expected) {} 1091 assertFalse(iterator.hasNext()); 1092 } 1093 1094 public void testGet_negativeIndex() { 1095 List<String> list = newArrayList("a", "b", "c"); 1096 Iterator<String> iterator = list.iterator(); 1097 try { 1098 get(iterator, -1); 1099 fail(); 1100 } catch (IndexOutOfBoundsException expected) {} 1101 } 1102 1103 public void testGet_withDefault_basic() { 1104 List<String> list = newArrayList(); 1105 list.add("a"); 1106 list.add("b"); 1107 Iterator<String> iterator = list.iterator(); 1108 assertEquals("a", get(iterator, 0, "c")); 1109 assertTrue(iterator.hasNext()); 1110 } 1111 1112 public void testGet_withDefault_atSize() { 1113 List<String> list = newArrayList(); 1114 list.add("a"); 1115 list.add("b"); 1116 Iterator<String> iterator = list.iterator(); 1117 assertEquals("c", get(iterator, 2, "c")); 1118 assertFalse(iterator.hasNext()); 1119 } 1120 1121 public void testGet_withDefault_pastEnd() { 1122 List<String> list = newArrayList(); 1123 list.add("a"); 1124 list.add("b"); 1125 Iterator<String> iterator = list.iterator(); 1126 assertEquals("c", get(iterator, 3, "c")); 1127 assertFalse(iterator.hasNext()); 1128 } 1129 1130 public void testGet_withDefault_negativeIndex() { 1131 List<String> list = newArrayList(); 1132 list.add("a"); 1133 list.add("b"); 1134 Iterator<String> iterator = list.iterator(); 1135 try { 1136 get(iterator, -1, "c"); 1137 fail(); 1138 } catch (IndexOutOfBoundsException expected) { 1139 // pass 1140 } 1141 assertTrue(iterator.hasNext()); 1142 } 1143 1144 public void testAdvance_basic() { 1145 List<String> list = newArrayList(); 1146 list.add("a"); 1147 list.add("b"); 1148 Iterator<String> iterator = list.iterator(); 1149 advance(iterator, 1); 1150 assertEquals("b", iterator.next()); 1151 } 1152 1153 public void testAdvance_pastEnd() { 1154 List<String> list = newArrayList(); 1155 list.add("a"); 1156 list.add("b"); 1157 Iterator<String> iterator = list.iterator(); 1158 advance(iterator, 5); 1159 assertFalse(iterator.hasNext()); 1160 } 1161 1162 public void testAdvance_illegalArgument() { 1163 List<String> list = newArrayList("a", "b", "c"); 1164 Iterator<String> iterator = list.iterator(); 1165 try { 1166 advance(iterator, -1); 1167 fail(); 1168 } catch (IllegalArgumentException expected) {} 1169 } 1170 1171 public void testFrequency() { 1172 List<String> list = newArrayList("a", null, "b", null, "a", null); 1173 assertEquals(2, Iterators.frequency(list.iterator(), "a")); 1174 assertEquals(1, Iterators.frequency(list.iterator(), "b")); 1175 assertEquals(0, Iterators.frequency(list.iterator(), "c")); 1176 assertEquals(0, Iterators.frequency(list.iterator(), 4.2)); 1177 assertEquals(3, Iterators.frequency(list.iterator(), null)); 1178 } 1179 1180 public void testRemoveAll() { 1181 List<String> list = newArrayList("a", "b", "c", "d", "e"); 1182 assertTrue(Iterators.removeAll( 1183 list.iterator(), newArrayList("b", "d", "f"))); 1184 assertEquals(newArrayList("a", "c", "e"), list); 1185 assertFalse(Iterators.removeAll( 1186 list.iterator(), newArrayList("x", "y", "z"))); 1187 assertEquals(newArrayList("a", "c", "e"), list); 1188 } 1189 1190 public void testRemoveIf() { 1191 List<String> list = newArrayList("a", "b", "c", "d", "e"); 1192 assertTrue(Iterators.removeIf( 1193 list.iterator(), 1194 new Predicate<String>() { 1195 @Override 1196 public boolean apply(String s) { 1197 return s.equals("b") || s.equals("d") || s.equals("f"); 1198 } 1199 })); 1200 assertEquals(newArrayList("a", "c", "e"), list); 1201 assertFalse(Iterators.removeIf( 1202 list.iterator(), 1203 new Predicate<String>() { 1204 @Override 1205 public boolean apply(String s) { 1206 return s.equals("x") || s.equals("y") || s.equals("z"); 1207 } 1208 })); 1209 assertEquals(newArrayList("a", "c", "e"), list); 1210 } 1211 1212 public void testRetainAll() { 1213 List<String> list = newArrayList("a", "b", "c", "d", "e"); 1214 assertTrue(Iterators.retainAll( 1215 list.iterator(), newArrayList("b", "d", "f"))); 1216 assertEquals(newArrayList("b", "d"), list); 1217 assertFalse(Iterators.retainAll( 1218 list.iterator(), newArrayList("b", "e", "d"))); 1219 assertEquals(newArrayList("b", "d"), list); 1220 } 1221 1222 public void testConsumingIterator() { 1223 // Test data 1224 List<String> list = Lists.newArrayList("a", "b"); 1225 1226 // Test & Verify 1227 Iterator<String> consumingIterator = 1228 Iterators.consumingIterator(list.iterator()); 1229 1230 assertEquals("Iterators.consumingIterator(...)", consumingIterator.toString()); 1231 1232 assertThat(list).has().exactly("a", "b").inOrder(); 1233 1234 assertTrue(consumingIterator.hasNext()); 1235 assertThat(list).has().exactly("a", "b").inOrder(); 1236 assertEquals("a", consumingIterator.next()); 1237 assertThat(list).has().item("b"); 1238 1239 assertTrue(consumingIterator.hasNext()); 1240 assertEquals("b", consumingIterator.next()); 1241 assertThat(list).isEmpty(); 1242 1243 assertFalse(consumingIterator.hasNext()); 1244 } 1245 1246 public void testIndexOf_consumedData() { 1247 Iterator<String> iterator = 1248 Lists.newArrayList("manny", "mo", "jack").iterator(); 1249 assertEquals(1, Iterators.indexOf(iterator, Predicates.equalTo("mo"))); 1250 assertEquals("jack", iterator.next()); 1251 assertFalse(iterator.hasNext()); 1252 } 1253 1254 public void testIndexOf_consumedDataWithDuplicates() { 1255 Iterator<String> iterator = 1256 Lists.newArrayList("manny", "mo", "mo", "jack").iterator(); 1257 assertEquals(1, Iterators.indexOf(iterator, Predicates.equalTo("mo"))); 1258 assertEquals("mo", iterator.next()); 1259 assertEquals("jack", iterator.next()); 1260 assertFalse(iterator.hasNext()); 1261 } 1262 1263 public void testIndexOf_consumedDataNoMatch() { 1264 Iterator<String> iterator = 1265 Lists.newArrayList("manny", "mo", "mo", "jack").iterator(); 1266 assertEquals(-1, Iterators.indexOf(iterator, Predicates.equalTo("bob"))); 1267 assertFalse(iterator.hasNext()); 1268 } 1269 1270 @SuppressWarnings("deprecation") 1271 public void testUnmodifiableIteratorShortCircuit() { 1272 Iterator<String> mod = Lists.newArrayList("a", "b", "c").iterator(); 1273 UnmodifiableIterator<String> unmod = Iterators.unmodifiableIterator(mod); 1274 assertNotSame(mod, unmod); 1275 assertSame(unmod, Iterators.unmodifiableIterator(unmod)); 1276 assertSame(unmod, Iterators.unmodifiableIterator((Iterator<String>) unmod)); 1277 } 1278 1279 @SuppressWarnings("deprecation") 1280 public void testPeekingIteratorShortCircuit() { 1281 Iterator<String> nonpeek = Lists.newArrayList("a", "b", "c").iterator(); 1282 PeekingIterator<String> peek = Iterators.peekingIterator(nonpeek); 1283 assertNotSame(peek, nonpeek); 1284 assertSame(peek, Iterators.peekingIterator(peek)); 1285 assertSame(peek, Iterators.peekingIterator((Iterator<String>) peek)); 1286 } 1287 } 1288