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