small refactor and void library
Signed-off-by: Aleksei (Faumaray) Selikhov <contact@faumaray.com>
This commit is contained in:
1 parent
74b89ae3ca
commit
083b84e11a
12 files changed
+646
-343
No files matched your search
+103
-36
@@ -1,16 +1,39 @@
|
||||
#include "testing.hpp"
|
||||
|
||||
#include "settlement/core/deterministic_rng.hpp"
|
||||
#include "settlement/core/world.hpp"
|
||||
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Suite.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cstdint>
|
||||
#include <iostream>
|
||||
#include <spanstream>
|
||||
#include <sstream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace std::string_view_literals;
|
||||
using Void::Testing::require_eq;
|
||||
using Void::Testing::Test;
|
||||
|
||||
// Wire bytes for an empty 5-by-5 world with seed 0x0102030405060708.
|
||||
constexpr auto empty_save_v13 =
|
||||
"SETTLERS"
|
||||
"\x0d\0\0\0" "\x08\x07\x06\x05\x04\x03\x02\x01" "\x05\0\0\0" "\x05\0\0\0"
|
||||
"\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0" // Tick and path counters
|
||||
"\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0" "\0" // Job and meal counters, hunger flag
|
||||
"\x01\0\0\0\0\0\0\0" "\x01\0\0\0\0\0\0\0" // Next agent and job IDs
|
||||
"\x01\0\0\0\0\0\0\0" "\x01\0\0\0\0\0\0\0" // Next resource and storage IDs
|
||||
"\0\0\0\0\0" "\0\0\0\0\0" "\0\0\0\0\0" "\0\0\0\0\0" "\0\0\0\0\0" // Walkable cells
|
||||
"\0\0\0\0" "\0\0\0\0" "\0\0\0\0" "\0\0\0\0" "\0\0\0\0" // Empty record counts
|
||||
"\0\0\0\0" "\0\0\0\0\0\0\0\0" // Scenario flags, priority and sawmill
|
||||
"\0\0\0\0\0\0\0\0" "\0\0\0\0\0\0\0\0" // Player sequences
|
||||
"\0\0\0\0\0\0\0\0" "\0"sv; // Command revision and staged beds flag
|
||||
static_assert(empty_save_v13.size() == 183);
|
||||
|
||||
void require(bool condition, const char* message) {
|
||||
if (!condition) {
|
||||
throw std::runtime_error(message);
|
||||
@@ -33,9 +56,18 @@ std::unique_ptr<settlement::World> save_roundtrip(const settlement::World& world
|
||||
return restored;
|
||||
}
|
||||
|
||||
using Void::Testing::Test;
|
||||
void require_save_error(std::string_view bytes, std::string_view expected) {
|
||||
std::istringstream file(std::string(bytes), std::ios::binary);
|
||||
try {
|
||||
static_cast<void>(settlement::World::read_save(file));
|
||||
} catch (const std::runtime_error& error) {
|
||||
require_eq(std::string_view(error.what()), expected);
|
||||
return;
|
||||
}
|
||||
throw std::runtime_error("invalid save was accepted");
|
||||
}
|
||||
|
||||
struct CoreTests : Void::Testing::Suite<CoreTests> {
|
||||
struct [[= Void::Testing::name("CoreTests")]] CoreTests : Void::Testing::Suite<CoreTests> {
|
||||
Test test_parallel_pathfinding_completes_the_jobs();
|
||||
Test test_single_worker_configuration_is_rejected();
|
||||
Test test_rng_repeats_for_same_seed();
|
||||
@@ -46,6 +78,9 @@ struct CoreTests : Void::Testing::Suite<CoreTests> {
|
||||
[[= Void::Testing::tag("construction")]] Test test_cabin_placement_queues_a_complete_order();
|
||||
Test test_hungry_resident_reserves_and_eats_a_berry();
|
||||
Test test_invalid_save_is_rejected();
|
||||
Test test_save_version_13_matches_wire_fixture();
|
||||
Test test_legacy_save_footers();
|
||||
Test test_save_reports_output_failure();
|
||||
Test test_two_players_share_the_first_colony();
|
||||
Test test_bed_parts_are_delivered_separately();
|
||||
Test test_resident_uses_assigned_bed_and_sleep_survives_save();
|
||||
@@ -75,14 +110,13 @@ Test CoreTests::test_parallel_pathfinding_completes_the_jobs() {
|
||||
}
|
||||
for (int tick = 0; tick < 120; ++tick) world.step();
|
||||
require(world.jobs_completed() == 24, "parallel pathfinding did not finish every job");
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_single_worker_configuration_is_rejected() {
|
||||
try {
|
||||
settlement::World world(8, 8, 8, 1);
|
||||
} catch (const std::invalid_argument&) {
|
||||
return {};
|
||||
return;
|
||||
}
|
||||
throw std::runtime_error("world accepted a single worker");
|
||||
}
|
||||
@@ -91,9 +125,8 @@ Test CoreTests::test_rng_repeats_for_same_seed() {
|
||||
settlement::DeterministicRng left(12345);
|
||||
settlement::DeterministicRng right(12345);
|
||||
for (int index = 0; index < 1000; ++index) {
|
||||
expect_eq(left.next_u64(), right.next_u64());
|
||||
require_eq(left.next_u64(), right.next_u64());
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_work_jobs_complete() {
|
||||
@@ -116,7 +149,6 @@ Test CoreTests::test_work_jobs_complete() {
|
||||
"completed job retained an active state");
|
||||
require(world.agents().front().state == settlement::AgentState::Idle,
|
||||
"worker did not return to idle after completing a job");
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_astar_route_uses_opening_in_wall() {
|
||||
@@ -139,7 +171,6 @@ Test CoreTests::test_astar_route_uses_opening_in_wall() {
|
||||
}
|
||||
require(world.path_searches() > 0, "path search counter was not updated");
|
||||
require(world.path_nodes_visited() > 0, "A* did not report expanded nodes");
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_residents_gather_and_deliver_resources() {
|
||||
@@ -163,7 +194,6 @@ Test CoreTests::test_residents_gather_and_deliver_resources() {
|
||||
"storage retained reservations after deliveries completed");
|
||||
require(world.resource_nodes()[0].quantity == 0 && world.resource_nodes()[1].quantity == 0,
|
||||
"resource nodes retained units after deliveries completed");
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_production_reservations_and_construction() {
|
||||
@@ -244,8 +274,6 @@ Test CoreTests::test_production_reservations_and_construction() {
|
||||
"the builder skipped a delivery or construction stage");
|
||||
require(world.storages()[0].items.empty() && world.storages()[1].items.empty(),
|
||||
"building did not consume the crafted materials from both stores");
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_cabin_placement_queues_a_complete_order() {
|
||||
@@ -268,7 +296,6 @@ Test CoreTests::test_cabin_placement_queues_a_complete_order() {
|
||||
} catch (const std::invalid_argument&) {
|
||||
require(orders.size() == 6, "invalid placement partially queued new orders");
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_hungry_resident_reserves_and_eats_a_berry() {
|
||||
@@ -318,7 +345,6 @@ Test CoreTests::test_hungry_resident_reserves_and_eats_a_berry() {
|
||||
static_cast<void>(world.add_job(pantry, 1));
|
||||
world.step();
|
||||
require(world.jobs_completed() == 2, "the resident did not resume work after eating");
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_invalid_save_is_rejected() {
|
||||
@@ -337,7 +363,67 @@ Test CoreTests::test_invalid_save_is_rejected() {
|
||||
require(rejected(bytes + 'x'), "save with trailing data was accepted");
|
||||
bytes[8] = 127;
|
||||
require(rejected(bytes), "unknown save version was accepted");
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_save_version_13_matches_wire_fixture() {
|
||||
constexpr std::uint64_t seed = 0x0102030405060708;
|
||||
const settlement::World world(seed, 5, 5, 2);
|
||||
require(save_contents(world) == empty_save_v13, "version 13 save bytes changed");
|
||||
|
||||
std::istringstream file(std::string(empty_save_v13), std::ios::binary);
|
||||
const auto restored = settlement::World::read_save(file, 2);
|
||||
require_eq(restored->seed(), seed);
|
||||
require_eq(restored->width(), 5);
|
||||
require_eq(restored->height(), 5);
|
||||
require(save_contents(*restored) == empty_save_v13, "version 13 fixture changed after loading");
|
||||
}
|
||||
|
||||
Test CoreTests::test_legacy_save_footers() {
|
||||
constexpr auto incomplete = "Save is incomplete or unreadable."sv;
|
||||
constexpr auto checksum_mismatch = "Legacy save integrity footer does not match."sv;
|
||||
constexpr auto trailing_data = "Save contains unexpected trailing data."sv;
|
||||
constexpr std::size_t version_offset = 8;
|
||||
constexpr auto v8_footer = "\x33\x73\x67\x28\xa8\x53\x56\xfa"sv;
|
||||
|
||||
// Version 1 ended at the record counts. These footers are fixed FNV-1a
|
||||
// digests of the wire payloads, including SETTLERS and the version field.
|
||||
std::string v1(empty_save_v13.substr(0, 146));
|
||||
v1[version_offset] = '\x01';
|
||||
v1 += "\x58\xc1\x77\x19\x3a\x69\xca\x2c"sv;
|
||||
std::string v8(empty_save_v13);
|
||||
v8[version_offset] = '\x08';
|
||||
v8 += v8_footer;
|
||||
std::string v9(empty_save_v13);
|
||||
v9[version_offset] = '\x09';
|
||||
|
||||
for (const auto* bytes : {&v1, &v8, &v9}) {
|
||||
std::istringstream file(*bytes, std::ios::binary);
|
||||
const auto restored = settlement::World::read_save(file);
|
||||
require(save_contents(*restored) == empty_save_v13, "historical save did not preserve its state");
|
||||
}
|
||||
|
||||
require_save_error(std::string_view(v8).substr(0, empty_save_v13.size()), incomplete);
|
||||
require_save_error(std::string_view(v8).substr(0, v8.size() - 1), incomplete);
|
||||
require_save_error(v8 + 'x', trailing_data);
|
||||
require_save_error(v9 + std::string(v8_footer), trailing_data);
|
||||
|
||||
v8.back() = '\0';
|
||||
require_save_error(v8, checksum_mismatch);
|
||||
}
|
||||
|
||||
Test CoreTests::test_save_reports_output_failure() {
|
||||
const settlement::World world(0x0102030405060708, 5, 5, 2);
|
||||
std::array<char, 10> buffer{};
|
||||
std::ospanstream file(buffer);
|
||||
try {
|
||||
world.write_save(file);
|
||||
} catch (const std::runtime_error& error) {
|
||||
require_eq(std::string_view(error.what()), "Could not write the save."sv);
|
||||
require(file.bad(), "short write did not set the stream's badbit");
|
||||
require_eq(std::string_view(buffer.data(), buffer.size()), empty_save_v13.substr(0, buffer.size()));
|
||||
return;
|
||||
}
|
||||
throw std::runtime_error("save succeeded despite a short write");
|
||||
}
|
||||
|
||||
Test CoreTests::test_two_players_share_the_first_colony() {
|
||||
@@ -383,7 +469,6 @@ Test CoreTests::test_two_players_share_the_first_colony() {
|
||||
"the shared colony could not feed its residents and complete the objective");
|
||||
auto replay = save_roundtrip(*world);
|
||||
require(replay->scenario_progress().completed, "completion was lost after loading");
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_bed_parts_are_delivered_separately() {
|
||||
@@ -416,7 +501,6 @@ Test CoreTests::test_bed_parts_are_delivered_separately() {
|
||||
"the beds were not completed through three separate deliveries");
|
||||
require(world->storages().back().item_count(ItemType::Cloth) == 8,
|
||||
"two beds must consume four pieces of cloth");
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_resident_uses_assigned_bed_and_sleep_survives_save() {
|
||||
@@ -467,7 +551,6 @@ Test CoreTests::test_resident_uses_assigned_bed_and_sleep_survives_save() {
|
||||
}
|
||||
require(from_route && woke_rested && rested_on_ground, "sleep and recovery phases were not observed");
|
||||
require(world.jobs_completed() == 2, "residents did not finish their existing work");
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_fatigue_slows_work_without_interrupting_it() {
|
||||
@@ -483,7 +566,6 @@ Test CoreTests::test_fatigue_slows_work_without_interrupting_it() {
|
||||
const auto before = resident.work_remaining;
|
||||
for (int i = 0; i < 6; ++i) world.step();
|
||||
require(before - resident.work_remaining == 2, "hunger and fatigue did not slow the ongoing work");
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_ground_rest_never_uses_another_residents_bed() {
|
||||
@@ -525,7 +607,6 @@ Test CoreTests::test_ground_rest_never_uses_another_residents_bed() {
|
||||
require(world.assigned_bed(owner)->position == new_cell, "moving a bed lost its owner");
|
||||
require(world.apply_command({1, 3, CommandKind::DemolishObject, new_cell}).status == CommandStatus::Accepted &&
|
||||
world.assigned_bed(owner) == nullptr, "demolishing a bed left a stale assignment");
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_fields_mines_and_reindexing_active_hauls() {
|
||||
@@ -579,7 +660,6 @@ Test CoreTests::test_fields_mines_and_reindexing_active_hauls() {
|
||||
"growing, regrowing, or mining did not deliver resources");
|
||||
require(apply(CommandKind::PlaceStorage, {11, 7}) == CommandStatus::Accepted,
|
||||
"a new storage could not be placed");
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_specialists_take_their_own_jobs_and_finish_prior_assignments() {
|
||||
@@ -636,7 +716,6 @@ Test CoreTests::test_specialists_take_their_own_jobs_and_finish_prior_assignment
|
||||
}
|
||||
require(delivered_stone && later_cut_wood, "the reassigned miner did not finish mining and then cut wood");
|
||||
require(world.scenario_progress().assigned_beds == 1, "the builder did not complete the bed");
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_flax_and_loom_supply_a_bed_without_starting_cloth() {
|
||||
@@ -713,7 +792,6 @@ Test CoreTests::test_flax_and_loom_supply_a_bed_without_starting_cloth() {
|
||||
require(world.storages().front().item_count(ItemType::Wood) == 0 &&
|
||||
world.storages().front().item_count(ItemType::Loom) == 0,
|
||||
"the installed loom did not consume the materials and finished item exactly once");
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_field_growth_requires_a_worker() {
|
||||
@@ -743,7 +821,6 @@ Test CoreTests::test_field_growth_requires_a_worker() {
|
||||
require(save_contents(world) == save_contents(*restored), "sowing did not resume after loading");
|
||||
}
|
||||
require(world.resource_nodes().front().quantity > 0, "planting without a storage did not grow flax");
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_opening_a_mine_invalidates_unreachable_routes() {
|
||||
@@ -765,7 +842,6 @@ Test CoreTests::test_opening_a_mine_invalidates_unreachable_routes() {
|
||||
world.step();
|
||||
require(world.agents().front().resource_node_index == 0 &&
|
||||
world.agents().front().state == AgentState::Travelling, "terrain edit left a stale route in the cache");
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_object_tools_work_in_an_older_demo_world() {
|
||||
@@ -787,7 +863,6 @@ Test CoreTests::test_object_tools_work_in_an_older_demo_world() {
|
||||
require(edit(CommandKind::DemolishObject, {4, 5}) == CommandStatus::Accepted,
|
||||
"a field in a demonstration could not be demolished");
|
||||
static_cast<void>(save_roundtrip(*world));
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_room_builds_and_saves_as_separate_parts() {
|
||||
@@ -830,7 +905,6 @@ Test CoreTests::test_room_builds_and_saves_as_separate_parts() {
|
||||
resumed->production_orders().size() == 16 && resumed->can_place_storage(floor),
|
||||
"demolition removed the floor instead of the furniture");
|
||||
static_cast<void>(save_roundtrip(*resumed));
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_furnished_rooms_queue_and_build_their_contents() {
|
||||
@@ -907,7 +981,6 @@ Test CoreTests::test_furnished_rooms_queue_and_build_their_contents() {
|
||||
}
|
||||
static_cast<void>(save_roundtrip(*resumed));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_carpenter_delivers_the_door_before_installation() {
|
||||
@@ -995,7 +1068,6 @@ Test CoreTests::test_carpenter_delivers_the_door_before_installation() {
|
||||
world->storages()[1].position}).status == CommandStatus::Accepted && crafted_orders() == 3,
|
||||
"losing the stock did not queue a replacement for the waiting room");
|
||||
static_cast<void>(save_roundtrip(*world));
|
||||
return {};
|
||||
}
|
||||
|
||||
Test CoreTests::test_carpenter_makes_furnishings_for_rooms() {
|
||||
@@ -1107,11 +1179,6 @@ Test CoreTests::test_carpenter_makes_furnishings_for_rooms() {
|
||||
count_orders(variant.craft) == 2, "a room ignored the matching furnishing already in stock");
|
||||
static_cast<void>(save_roundtrip(*world));
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
return CoreTests::run(argc, argv);
|
||||
}
|
||||
Reference in new issue
Block a user