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

@@ -14,15 +14,9 @@ public class CompactedCoords {
}
public static int[] uncompressXZ(int compactValue) {
//TODO не нравится мне такие костыли
return new int[]{
(int)(short) (compactValue >> 16),
(int)(short) (compactValue | 0xFFFF0000)
compactValue >> 16,
(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;
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 {
@Test
void compressXZ() {
ThreadLocalRandom random = ThreadLocalRandom.current();
private static Stream<Arguments> streamTestParams() {
return Stream.of(
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++) {
final int x = random.nextInt(Short.MIN_VALUE, Short.MAX_VALUE);
final int z = random.nextInt(Short.MIN_VALUE, Short.MAX_VALUE);
@ParameterizedTest
@MethodSource("streamTestParams")
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);
int[] xz = CompactedCoords.uncompressXZ(compressXZ);
assertEquals(x, xz[0]);
assertEquals(z, xz[1]);
}
assertTrue(x == xz[0] && z == xz[1],
String.format("x = %d, vx = %d; z = %d, vz = %d",
x, xz[0],
z, xz[1]));
}
}