Files
aevolution/tests/world_coordinates_tests.cpp

151 lines
6.6 KiB
C++

#include "evo/world/coordinates.hpp"
#include <array>
#include <cstdio>
#include <limits>
#include <source_location>
#include <stdexcept>
#include <string>
namespace {
using namespace evo::world;
void check(bool condition, std::source_location at = std::source_location::current()) {
if (!condition) {
throw std::runtime_error("world coordinate line " + std::to_string(at.line()));
}
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
template <typename T> bool refused(const std::expected<T, WorldError>& result, WorldError error) {
return !result && result.error() == error;
}
constexpr std::array<std::uint32_t, 4> edges{8, 16, 32, 64};
void independent_interval_oracle() {
// Enumerate known half-open chunk intervals rather than deriving an expected
// quotient using the same division as the implementation. Pair different
// signed axes so a swapped/reused coordinate is observable.
for (const auto edge : edges) {
for (std::int64_t chunk = -20; chunk <= 20; ++chunk) {
for (std::uint32_t local = 0; local < edge; ++local) {
const ChunkLocation expected{{chunk, -chunk, chunk + 3},
{local, edge - 1 - local, (local + 1) % edge}};
const VoxelAddress voxel{chunk * edge + local, -chunk * edge + (edge - 1 - local),
(chunk + 3) * edge + ((local + 1) % edge)};
const auto actual = split(voxel, edge);
CHECK(actual && *actual == expected);
const auto rebuilt = join(expected, edge);
CHECK(rebuilt && *rebuilt == voxel);
}
}
}
const auto seam = split({-17, -16, -1}, 16);
CHECK(seam && *seam == ChunkLocation{{-2, -1, -1}, {15, 0, 15}});
}
void every_ordinal_has_one_local_cell() {
for (const auto edge : edges) {
std::uint32_t counter = 0;
for (std::uint32_t z = 0; z < edge; ++z) {
for (std::uint32_t y = 0; y < edge; ++y) {
for (std::uint32_t x = 0; x < edge; ++x) {
const LocalCell expected{x, y, z};
const auto index = ordinal(expected, edge);
CHECK(index && *index == counter);
const auto decoded = local_from_ordinal(counter, edge);
CHECK(decoded && *decoded == expected);
++counter;
}
}
}
CHECK(refused(local_from_ordinal(counter, edge), WorldError::ordinal_out_of_range));
CHECK(refused(local_from_ordinal(std::numeric_limits<std::uint32_t>::max(), edge),
WorldError::ordinal_out_of_range));
}
}
void envelope_corners_and_metrics() {
constexpr std::array<std::int64_t, 2> ends{-voxel_limit, voxel_limit - 1};
for (const auto edge : edges) {
for (const auto x : ends) {
for (const auto y : ends) {
for (const auto z : ends) {
const VoxelAddress voxel{x, y, z};
const auto at = split(voxel, edge);
CHECK(at);
const auto restored = join(*at, edge);
CHECK(restored && *restored == voxel);
const auto mm = lower_corner_mm(voxel);
CHECK(mm && mm->x.value == x * 1000 && mm->y.value == y * 1000 &&
mm->z.value == z * 1000);
}
}
}
}
const auto metric = lower_corner_mm({-1, 2, -3});
CHECK(metric && *metric == MetricCorner{{-1000}, {2000}, {-3000}});
}
void invalid_values_fail_before_arithmetic() {
constexpr auto maximum = std::numeric_limits<std::int64_t>::max();
constexpr auto minimum = std::numeric_limits<std::int64_t>::min();
constexpr auto unsigned_maximum = std::numeric_limits<std::uint32_t>::max();
for (const auto edge : {0U, 1U, 7U, 9U, 15U, 63U, 65U, unsigned_maximum}) {
CHECK(refused(split({minimum, maximum, 0}, edge), WorldError::invalid_edge));
CHECK(refused(join({{minimum, maximum, 0}, {}}, edge), WorldError::invalid_edge));
CHECK(refused(ordinal({}, edge), WorldError::invalid_edge));
CHECK(refused(local_from_ordinal(0, edge), WorldError::invalid_edge));
}
for (const auto edge : edges) {
for (const auto bad : {minimum, maximum, -voxel_limit - 1, voxel_limit}) {
for (const auto voxel :
{VoxelAddress{bad, 0, 0}, VoxelAddress{0, bad, 0}, VoxelAddress{0, 0, bad}}) {
CHECK(refused(split(voxel, edge), WorldError::voxel_out_of_range));
CHECK(refused(lower_corner_mm(voxel), WorldError::voxel_out_of_range));
}
}
const auto limit = voxel_limit / edge;
for (const auto bad : {minimum, maximum, -limit - 1, limit}) {
for (const auto chunk :
{ChunkAddress{bad, 0, 0}, ChunkAddress{0, bad, 0}, ChunkAddress{0, 0, bad}}) {
CHECK(refused(join({chunk, {}}, edge), WorldError::chunk_out_of_range));
}
}
for (const auto bad : {edge, unsigned_maximum}) {
for (const auto local :
{LocalCell{bad, 0, 0}, LocalCell{0, bad, 0}, LocalCell{0, 0, bad}}) {
CHECK(refused(join({{}, local}, edge), WorldError::local_out_of_range));
CHECK(refused(ordinal(local, edge), WorldError::local_out_of_range));
}
}
}
}
void logical_fill_is_not_a_material_database() {
const auto empty = CellSample::create({0}, 0);
CHECK(empty && empty->material() == MaterialId{0} && empty->fill() == 0);
CHECK(refused(CellSample::create({1}, 0), WorldError::invalid_cell));
for (std::uint32_t fill = 1; fill <= fill_scale; ++fill) {
CHECK(refused(CellSample::create({0}, fill), WorldError::invalid_cell));
const auto sample = CellSample::create({65535}, fill);
CHECK(sample && sample->fill() == fill && sample->material() == MaterialId{65535});
}
CHECK(refused(CellSample::create({1}, fill_scale + 1), WorldError::invalid_cell));
CHECK(refused(CellSample::create({1}, std::numeric_limits<std::uint32_t>::max()),
WorldError::invalid_cell));
}
} // namespace
int main() try {
independent_interval_oracle();
every_ordinal_has_one_local_cell();
envelope_corners_and_metrics();
invalid_values_fail_before_arithmetic();
logical_fill_is_not_a_material_database();
std::puts("PASS bounded world coordinates, negative seams, ordinals and cell samples");
return 0;
} catch (const std::exception& error) {
std::fprintf(stderr, "%s\n", error.what());
return 1;
} catch (...) {
return 1;
}