Something went wrong. Try again.
[Mirror] Libreopolis is a cross-platform city simulation game fully compatible with SimCity 2000. gitlab.com/conasg/libreopolis
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223#include "sc2k/io/native_thing_continuation.hpp"
#include <cstddef>#include <cstdint>#include <cstdio>#include <limits>#include <span>#include <vector>
#include "test_harness.hpp"
using namespace sc2k;
namespace {
void set_u16(std::vector<std::uint8_t>& bytes, std::size_t offset, std::uint16_t value) { bytes[offset] = static_cast<std::uint8_t>(value); bytes[offset + 1] = static_cast<std::uint8_t>(value >> 8);}
void set_u32(std::vector<std::uint8_t>& bytes, std::size_t offset, std::uint32_t value) { for (int byte = 0; byte < 4; ++byte) bytes[offset + static_cast<std::size_t>(byte)] = static_cast<std::uint8_t>(value >> (8 * byte));}
sim::ThingContinuationState populated_state() { sim::ThingContinuationState state; state.vehicleRng.seed(0x12345678u); sim::CargoShipContinuation first; first.slot = 5; first.rawGoalX = 20; first.rawGoalY = 21; first.dwell = 172; first.berthX = 30; first.berthY = 31; first.pierX = 30; first.pierY = 32; first.waypoints = {{10, 11}, {15, 16}, {30, 31}}; first.cursor = 2; state.cargoShips.push_back(first);
sim::CargoShipContinuation second; second.slot = 17; second.rawGoalX = 100; second.rawGoalY = 101; second.failed = true; state.cargoShips.push_back(second); CHECK(sim::validate_thing_continuation(state) == sim::ThingContinuationValidationError::None); return state;}
City matching_city() { City city; for (int x = 8; x <= 35; ++x) for (int y = 8; y <= 35; ++y) { city.xter.at(x, y) = 0x10; city.xbit.at(x, y) = 0x04; } city.xbld.at(30, 32) = 0xdf; constexpr std::uint8_t slot = 5; city.xthg[slot * 12 + 0] = 3; city.xthg[slot * 12 + 2] = 3; city.xthg[slot * 12 + 3] = 10; city.xthg[slot * 12 + 4] = 11; city.xthg[slot * 12 + 8] = 20; city.xthg[slot * 12 + 9] = 21; city.xtxt.at(10, 11) = static_cast<std::uint8_t>( static_cast<unsigned>(xtxt::kThingFirst) + slot); sim::ThingContinuationState state = populated_state(); state.cargoShips.resize(1); CHECK(sim::thing_continuation_matches_city(state, city)); return city;}
void test_round_trip_and_shape() { const sim::ThingContinuationState state = populated_state(); const auto first = io::encode_native_thing_continuation(state); const auto second = io::encode_native_thing_continuation(state); CHECK(first.has_value()); CHECK(first == second); if (!first) return; constexpr std::size_t expected = io::kNativeThingContinuationHeaderBytes + 2 * io::kNativeCargoShipContinuationBytes + 3 * io::kNativeCargoShipWaypointBytes; CHECK(first->size() == expected); CHECK((*first)[0] == 'T' && (*first)[1] == 'H' && (*first)[2] == 'N' && (*first)[3] == '1'); CHECK(io::decode_native_thing_continuation(*first) == state);
const sim::ThingContinuationState empty; const auto encodedEmpty = io::encode_native_thing_continuation(empty); CHECK(encodedEmpty.has_value()); CHECK(encodedEmpty && encodedEmpty->size() == io::kNativeThingContinuationHeaderBytes); CHECK(encodedEmpty && io::decode_native_thing_continuation(*encodedEmpty) == empty);}
void test_structural_reserved_and_overflow_rejection() { const auto encoded = io::encode_native_thing_continuation(populated_state()); CHECK(encoded.has_value()); if (!encoded) return;
for (std::size_t size = 0; size < encoded->size(); ++size) { const std::span<const std::uint8_t> prefix(encoded->data(), size); CHECK(!io::decode_native_thing_continuation(prefix)); } auto malformed = *encoded; malformed.push_back(0); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; malformed[3] = '2'; CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 4, 2); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 6, 1); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 14, 1); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 12, 40); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; malformed[17] = 2; CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 18, 1); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 26, 1); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u32(malformed, 40, std::numeric_limits<std::uint32_t>::max()); CHECK(!io::decode_native_thing_continuation(malformed));
std::vector<std::uint8_t> oversized( io::kMaximumNativeThingContinuationPayloadBytes + 1, 0); CHECK(!io::decode_native_thing_continuation(oversized));}
void test_semantic_and_canonical_rejection() { const auto encoded = io::encode_native_thing_continuation(populated_state()); CHECK(encoded.has_value()); if (!encoded) return;
auto malformed = *encoded; malformed[16] = 0; CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 20, 0xfffeu); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 20, 0xffffu); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 24, sim::kMaximumCargoShipDwellTicks + 1); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u32(malformed, 36, 4); CHECK(!io::decode_native_thing_continuation(malformed)); malformed = *encoded; set_u16(malformed, 48, 10); set_u16(malformed, 50, 11); CHECK(!io::decode_native_thing_continuation(malformed));
// The second variable-size record begins after the first record's three waypoints. malformed = *encoded; constexpr std::size_t secondRecord = io::kNativeThingContinuationHeaderBytes + io::kNativeCargoShipContinuationBytes + 3 * io::kNativeCargoShipWaypointBytes; malformed[secondRecord] = 4; CHECK(!io::decode_native_thing_continuation(malformed));
sim::ThingContinuationState invalid = populated_state(); invalid.cargoShips[0].cursor = 99; CHECK(!io::encode_native_thing_continuation(invalid)); invalid = populated_state(); invalid.cargoShips[0].waypoints.resize( sim::kMaximumCargoShipWaypoints + 1, {1, 1}); CHECK(!io::encode_native_thing_continuation(invalid));}
void test_city_checked_overloads() { City city = matching_city(); sim::ThingContinuationState state = populated_state(); state.cargoShips.resize(1); const auto encoded = io::encode_native_thing_continuation(state, city); CHECK(encoded.has_value()); CHECK(encoded && io::decode_native_thing_continuation(*encoded, city) == state);
city.xtxt.at(10, 11) = 0; CHECK(!io::encode_native_thing_continuation(state, city)); CHECK(encoded && !io::decode_native_thing_continuation(*encoded, city));}
} // namespace
int main() { test_round_trip_and_shape(); test_structural_reserved_and_overflow_rejection(); test_semantic_and_canonical_rejection(); test_city_checked_overloads();
if (failures == 0) { std::printf("native thing continuation: all checks passed\n"); return 0; } std::printf("native thing continuation: %d failure(s)\n", failures); return 1;}