okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221rc1 merge split-hip (2c1771dce): AMD split, opt-in3227bd218

main
2.7 KiB55 linesraw
1// The peer-push decision (src/hip/peer_policy.hpp): FNX_PEER=0 forces the host exchange, a failing probe leaves the host exchange for the whole run with one plain
2// line, a forced probe failure (FNX_PEER_PROBE_FAIL=1) does the same, and nothing runs unless the policy says so. CPU only.
3#include <cstdio>
4#include <cstdlib>
5#include <string>
6
7#include "../src/hip/peer_policy.hpp"
8
9static int bad = 0;
10#define CHECK(c) do { if (!(c)) { std::printf("FAIL line %d: %s\n", __LINE__, #c); ++bad; } } while (0)
11
12int main() {
13    using namespace fnx;
14    int calls = 0;
15    auto pass = [&](std::string& why) { ++calls; why = "all pairs ok"; return true; };
16    auto fail = [&](std::string& why) { ++calls; why = "card 0 -> card 2: 3 of 1048576 words arrived wrong"; return false; };
17    unsetenv("FNX_PEER_PROBE_FAIL");
18
19    // FNX_PEER=0: the host exchange, no probe, no line, whatever the default
20    calls = 0;
21    PeerDecision d = peer_decide("0", true, pass);
22    CHECK(!d.use && d.line.empty() && calls == 0);
23    // unset and the default off: nothing, no probe
24    d = peer_decide(nullptr, false, pass);
25    CHECK(!d.use && d.line.empty() && calls == 0);
26    // unset and the default on: the probe runs and decides
27    d = peer_decide(nullptr, true, pass);
28    CHECK(d.use && calls == 1 && d.line.find("Peer push between the cards is on") == 0);
29    // FNX_PEER=1: the probe runs even if the default is off
30    calls = 0;
31    d = peer_decide("1", false, pass);
32    CHECK(d.use && calls == 1);
33    // a failing probe: the host exchange for the whole run and one plain line naming the reason
34    calls = 0;
35    d = peer_decide("1", false, fail);
36    CHECK(!d.use && calls == 1);
37    CHECK(d.line == "The cards cannot push into each other's memory here (card 0 -> card 2: 3 of 1048576 words arrived wrong): using the host exchange for this run.");
38    CHECK(d.line.find('\n') == std::string::npos);
39    // the same through the default
40    d = peer_decide(nullptr, true, fail);
41    CHECK(!d.use);
42    // a forced probe failure (the tester hook): the probe is not even called, the line says why
43    setenv("FNX_PEER_PROBE_FAIL", "1", 1);
44    calls = 0;
45    d = peer_decide("1", false, pass);
46    CHECK(!d.use && calls == 0 && d.line.find("forced failure (FNX_PEER_PROBE_FAIL=1)") != std::string::npos && d.line.find("using the host exchange for this run.") != std::string::npos);
47    d = peer_decide(nullptr, true, pass);
48    CHECK(!d.use && calls == 0);
49    // but FNX_PEER=0 stays silent and off
50    d = peer_decide("0", true, pass);
51    CHECK(!d.use && d.line.empty());
52    unsetenv("FNX_PEER_PROBE_FAIL");
53    std::printf(bad ? "peer_policy_test: %d FAILED\n" : "peer_policy_test: ok\n", bad);
54    return bad ? 1 : 0;
55}