okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fe57a0cb5
main
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" ); 18if (!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 ; 25explicit Fed (const std::string & script ) { 26 std::istringstream is (script ); 27in .run (is ); 28 std::string line ; 29long n = 0 ; 30while (in .next (line ,n , false))q .emplace_back (line ,n ); 31 } 32}; 33 34}// namespace 35 36int main () { 37 { 38Fed f ("GEN 5 1,2\nBGEN 1 5 3\nQUIT\n" ); 39expect (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" ); 41expect (f .in .closed (),"closed at the end of input with the queue drained" ); 42 } 43 { 44Fed f ("STOP\nGEN 5 1,2\n" ); 45f .in .begin (f .q [0 ].second ); 46expect (!f .in .stop_now ,"a STOP before a GEN's line does not stop it" ); 47f .in .end (); 48 } 49 { 50Fed f ("GEN 5 1,2\nSTOP\n" ); 51f .in .begin (f .q [0 ].second ); 52expect (f .in .stop_now ,"a STOP after a GEN's line stops it, though the engine reaches it later" ); 53f .in .end (); 54expect (!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 59Fed f ("GEN 5 1\n" ); 60f .in .begin (f .q [0 ].second ); 61 std::istringstream more ("STOP\nGEN 5 2\n" ); 62f .in .run (more ); 63expect (f .in .stop_now ,"a STOP for the running request survives the next request's line" ); 64f .in .end (); 65 std::string line ; 66long n = 0 ; 67f .in .next (line ,n , false); 68f .in .begin (n ); 69expect (!f .in .stop_now ,"... and the request whose line came after it is not stopped" ); 70f .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) 75Fed f ("GEN 5 1\nGEN 5 2\nSTOP\n" ); 76f .in .begin (f .q [0 ].second ); 77const bool first = f .in .stop_now ; 78f .in .end (); 79f .in .begin (f .q [1 ].second ); 80expect (!first && f .in .stop_now ,"a STOP after two queued requests' lines stops the latest and not the older one" ); 81f .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 85Fed f ("GEN 5 1\nGEN 5 2\n" ); 86f .in .begin (f .q [0 ].second ); 87 std::istringstream more ("STOP\n" ); 88f .in .run (more ); 89expect (!f .in .stop_now ,"a STOP for the latest line does not stop an older running request" ); 90f .in .end (); 91f .in .begin (f .q [1 ].second ); 92expect (f .in .stop_now ,"... and stops the latest when it begins" ); 93f .in .end (); 94 } 95 { 96Fed f ("BSTOP 2\nBYIELD 2\nBGEN 2 5 1\n" ); 97const long n = f .q [0 ].second ; 98expect (!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 { 101Fed f ("BGEN 2 5 1\nBGEN 3 5 1\nBSTOP 2\nBYIELD 3\n" ); 102const long a = f .q [0 ].second ,b = f .q [1 ].second ; 103expect (f .in .bstopped (2 ,a )&& !f .in .bstopped (3 ,b ),"BSTOP acts on its own slot's request only" ); 104expect (f .in .yielded (3 ,b )&& !f .in .yielded (2 ,a ),"BYIELD acts on its own slot's request only" ); 105 } 106 { 107Fed f ("BGEN 2 5 1\nBSTOP 2\nBGEN 2 5 9\n" ); 108expect (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 { 112Fed f ("GEN 5 1,2\nBYIELD 4\n" ); 113expect (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 { 116Fed f ("BYIELD 4\nGEN 5 1,2\n" ); 117expect (f .in .solo_yield (f .q [0 ].second )== -1 ,"a BYIELD before a GEN's line does not act on it" ); 118 } 119 { 120Fed f ("GEN 5 1\nBYIELD 4\nGEN 5 2\n" ); 121expect (f .in .solo_yield (f .q [1 ].second )== -1 ,"a BYIELD for one GEN does not act on the next" ); 122 } 123 { 124Fed 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 127expect (f .in .yielded (3 ,f .q [1 ].second ),"BYIELD for a BGEN's slot acts on that BGEN" ); 128 } 129 { 130Fed f ("BSTOP 9\nBYIELD -1\nGEN 5 1\n" ); 131expect (f .q .size ()== 1 && f .q [0 ].first == "GEN 5 1" ,"BSTOP / BYIELD outside the slots are dropped, not queued" ); 132expect (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) 136Fed f ("BSTOP x\n" ); 137expect (f .q .size ()== 1 && f .q [0 ].second == 0 ,"a malformed BSTOP is queued as a line, not a request" ); 138 } 139 { 140Fed f ("GEN 5 1\r\nSTOP\r\n" ); 141f .in .begin (f .q [0 ].second ); 142expect (f .q [0 ].first == "GEN 5 1" && f .in .stop_now ,"CRLF line ends" ); 143f .in .end (); 144 } 145 std::printf (failures ?"sfnx_input_test: %d FAILED\n" :"sfnx_input_test: all ok\n" ,failures ); 146return failures ?1 :0 ; 147}