Files
aevolution/tests/world_generation_tests.cpp

187 lines
7.4 KiB
C++

#include "evo/world/generation.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 generation line " + std::to_string(at.line()));
}
}
#define CHECK(...) check(static_cast<bool>(__VA_ARGS__))
template <typename T> bool refused(const std::expected<T, GenerationError>& r, GenerationError e) {
return !r && r.error() == e;
}
std::uint64_t fingerprint(const std::vector<CellSample>& cells) {
std::uint64_t result = 14695981039346656037ULL;
for (const auto cell : cells) {
for (const auto value : {cell.material().value, cell.fill()}) {
for (unsigned shift = 0; shift < 16; shift += 8) {
result = (result ^ ((value >> shift) & 255U)) * 1099511628211ULL;
}
}
}
return result;
}
struct Golden {
std::uint64_t seed;
ChunkAddress address;
std::uint64_t hash;
};
// Frozen from the independent Python integer oracle, before running C++ generation.
constexpr std::array<Golden, 12> goldens{{
{0ULL, {0, 0, 0}, 14125776257468910697ULL},
{0ULL, {-1, -1, -1}, 7242967182163540465ULL},
{0ULL, {-2, 3, 0}, 4316025128172914782ULL},
{0ULL, {4, -3, -1}, 1854350054262447477ULL},
{123456ULL, {0, 0, 0}, 448061536662205469ULL},
{123456ULL, {-1, -1, -1}, 14519177808023994198ULL},
{123456ULL, {-2, 3, 0}, 7503881715723395046ULL},
{123456ULL, {4, -3, -1}, 10763600219040791926ULL},
{18446744073709551615ULL, {0, 0, 0}, 9340024193566171638ULL},
{18446744073709551615ULL, {-1, -1, -1}, 17378676633817032737ULL},
{18446744073709551615ULL, {-2, 3, 0}, 521055241663119581ULL},
{18446744073709551615ULL, {4, -3, -1}, 6558335077292188849ULL},
}};
void frozen_and_order_independent() {
for (std::size_t reverse = goldens.size(); reverse > 0; --reverse) {
const auto g = goldens[reverse - 1];
const auto first = generate_base_chunk({g.seed}, g.address);
CHECK(first && first->size() == base_chunk_cells && fingerprint(*first) == g.hash);
const auto unrelated = generate_base_chunk({g.seed ^ 123U}, {9, -10, 2});
CHECK(unrelated);
const auto again = generate_base_chunk({g.seed}, g.address);
CHECK(again && *again == *first);
}
for (const auto g : goldens) {
const auto result = generate_base_chunk({g.seed}, g.address);
CHECK(result && fingerprint(*result) == g.hash);
}
}
void samples_seams_and_materials() {
// Independent rectangular enumeration straddles negative and positive chunk
// faces on all axes. A halo sample from either side has one global definition.
for (std::int64_t cz = -2; cz <= 1; ++cz) {
for (std::int64_t cy = -2; cy <= 1; ++cy) {
for (std::int64_t cx = -2; cx <= 1; ++cx) {
const auto chunk = generate_base_chunk({73}, {cx, cy, cz});
CHECK(chunk);
std::size_t index = 0;
for (std::int64_t z = 0; z < 16; ++z) {
for (std::int64_t y = 0; y < 16; ++y) {
for (std::int64_t x = 0; x < 16; ++x) {
const auto point =
base_sample({73}, {cx * 16 + x, cy * 16 + y, cz * 16 + z});
CHECK(point && *point == (*chunk)[index++]);
CHECK(material_name(1, point->material()));
CHECK(point->fill() == (point->material().value == 0 ? 0 : fill_scale));
}
}
}
}
}
}
// Test stratigraphy independently of hash implementation by locating the
// first void cell; exactly three regolith cells must lie immediately below.
for (std::int64_t x = -32; x <= 32; ++x) {
std::int64_t height = -12;
while (height <= 8) {
const auto at = base_sample({91}, {x, -x, height});
CHECK(at);
if (at->material().value == 0) {
break;
}
++height;
}
CHECK(height >= -8 && height <= 7);
for (std::int64_t z = -16; z <= 16; ++z) {
const auto at = base_sample({91}, {x, -x, z});
CHECK(at && at->material().value == (z >= height ? 0 : (z >= height - 3 ? 2 : 1)));
}
}
CHECK(*material_name(1, {0}) == "void");
CHECK(*material_name(1, {1}) == "rock");
CHECK(*material_name(1, {2}) == "regolith");
}
void rejection_and_extremes() {
using E = GenerationError;
constexpr auto lo = std::numeric_limits<std::int64_t>::min();
constexpr auto hi = std::numeric_limits<std::int64_t>::max();
for (const auto version : {0U, 2U, std::numeric_limits<std::uint32_t>::max()}) {
for (unsigned field = 0; field < 4; ++field) {
BaseDescriptor d{};
E error{};
switch (field) {
case 0:
d.coordinate_version = version;
error = E::unsupported_coordinates;
break;
case 1:
d.generator_version = version;
error = E::unsupported_generator;
break;
case 2:
d.material_version = version;
error = E::unsupported_materials;
break;
default:
d.edge = version;
error = E::unsupported_edge;
break;
}
CHECK(refused(base_sample(d, {hi, lo, 0}), error));
CHECK(refused(generate_base_chunk(d, {hi, lo, 0}), error));
}
CHECK(refused(material_name(version, {0}), E::unsupported_materials));
}
for (const auto edge : {8U, 32U, 64U}) {
BaseDescriptor d{};
d.edge = edge;
CHECK(refused(generate_base_chunk(d, {}), E::unsupported_edge));
}
for (const auto id : {3U, 65535U}) {
CHECK(refused(material_name(1, {static_cast<std::uint16_t>(id)}), E::unknown_material));
}
for (const auto bad : {lo, hi, -voxel_limit - 1, voxel_limit}) {
for (const auto a :
{VoxelAddress{bad, 0, 0}, VoxelAddress{0, bad, 0}, VoxelAddress{0, 0, bad}}) {
CHECK(refused(base_sample({}, a), E::invalid_voxel));
}
}
constexpr auto limit = voxel_limit / 16;
for (const auto bad : {lo, hi, -limit - 1, limit}) {
for (const auto a :
{ChunkAddress{bad, 0, 0}, ChunkAddress{0, bad, 0}, ChunkAddress{0, 0, bad}}) {
CHECK(refused(generate_base_chunk({}, a), E::invalid_chunk));
}
}
for (const auto x : {-limit, limit - 1}) {
for (const auto y : {-limit, limit - 1}) {
for (const auto z : {-limit, limit - 1}) {
const auto chunk = generate_base_chunk({}, {x, y, z});
CHECK(chunk && chunk->size() == 4096);
CHECK((*chunk)[0] == *base_sample({}, {x * 16, y * 16, z * 16}));
CHECK((*chunk)[4095] == *base_sample({}, {x * 16 + 15, y * 16 + 15, z * 16 + 15}));
}
}
}
}
} // namespace
int main() try {
frozen_and_order_independent();
samples_seams_and_materials();
rejection_and_extremes();
return 0;
} catch (const std::exception& e) {
std::fprintf(stderr, "%s\n", e.what());
return 1;
} catch (...) {
return 1;
}