-
Notifications
You must be signed in to change notification settings - Fork 31
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
md5sha256
wants to merge
8
commits into
main
Choose a base branch
from
tests/copy-utils
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
24e1a1b
Add unit tests for CopyUtils
md5sha256 025f787
Merge branch 'main' into tests/copy-utils
md5sha256 dc88fb3
Make DummyData package-private
md5sha256 ba66f66
Add unit tests for CopyUtils
md5sha256 2aea412
Make DummyData package-private
md5sha256 600ba51
Merge branch 'tests/copy-utils' of https://github.com/baked-libs/doug…
md5sha256 3d042aa
Address PR changes
md5sha256 a94b916
Merge branch 'main' into tests/copy-utils
TheBusyBiscuit File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
49 changes: 49 additions & 0 deletions
49
dough-data/src/test/java/io/github/bakedlibs/dough/collections/DummyData.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + '}'; | ||
} | ||
|
||
} |
109 changes: 109 additions & 0 deletions
109
dough-data/src/test/java/io/github/bakedlibs/dough/collections/TestCopyUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} | ||
} | ||
|
||
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); | ||
} | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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