Archived
0

исправление работы CompactedCoords

This commit is contained in:
2018-12-26 16:07:05 +03:00
parent 41efdda320
commit 467ef26468
3 changed files with 30 additions and 23 deletions

View File

@@ -67,6 +67,7 @@ subprojects {
/* Testing */ /* Testing */
testImplementation (group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: junit_version) testImplementation (group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: junit_version)
testRuntimeOnly(group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junit_version) testRuntimeOnly(group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: junit_version)
testImplementation(group: 'org.junit.jupiter', name: 'junit-jupiter-params', version: junit_version)
testCompile (group: 'org.slf4j', name: 'slf4j-simple', version: slf4j_version) testCompile (group: 'org.slf4j', name: 'slf4j-simple', version: slf4j_version)
testCompile (group: 'org.mockito', name: 'mockito-core', version: '1.10.19') testCompile (group: 'org.mockito', name: 'mockito-core', version: '1.10.19')
testCompile (group: 'org.springframework', name: 'spring-test', version: spring_version) testCompile (group: 'org.springframework', name: 'spring-test', version: spring_version)

View File

@@ -14,15 +14,9 @@ public class CompactedCoords {
} }
public static int[] uncompressXZ(int compactValue) { public static int[] uncompressXZ(int compactValue) {
//TODO не нравится мне такие костыли
return new int[]{ return new int[]{
(int)(short) (compactValue >> 16), compactValue >> 16,
(int)(short) (compactValue | 0xFFFF0000) (compactValue & 0x8000) > 0 ? compactValue | 0xFFFF0000 : compactValue & 0xFFFF
}; };
} }
private static int floor_double(double value) {
int i = (int)value;
return value < (double)i ? i - 1 : i;
}
} }

View File

@@ -1,25 +1,37 @@
package mc.core.utils; package mc.core.utils;
import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import java.util.concurrent.ThreadLocalRandom; import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertTrue;
class CompactedCoordsTest { class CompactedCoordsTest {
@Test private static Stream<Arguments> streamTestParams() {
void compressXZ() { return Stream.of(
ThreadLocalRandom random = ThreadLocalRandom.current(); Arguments.of(Short.MIN_VALUE, Short.MIN_VALUE),
Arguments.of(Short.MIN_VALUE, Short.MAX_VALUE),
Arguments.of(Short.MAX_VALUE, Short.MAX_VALUE),
Arguments.of(Short.MAX_VALUE, Short.MIN_VALUE),
Arguments.of(0, 0),
Arguments.of(-1, -1),
Arguments.of(-1, 1),
Arguments.of(1, 1),
Arguments.of(1, -1)
);
}
for (int i = 0; i < 100; i++) { @ParameterizedTest
final int x = random.nextInt(Short.MIN_VALUE, Short.MAX_VALUE); @MethodSource("streamTestParams")
final int z = random.nextInt(Short.MIN_VALUE, Short.MAX_VALUE); void testCompress(int x, int z) {
final int compressXZ = CompactedCoords.compressXZ(x, z);
int[] xz = CompactedCoords.uncompressXZ(compressXZ);
final int compressXZ = CompactedCoords.compressXZ(x, z); assertTrue(x == xz[0] && z == xz[1],
int[] xz = CompactedCoords.uncompressXZ(compressXZ); String.format("x = %d, vx = %d; z = %d, vz = %d",
x, xz[0],
assertEquals(x, xz[0]); z, xz[1]));
assertEquals(z, xz[1]);
}
} }
} }