diff --git a/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java b/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java index b2b394bbeb..73cfd8e1e5 100644 --- a/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java @@ -38,6 +38,7 @@ import java.util.LinkedList; import java.util.List; import java.util.Set; +import java.util.Objects; import org.apache.commons.collections4.bag.HashBag; import org.apache.commons.lang3.StringUtils; @@ -300,6 +301,92 @@ public void testDuplicatSetAllSameInDeque() { assertEquals(new HashSet<>(Arrays.asList(5)), IterableUtils.duplicateSet(input)); } + @Test + public void testDuplicateListMixedElementTypes() { + final List input = Arrays.asList(1, "a", 2, "a", 3, "b", "b"); + final List expected = Arrays.asList("a", "b"); + assertEquals(expected, IterableUtils.duplicateList(input)); + } + + @Test + public void testDuplicateListNestedCollections() { + final List> input = Arrays.asList( + Arrays.asList(1, 2, 3), + Arrays.asList(1, 2, 3), + Arrays.asList(4, 5, 6), + Arrays.asList(4, 5, 6) + ); + final List> expected = Arrays.asList( + Arrays.asList(1, 2, 3), + Arrays.asList(4, 5, 6) + ); + assertEquals(expected, IterableUtils.duplicateList(input)); + } + + @Test + public void testDuplicateListCustomObjects() { + class Person { + String name; + int age; + + Person(String name, int age) { + this.name = name; + this.age = age; + } + + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + Person person = (Person) o; + return age == person.age && name.equals(person.name); + } + + @Override + public int hashCode() { + return Objects.hash(name, age); + } + } + + final List input = Arrays.asList( + new Person("Linus", 30), + new Person("Torvalds", 25), + new Person("Linus", 30), + new Person("Tim", 35), + new Person("Torvalds", 25) + ); + final List expected = Arrays.asList( + new Person("Linus", 30), + new Person("Torvalds", 25) + ); + assertEquals(expected, IterableUtils.duplicateList(input)); + } + + @Test + public void testDuplicateListLargeScaleData() { + final List input = new ArrayList<>(); + for (int i = 0; i < 1000; i++) { + input.add(i % 100); + } + final List expected = new ArrayList<>(); + for (int i = 0; i < 100; i++) { + expected.add(i); + } + final List actual = IterableUtils.duplicateList(input); + assertEquals(expected, actual); + } + + @Test + public void testDuplicateListImmutableCollections() { + final List input = Collections.unmodifiableList(Arrays.asList(1, 2, 3, 1, 2, 4)); + final List expected = Arrays.asList(1, 2); + assertEquals(expected, IterableUtils.duplicateList(input)); + } + @Test public void testFind() { Predicate testPredicate = equalPredicate(4);