From 277063fceb14bdff34c5ee4a574b6a4fbd7813e6 Mon Sep 17 00:00:00 2001 From: HEMANTH M <128402149+hemanth0525@users.noreply.github.com> Date: Thu, 5 Sep 2024 00:02:01 +0530 Subject: [PATCH] Add diverse test cases for IterableUtils.duplicateList: - Mixed element types - Nested collections - Custom objects - Large-scale data - Immutable collections --- .../commons/collections4/ListUtils.java | 33 ------- .../collections4/IterableUtilsTest.java | 87 +++++++++++++++++++ .../commons/collections4/ListUtilsTest.java | 43 --------- 3 files changed, 87 insertions(+), 76 deletions(-) diff --git a/src/main/java/org/apache/commons/collections4/ListUtils.java b/src/main/java/org/apache/commons/collections4/ListUtils.java index fd50c05267..04744234ac 100644 --- a/src/main/java/org/apache/commons/collections4/ListUtils.java +++ b/src/main/java/org/apache/commons/collections4/ListUtils.java @@ -24,7 +24,6 @@ import java.util.Iterator; import java.util.List; import java.util.Objects; -import java.util.Set; import org.apache.commons.collections4.bag.HashBag; import org.apache.commons.collections4.functors.DefaultEquator; @@ -174,38 +173,6 @@ public static List fixedSizeList(final List list) { return FixedSizeList.fixedSizeList(list); } - /** - * Finds and returns a list of duplicate elements in the given list. - *

- * This method uses two sets: one for tracking seen elements and one for - * collecting duplicates. It iterates through the list once and collects - * duplicates in a result list. - *

- * - * @param the type of elements in the list - * @param list the list to check for duplicates, must not be null - * @return a list of duplicate elements, or an empty list if no duplicates are found - * @throws NullPointerException if the list is null - * @since 4.5.0-M3 - */ - public static List findDuplicates(final List list) { - Objects.requireNonNull(list, "The input list must not be null."); - - Set seen = new HashSet<>(); - Set duplicates = new HashSet<>(); - List result = new ArrayList<>(); - - for (E element : list) { - if (!seen.add(element)) { - if (duplicates.add(element)) { - result.add(element); - } - } - } - - return result; - } - /** * Gets the first element of a list. *

diff --git a/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java b/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java index b2b394bbeb..6af4da29e7 100644 --- a/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/IterableUtilsTest.java @@ -37,6 +37,7 @@ import java.util.LinkedHashSet; import java.util.LinkedList; import java.util.List; +import java.util.Objects; import java.util.Set; import org.apache.commons.collections4.bag.HashBag; @@ -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); diff --git a/src/test/java/org/apache/commons/collections4/ListUtilsTest.java b/src/test/java/org/apache/commons/collections4/ListUtilsTest.java index 3403f06dc9..70c0b3426b 100644 --- a/src/test/java/org/apache/commons/collections4/ListUtilsTest.java +++ b/src/test/java/org/apache/commons/collections4/ListUtilsTest.java @@ -103,49 +103,6 @@ public void testEquals() { assertFalse(ListUtils.isEqualList(list1, list2)); } - @Test - public void testFindDuplicatesWithDuplicates() { - List input = Arrays.asList(1, 2, 3, 2, 4, 5, 3); - List expected = Arrays.asList(2, 3); - List actual = ListUtils.findDuplicates(input); - assertEquals(expected, actual, "The list should contain only the duplicate elements."); - } - - @Test - public void testFindDuplicatesNoDuplicates() { - List input = Arrays.asList(1, 2, 3, 4, 5); - assertTrue(ListUtils.findDuplicates(input).isEmpty(), "The list should be empty as the input list has no duplicates."); - } - - @Test - public void testFindDuplicatesEmptyList() { - List input = Arrays.asList(); - assertTrue(ListUtils.findDuplicates(input).isEmpty(), "The list should be empty as the input list is empty."); - } - - @Test - public void testFindDuplicatesSingleElement() { - List input = Arrays.asList(1); - assertTrue(ListUtils.findDuplicates(input).isEmpty(), "The list should be empty as there is only one element."); - } - - - @Test - public void testFindDuplicatesMultipleDuplicates() { - List input = Arrays.asList(1, 1, 2, 2, 3, 3, 4, 4); - List expected = Arrays.asList(1, 2, 3, 4); - List actual = ListUtils.findDuplicates(input); - assertEquals(expected, actual, "The list should contain all duplicate elements."); - } - - @Test - public void testFindDuplicatesAllSameElements() { - List input = Arrays.asList(5, 5, 5, 5); - List expected = Arrays.asList(5); - List actual = ListUtils.findDuplicates(input); - assertEquals(expected, actual, "The list should contain the single element as all elements are the same."); - } - @Test public void testGetFirst() { assertEquals(a, ListUtils.getFirst(fullList));