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);
|
||||
}
|
||||
+156
-41
@@ -1,28 +1,156 @@
|
||||
#include "testing.hpp"
|
||||
|
||||
#include "lan_session.hpp"
|
||||
#include "lan_wire.hpp"
|
||||
|
||||
#include <Void/Testing/Assert.hpp>
|
||||
#include <Void/Testing/Suite.hpp>
|
||||
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <thread>
|
||||
|
||||
namespace {
|
||||
|
||||
using namespace std::chrono_literals;
|
||||
using namespace std::string_view_literals;
|
||||
using namespace settlement;
|
||||
using namespace settlement::viewer;
|
||||
|
||||
void require(bool condition, const char* message) {
|
||||
if (!condition) throw std::runtime_error(message);
|
||||
}
|
||||
|
||||
using Void::Testing::expect_throw;
|
||||
using Void::Testing::require;
|
||||
using Void::Testing::Test;
|
||||
|
||||
constexpr auto hello_bytes = "\x0d\x00\x00\x00\x01\x0b\x00\x00\x00\x10\x32\x54\x76\x98\xba\xdc\xfe"sv;
|
||||
constexpr auto command_bytes = "\x23\x00\x00\x00\x03"
|
||||
"\x11\x22\x33\x44\x55\x66\x77\x88\x07"
|
||||
"\xfe\xff\xff\xff\x78\x56\x34\x12\xa5"
|
||||
"\x04\x03\x02\x01\xfc\xfc\xfd\xfe\x87\x96\xa5\xb4\xc3\xd2\xe1\xf0"sv;
|
||||
constexpr auto receipt_bytes = "\x0a\x00\x00\x00\x04\x04\x10\x32\x54\x76\x98\xba\xdc\xfe"sv;
|
||||
constexpr auto ping_bytes = "\x01\x00\x00\x00\x06"sv;
|
||||
|
||||
struct LanTests : Void::Testing::Suite<LanTests> {
|
||||
[[= Void::Testing::tag("wire")]] Test test_protocol_11_fixed_messages();
|
||||
[[= Void::Testing::tag("wire")]] Test test_fragmented_and_coalesced_frames();
|
||||
[[= Void::Testing::tag("wire")]] Test test_frame_size_limits();
|
||||
[[= Void::Testing::tag("lan")]] Test test_shared_colony_over_loopback();
|
||||
};
|
||||
|
||||
Test LanTests::test_protocol_11_fixed_messages() {
|
||||
using namespace lan_wire;
|
||||
constexpr Hello hello{protocol_version, 0xfedcba9876543210ULL};
|
||||
constexpr PlayerCommand command{
|
||||
1, 0x8877665544332211ULL, CommandKind::MoveObject, {-2, 0x12345678}, 0xa5,
|
||||
{0x01020304, -0x01020304}, 0xf0e1d2c3b4a59687ULL
|
||||
};
|
||||
constexpr Receipt receipt{CommandStatus::InvalidTarget, 0xfedcba9876543210ULL};
|
||||
|
||||
std::string encoded;
|
||||
const auto hello_payload = encode_hello(hello);
|
||||
append_frame(encoded, Message::Hello, std::string_view(hello_payload));
|
||||
require(encoded == hello_bytes);
|
||||
encoded.clear();
|
||||
const auto command_payload = encode_command(command);
|
||||
append_frame(encoded, Message::Command, std::string_view(command_payload));
|
||||
require(encoded == command_bytes);
|
||||
encoded.clear();
|
||||
const auto receipt_payload = encode_receipt(receipt);
|
||||
append_frame(encoded, Message::Receipt, std::string_view(receipt_payload));
|
||||
require(encoded == receipt_bytes);
|
||||
|
||||
const auto hello_frame = next_frame(hello_bytes);
|
||||
require(hello_frame && hello_frame->message == Message::Hello && hello_frame->size == hello_bytes.size());
|
||||
const auto decoded_hello = decode_hello(hello_frame->payload);
|
||||
require(decoded_hello && decoded_hello->version == 11 && decoded_hello->key == hello.key);
|
||||
require(!decode_hello(hello_frame->payload.substr(1)));
|
||||
require(!decode_hello(std::string(hello_frame->payload) + '\0'));
|
||||
|
||||
const auto command_frame = next_frame(command_bytes);
|
||||
require(command_frame && command_frame->message == Message::Command && command_frame->size == command_bytes.size());
|
||||
const auto decoded_command = decode_command(command_frame->payload);
|
||||
require(decoded_command.has_value());
|
||||
require(decoded_command->player == 2);
|
||||
require(decoded_command->sequence == command.sequence);
|
||||
require(decoded_command->kind == command.kind);
|
||||
require(decoded_command->cell == command.cell);
|
||||
require(decoded_command->value == command.value);
|
||||
require(decoded_command->destination == command.destination);
|
||||
require(decoded_command->resident == command.resident);
|
||||
require(!decode_command(command_frame->payload.substr(1)));
|
||||
require(!decode_command(std::string(command_frame->payload) + '\0'));
|
||||
|
||||
const auto receipt_frame = next_frame(receipt_bytes);
|
||||
require(receipt_frame && receipt_frame->message == Message::Receipt && receipt_frame->size == receipt_bytes.size());
|
||||
const auto decoded_receipt = decode_receipt(receipt_frame->payload);
|
||||
require(decoded_receipt && decoded_receipt->status == receipt.status &&
|
||||
decoded_receipt->next_sequence == receipt.next_sequence);
|
||||
require(!decode_receipt(receipt_frame->payload.substr(1)));
|
||||
require(!decode_receipt(std::string(receipt_frame->payload) + '\0'));
|
||||
}
|
||||
|
||||
Test LanTests::test_fragmented_and_coalesced_frames() {
|
||||
using namespace lan_wire;
|
||||
std::string received(command_bytes.substr(0, 3));
|
||||
require(!next_frame(received));
|
||||
received.append(command_bytes.substr(3, 8));
|
||||
require(!next_frame(received));
|
||||
received.append(command_bytes.substr(11));
|
||||
received.append(ping_bytes);
|
||||
received.append(hello_bytes.substr(0, 5));
|
||||
|
||||
std::size_t consumed = 0;
|
||||
{
|
||||
const auto command = next_frame(received);
|
||||
require(command && command->message == Message::Command && command->size == command_bytes.size());
|
||||
require(command->payload.data() == received.data() + 5);
|
||||
require(command->payload == command_bytes.substr(5));
|
||||
|
||||
const auto ping = next_frame(std::string_view(received).substr(command->size));
|
||||
require(ping && ping->message == Message::Ping && ping->payload.empty() && ping->size == ping_bytes.size());
|
||||
consumed = command->size + ping->size;
|
||||
require(!next_frame(std::string_view(received).substr(consumed)));
|
||||
}
|
||||
|
||||
received.erase(0, consumed);
|
||||
received.append(hello_bytes.substr(5));
|
||||
const auto hello = next_frame(received);
|
||||
require(hello && hello->message == Message::Hello && hello->size == received.size());
|
||||
require(hello->payload == hello_bytes.substr(5));
|
||||
}
|
||||
|
||||
Test LanTests::test_frame_size_limits() {
|
||||
using namespace lan_wire;
|
||||
constexpr auto empty_prefix = "\x00\x00\x00\x00"sv;
|
||||
constexpr auto max_prefix = "\x00\x00\x40\x00"sv;
|
||||
constexpr auto oversized_prefix = "\x01\x00\x40\x00"sv;
|
||||
|
||||
require(!next_frame(empty_prefix.substr(0, 3)));
|
||||
require(!next_frame(oversized_prefix.substr(0, 3)));
|
||||
expect_throw<std::runtime_error>([] { static_cast<void>(next_frame(empty_prefix)); });
|
||||
expect_throw<std::runtime_error>([] { static_cast<void>(next_frame(oversized_prefix)); });
|
||||
require(!next_frame(max_prefix));
|
||||
|
||||
std::string output;
|
||||
append_frame(output, Message::Ping);
|
||||
require(output == ping_bytes);
|
||||
|
||||
std::string payload(4 * 1024 * 1024 - 1, '\xa5');
|
||||
output.clear();
|
||||
append_frame(output, Message::Snapshot, payload);
|
||||
require(output.starts_with(max_prefix));
|
||||
{
|
||||
const auto snapshot = next_frame(output);
|
||||
require(snapshot && snapshot->message == Message::Snapshot && snapshot->size == output.size());
|
||||
require(snapshot->payload == payload);
|
||||
payload.front() = '\0';
|
||||
require(snapshot->payload.front() == '\xa5');
|
||||
}
|
||||
|
||||
payload.push_back('\0');
|
||||
expect_throw<std::runtime_error>([&] { append_frame(output, Message::Snapshot, payload); });
|
||||
require(output.size() == ping_bytes.size() + payload.size() - 1);
|
||||
}
|
||||
|
||||
Test LanTests::test_shared_colony_over_loopback() {
|
||||
auto authoritative = make_first_colony(4);
|
||||
auto client_view = make_first_colony(4);
|
||||
@@ -47,23 +175,20 @@ Test LanTests::test_shared_colony_over_loopback() {
|
||||
guest.join("127.0.0.1", wrong_code, port);
|
||||
pump_until([&] { return guest.state() == LanState::GuestDisconnected; },
|
||||
"a guest with the wrong code was not disconnected");
|
||||
require(host.state() == LanState::Hosting, "an invalid guest occupied the player slot");
|
||||
require(host.state() == LanState::Hosting);
|
||||
|
||||
guest.join("127.0.0.1", code, port);
|
||||
pump_until([&] { return guest.state() == LanState::GuestConnected; }, "the guest could not join");
|
||||
require(client_view->tick() == 25 && client_view->agents().size() == authoritative->agents().size(),
|
||||
"joining did not deliver the host's current snapshot");
|
||||
require(!guest.submit(CommandKind::SetPaused, {}, 1), "a guest could control pause");
|
||||
require(guest.submit(CommandKind::PlaceBed, {16, 18}, static_cast<std::uint8_t>(BedBase::Stone)),
|
||||
"the guest could not send a build command");
|
||||
require(client_view->tick() == 25 && client_view->agents().size() == authoritative->agents().size());
|
||||
require(!guest.submit(CommandKind::SetPaused, {}, 1));
|
||||
require(guest.submit(CommandKind::PlaceBed, {16, 18}, static_cast<std::uint8_t>(BedBase::Stone)));
|
||||
pump_until([&] {
|
||||
return client_view->production_orders().size() == 1 &&
|
||||
client_view->production_orders().front().position == GridPosition{16, 18} &&
|
||||
client_view->production_orders().front().bed_base == BedBase::Stone;
|
||||
}, "the guest's bed was not applied and synchronized");
|
||||
require(authoritative->production_orders().front().bed_base == BedBase::Stone,
|
||||
"the guest's choice of bed material was lost");
|
||||
require(guest.submit(CommandKind::PlaceBed, {16, 18}, 0), "the guest could not send a conflicting command");
|
||||
require(authoritative->production_orders().front().bed_base == BedBase::Stone);
|
||||
require(guest.submit(CommandKind::PlaceBed, {16, 18}, 0));
|
||||
pump_until([&] {
|
||||
const auto rejected = guest.take_rejection();
|
||||
return rejected && rejected->status == CommandStatus::InvalidTarget &&
|
||||
@@ -71,7 +196,7 @@ Test LanTests::test_shared_colony_over_loopback() {
|
||||
rejected->value == 0;
|
||||
},
|
||||
"a conflicting guest command did not return a rejection");
|
||||
require(authoritative->production_orders().size() == 1, "the same cell was claimed twice");
|
||||
require(authoritative->production_orders().size() == 1);
|
||||
|
||||
guest.leave();
|
||||
pump_until([&] { return host.state() == LanState::Hosting; }, "the guest slot was not released");
|
||||
@@ -79,9 +204,8 @@ Test LanTests::test_shared_colony_over_loopback() {
|
||||
guest.join("127.0.0.1", code, port);
|
||||
pump_until([&] { return guest.state() == LanState::GuestConnected; }, "the guest could not rejoin");
|
||||
require(client_view->tick() == authoritative->tick() && client_view->next_command_sequence(2) == 2 &&
|
||||
client_view->production_orders().size() == 1,
|
||||
"rejoining did not restore the current colony and guest sequence");
|
||||
require(guest.submit(CommandKind::SetForaging, {14, 12}, 1), "the reconnected guest could not send a command");
|
||||
client_view->production_orders().size() == 1);
|
||||
require(guest.submit(CommandKind::SetForaging, {14, 12}, 1));
|
||||
pump_until([&] {
|
||||
if (client_view->next_command_sequence(2) != 3) return false;
|
||||
for (const auto& node : client_view->resource_nodes()) {
|
||||
@@ -96,13 +220,13 @@ Test LanTests::test_shared_colony_over_loopback() {
|
||||
return false;
|
||||
};
|
||||
for (int i = 0; i < 600 && !owner_sleeping(*authoritative); ++i) authoritative->step();
|
||||
require(owner_sleeping(*authoritative), "the owner went without sleep");
|
||||
require(owner_sleeping(*authoritative));
|
||||
pump_until([&] {
|
||||
return client_view->tick() == authoritative->tick() && owner_sleeping(*client_view);
|
||||
}, "the host's sleep state did not reach the guest");
|
||||
const GridPosition field{14, 15};
|
||||
const GridPosition new_field{14, 17};
|
||||
require(guest.submit(CommandKind::PlaceBerryField, field, 0), "the guest could not plant a field");
|
||||
require(guest.submit(CommandKind::PlaceBerryField, field, 0));
|
||||
const auto has_field = [&](GridPosition position) {
|
||||
for (const auto& node : client_view->resource_nodes()) {
|
||||
if (node.site == ResourceSite::BerryField && node.position == position) return true;
|
||||
@@ -110,24 +234,23 @@ Test LanTests::test_shared_colony_over_loopback() {
|
||||
return false;
|
||||
};
|
||||
pump_until([&] { return has_field(field); }, "the planted field was not synchronized");
|
||||
require(guest.submit(CommandKind::MoveObject, field, 0, new_field), "the guest could not move the field");
|
||||
require(guest.submit(CommandKind::MoveObject, field, 0, new_field));
|
||||
pump_until([&] { return has_field(new_field) && !has_field(field); }, "the move destination was not synchronized");
|
||||
require(guest.submit(CommandKind::DemolishObject, new_field, 0), "the guest could not remove the field");
|
||||
require(guest.submit(CommandKind::DemolishObject, new_field, 0));
|
||||
pump_until([&] { return !has_field(new_field); },
|
||||
"demolishing a field was not synchronized");
|
||||
const auto resident = authoritative->agents().back().id;
|
||||
require(guest.submit(CommandKind::AssignProfession, {}, static_cast<std::uint8_t>(Profession::Miner), {}, resident),
|
||||
"the guest could not assign a profession");
|
||||
require(guest.submit(CommandKind::AssignProfession, {}, static_cast<std::uint8_t>(Profession::Miner), {}, resident));
|
||||
pump_until([&] {
|
||||
return client_view->agents().back().id == resident &&
|
||||
client_view->agents().back().profession == Profession::Miner;
|
||||
}, "the assigned profession was not synchronized");
|
||||
require(guest.submit(CommandKind::PlaceFlaxField, field, 0), "the guest could not plant flax");
|
||||
require(guest.submit(CommandKind::PlaceFlaxField, field, 0));
|
||||
pump_until([&] {
|
||||
return client_view->resource_nodes().back().site == ResourceSite::FlaxField &&
|
||||
client_view->resource_nodes().back().position == field;
|
||||
}, "the flax field did not arrive in the guest snapshot");
|
||||
require(guest.submit(CommandKind::PlaceLoom, new_field, 0), "the guest could not place a loom");
|
||||
require(guest.submit(CommandKind::PlaceLoom, new_field, 0));
|
||||
pump_until([&] {
|
||||
const auto& orders = client_view->production_orders();
|
||||
return orders.size() >= 2 && orders.back().recipe == RecipeId::CraftLoom &&
|
||||
@@ -138,7 +261,7 @@ Test LanTests::test_shared_colony_over_loopback() {
|
||||
authoritative->step();
|
||||
}
|
||||
require(authoritative->resource_nodes().back().needs_tending() &&
|
||||
authoritative->resource_nodes().back().reserved_workers == 1, "no resident reserved the field for sowing");
|
||||
authoritative->resource_nodes().back().reserved_workers == 1);
|
||||
pump_until([&] {
|
||||
return client_view->tick() == authoritative->tick() &&
|
||||
client_view->resource_nodes().back().needs_tending() &&
|
||||
@@ -154,30 +277,22 @@ Test LanTests::test_shared_colony_over_loopback() {
|
||||
found_room = true;
|
||||
}
|
||||
}
|
||||
require(found_room, "could not find a test plot for a room");
|
||||
require(found_room);
|
||||
const auto entrance = static_cast<std::uint8_t>(room.entrance);
|
||||
const auto previous_size = authoritative->production_orders().size();
|
||||
require(guest.submit(CommandKind::PlaceRoom, room.first, entrance, room.last),
|
||||
"guest could not send the room");
|
||||
require(guest.submit(CommandKind::PlaceRoom, room.first, entrance, room.last));
|
||||
pump_until([&] {
|
||||
return client_view->production_orders().size() == previous_size + 10 &&
|
||||
client_view->production_orders().back().recipe == RecipeId::CraftWoodenDoor;
|
||||
}, "snapshot did not carry the room and carpenter's order");
|
||||
require(guest.submit(CommandKind::PlaceRoom, room.first, entrance, room.last),
|
||||
"guest could not send the conflicting room");
|
||||
require(guest.submit(CommandKind::PlaceRoom, room.first, entrance, room.last));
|
||||
pump_until([&] {
|
||||
const auto rejected = guest.take_rejection();
|
||||
return rejected && rejected->kind == CommandKind::PlaceRoom && rejected->cell == room.first &&
|
||||
rejected->destination == room.last && rejected->value == entrance &&
|
||||
rejected->status == CommandStatus::InvalidTarget;
|
||||
}, "room rejection lost the geometry needed to restore the draft");
|
||||
require(authoritative->production_orders().size() == previous_size + 10, "room conflict scheduled extra work");
|
||||
std::cout << "Loopback LAN checks passed.\n";
|
||||
return {};
|
||||
require(authoritative->production_orders().size() == previous_size + 10);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
return LanTests::run(argc, argv);
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
#include <Void/Testing/Runner.hpp>
|
||||
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <iostream>
|
||||
#include <string_view>
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
// Void parses its own options but ignores stray positional arguments.
|
||||
constexpr std::array<std::string_view, 10> options_with_values{
|
||||
"--filter", "--suite", "--reporter", "--output", "--tag",
|
||||
"--log-file", "--selftest-child", "--seed", "--repeat", "--parallel",
|
||||
};
|
||||
for (int i = 1; i < argc; ++i) {
|
||||
const std::string_view argument = argv[i];
|
||||
if (!argument.starts_with('-')) {
|
||||
std::cerr << "error: Unexpected positional argument '" << argument << "'\n";
|
||||
return 2;
|
||||
}
|
||||
if (argument == "--tag" && (i + 1 == argc || argv[i + 1][0] == '\0')) {
|
||||
std::cerr << "error: --tag requires a non-empty value\n";
|
||||
return 2;
|
||||
}
|
||||
if (i + 1 < argc && std::ranges::contains(options_with_values, argument)) {
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
||||
auto& runner = Void::Testing::Runner::instance();
|
||||
auto config = runner.parse_args(argc, argv);
|
||||
// An explicit match-all filter makes an empty registry fail on a default run.
|
||||
if (config.filter_pattern.empty()) {
|
||||
config.filter_pattern = ".*";
|
||||
}
|
||||
const auto result = runner.run(config);
|
||||
if (!result.config_error.empty()) {
|
||||
return 2;
|
||||
}
|
||||
return result.failed > 0 || result.crashed > 0 ? 1 : 0;
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <iostream>
|
||||
#include <meta>
|
||||
#include <source_location>
|
||||
#include <stdexcept>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
|
||||
namespace Void::Testing {
|
||||
|
||||
struct Test {};
|
||||
|
||||
struct AnnotationText {
|
||||
char text[96]{};
|
||||
|
||||
consteval AnnotationText(std::string_view value) {
|
||||
if (value.size() >= sizeof(text)) throw "Test annotation is too long";
|
||||
value.copy(text, value.size());
|
||||
}
|
||||
|
||||
constexpr std::string_view view() const noexcept { return text; }
|
||||
};
|
||||
|
||||
struct Tag { AnnotationText value; };
|
||||
struct Skip { AnnotationText value; };
|
||||
|
||||
consteval Tag tag(std::string_view value) { return {value}; }
|
||||
consteval Skip skip(std::string_view reason) { return {reason}; }
|
||||
|
||||
namespace detail {
|
||||
|
||||
namespace meta = std::meta;
|
||||
|
||||
template <meta::info Member, class Annotation>
|
||||
std::vector<std::string> annotation_values() {
|
||||
std::vector<std::string> values;
|
||||
template for (constexpr auto a : std::define_static_array(meta::annotations_of_with_type(Member, ^^Annotation))) {
|
||||
constexpr auto annotation = meta::extract<Annotation>(a);
|
||||
values.emplace_back(annotation.value.view());
|
||||
}
|
||||
return values;
|
||||
}
|
||||
|
||||
consteval bool is_test(meta::info member) {
|
||||
return meta::is_function(member) && !meta::is_static_member(member) &&
|
||||
!meta::is_constructor(member) && !meta::is_destructor(member) &&
|
||||
meta::parameters_of(member).empty() && meta::return_type_of(member) == ^^Test;
|
||||
}
|
||||
|
||||
} // namespace detail
|
||||
|
||||
template <class Derived>
|
||||
struct Suite {
|
||||
template <class Actual, class Expected>
|
||||
static void expect_eq(const Actual& actual, const Expected& expected,
|
||||
std::source_location at = std::source_location::current()) {
|
||||
if (!(actual == expected)) {
|
||||
throw std::runtime_error(std::string(at.file_name()) + ":" + std::to_string(at.line()) +
|
||||
": expected equal values");
|
||||
}
|
||||
}
|
||||
|
||||
static int run(int argc, char** argv) {
|
||||
std::string_view selected_tag;
|
||||
if (argc == 3 && std::string_view(argv[1]) == "--tag" && argv[2][0] != '\0') {
|
||||
selected_tag = argv[2];
|
||||
} else if (argc != 1) {
|
||||
std::cerr << "Usage: " << argv[0] << " [--tag NAME]\n";
|
||||
return 1;
|
||||
}
|
||||
namespace meta = std::meta;
|
||||
int matched = 0, failed = 0, skipped = 0;
|
||||
template for (constexpr auto member : std::define_static_array(meta::members_of(^^Derived,
|
||||
meta::access_context::current()))) {
|
||||
if constexpr (detail::is_test(member)) {
|
||||
const auto tags = detail::annotation_values<member, Tag>();
|
||||
if (selected_tag.empty() || std::ranges::find(tags, selected_tag) != tags.end()) {
|
||||
++matched;
|
||||
constexpr auto name = meta::identifier_of(member);
|
||||
const auto reasons = detail::annotation_values<member, Skip>();
|
||||
if (!reasons.empty()) {
|
||||
++skipped;
|
||||
std::cout << "[SKIP] " << name << " (" << reasons.front() << ")\n";
|
||||
} else {
|
||||
try {
|
||||
Derived suite{};
|
||||
if constexpr (requires { suite.setup(); }) suite.setup();
|
||||
suite.[:member:]();
|
||||
std::cout << "[PASS] " << name << '\n';
|
||||
} catch (const std::exception& error) {
|
||||
++failed;
|
||||
std::cerr << "[FAIL] " << name << ": " << error.what() << '\n';
|
||||
} catch (...) {
|
||||
++failed;
|
||||
std::cerr << "[FAIL] " << name << ": unexpected exception\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if (matched == 0) {
|
||||
std::cerr << "No tests matched";
|
||||
if (!selected_tag.empty()) std::cerr << " tag " << selected_tag;
|
||||
std::cerr << '\n';
|
||||
return 1;
|
||||
}
|
||||
std::cout << matched - failed - skipped << " passed, " << failed << " failed, " << skipped << " skipped\n";
|
||||
return failed == 0 ? 0 : 1;
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace Void::Testing
|
||||
Reference in new issue
Block a user