From 8fbce4fd48af44faef8345b879f3c45e959f6eaa Mon Sep 17 00:00:00 2001 From: daoge_cmd <3523206925@qq.com> Date: Sat, 18 May 2024 16:18:15 +0800 Subject: [PATCH] test: more tests for util classes --- .../allaymc/api/utils/AllayNbtUtilsTest.java | 20 +++++ .../java/org/allaymc/api/utils/UtilsTest.java | 14 +++ .../allaymc/server/utils/HashUtilsTest.java | 85 ++++++++++--------- 3 files changed, 79 insertions(+), 40 deletions(-) diff --git a/Allay-Server/src/test/java/org/allaymc/api/utils/AllayNbtUtilsTest.java b/Allay-Server/src/test/java/org/allaymc/api/utils/AllayNbtUtilsTest.java index 07fc866c0..dddeadab9 100644 --- a/Allay-Server/src/test/java/org/allaymc/api/utils/AllayNbtUtilsTest.java +++ b/Allay-Server/src/test/java/org/allaymc/api/utils/AllayNbtUtilsTest.java @@ -27,4 +27,24 @@ void base64ToNbt() { var nbtMap = AllayNbtUtils.base64ToNbt(NBT_BASE_64); assertTrue(nbtMap.getBoolean("testFlag")); } + + @Test + void testWriteVector3f() { + var nbt = NbtMap.builder(); + AllayNbtUtils.writeVector3f(nbt, "test", "x", "y", "z", new org.joml.Vector3f(1, 2, 3)); + var nbtMap = nbt.build(); + assertEquals(1, nbtMap.getCompound("test").getFloat("x")); + assertEquals(2, nbtMap.getCompound("test").getFloat("y")); + assertEquals(3, nbtMap.getCompound("test").getFloat("z")); + } + + @Test + void testReadVector3f() { + var nbt = NbtMap.builder(); + nbt.putCompound("test", NbtMap.builder().putFloat("x", 1).putFloat("y", 2).putFloat("z", 3).build()); + var vector3f = AllayNbtUtils.readVector3f(nbt.build(), "test", "x", "y", "z"); + assertEquals(1, vector3f.x); + assertEquals(2, vector3f.y); + assertEquals(3, vector3f.z); + } } \ No newline at end of file diff --git a/Allay-Server/src/test/java/org/allaymc/api/utils/UtilsTest.java b/Allay-Server/src/test/java/org/allaymc/api/utils/UtilsTest.java index 954b5d371..160a18c9d 100644 --- a/Allay-Server/src/test/java/org/allaymc/api/utils/UtilsTest.java +++ b/Allay-Server/src/test/java/org/allaymc/api/utils/UtilsTest.java @@ -1,5 +1,7 @@ package org.allaymc.api.utils; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -25,4 +27,16 @@ void testComputeRequiredBits() { assertEquals(6, Utils.computeRequiredBits(32)); assertEquals(6, Utils.computeRequiredBits(33)); } + + @Test + void testAppendBytes() { + assertEquals(new String(new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9}), new String(Utils.appendBytes(new byte[]{1, 2, 3}, new byte[]{4, 5, 6}, new byte[]{7, 8, 9}))); + } + + @Test + void testConvertByteBuf2Array() { + byte[] bytes = new byte[]{1, 2, 3, 4, 5, 6, 7, 8, 9}; + ByteBuf buf = Unpooled.wrappedBuffer(bytes); + assertEquals(new String(bytes), new String(Utils.convertByteBuf2Array(buf))); + } } diff --git a/Allay-Server/src/test/java/org/allaymc/server/utils/HashUtilsTest.java b/Allay-Server/src/test/java/org/allaymc/server/utils/HashUtilsTest.java index 30e620845..250f0b930 100644 --- a/Allay-Server/src/test/java/org/allaymc/server/utils/HashUtilsTest.java +++ b/Allay-Server/src/test/java/org/allaymc/server/utils/HashUtilsTest.java @@ -17,55 +17,60 @@ @ExtendWith({AllayTestExtension.class}) public class HashUtilsTest { - // TODO: Needs adaptation, as wood type has been separated since 1.20.70 -// static BlockState testBlockState; -// static final int testBlockStateHash = -1715809305; -// -// @BeforeAll -// static void before() { -// testBlockState = OAK_WOOD_TYPE.getDefaultState(); -// } -// -// @Test -// public void testComputeBlockStateHash1() { -// var value = HashUtils.computeBlockStateHash(testBlockState.getBlockType().getIdentifier(), new ArrayList<>(testBlockState.getPropertyValues().values())); -// Assertions.assertEquals(testBlockStateHash, value); -// } -// -// @Test -// public void testComputeBlockStateHash2() { -// BlockPropertyType.BlockPropertyValue[] array = testBlockState.getPropertyValues().values().toArray(BlockPropertyType.BlockPropertyValue[]::new); -// var value = HashUtils.computeBlockStateHash(testBlockState.getBlockType().getIdentifier(), array); -// Assertions.assertEquals(testBlockStateHash, value); -// } -// -// @Test -// public void test_fnv1a_32_nbt() { -// NbtMap tag = NbtMap.builder().putString("name", "minecraft:wood") -// .putCompound("states", NbtMap.builder() -// .putString("pillar_axis", "x") -// .putByte("stripped_bit", (byte) 0) -// .putString("wood_type", "acacia") -// .build()) -// .build(); -// Assertions.assertEquals(testBlockStateHash, HashUtils.fnv1a_32_nbt(tag)); -// } + static BlockState testBlockState; + static final int testBlockStateHash = 567193200; + + @BeforeAll + static void before() { + testBlockState = OAK_WOOD_TYPE.getDefaultState(); + } + + @Test + public void testComputeBlockStateHash1() { + var value = HashUtils.computeBlockStateHash(testBlockState.getBlockType().getIdentifier(), new ArrayList<>(testBlockState.getPropertyValues().values())); + Assertions.assertEquals(testBlockStateHash, value); + } + + @Test + public void testComputeBlockStateHash2() { + BlockPropertyType.BlockPropertyValue[] array = testBlockState.getPropertyValues().values().toArray(BlockPropertyType.BlockPropertyValue[]::new); + var value = HashUtils.computeBlockStateHash(testBlockState.getBlockType().getIdentifier(), array); + Assertions.assertEquals(testBlockStateHash, value); + } @Test - public void test_hashXZ() { + public void testFnv1a32Nbt() { + NbtMap tag = NbtMap.builder().putString("name", "minecraft:oak_wood") + .putCompound("states", NbtMap.builder() + .putString("pillar_axis", "x") + .build()) + .build(); + Assertions.assertEquals(testBlockStateHash, HashUtils.fnv1a_32_nbt(tag)); + } + + @Test + public void testHashXZ() { long l = HashUtils.hashXZ(13, 53); Assertions.assertEquals(55834574901L, l); } @Test - public void test_getXFromHashXZ() { - int x = HashUtils.getXFromHashXZ(55834574901L); - Assertions.assertEquals(13, x); + public void testGetXZFromHashXZ() { + Assertions.assertEquals(13, HashUtils.getXFromHashXZ(55834574901L)); + Assertions.assertEquals(53, HashUtils.getZFromHashXZ(55834574901L)); } @Test - public void test_getZFromHashXZ() { - int z = HashUtils.getZFromHashXZ(55834574901L); - Assertions.assertEquals(53, z); + public void testHashChunkXYZ() { + int l = HashUtils.hashChunkXYZ(8, 64, 8); + Assertions.assertEquals(-2147482616, l); } + + @Test + public void testGetXYZFromHashChunkXYZ() { + Assertions.assertEquals(8, HashUtils.getXFromHashChunkXYZ(-2147482616)); + Assertions.assertEquals(64, HashUtils.getYFromHashChunkXYZ(-2147482616)); + Assertions.assertEquals(8, HashUtils.getZFromHashChunkXYZ(-2147482616)); + } + }