okami.mom/strata

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

Niko1221rc1 merge split (1ba231217)fe57a0cb5

main
5.8 KiB147 linesraw
1// sfnx_input_test: sfnx's stdin handling (sfnx_input.hpp) on scripted input, no GPU: which request each STOP, BSTOP
2// and BYIELD acts on, whatever order the lines come in and however long a request waits in the queue.
3//
4//   bin/sfnx_input_test
5#include "sfnx_input.hpp"
6
7#include <cstdio>
8#include <sstream>
9#include <string>
10#include <vector>
11
12namespace {
13
14int failures = 0;
15
16void expect(bool ok, const char* what) {
17    std::printf("%-72s %s\n", what, ok ? "ok" : "FAIL");
18    if (!ok) ++failures;
19}
20
21// the queued lines of `script` (all of it read first: the engine had not reached any of them), with their numbers
22struct Fed {
23    sfnx::Input in;
24    std::vector<std::pair<std::string, long>> q;
25    explicit Fed(const std::string& script) {
26        std::istringstream is(script);
27        in.run(is);
28        std::string line;
29        long n = 0;
30        while (in.next(line, n, false)) q.emplace_back(line, n);
31    }
32};
33
34}  // namespace
35
36int main() {
37    {
38        Fed f("GEN 5 1,2\nBGEN 1 5 3\nQUIT\n");
39        expect(f.q.size() == 3 && f.q[0].second == 1 && f.q[1].second == 2 && f.q[2].second == 0,
40               "requests numbered in arrival order, other lines 0");
41        expect(f.in.closed(), "closed at the end of input with the queue drained");
42    }
43    {
44        Fed f("STOP\nGEN 5 1,2\n");
45        f.in.begin(f.q[0].second);
46        expect(!f.in.stop_now, "a STOP before a GEN's line does not stop it");
47        f.in.end();
48    }
49    {
50        Fed f("GEN 5 1,2\nSTOP\n");
51        f.in.begin(f.q[0].second);
52        expect(f.in.stop_now, "a STOP after a GEN's line stops it, though the engine reaches it later");
53        f.in.end();
54        expect(!f.in.stop_now, "stop_now is down between requests");
55    }
56    {
57        // the case that clearing on arrival got wrong: A runs, STOP is meant for it, and B's line comes in before the
58        // engine looks at the flag
59        Fed f("GEN 5 1\n");
60        f.in.begin(f.q[0].second);
61        std::istringstream more("STOP\nGEN 5 2\n");
62        f.in.run(more);
63        expect(f.in.stop_now, "a STOP for the running request survives the next request's line");
64        f.in.end();
65        std::string line;
66        long n = 0;
67        f.in.next(line, n, false);
68        f.in.begin(n);
69        expect(!f.in.stop_now, "... and the request whose line came after it is not stopped");
70        f.in.end();
71    }
72    {
73        // a STOP after two queued lines: for the latest only (the front end sends a request's line and its STOP under one
74        // control lock; the older line is another client's request)
75        Fed f("GEN 5 1\nGEN 5 2\nSTOP\n");
76        f.in.begin(f.q[0].second);
77        const bool first = f.in.stop_now;
78        f.in.end();
79        f.in.begin(f.q[1].second);
80        expect(!first && f.in.stop_now, "a STOP after two queued requests' lines stops the latest and not the older one");
81        f.in.end();
82    }
83    {
84        // a STOP while an older request runs and a newer line is queued: for the newer one, not the running one
85        Fed f("GEN 5 1\nGEN 5 2\n");
86        f.in.begin(f.q[0].second);
87        std::istringstream more("STOP\n");
88        f.in.run(more);
89        expect(!f.in.stop_now, "a STOP for the latest line does not stop an older running request");
90        f.in.end();
91        f.in.begin(f.q[1].second);
92        expect(f.in.stop_now, "... and stops the latest when it begins");
93        f.in.end();
94    }
95    {
96        Fed f("BSTOP 2\nBYIELD 2\nBGEN 2 5 1\n");
97        const long n = f.q[0].second;
98        expect(!f.in.bstopped(2, n) && !f.in.yielded(2, n), "BSTOP / BYIELD before a slot's BGEN line do not act on it");
99    }
100    {
101        Fed f("BGEN 2 5 1\nBGEN 3 5 1\nBSTOP 2\nBYIELD 3\n");
102        const long a = f.q[0].second, b = f.q[1].second;
103        expect(f.in.bstopped(2, a) && !f.in.bstopped(3, b), "BSTOP acts on its own slot's request only");
104        expect(f.in.yielded(3, b) && !f.in.yielded(2, a), "BYIELD acts on its own slot's request only");
105    }
106    {
107        Fed f("BGEN 2 5 1\nBSTOP 2\nBGEN 2 5 9\n");
108        expect(f.in.bstopped(2, f.q[0].second) && !f.in.bstopped(2, f.q[1].second),
109               "a slot's next BGEN is not stopped by the BSTOP for the one before");
110    }
111    {
112        Fed f("GEN 5 1,2\nBYIELD 4\n");
113        expect(f.in.solo_yield(f.q[0].second) == 4, "BYIELD after a GEN's line gives its read way into the slot named");
114    }
115    {
116        Fed f("BYIELD 4\nGEN 5 1,2\n");
117        expect(f.in.solo_yield(f.q[0].second) == -1, "a BYIELD before a GEN's line does not act on it");
118    }
119    {
120        Fed f("GEN 5 1\nBYIELD 4\nGEN 5 2\n");
121        expect(f.in.solo_yield(f.q[1].second) == -1, "a BYIELD for one GEN does not act on the next");
122    }
123    {
124        Fed f("GEN 5 1\nBGEN 3 5 1\nBYIELD 3\n");
125        // a BYIELD names a slot: for BGEN 3 it is that read's; the solo request before it is not on the control lines
126        // then (the front end sends one request's control lines at a time), so its number is behind
127        expect(f.in.yielded(3, f.q[1].second), "BYIELD for a BGEN's slot acts on that BGEN");
128    }
129    {
130        Fed f("BSTOP 9\nBYIELD -1\nGEN 5 1\n");
131        expect(f.q.size() == 1 && f.q[0].first == "GEN 5 1", "BSTOP / BYIELD outside the slots are dropped, not queued");
132        expect(f.q[0].second == 1, "and take no request number");
133    }
134    {
135        // "BSTOP x" has no slot number: it is no control line, so it is queued (the engine answers ERR for it)
136        Fed f("BSTOP x\n");
137        expect(f.q.size() == 1 && f.q[0].second == 0, "a malformed BSTOP is queued as a line, not a request");
138    }
139    {
140        Fed f("GEN 5 1\r\nSTOP\r\n");
141        f.in.begin(f.q[0].second);
142        expect(f.q[0].first == "GEN 5 1" && f.in.stop_now, "CRLF line ends");
143        f.in.end();
144    }
145    std::printf(failures ? "sfnx_input_test: %d FAILED\n" : "sfnx_input_test: all ok\n", failures);
146    return failures ? 1 : 0;
147}