|
4 | 4 | // This program reads stdin and asserts that it is the expected value repeated |
5 | 5 | // until stdin ends |
6 | 6 |
|
7 | | -// Intentionally a prime length to make hitting buffering edge cases more likely |
8 | | -const char expected_repeat[] = {'e', 'x', 'a', 'm', 'p', 'l', 'e'}; |
9 | | - |
10 | 7 | int main(int argc, char** argv) |
11 | 8 | { |
12 | | - size_t consumed_prefix = 0; |
13 | 9 | char buffer[20]; |
| 10 | + // The repeated string 'example' is intentionally a prime length to make hitting buffering edge |
| 11 | + // cases more likely |
| 12 | + const char expected[] = "exampleexampleexampleexamp"; |
| 13 | + size_t offset = 0; // always between 0 and 6 |
14 | 14 | for (;;) |
15 | 15 | { |
16 | 16 | size_t read_amount = fread(buffer, 1, sizeof(buffer), stdin); |
17 | 17 | if (argc > 1) |
18 | 18 | { |
19 | 19 | puts(argv[1]); |
| 20 | + fflush(stdout); |
20 | 21 | } |
21 | | - |
22 | 22 | if (read_amount == 0) |
23 | 23 | { |
24 | 24 | if (feof(stdin)) |
25 | 25 | { |
26 | 26 | puts("success"); |
27 | 27 | return 0; |
28 | 28 | } |
29 | | - |
30 | 29 | return 1; |
31 | 30 | } |
32 | 31 |
|
33 | | - size_t prefix_check = read_amount; |
34 | | - if (prefix_check <= (sizeof(expected_repeat) - consumed_prefix)) |
35 | | - { |
36 | | - // check a partial suffix (this read didn't include a complete suffix) |
37 | | - if (memcmp(buffer, expected_repeat + consumed_prefix, prefix_check) != 0) |
38 | | - { |
39 | | - return 2; |
40 | | - } |
41 | | - |
42 | | - consumed_prefix += prefix_check; |
43 | | - continue; |
44 | | - } |
45 | | - |
46 | | - // check a whole suffix to realign to expected_repeat blocks |
47 | | - prefix_check = sizeof(expected_repeat) - consumed_prefix; |
48 | | - if (memcmp(buffer, expected_repeat + consumed_prefix, prefix_check) != 0) |
49 | | - { |
50 | | - return 3; |
51 | | - } |
52 | | - |
53 | | - size_t checked = prefix_check; |
54 | | - for (; read_amount - checked >= sizeof(expected_repeat); checked += sizeof(expected_repeat)) |
55 | | - { |
56 | | - if (memcmp(buffer + checked, expected_repeat, sizeof(expected_repeat)) != 0) |
57 | | - { |
58 | | - return 4; |
59 | | - } |
60 | | - } |
61 | | - |
62 | | - consumed_prefix = read_amount - checked; |
63 | | - if (memcmp(buffer + checked, expected_repeat, consumed_prefix) != 0) |
| 32 | + if (memcmp(buffer, expected + offset, read_amount) != 0) |
64 | 33 | { |
65 | | - return 5; |
| 34 | + return 2; |
66 | 35 | } |
| 36 | + offset = (offset + read_amount) % 7; |
67 | 37 | } |
68 | 38 | } |
0 commit comments