Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for CopyUtils #62

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package io.github.bakedlibs.dough.collections;

import javax.annotation.Nonnull;

class DummyData implements Cloneable {

private final int value;

DummyData(int value) {
this.value = value;
}

private DummyData(@Nonnull DummyData other) {
this.value = other.value;
}

@Override
public @Nonnull DummyData clone() {
try {
super.clone();
} catch (CloneNotSupportedException ex) {
ex.printStackTrace();
}
return new DummyData(this);
}

@Override
public boolean equals(final Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;

DummyData dummyData = (DummyData) o;

return value == dummyData.value;
}

@Override
public int hashCode() {
return value;
}

@Override
public String toString() {
return "DummyData{" + "value=" + value + '}';
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package io.github.bakedlibs.dough.collections;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import javax.annotation.Nonnull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

class TestCopyUtils {

private static <T> void assertHaveEqualElements(@Nonnull Collection<T> c1, @Nonnull Collection<T> c2) {
Assertions.assertEquals(c1.size(), c2.size());
Assertions.assertTrue(c1.containsAll(c2));
}

private static <K, V> void assertHaveEqualElements(@Nonnull Map<K, V> m1, @Nonnull Map<K, V> m2) {
Assertions.assertEquals(m1.size(), m2.size());
for (Map.Entry<K, V> entry : m1.entrySet()) {
Assertions.assertEquals(m2.get(entry.getKey()), entry.getValue());
}
}

private static <T> void assertHaveClonedElements(@Nonnull Collection<T> c1, @Nonnull Collection<T> c2) {
Assertions.assertEquals(c1.size(), c2.size());
for (T t1 : c1) {
for (T t2 : c2) {
// If they are the same, it did not clone and thus we fail.
Assertions.assertNotSame(t1, t2);
if (t1.equals(t2)) {
// If they are equal, the element has been cloned, thus, we break and check the next element.
break;
}
}
}
}
Comment on lines +29 to +41
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Collections can be unordered, making this fail. This implementations only works for ordered/sorted collections, aka Lists.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would this fail if it was unordered? Maybe I'm just very sleepy but I can't seem to think of why this would fail if it wasn't ordered


private static <K, V> void assertHaveClonedElements(@Nonnull Map<K, V> m1, @Nonnull Map<K, V> m2) {
Assertions.assertEquals(m1.size(), m2.size());
for (Map.Entry<K, V> entry : m1.entrySet()) {
V otherValue = m2.get(entry.getKey());
// If they are the same, it did not clone and thus we fail.
Assertions.assertNotSame(entry.getValue(), otherValue);
if (entry.getValue().equals(otherValue)) {
// If they are equal, the element has been cloned, thus, we break and check the next element.
break;
}
}
}


@Test
@DisplayName("Test if collections are deeply cloned properly")
void testCloningCollections() {
Collection<DummyData> dummyCollection =
Arrays.asList(new DummyData(1), new DummyData(2), new DummyData(3));
Collection<DummyData> clonedCollection =
CopyUtils.deepCopy(dummyCollection, DummyData::clone, ArrayList::new);
assertHaveEqualElements(dummyCollection, clonedCollection);
assertHaveClonedElements(dummyCollection, clonedCollection);
}

@Test
@DisplayName("Test if maps are deeply cloned properly")
void testCloningMaps() {
Map<Integer, DummyData> dummyMap = new HashMap<>();
dummyMap.put(1, new DummyData(1));
dummyMap.put(2, new DummyData(2));
dummyMap.put(3, new DummyData(3));
Map<Integer, DummyData> clonedMap = CopyUtils.deepCopy(dummyMap, DummyData::clone, HashMap::new);
assertHaveEqualElements(dummyMap, clonedMap);
assertHaveClonedElements(dummyMap, clonedMap);
// We first perform a shallow copy from clonedMap
Map<Integer, DummyData> clonedMapDeepCopy = new HashMap<>(clonedMap);
// We then mutate the shallow copy and turn it into a deep copy.
CopyUtils.deepCopy(clonedMapDeepCopy, DummyData::clone);
assertHaveEqualElements(clonedMap, clonedMapDeepCopy);
assertHaveClonedElements(clonedMap, clonedMapDeepCopy);
}

@Test
@DisplayName("Test if arrays are deeply cloned properly")
void testCloningArrays() {
DummyData[] dummyArray = new DummyData[]{new DummyData(1), new DummyData(2), new DummyData(3)};
DummyData[] clonedArray = CopyUtils.deepCopy(dummyArray, DummyData::clone, DummyData[]::new);
assertHaveEqualElements(Arrays.asList(dummyArray), Arrays.asList(clonedArray));
assertHaveClonedElements(Arrays.asList(dummyArray), Arrays.asList(clonedArray));
DummyData[] deepClonedArray = new DummyData[clonedArray.length];
CopyUtils.deepCopy(clonedArray, DummyData::clone, deepClonedArray);
assertHaveEqualElements(Arrays.asList(dummyArray), Arrays.asList(deepClonedArray));
assertHaveClonedElements(Arrays.asList(dummyArray), Arrays.asList(deepClonedArray));
// Cannot clone if the length of the sink < length of source
DummyData[] invalidArray = new DummyData[clonedArray.length - 1];
Assertions.assertThrows(IllegalArgumentException.class, () -> CopyUtils.deepCopy(dummyArray, DummyData::clone, invalidArray));
try {
DummyData[] validArray = new DummyData[clonedArray.length + 1];
CopyUtils.deepCopy(dummyArray, DummyData::clone, validArray);
} catch (Exception ex) {
// Unexpected failure
Assertions.fail(ex);
}
}

}