Skip to content

Commit

Permalink
Add more Comprehensive tests IterableUtils.duplicateList
Browse files Browse the repository at this point in the history
- Mixed element types
- Nested collections
- Custom objects
- Large-scale data
- Immutable collections
  • Loading branch information
hemanth0525 committed Sep 8, 2024
1 parent 675af44 commit bcb765b
Showing 1 changed file with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -300,6 +301,92 @@ public void testDuplicatSetAllSameInDeque() {
assertEquals(new HashSet<>(Arrays.asList(5)), IterableUtils.duplicateSet(input));
}

@Test
public void testDuplicateListMixedElementTypes() {
final List<Object> input = Arrays.asList(1, "a", 2, "a", 3, "b", "b");
final List<Object> expected = Arrays.asList("a", "b");
assertEquals(expected, IterableUtils.duplicateList(input));
}

@Test
public void testDuplicateListNestedCollections() {
final List<List<Integer>> 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<List<Integer>> 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<Person> 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<Person> expected = Arrays.asList(
new Person("Linus", 30),
new Person("Torvalds", 25)
);
assertEquals(expected, IterableUtils.duplicateList(input));
}

@Test
public void testDuplicateListLargeScaleData() {
final List<Integer> input = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
input.add(i % 100);
}
final List<Integer> expected = new ArrayList<>();
for (int i = 0; i < 100; i++) {
expected.add(i);
}
final List<Integer> actual = IterableUtils.duplicateList(input);
assertEquals(expected, actual);
}

@Test
public void testDuplicateListImmutableCollections() {
final List<Integer> input = Collections.unmodifiableList(Arrays.asList(1, 2, 3, 1, 2, 4));
final List<Integer> expected = Arrays.asList(1, 2);
assertEquals(expected, IterableUtils.duplicateList(input));
}

@Test
public void testFind() {
Predicate<Number> testPredicate = equalPredicate(4);
Expand Down

0 comments on commit bcb765b

Please sign in to comment.