101 lines
4.3 KiB
C++
101 lines
4.3 KiB
C++
#include "gateway/npc_text.hpp"
|
|
#include <algorithm>
|
|
#include <array>
|
|
#include <bit>
|
|
#include <cmath>
|
|
#include <iostream>
|
|
#include <limits>
|
|
#include <random>
|
|
#include <stdexcept>
|
|
|
|
using namespace gateway;
|
|
|
|
namespace {
|
|
// The pinned Trinity urandweighted implementation uses std::discrete_distribution.
|
|
struct FixedRoll {
|
|
using result_type = uint64_t;
|
|
uint64_t value;
|
|
static constexpr result_type min() { return 0; }
|
|
static constexpr result_type max() { return UINT64_MAX; }
|
|
result_type operator()() { return value; }
|
|
};
|
|
|
|
void check(bool condition, const char* reason) {
|
|
if (!condition) throw std::runtime_error(reason);
|
|
}
|
|
|
|
Bytes reply(const std::array<float, 8>& weights, const std::array<uint32_t, 8>& ids) {
|
|
Bytes out;
|
|
le(out, 564, 4); le(out, 0x80, 1); le(out, 64, 4);
|
|
for (float v : weights) le(out, std::bit_cast<uint32_t>(v), 4);
|
|
for (uint32_t v : ids) le(out, v, 4);
|
|
return out;
|
|
}
|
|
|
|
void rejects(std::span<const uint8_t> body, double roll) {
|
|
try { npc_text::choose_greeting(body, roll); }
|
|
catch (const Error&) { return; }
|
|
throw std::runtime_error("malformed response or invalid roll accepted");
|
|
}
|
|
}
|
|
|
|
int main() try {
|
|
const auto weighted = reply({0, 1.5f, 0, 0, 4.5f}, {0, 2529, 0, 8229, 910210});
|
|
const double last = std::nextafter(1.0, 0.0);
|
|
for (double roll : {0.0, std::nextafter(0.25, 0.0), 0.25})
|
|
check(npc_text::choose_greeting(weighted, roll) == 2529, "first weighted interval");
|
|
for (double roll : {std::nextafter(0.25, 1.0), 0.9, last})
|
|
check(npc_text::choose_greeting(weighted, roll) == 910210, "second weighted interval");
|
|
const std::array<float, 8> oracle_weights{1.5f, 0.0f, 4.5f};
|
|
const std::array<uint32_t, 8> oracle_ids{2529, 0, 910210};
|
|
const auto oracle_body = reply(oracle_weights, oracle_ids);
|
|
std::discrete_distribution<size_t> oracle(oracle_weights.begin(), oracle_weights.end());
|
|
for (double roll : {0.0, std::nextafter(0.25, 0.0), 0.25, std::nextafter(0.25, 1.0), 0.8, last}) {
|
|
FixedRoll engine{static_cast<uint64_t>(std::ldexp(roll, 64))};
|
|
check(npc_text::choose_greeting(oracle_body, roll) == oracle_ids[oracle(engine)],
|
|
"selection differs from the native weighted distribution");
|
|
}
|
|
const float huge = std::numeric_limits<float>::max();
|
|
check(npc_text::choose_greeting(reply({huge, huge}, {1, 2}), 0.75) == 2,
|
|
"weights accumulate without float overflow");
|
|
const float tiny = std::numeric_limits<float>::denorm_min();
|
|
check(npc_text::choose_greeting(reply({0, tiny}, {0, 2}), last) == 2,
|
|
"a positive subnormal weight remains selectable");
|
|
check(npc_text::choose_greeting(reply({}, {0, 2529, 0, 8229}), 0.3) == 2529,
|
|
"zero total shows the first authored greeting (Tortoise zero-weight rows)");
|
|
|
|
Bytes original;
|
|
le(original, 564, 4);
|
|
for (unsigned i = 0; i < 8; ++i) {
|
|
le(original, 0, 4);
|
|
cstring(original, "Greetings $N"); cstring(original, "Greetings $N");
|
|
for (unsigned j = 0; j < 7; ++j) le(original, 0, 4);
|
|
}
|
|
const auto fallback = npc_text::native_reply(original);
|
|
for (double roll : {0.0, 0.5, last})
|
|
check(npc_text::choose_greeting(fallback, roll) == 16777200, "original fallback");
|
|
auto denied = npc_text::native_reply(original, [](const auto&) { return 0u; });
|
|
check(npc_text::choose_greeting(denied, 0) == 0, "unresolved lookup stays unavailable");
|
|
denied.push_back(0);
|
|
rejects(denied, 0);
|
|
for (size_t i = 0; i < weighted.size(); ++i)
|
|
rejects(std::span(weighted).first(i), 0);
|
|
auto corrupt = weighted;
|
|
corrupt.push_back(0); rejects(corrupt, 0);
|
|
corrupt = weighted; corrupt[4] = 0x81; rejects(corrupt, 0);
|
|
corrupt = weighted; corrupt[5] = 63; rejects(corrupt, 0);
|
|
corrupt = weighted; std::fill_n(corrupt.begin(), 4, 0); rejects(corrupt, 0);
|
|
rejects(reply({1, 1}, {3}), 0);
|
|
rejects(reply({}, {}), 0);
|
|
for (float w : {-1.f, std::numeric_limits<float>::quiet_NaN(), std::numeric_limits<float>::infinity()})
|
|
rejects(reply({1, w}, {1, 2}), 0);
|
|
for (double roll : {-0.01, 1.0, std::numeric_limits<double>::quiet_NaN(), std::numeric_limits<double>::infinity()})
|
|
rejects(weighted, roll);
|
|
|
|
std::cout << "NPC greeting choice, exact fallback and malformed replies passed.\n";
|
|
return 0;
|
|
} catch (const std::exception& e) {
|
|
std::cerr << e.what() << '\n';
|
|
return 1;
|
|
}
|