rps-cuda/tests/contracts.cpp
2026-11-26 09:07:21 +03:00

103 lines
4 KiB
C++

#include "wire.hpp"
static void transitions() {
constexpr int orders[6][3] = {{1, 2, 3}, {1, 3, 2}, {2, 1, 3},
{2, 3, 1}, {3, 1, 2}, {3, 2, 1}};
for (auto &order : orders)
for (int state = 0; state < 4; ++state)
for (int mask = 0; mask < 8; ++mask) {
const auto seen = [mask](int species) {
return bool(mask & (1 << (species - 1)));
};
int expected = state;
if (state == 0) {
for (auto species : order)
if (seen(species)) {
expected = species;
break;
}
} else if (seen(state % 3 + 1))
expected = state % 3 + 1;
bool a = state & 1, b = state & 2, empty = !a && !b;
bool wins =
(a && !b && seen(2)) || (!a && b && seen(3)) || (a && b && seen(1));
bool candidates[4]{}, higher = false;
for (auto species : order) {
candidates[species] = seen(species) && !higher;
higher |= seen(species);
}
bool low = (a ^ (wins && (a ^ b))) ||
(empty && (candidates[1] || candidates[3]));
bool high =
(b ^ (wins && a)) || (empty && (candidates[2] || candidates[3]));
require((int(low) + 2 * high) == expected && (!state || expected),
"transition equation or occupancy invariant");
}
}
int main(int argc, char **argv) {
transitions();
auto raw = readConfig(argv[1]);
auto c = parse(raw);
require(c.seed == 42 && c.bytes() == 786432, "example");
for (auto [seed, p] : std::vector<std::pair<u64, int>>{
{0, 5}, {1, 0}, {42, 4}, {12345, 0}, {UINT64_MAX, 3}})
require(priority(seed) == p, "EVP vector");
for (auto [seed, hash] :
std::vector<std::pair<u64, u64>>{{0, 0x6674f8e1bf06b711ULL},
{1, 0x6dfadca188a614c0ULL},
{2, 0xc7e589e42b2fc13dULL},
{4, 0x6db8b412fa32d5d4ULL},
{5, 0x32deeebde34fa78fULL},
{8, 0x2873390fd319c916ULL},
{42, 0x7c62b6526f730efaULL},
{12345, 0xafab81b606544998ULL},
{UINT64_MAX, 0x88e38984a5df2ff7ULL}})
require(hash64(seed) == hash, "exact EVP digest prefix");
auto reject = [](std::string s) {
try {
parse(s);
} catch (const std::exception &) {
return;
}
throw std::runtime_error("invalid config accepted");
};
reject("{\"protocol_version\":1," + raw.substr(raw.find('{') + 1));
for (auto seed : {"01", "-1", "18446744073709551616", "42 "}) {
auto j = Json::parse(raw);
j["simulation"]["seed"] = seed;
reject(j.dump());
}
auto j = Json::parse(raw);
j["view"]["roi"]["x"] = UINT32_MAX;
reject(j.dump());
j = Json::parse(raw);
j["simulation"]["width"] = 1024.0;
reject(j.dump());
j = Json::parse(raw);
j["simulation"]["seed"] = 42;
reject(j.dump());
j = Json::parse(raw);
j["snapshots"]["fps"] = 3;
reject(j.dump());
reject(raw + std::string(1, char(0)) + "suffix");
for (auto [path, value] : std::vector<std::pair<const char *, Json>>{
{"/simulation/width", 0},
{"/simulation/width", 1025},
{"/simulation/height", 49},
{"/simulation/neighbors", 6},
{"/simulation/generations", 0},
{"/simulation/max_generations_per_second", 241},
{"/protocol_version", 2},
{"/view/aggregation_side", 3},
{"/view/roi/x", 1},
{"/view/roi/width", 0},
{"/view/roi/height", -1},
{"/view/palette", nullptr},
{"/view/aggregation_side", 1}}) {
auto bad = Json::parse(raw);
bad[Json::json_pointer(path)] = value;
reject(bad.dump());
}
Bytes b;
put(b, UINT64_C(0x0123456789abcdef), 8);
require(b == Bytes{1, 35, 69, 103, 137, 171, 205, 239}, "wire order");
}