diff options
author | Aaron <51387595+AzureAaron@users.noreply.github.com> | 2024-04-06 04:57:46 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-04-06 04:57:46 -0400 |
commit | b3623837bda701610868552005a25a5e5c148b8f (patch) | |
tree | 276c2edf10819583ef849e42f14b312bdc5ff375 /src/test/java | |
parent | eadb318304b25594ea12466626a2c834da1d20cf (diff) | |
download | Skyblocker-b3623837bda701610868552005a25a5e5c148b8f.tar.gz Skyblocker-b3623837bda701610868552005a25a5e5c148b8f.tar.bz2 Skyblocker-b3623837bda701610868552005a25a5e5c148b8f.zip |
Instanced Utils (#632)
* Instanced Utils
* Use default equals and toString
Diffstat (limited to 'src/test/java')
-rw-r--r-- | src/test/java/de/hysky/skyblocker/utils/InstancedUtilsTest.java | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/src/test/java/de/hysky/skyblocker/utils/InstancedUtilsTest.java b/src/test/java/de/hysky/skyblocker/utils/InstancedUtilsTest.java new file mode 100644 index 00000000..e5dbbff6 --- /dev/null +++ b/src/test/java/de/hysky/skyblocker/utils/InstancedUtilsTest.java @@ -0,0 +1,93 @@ +package de.hysky.skyblocker.utils; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +public class InstancedUtilsTest { + + @Test + void testSameInstanceEqual() { + Vector3i vec1 = new Vector3i(8, 8, 8); + + Assertions.assertEquals(vec1, vec1); + } + + @Test + void testSameFieldValuesEqual() { + Vector3i vec1 = new Vector3i(8, 8, 8); + Vector3i vec2 = new Vector3i(8, 8, 8); + + Assertions.assertEquals(vec1, vec2); + } + + @Test + void testDifferentFieldValuesEqual() { + Vector3i vec1 = new Vector3i(8, 8, 8); + Vector3i vec2 = new Vector3i(-8, -8, -8); + + Assertions.assertNotEquals(vec1, vec2); + } + + @Test + void testHashCodeOfEqualFieldValues() { + Vector3i vec1 = new Vector3i(8, 8, 8); + Vector3i vec2 = new Vector3i(8, 8, 8); + + Assertions.assertEquals(vec1.hashCode(), vec2.hashCode()); + } + + @Test + void testHashCodeOfDifferentFieldValues() { + Vector3i vec1 = new Vector3i(8, 8, 8); + Vector3i vec2 = new Vector3i(-8, -8, -8); + + Assertions.assertNotEquals(vec1.hashCode(), vec2.hashCode()); + } + + @Test + void testToString() { + Vector3i vec1 = new Vector3i(8, 8, 8); + + Assertions.assertEquals(vec1.toString(), "Vector3i[x=8, y=8, z=8]"); + } + + @SuppressWarnings("unused") + private static class Vector3i { + final int x; + final int y; + final int z; + + Vector3i(int x, int y, int z) { + this.x = x; + this.y = y; + this.z = z; + } + + @Override + public boolean equals(Object o) { + try { + return (boolean) InstancedUtils.equals(getClass()).invokeExact(this, o); + } catch (Throwable ignored) { + return super.equals(o); + } + } + + @Override + public int hashCode() { + try { + return (int) InstancedUtils.hashCode(getClass()).invokeExact(this); + } catch (Throwable ignored) { + return System.identityHashCode(this); + } + } + + @Override + public String toString() { + try { + return (String) InstancedUtils.toString(getClass()).invokeExact(this); + } catch (Throwable ignored) { + return super.toString(); + } + } + } +} |