Implement M5.4 bounded compact chunk ownership
This commit is contained in:
@@ -0,0 +1,181 @@
|
||||
#include "evo/world/chunks.hpp"
|
||||
#include <array>
|
||||
#include <cstdio>
|
||||
#include <limits>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
namespace {
|
||||
using namespace evo::world;
|
||||
void check(bool condition, std::source_location at = std::source_location::current()) {
|
||||
if (!condition) {
|
||||
throw std::runtime_error("world chunk line " + std::to_string(at.line()));
|
||||
}
|
||||
}
|
||||
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
|
||||
template <typename T> bool refused(const std::expected<T, ChunkError>& r, ChunkError e) {
|
||||
return !r && r.error() == e;
|
||||
}
|
||||
CellSample cell(std::uint16_t id, std::uint32_t fill) {
|
||||
const auto result = CellSample::create({id}, fill);
|
||||
CHECK(result);
|
||||
return *result;
|
||||
}
|
||||
void compact_layouts() {
|
||||
for (const auto count : {1U, 2U, 255U, 256U, 257U, 4096U}) {
|
||||
std::vector<CellSample> input;
|
||||
input.reserve(4096);
|
||||
for (std::uint32_t i = 0; i < 4096; ++i) {
|
||||
// Same material, distinct fill: palette identity must include both.
|
||||
input.push_back(cell(1, 1 + i % count));
|
||||
}
|
||||
auto compact = CompactCells::create(input);
|
||||
CHECK(compact);
|
||||
CHECK(compact->layout() == (count == 1
|
||||
? CellLayout::uniform
|
||||
: (count > 256 ? CellLayout::dense : CellLayout::palette)));
|
||||
CHECK(compact->payload_bytes() ==
|
||||
(count == 1 ? 0 : (count > 256 ? 16384 : 4096 + 4 * count)));
|
||||
for (std::uint32_t i = 0; i < 4096; ++i) {
|
||||
const auto value = compact->read(i);
|
||||
CHECK(value && *value == input[i]);
|
||||
}
|
||||
CHECK(refused(compact->read(4096), ChunkError::invalid_local));
|
||||
CHECK(refused(compact->read(std::numeric_limits<std::uint32_t>::max()),
|
||||
ChunkError::invalid_local));
|
||||
auto moved = std::move(*compact);
|
||||
CHECK(compact->payload_bytes() == 0 && compact->read(0)->material().value == 0);
|
||||
CHECK(moved.read(4095) && *moved.read(4095) == input[4095]);
|
||||
*compact = std::move(moved);
|
||||
// Explicitly test the documented empty-void moved-from guarantee.
|
||||
// NOLINTNEXTLINE(bugprone-use-after-move)
|
||||
CHECK(moved.payload_bytes() == 0 && moved.read(0)->fill() == 0);
|
||||
CHECK(*compact->read(0) == input[0]);
|
||||
}
|
||||
std::vector<CellSample> empty;
|
||||
CHECK(refused(CompactCells::create(empty), ChunkError::invalid_cells));
|
||||
empty.assign(4097, cell(0, 0));
|
||||
CHECK(refused(CompactCells::create(empty), ChunkError::invalid_cells));
|
||||
empty.resize(4096, cell(0, 0));
|
||||
auto uniform = CompactCells::create(empty);
|
||||
CHECK(uniform && uniform->layout() == CellLayout::uniform && uniform->payload_bytes() == 0);
|
||||
// Distinct material identities must survive, even with identical fill.
|
||||
for (std::uint32_t i = 0; i < 4096; ++i) {
|
||||
empty[i] = cell(static_cast<std::uint16_t>(1 + i % 3), 65535);
|
||||
}
|
||||
const auto materials = CompactCells::create(empty);
|
||||
CHECK(materials && materials->layout() == CellLayout::palette);
|
||||
for (std::uint32_t i = 0; i < 4096; ++i) {
|
||||
CHECK(*materials->read(i) == empty[i]);
|
||||
}
|
||||
}
|
||||
void bounded_store() {
|
||||
const auto minimum = ChunkStore::minimum_bytes(2);
|
||||
CHECK(minimum);
|
||||
auto made = ChunkStore::create({73}, 2, *minimum + 32768);
|
||||
CHECK(made);
|
||||
auto& store = **made;
|
||||
CHECK(store.size() == 0 && store.accounted_bytes() == *minimum);
|
||||
CHECK(refused(store.view({}), ChunkError::missing));
|
||||
const auto a = store.load({-1, 0, -1});
|
||||
CHECK(a);
|
||||
const auto first_bytes = store.accounted_bytes();
|
||||
const auto same = store.load({-1, 0, -1});
|
||||
CHECK(same && same->revision() == a->revision() && store.accounted_bytes() == first_bytes);
|
||||
const auto b = store.load({65535, -65536, 65535});
|
||||
CHECK(b);
|
||||
CHECK(store.size() == 2 && store.accounted_bytes() == first_bytes); // far uniform void
|
||||
CHECK(refused(store.load({7, 8, 9}), ChunkError::capacity));
|
||||
for (std::uint32_t i = 0; i < 4096; ++i) {
|
||||
const auto local = *local_from_ordinal(i, 16);
|
||||
const auto expected =
|
||||
base_sample({73}, {std::int64_t{-16} + local.x, local.y, std::int64_t{-16} + local.z});
|
||||
const auto actual = store.read(*a, local);
|
||||
CHECK(expected && actual && *expected == *actual);
|
||||
}
|
||||
CHECK(refused(store.read(*a, {16, 0, 0}), ChunkError::invalid_local));
|
||||
CHECK(store.evict(*a));
|
||||
CHECK(refused(store.read(*a, {}), ChunkError::stale_view));
|
||||
CHECK(refused(store.evict(*a), ChunkError::stale_view));
|
||||
CHECK(refused(store.mark_dirty(*a), ChunkError::stale_view));
|
||||
const auto reloaded = store.load({-1, 0, -1});
|
||||
CHECK(reloaded);
|
||||
CHECK(reloaded->revision() != a->revision());
|
||||
CHECK(refused(store.read(*a, {}), ChunkError::stale_view));
|
||||
CHECK(store.accounted_bytes() == first_bytes);
|
||||
CHECK(*store.read(*reloaded, {0, 0, 0}) == *base_sample({73}, {-16, 0, -16}));
|
||||
const auto dirty = store.mark_dirty(*reloaded);
|
||||
CHECK(dirty);
|
||||
CHECK(refused(store.read(*reloaded, {}), ChunkError::stale_view));
|
||||
CHECK(refused(store.evict(*dirty), ChunkError::dirty));
|
||||
CHECK(store.read(*dirty, {}));
|
||||
const auto repeated = store.mark_dirty(*dirty);
|
||||
CHECK(repeated && repeated->revision() == dirty->revision());
|
||||
CHECK(store.size() == 2 && store.accounted_bytes() == first_bytes);
|
||||
auto other = ChunkStore::create({73}, 2, *minimum + 32768);
|
||||
CHECK(other);
|
||||
CHECK(refused((*other)->read(*dirty, {}), ChunkError::stale_view));
|
||||
CHECK(refused((*other)->evict(*dirty), ChunkError::stale_view));
|
||||
CHECK(refused((*other)->mark_dirty(*dirty), ChunkError::stale_view));
|
||||
// Dirty state here is only a test pin, no modified geometry is discarded.
|
||||
}
|
||||
void budgets_and_rejection() {
|
||||
using E = ChunkError;
|
||||
CHECK(refused(ChunkStore::minimum_bytes(0), E::invalid_config));
|
||||
CHECK(refused(ChunkStore::minimum_bytes(std::numeric_limits<std::size_t>::max()),
|
||||
E::invalid_config));
|
||||
const auto minimum = *ChunkStore::minimum_bytes(3);
|
||||
CHECK(refused(ChunkStore::create({}, 3, minimum - 1), E::memory_budget));
|
||||
BaseDescriptor invalid{};
|
||||
invalid.generator_version = 2;
|
||||
CHECK(refused(ChunkStore::create(invalid, 3, minimum), E::invalid_descriptor));
|
||||
auto created = ChunkStore::create({}, 3, minimum);
|
||||
CHECK(created);
|
||||
auto& store = **created;
|
||||
const auto uniform = store.load({1000, -1000, 1000});
|
||||
CHECK(uniform);
|
||||
CHECK(store.accounted_bytes() == minimum);
|
||||
CHECK(refused(store.load({0, 0, 0}), E::memory_budget));
|
||||
CHECK(store.size() == 1 && store.accounted_bytes() == minimum && store.read(*uniform, {}));
|
||||
CHECK(refused(store.view({0, 0, 0}), E::missing));
|
||||
const auto base = generate_base_chunk({}, {});
|
||||
CHECK(base);
|
||||
const auto compact = CompactCells::create(*base);
|
||||
CHECK(compact);
|
||||
const auto needed = compact->payload_bytes();
|
||||
CHECK(needed > 0);
|
||||
auto exact = ChunkStore::create({}, 3, minimum + needed);
|
||||
CHECK(exact);
|
||||
const auto at = (*exact)->load({});
|
||||
CHECK(at);
|
||||
CHECK((*exact)->accounted_bytes() == minimum + needed);
|
||||
CHECK(refused((*exact)->load({1, 0, 0}), E::memory_budget));
|
||||
CHECK((*exact)->evict(*at));
|
||||
CHECK((*exact)->accounted_bytes() == minimum);
|
||||
CHECK((*exact)->load({1, 0, 0}));
|
||||
constexpr auto low = std::numeric_limits<std::int64_t>::min();
|
||||
constexpr auto high = std::numeric_limits<std::int64_t>::max();
|
||||
for (const auto bad : std::array<std::int64_t, 4>{low, high, -65537, 65536}) {
|
||||
for (const auto addr :
|
||||
{ChunkAddress{bad, 0, 0}, ChunkAddress{0, bad, 0}, ChunkAddress{0, 0, bad}}) {
|
||||
CHECK(refused(store.load(addr), E::invalid_address));
|
||||
CHECK(refused(store.view(addr), E::invalid_address));
|
||||
}
|
||||
}
|
||||
CHECK(store.size() == 1 && store.accounted_bytes() == minimum);
|
||||
}
|
||||
} // namespace
|
||||
int main() try {
|
||||
compact_layouts();
|
||||
bounded_store();
|
||||
budgets_and_rejection();
|
||||
return 0;
|
||||
} catch (const std::exception& e) {
|
||||
std::fprintf(stderr, "%s\n", e.what());
|
||||
return 1;
|
||||
} catch (...) {
|
||||
return 1;
|
||||
}
|
||||
Reference in New Issue
Block a user