Skip to content

Commit

Permalink
test: more tests for util classes
Browse files Browse the repository at this point in the history
  • Loading branch information
smartcmd committed May 18, 2024
1 parent 027b1c8 commit 8fbce4f
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
14 changes: 14 additions & 0 deletions Allay-Server/src/test/java/org/allaymc/api/utils/UtilsTest.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

}

0 comments on commit 8fbce4f

Please sign in to comment.