From 6da744ffb179caaab00f02b44b5bde4365f0783d Mon Sep 17 00:00:00 2001 From: DualFroz Date: Fri, 28 Aug 2026 16:06:26 +0200 Subject: [PATCH] test: add tests for region-file chunk storage in 32x32 chunk pages --- tests/testRegion.cpp | 89 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 89 insertions(+) create mode 100644 tests/testRegion.cpp diff --git a/tests/testRegion.cpp b/tests/testRegion.cpp new file mode 100644 index 0000000..4cbb077 --- /dev/null +++ b/tests/testRegion.cpp @@ -0,0 +1,89 @@ +#include "testHarness.h" +#include "vf/Region.h" + +using namespace vf; + +namespace { + +Chunk patternChunk(int seed) { + Chunk chunk; + for (int x = 0; x < chunkSize; ++x) + for (int y = 0; y < chunkSize; ++y) + for (int z = 0; z < chunkSize; ++z) { + int v = (x + y * 2 + z * 3 + seed) % static_cast(Block::Count); + chunk.set(x, y, z, static_cast(v)); + } + return chunk; +} + +} // namespace + +VF_TEST(regionCoordFloorsNegatives) { + CHECK(regionForChunk({0, 0, 0}) == (RegionCoord{0, 0})); + CHECK(regionForChunk({15, 4, 15}) == (RegionCoord{0, 0})); + CHECK(regionForChunk({16, 0, 0}) == (RegionCoord{1, 0})); + CHECK(regionForChunk({-1, 0, -1}) == (RegionCoord{-1, -1})); + CHECK(regionForChunk({-16, 0, -17}) == (RegionCoord{-1, -2})); +} + +VF_TEST(regionPutGetRoundTrips) { + Region region({0, 0}); + Chunk chunk = patternChunk(7); + region.putChunk({1, 2, 3}, chunk); + CHECK(region.hasChunk({1, 2, 3})); + CHECK(!region.hasChunk({4, 4, 4})); + + Chunk back; + CHECK(region.getChunk({1, 2, 3}, back)); + CHECK(back.raw() == chunk.raw()); + CHECK(!region.getChunk({9, 9, 9}, back)); +} + +VF_TEST(regionSerializeRoundTrips) { + Region region({2, -1}); + Chunk a = patternChunk(1), b = patternChunk(2), c = patternChunk(3); + region.putChunk({0, 0, 0}, a); + region.putChunk({0, 5, 1}, b); + region.putChunk({3, 1, 2}, c); + + std::vector bytes = region.serialize(); + Region restored; + CHECK(Region::deserialize(bytes.data(), bytes.size(), restored)); + CHECK(restored.location() == (RegionCoord{2, -1})); + CHECK_EQ(restored.chunkCount(), static_cast(3)); + + Chunk out; + CHECK(restored.getChunk({0, 5, 1}, out)); + CHECK(out.raw() == b.raw()); + CHECK(restored.getChunk({3, 1, 2}, out)); + CHECK(out.raw() == c.raw()); +} + +VF_TEST(regionRejectsBadInput) { + Region region({0, 0}); + region.putChunk({0, 0, 0}, patternChunk(4)); + std::vector bytes = region.serialize(); + + Region out; + bytes[0] ^= 0xff; + CHECK(!Region::deserialize(bytes.data(), bytes.size(), out)); + bytes[0] ^= 0xff; + CHECK(!Region::deserialize(bytes.data(), bytes.size() / 2, out)); // truncated +} + +VF_TEST(regionWorldSplitCoversMultipleRegions) { + World world; + world.setBlock(1, 1, 1, Block::Stone); // chunk (0,0,0) -> region (0,0) + world.setBlock(20 * chunkSize, 0, 0, Block::Grass); // chunk (20,0,0) -> region (1,0) + world.setBlock(-1, 40, -1, Block::Sand); // chunk (-1,2,-1) -> region (-1,-1) + + auto regions = splitWorldIntoRegions(world); + CHECK(regions.size() >= 3); + + std::vector blob = serializeRegions(world); + World restored; + CHECK(deserializeRegions(blob.data(), blob.size(), restored)); + CHECK(restored.blockAt(1, 1, 1) == Block::Stone); + CHECK(restored.blockAt(20 * chunkSize, 0, 0) == Block::Grass); + CHECK(restored.blockAt(-1, 40, -1) == Block::Sand); +}