|
21 | 21 | import static com.google.common.collect.Lists.newArrayList;
|
22 | 22 | import static com.google.common.truth.Truth.assertThat;
|
23 | 23 | import static java.util.Collections.nCopies;
|
| 24 | +import static org.junit.Assert.assertThrows; |
24 | 25 |
|
25 | 26 | import com.google.common.annotations.GwtCompatible;
|
26 | 27 | import com.google.common.annotations.GwtIncompatible;
|
@@ -498,4 +499,58 @@ public void testToStringImplWithNullEntries() throws Exception {
|
498 | 499 |
|
499 | 500 | assertEquals(list.toString(), Collections2.toStringImpl(list));
|
500 | 501 | }
|
| 502 | + |
| 503 | + public void testGetElementOutOfRangeWithSet() { |
| 504 | + assertThrows(IndexOutOfBoundsException.class, () -> Collections2.getElement(Sets.newHashSet(), -1)); |
| 505 | + assertThrows(IndexOutOfBoundsException.class, () -> Collections2.getElement(Sets.newHashSet(), 0)); |
| 506 | + assertThrows(IndexOutOfBoundsException.class, () -> Collections2.getElement(Sets.newHashSet(), 1)); |
| 507 | + assertThrows(IndexOutOfBoundsException.class, () -> Collections2.getElement(Sets.newHashSet("a"), -1)); |
| 508 | + assertThrows(IndexOutOfBoundsException.class, () -> Collections2.getElement(Sets.newHashSet("a"), 1)); |
| 509 | + assertThrows(IndexOutOfBoundsException.class, () -> Collections2.getElement(Sets.newHashSet("a"), -1)); |
| 510 | + assertThrows(IndexOutOfBoundsException.class, () -> Collections2.getElement(Sets.newHashSet("a", "b"), 2)); |
| 511 | + } |
| 512 | + |
| 513 | + public void testGetElementOutOfRangeWithList() { |
| 514 | + assertThrows(IndexOutOfBoundsException.class, () -> Collections2.getElement(Lists.newArrayList(), -1)); |
| 515 | + assertThrows(IndexOutOfBoundsException.class, () -> Collections2.getElement(Lists.newArrayList(), 0)); |
| 516 | + assertThrows(IndexOutOfBoundsException.class, () -> Collections2.getElement(Lists.newArrayList(), 1)); |
| 517 | + assertThrows(IndexOutOfBoundsException.class, () -> Collections2.getElement(Lists.newArrayList("a"), -1)); |
| 518 | + assertThrows(IndexOutOfBoundsException.class, () -> Collections2.getElement(Lists.newArrayList("a"), 1)); |
| 519 | + assertThrows(IndexOutOfBoundsException.class, () -> Collections2.getElement(Lists.newArrayList("a"), -1)); |
| 520 | + assertThrows(IndexOutOfBoundsException.class, () -> Collections2.getElement(Lists.newArrayList("a", "b"), 2)); |
| 521 | + } |
| 522 | + |
| 523 | + public void testGetElementFromSet() { |
| 524 | + assertThat(Collections2.getElement(Sets.newHashSet("a"), 0)).isEqualTo("a"); |
| 525 | + assertThat(Collections2.getElement(Sets.newHashSet("b"), 0)).isEqualTo("b"); |
| 526 | + assertThat(Collections2.getElement(Sets.newHashSet("a", "b"), 0)).isEqualTo("a"); |
| 527 | + assertThat(Collections2.getElement(Sets.newHashSet("a", "b"), 1)).isEqualTo("b"); |
| 528 | + assertThat(Collections2.getElement(Sets.newHashSet("b", "a"), 0)).isEqualTo("a"); |
| 529 | + assertThat(Collections2.getElement(Sets.newHashSet("b", "a"), 1)).isEqualTo("b"); |
| 530 | + } |
| 531 | + |
| 532 | + public void testGetElementFromList() { |
| 533 | + assertThat(Collections2.getElement(Lists.newArrayList("a"), 0)).isEqualTo("a"); |
| 534 | + assertThat(Collections2.getElement(Lists.newArrayList("a", "b"), 0)).isEqualTo("a"); |
| 535 | + assertThat(Collections2.getElement(Lists.newArrayList("a", "b"), 1)).isEqualTo("b"); |
| 536 | + assertThat(Collections2.getElement(Lists.newArrayList("b", "a"), 0)).isEqualTo("b"); |
| 537 | + assertThat(Collections2.getElement(Lists.newArrayList("b", "a"), 1)).isEqualTo("a"); |
| 538 | + } |
| 539 | + |
| 540 | + public void testGetElementFromImmutableSet() { |
| 541 | + assertThat(Collections2.getElement(ImmutableSet.of("a"), 0)).isEqualTo("a"); |
| 542 | + assertThat(Collections2.getElement(ImmutableSet.of("b"), 0)).isEqualTo("b"); |
| 543 | + assertThat(Collections2.getElement(ImmutableSet.of("a", "b"), 0)).isEqualTo("a"); |
| 544 | + assertThat(Collections2.getElement(ImmutableSet.of("a", "b"), 1)).isEqualTo("b"); |
| 545 | + assertThat(Collections2.getElement(ImmutableSet.of("b", "a"), 0)).isEqualTo("a"); |
| 546 | + assertThat(Collections2.getElement(ImmutableSet.of("b", "a"), 1)).isEqualTo("b"); |
| 547 | + } |
| 548 | + |
| 549 | + public void testGetElementFromImmutableList() { |
| 550 | + assertThat(Collections2.getElement(ImmutableList.of("a"), 0)).isEqualTo("a"); |
| 551 | + assertThat(Collections2.getElement(ImmutableList.of("a", "b"), 0)).isEqualTo("a"); |
| 552 | + assertThat(Collections2.getElement(ImmutableList.of("a", "b"), 1)).isEqualTo("b"); |
| 553 | + assertThat(Collections2.getElement(ImmutableList.of("b", "a"), 0)).isEqualTo("b"); |
| 554 | + assertThat(Collections2.getElement(ImmutableList.of("b", "a"), 1)).isEqualTo("a"); |
| 555 | + } |
501 | 556 | }
|
0 commit comments