Archived
0

доблены тесты для сравнения Location's

This commit is contained in:
2018-09-07 22:18:29 +03:00
parent ef7bde7138
commit ea1e159c87
4 changed files with 89 additions and 4 deletions

View File

@@ -8,6 +8,8 @@ import lombok.Getter;
import lombok.Setter;
import mc.core.world.World;
import java.util.Objects;
public class EntityLocation extends Location implements Cloneable {
@Getter
@Setter
@@ -31,4 +33,19 @@ public class EntityLocation extends Location implements Cloneable {
public EntityLocation clone() {
return (EntityLocation) super.clone();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
if (!super.equals(o)) return false;
EntityLocation that = (EntityLocation) o;
return Float.compare(that.yaw, yaw) == 0 &&
Float.compare(that.pitch, pitch) == 0;
}
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), yaw, pitch);
}
}

View File

@@ -13,6 +13,7 @@ import mc.core.world.chunk.ChunkSection;
import java.lang.ref.Reference;
import java.lang.ref.WeakReference;
import java.util.Objects;
public class Location implements Cloneable {
@Getter
@@ -88,4 +89,20 @@ public class Location implements Cloneable {
return null;
}
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
Location location = (Location) obj;
return Double.compare(location.x, x) == 0 &&
Double.compare(location.y, y) == 0 &&
Double.compare(location.z, z) == 0 &&
Objects.equals(refWorld.get(), location.refWorld.get());
}
@Override
public int hashCode() {
return Objects.hash(x, y, z, refWorld.get());
}
}

View File

@@ -1,19 +1,30 @@
package mc.core;
import mc.core.world.World;
import org.junit.Before;
import org.junit.Test;
import java.util.concurrent.ThreadLocalRandom;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertSame;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class TestEntityLocation {
private World mockWorld;
@Before
public void before() {
mockWorld = mock(World.class);
when(mockWorld.getName()).thenReturn("mock_world");
}
@Test
public void cloneTest() {
World dummyWorld = mock(World.class);
EntityLocation firstLocation = new EntityLocation(10, 20, 30, 40, 50, dummyWorld);
assertSame("Lost world reference before cloning", dummyWorld, firstLocation.getWorld());
EntityLocation firstLocation = new EntityLocation(10, 20, 30, 40, 50, mockWorld);
assertSame("Lost world reference before cloning", mockWorld, firstLocation.getWorld());
EntityLocation locationClone = firstLocation.clone();
assertEquals("X mismatch", firstLocation.getX(), locationClone.getX(), 0);
@@ -23,4 +34,25 @@ public class TestEntityLocation {
assertEquals("Yaw mismatch", firstLocation.getYaw(), locationClone.getYaw(), 0);
assertSame("World mismatch (accidental clone of the World object?)", firstLocation.getWorld(), locationClone.getWorld());
}
@Test
public void testEquals() {
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
final double minD = 0.0d, maxD = 10.0d;
final float minF = 0.0f, maxF = 359.9f;
final double x = rnd.nextDouble(minD, maxD);
final double y = rnd.nextDouble(minD, maxD);
final double z = rnd.nextDouble(minD, maxD);
final float yaw = rnd.nextFloat() * (maxF - minF) + minF;
final float pitch = rnd.nextFloat() * (maxF - minF) + minF;
EntityLocation loc1 = new EntityLocation(x, y, z, yaw, pitch, mockWorld);
EntityLocation loc2 = new EntityLocation(x, y, z, yaw, pitch, mockWorld);
assertEquals(loc1, loc2);
loc2 = new EntityLocation(x+3, y+1, z+2, yaw, pitch, mockWorld);
assertNotEquals(loc1, loc2);
loc2 = new EntityLocation(x, y, z, yaw+5, pitch-1, mockWorld);
assertNotEquals(loc1, loc2);
}
}

View File

@@ -5,7 +5,10 @@ import mc.core.world.chunk.Chunk;
import org.junit.Before;
import org.junit.Test;
import java.util.concurrent.ThreadLocalRandom;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.mockito.Mockito.*;
public class TestLocation {
@@ -121,4 +124,20 @@ public class TestLocation {
assertEquals(-2, chunk.getX());
assertEquals(-2, chunk.getZ());
}
@Test
public void testEquals() {
final ThreadLocalRandom rnd = ThreadLocalRandom.current();
final double minD = 0.0d, maxD = 10.0d;
final double x = rnd.nextDouble(minD, maxD);
final double y = rnd.nextDouble(minD, maxD);
final double z = rnd.nextDouble(minD, maxD);
Location loc1 = new Location(x, y, z, world);
Location loc2 = new Location(x, y, z, world);
assertEquals(loc1, loc2);
loc2 = new Location(x+3, y+1, z+2, world);
assertNotEquals(loc1, loc2);
}
}