|
| 1 | +#include "../utilities/template.h" |
| 2 | + |
| 3 | +#include "../../content/various/FastInput.h" |
| 4 | +#include <unistd.h> |
| 5 | + |
| 6 | +constexpr int BUF_SIZE = sizeof(gc.buf); |
| 7 | + |
| 8 | +string tempdirname; |
| 9 | +string tempfilename; |
| 10 | + |
| 11 | +void test(const string& s, vi ints = {}) { |
| 12 | + gc.bc = gc.be = 0; |
| 13 | + ofstream fout(tempfilename); |
| 14 | + fout << s; |
| 15 | + fout.close(); |
| 16 | + FILE* ret = freopen(tempfilename.c_str(), "r", stdin); |
| 17 | + assert(ret == stdin); |
| 18 | + if (ints.empty()) { |
| 19 | + for (char c : s) { |
| 20 | + int c2 = gc(); |
| 21 | + assert(c == c2); |
| 22 | + } |
| 23 | + assert(gc() == 0); |
| 24 | + assert(gc() == 0); |
| 25 | + } else { |
| 26 | + for (int x : ints) { |
| 27 | + int y = readInt(); |
| 28 | + if (x != y) { |
| 29 | + cerr << "On input " << s << ", read " << y << " but expected " << x << endl; |
| 30 | + } |
| 31 | + assert(x == y); |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +int main() { |
| 37 | + // Unit test, not stress test, but oh well. |
| 38 | + char pattern[] = "/tmp/fastinputXXXXXX"; |
| 39 | + tempdirname = mkdtemp(pattern); |
| 40 | + tempfilename = tempdirname + "/stdin.txt"; |
| 41 | + |
| 42 | + // First test that the getchar implementation is correct: |
| 43 | + test(""); |
| 44 | + test("a"); |
| 45 | + test("ab"); |
| 46 | + string s; |
| 47 | + for (int i = 0; i < BUF_SIZE; i++) s += (char)(i % 13); |
| 48 | + test(s); |
| 49 | + for (int i = 0; i < BUF_SIZE * 10 + 1; i++) s += (char)(i % 13); |
| 50 | + test(s); |
| 51 | + for (int i = 0; i < BUF_SIZE - 2; i++) s += (char)(i % 13); |
| 52 | + test(s); |
| 53 | + for (int i = 0; i < BUF_SIZE + 2; i++) { |
| 54 | + assert(gc() == 0); |
| 55 | + } |
| 56 | + |
| 57 | + // Then test that readInt() is: |
| 58 | + test("1", {1}); |
| 59 | + test("12", {12}); |
| 60 | + test("9\n", {9}); |
| 61 | + test("12 ", {12}); |
| 62 | + test("-23\n", {-23}); |
| 63 | + test(" -4", {-4}); |
| 64 | + test(" 5\n", {5}); |
| 65 | + test("1 -2 ", {1, -2}); |
| 66 | + test(" -34 56 ", {-34, 56}); |
| 67 | + test(" \t\r\n5 -2 ", {5}); |
| 68 | + |
| 69 | + unlink(tempfilename.c_str()); |
| 70 | + rmdir(tempdirname.c_str()); |
| 71 | + cout << "Tests passed!" << endl; |
| 72 | +} |
0 commit comments