398 lines
18 KiB
JSON
398 lines
18 KiB
JSON
{
|
|
"context": {
|
|
"probe.h": "#ifndef PROBE_H\n#define PROBE_H\n#include <stdint.h>\n#include <stddef.h>\n#include <stdio.h>\n#define PROBE_ARGS 6u\n#define PROBE_WORDS 96u\n#define PROBE_HISTORY 16u\n#define PROBE_LINE 512u\n\ntypedef struct {\n char name;\n uint32_t args[PROBE_ARGS];\n unsigned count;\n} Command;\n\ntypedef struct {\n int rc;\n uint32_t value;\n uint32_t detail;\n} Result;\n\ntypedef struct {\n unsigned sequence;\n char command;\n Result result;\n uint32_t digest;\n} Record;\n\ntypedef struct {\n Record records[PROBE_HISTORY];\n unsigned next;\n unsigned used;\n uint64_t accepted;\n uint64_t rejected;\n uint64_t malformed;\n uint64_t bytes;\n} History;\n\ntypedef struct {\n int json;\n int brief;\n int history;\n int statistics;\n int help;\n} Options;\n\ntypedef struct {\n uint32_t words[PROBE_WORDS];\n} State;\n\nunsigned service_arity(char name);\nconst char *service_name(void);\nvoid service_help(FILE *out);\nvoid service_reset(State *state);\nResult service_step(State *state, const Command *command);\nint parse_command(const char *line, Command *command);\nint parse_options(int argc, char **argv, Options *options);\nint read_line(FILE *input, char *buffer, size_t capacity, size_t *bytes);\nint line_ignored(const char *line);\nvoid print_help(FILE *out);\nvoid print_observation(FILE *out, unsigned sequence, const Result *result,\n const State *state, const Options *options);\nvoid print_invalid(FILE *out, unsigned sequence, const Options *options);\nvoid history_reset(History *history);\nvoid history_push(History *history, unsigned sequence, const Command *command,\n const Result *result, const State *state);\nvoid history_invalid(History *history);\nvoid history_add_bytes(History *history, size_t count);\nvoid history_print(FILE *out, const History *history, const Options *options);\nuint32_t observation_digest(const State *state);\nvoid print_u32(FILE *out, uint32_t value);\nvoid print_u64(FILE *out, uint64_t value);\nvoid print_int(FILE *out, int value);\nint stream_finish(FILE *input, FILE *output);\n#endif\n",
|
|
"input.c": "#include \"probe.h\"\n#include <ctype.h>\n#include <errno.h>\n#include <stdlib.h>\n#include <string.h>\n\nstatic const char *skip_space(const char *cursor)\n{\n while (*cursor && isspace((unsigned char)*cursor)) {\n cursor++;\n }\n return cursor;\n}\n\nstatic int read_unsigned(const char **cursor, uint32_t *value)\n{\n const char *start = skip_space(*cursor);\n unsigned long long parsed;\n char *end;\n if (!isdigit((unsigned char)*start)) {\n return 0;\n }\n errno = 0;\n parsed = strtoull(start, &end, 0);\n if (end == start || errno != 0 || parsed > UINT32_MAX) {\n return 0;\n }\n if (*end && !isspace((unsigned char)*end) && *end != '#') {\n return 0;\n }\n *value = (uint32_t)parsed;\n *cursor = end;\n return 1;\n}\n\nint parse_command(const char *line, Command *command)\n{\n const char *cursor = skip_space(line);\n unsigned required;\n unsigned index;\n memset(command, 0, sizeof(*command));\n if (!*cursor || *cursor == '#') {\n return 0;\n }\n command->name = *cursor++;\n if (*cursor && !isspace((unsigned char)*cursor)) {\n return 0;\n }\n required = service_arity(command->name);\n if (required > PROBE_ARGS) {\n return 0;\n }\n for (index = 0u; index < required; index++) {\n if (!read_unsigned(&cursor, &command->args[index])) {\n return 0;\n }\n }\n cursor = skip_space(cursor);\n if (*cursor && *cursor != '#') {\n return 0;\n }\n command->count = required;\n return 1;\n}\n\nint line_ignored(const char *line)\n{\n const char *cursor = skip_space(line);\n return !*cursor || *cursor == '#';\n}\n\nint read_line(FILE *input, char *buffer, size_t capacity, size_t *bytes)\n{\n int ch;\n size_t used = 0u;\n int overflow = 0;\n *bytes = 0u;\n if (capacity < 2u) {\n return -1;\n }\n while ((ch = fgetc(input)) != EOF) {\n (*bytes)++;\n if (ch == '\\n') {\n break;\n }\n if (used + 1u < capacity) {\n buffer[used++] = (char)ch;\n } else {\n overflow = 1;\n }\n }\n buffer[used] = '\\0';\n if (ch == EOF && !*bytes) {\n return 0;\n }\n return overflow ? -1 : 1;\n}\n\nint parse_options(int argc, char **argv, Options *options)\n{\n int index;\n memset(options, 0, sizeof(*options));\n for (index = 1; index < argc; index++) {\n if (strcmp(argv[index], \"--json\") == 0) {\n options->json = 1;\n } else if (strcmp(argv[index], \"--brief\") == 0) {\n options->brief = 1;\n } else if (strcmp(argv[index], \"--history\") == 0) {\n options->history = 1;\n } else if (strcmp(argv[index], \"--stats\") == 0) {\n options->statistics = 1;\n } else if (strcmp(argv[index], \"--help\") == 0) {\n options->help = 1;\n } else {\n return 0;\n }\n }\n return 1;\n}\n\nvoid print_help(FILE *out)\n{\n fprintf(out, \"%s deterministic service desk\\n\", service_name());\n fputs(\"Usage: ./probe [--json] [--brief] [--history] [--stats]\\n\", out);\n fputs(\"Commands read from stdin; # begins a comment.\\n\", out);\n fputs(\"Numbers use unsigned 32-bit decimal or 0x notation.\\n\", out);\n fputs(\"--json prints structured observations.\\n\", out);\n fputs(\"--brief omits raw snapshot words.\\n\", out);\n fputs(\"--history prints the last sixteen outcomes to stderr.\\n\", out);\n fputs(\"--stats prints stream counters to stderr.\\n\", out);\n service_help(out);\n}\n\nint stream_finish(FILE *input, FILE *output)\n{\n if (ferror(input)) {\n return 2;\n }\n if (fflush(output) == EOF) {\n return 2;\n }\n if (ferror(output)) {\n return 2;\n }\n return 0;\n}\n",
|
|
"output.c": "#include \"probe.h\"\n#include <inttypes.h>\n\nvoid print_u32(FILE *out, uint32_t value)\n{\n fprintf(out, \"%\" PRIu32, value);\n}\n\nvoid print_u64(FILE *out, uint64_t value)\n{\n fprintf(out, \"%\" PRIu64, value);\n}\n\nvoid print_int(FILE *out, int value)\n{\n fprintf(out, \"%d\", value);\n}\n\nuint32_t observation_digest(const State *state)\n{\n uint32_t digest = 2166136261u;\n unsigned index;\n unsigned shift;\n for (index = 0u; index < PROBE_WORDS; index++) {\n for (shift = 0u; shift < 32u; shift += 8u) {\n digest ^= (state->words[index] >> shift) & 255u;\n digest *= 16777619u;\n }\n }\n return digest;\n}\n\nstatic void text_result(FILE *out, unsigned sequence, const Result *result)\n{\n fprintf(out, \"O %u \", sequence);\n print_int(out, result->rc);\n fputc(' ', out);\n print_u32(out, result->value);\n fputc(' ', out);\n print_u32(out, result->detail);\n}\n\nstatic void text_snapshot(FILE *out, const State *state)\n{\n unsigned index;\n for (index = 0u; index < PROBE_WORDS; index++) {\n fputc(' ', out);\n print_u32(out, state->words[index]);\n }\n}\n\nstatic void json_result(FILE *out, unsigned sequence, const Result *result)\n{\n fprintf(out, \"{\\\"sequence\\\":%u,\\\"rc\\\":\", sequence);\n print_int(out, result->rc);\n fputs(\",\\\"value\\\":\", out);\n print_u32(out, result->value);\n fputs(\",\\\"detail\\\":\", out);\n print_u32(out, result->detail);\n}\n\nstatic void json_snapshot(FILE *out, const State *state)\n{\n unsigned index;\n fputs(\",\\\"words\\\":[\", out);\n for (index = 0u; index < PROBE_WORDS; index++) {\n if (index) {\n fputc(',', out);\n }\n print_u32(out, state->words[index]);\n }\n fputc(']', out);\n}\n\nvoid print_observation(FILE *out, unsigned sequence, const Result *result,\n const State *state, const Options *options)\n{\n if (options->json) {\n json_result(out, sequence, result);\n if (!options->brief) {\n json_snapshot(out, state);\n }\n fputs(\",\\\"digest\\\":\", out);\n print_u32(out, observation_digest(state));\n fputs(\"}\\n\", out);\n } else {\n text_result(out, sequence, result);\n if (!options->brief) {\n text_snapshot(out, state);\n }\n fputc(' ', out);\n print_u32(out, observation_digest(state));\n fputc('\\n', out);\n }\n}\n\nvoid print_invalid(FILE *out, unsigned sequence, const Options *options)\n{\n if (options->json) {\n fprintf(out, \"{\\\"sequence\\\":%u,\\\"input_error\\\":true}\\n\", sequence);\n } else {\n fprintf(out, \"I %u\\n\", sequence);\n }\n}\n",
|
|
"history.c": "#include \"probe.h\"\n#include <string.h>\n\nvoid history_reset(History *history)\n{\n memset(history, 0, sizeof(*history));\n}\n\nvoid history_add_bytes(History *history, size_t count)\n{\n history->bytes += count;\n}\n\nvoid history_invalid(History *history)\n{\n history->malformed++;\n}\n\nvoid history_push(History *history, unsigned sequence, const Command *command,\n const Result *result, const State *state)\n{\n Record *record = &history->records[history->next];\n record->sequence = sequence;\n record->command = command->name;\n record->result = *result;\n record->digest = observation_digest(state);\n history->next = (history->next + 1u) % PROBE_HISTORY;\n if (history->used < PROBE_HISTORY) {\n history->used++;\n }\n if (result->rc == 0) {\n history->accepted++;\n } else {\n history->rejected++;\n }\n}\n\nstatic void print_record(FILE *out, const Record *record)\n{\n fprintf(out, \"record %u %c rc=\", record->sequence, record->command);\n print_int(out, record->result.rc);\n fputs(\" value=\", out);\n print_u32(out, record->result.value);\n fputs(\" detail=\", out);\n print_u32(out, record->result.detail);\n fputs(\" digest=\", out);\n print_u32(out, record->digest);\n fputc('\\n', out);\n}\n\nstatic void print_count(FILE *out, const char *name, uint64_t value)\n{\n fputs(name, out);\n fputc('=', out);\n print_u64(out, value);\n fputc(' ', out);\n}\n\nvoid history_print(FILE *out, const History *history, const Options *options)\n{\n unsigned index;\n unsigned first;\n if (options->history) {\n first = (history->next + PROBE_HISTORY - history->used) % PROBE_HISTORY;\n for (index = 0u; index < history->used; index++) {\n print_record(out, &history->records[(first + index) % PROBE_HISTORY]);\n }\n }\n if (options->statistics) {\n print_count(out, \"accepted\", history->accepted);\n print_count(out, \"rejected\", history->rejected);\n print_count(out, \"malformed\", history->malformed);\n print_count(out, \"bytes\", history->bytes);\n fputc('\\n', out);\n }\n}\n",
|
|
"main.c": "#include \"probe.h\"\n\nint main(int argc, char **argv)\n{\n State state;\n History history;\n Options options;\n char line[PROBE_LINE];\n unsigned sequence = 0u;\n size_t bytes;\n int read_status;\n if (!parse_options(argc, argv, &options)) {\n print_help(stderr);\n return 2;\n }\n if (options.help) {\n print_help(stdout);\n return 0;\n }\n service_reset(&state);\n history_reset(&history);\n while ((read_status = read_line(stdin, line, sizeof(line), &bytes)) != 0) {\n Command command;\n Result result;\n history_add_bytes(&history, bytes);\n if (read_status > 0 && line_ignored(line)) {\n continue;\n }\n sequence++;\n if (read_status < 0 || !parse_command(line, &command)) {\n history_invalid(&history);\n print_invalid(stdout, sequence, &options);\n continue;\n }\n result = service_step(&state, &command);\n history_push(&history, sequence, &command, &result, &state);\n print_observation(stdout, sequence, &result, &state, &options);\n }\n history_print(stderr, &history, &options);\n return stream_finish(stdin, stdout);\n}\n"
|
|
},
|
|
"cases": [
|
|
"Z 100 10 2",
|
|
"N 4 9",
|
|
"T 10",
|
|
"Q 4",
|
|
"Z 4294967290 10 2",
|
|
"N 1 33",
|
|
"T 10",
|
|
"Q 1",
|
|
"A 1 1 2779096452",
|
|
"A 1 2 2779096452",
|
|
"Q 1",
|
|
"Z 0 5 0",
|
|
"N 2 8",
|
|
"T 5",
|
|
"Q 2",
|
|
"Z 0 5 3",
|
|
"N 1 17",
|
|
"N 2 34",
|
|
"N 3 51",
|
|
"N 4 68",
|
|
"N 5 85",
|
|
"N 6 102",
|
|
"N 7 119",
|
|
"N 8 136",
|
|
"N 9 153",
|
|
"T 5",
|
|
"T 100",
|
|
"T 0",
|
|
"T 5",
|
|
"T 5",
|
|
"T 6",
|
|
"A 0 94 2779096452",
|
|
"T 1",
|
|
"A 6 55 2779096483",
|
|
"N 9 374",
|
|
"Q 6",
|
|
"T 100",
|
|
"Q 8",
|
|
"T 100",
|
|
"C 0",
|
|
"Q 2",
|
|
"Q 9",
|
|
"A 9 88 2779096953",
|
|
"A 0 89 2779096687",
|
|
"T 100",
|
|
"N 1 640",
|
|
"A 11 14 2779096300",
|
|
"T 1",
|
|
"A 5 64 2779096963",
|
|
"T 1",
|
|
"A 4 1 2779096695",
|
|
"Q 7",
|
|
"T 100",
|
|
"A 5 95 2779096550",
|
|
"A 3 8 2779096776",
|
|
"T 2147483648",
|
|
"T 4",
|
|
"T 6",
|
|
"T 0",
|
|
"N 2 567",
|
|
"T 100",
|
|
"Q 9",
|
|
"A 4 23 2779096921",
|
|
"N 1 49",
|
|
"N 4 68",
|
|
"C 10",
|
|
"N 8 889",
|
|
"T 100",
|
|
"N 11 908",
|
|
"A 3 91 2779096696",
|
|
"A 3 3 2779096811",
|
|
"T 0",
|
|
"A 8 9 2779096402",
|
|
"T 4",
|
|
"A 11 76 2779096362",
|
|
"N 8 377",
|
|
"A 4 33 2779096979",
|
|
"A 9 33 2779096530",
|
|
"N 10 16",
|
|
"A 11 73 2779096895",
|
|
"A 8 63 2779097060",
|
|
"T 2147483648",
|
|
"A 1 3 2779097085",
|
|
"N 2 626",
|
|
"Q 5",
|
|
"Q 8",
|
|
"N 3 821",
|
|
"A 8 98 2779096326",
|
|
"Q 6",
|
|
"T 100",
|
|
"C 7",
|
|
"T 5",
|
|
"N 6 166",
|
|
"Q 1",
|
|
"N 11 522",
|
|
"T 0",
|
|
"T 4",
|
|
"A 6 77 2779096091",
|
|
"A 11 79 2779096197",
|
|
"C 8",
|
|
"A 10 81 2779096103",
|
|
"N 9 680",
|
|
"C 10",
|
|
"A 5 97 2779096084",
|
|
"A 11 57 2779096127",
|
|
"A 4 88 2779096412",
|
|
"C 8",
|
|
"T 5",
|
|
"T 5",
|
|
"T 6",
|
|
"N 1 904",
|
|
"A 5 86 2779096488",
|
|
"A 4 85 2779096961",
|
|
"N 2 195",
|
|
"C 8",
|
|
"N 9 922",
|
|
"T 5",
|
|
"Q 6",
|
|
"N 0 69",
|
|
"T 1",
|
|
"T 0",
|
|
"Q 5",
|
|
"T 4",
|
|
"A 11 54 2779096419",
|
|
"T 6",
|
|
"N 11 554",
|
|
"T 0",
|
|
"Q 4",
|
|
"T 0",
|
|
"T 6",
|
|
"T 2147483648",
|
|
"N 7 165",
|
|
"Q 4",
|
|
"N 7 921",
|
|
"C 3",
|
|
"N 9 615",
|
|
"N 8 307",
|
|
"C 4",
|
|
"A 5 67 2779097042",
|
|
"N 10 264",
|
|
"Q 6",
|
|
"N 6 438",
|
|
"N 2 914",
|
|
"N 10 23",
|
|
"N 1 845",
|
|
"A 5 44 2779096338",
|
|
"A 10 58 2779096321",
|
|
"T 4",
|
|
"N 8 954",
|
|
"N 7 578",
|
|
"C 0",
|
|
"C 7",
|
|
"N 11 780",
|
|
"A 6 18 2779096853",
|
|
"A 5 90 2779096636",
|
|
"C 11",
|
|
"A 0 79 2779096274",
|
|
"N 10 250",
|
|
"A 4 29 2779096938",
|
|
"A 8 95 2779096126",
|
|
"C 3",
|
|
"Q 11",
|
|
"A 10 50 2779096395",
|
|
"A 3 19 2779096839",
|
|
"T 2147483648",
|
|
"T 0",
|
|
"N 6 696",
|
|
"T 0",
|
|
"Q 1",
|
|
"Q 2",
|
|
"N 4 692",
|
|
"N 10 228",
|
|
"T 6",
|
|
"C 10",
|
|
"N 11 96",
|
|
"N 3 788",
|
|
"T 6",
|
|
"C 5",
|
|
"Q 5",
|
|
"Q 1",
|
|
"T 2147483648",
|
|
"N 1 557",
|
|
"C 5",
|
|
"N 5 353",
|
|
"Q 5",
|
|
"T 1",
|
|
"T 2147483648",
|
|
"T 0",
|
|
"N 1 227",
|
|
"Q 9",
|
|
"N 3 306",
|
|
"Q 10",
|
|
"T 2147483648",
|
|
"N 2 936",
|
|
"T 100",
|
|
"A 3 58 2779096623",
|
|
"Q 5",
|
|
"T 6",
|
|
"A 0 62 2779096469",
|
|
"Q 8",
|
|
"N 9 175",
|
|
"C 7",
|
|
"A 3 14 2779096743",
|
|
"N 2 508",
|
|
"A 10 12 2779096232",
|
|
"N 10 132",
|
|
"Q 3",
|
|
"C 7",
|
|
"T 4",
|
|
"N 9 42",
|
|
"C 1",
|
|
"Q 9",
|
|
"A 0 46 2779096212",
|
|
"N 5 892",
|
|
"N 0 950",
|
|
"A 3 31 2779096521",
|
|
"T 100",
|
|
"Q 1",
|
|
"T 5",
|
|
"A 7 30 2779096281",
|
|
"A 5 2 2779097028",
|
|
"Q 4",
|
|
"T 0",
|
|
"Q 7",
|
|
"C 3",
|
|
"A 2 11 2779096305",
|
|
"N 0 923",
|
|
"A 7 26 2779096157",
|
|
"N 11 653",
|
|
"C 5",
|
|
"C 4",
|
|
"N 10 421",
|
|
"A 11 29 2779096195",
|
|
"T 5",
|
|
"N 8 531",
|
|
"Q 4",
|
|
"N 4 950",
|
|
"C 7",
|
|
"A 5 11 2779097077",
|
|
"T 1",
|
|
"N 7 23",
|
|
"N 6 434",
|
|
"A 1 24 2779096840",
|
|
"C 0",
|
|
"T 100",
|
|
"N 1 136",
|
|
"A 5 73 2779096349",
|
|
"A 0 55 2779096453",
|
|
"N 7 169",
|
|
"T 0",
|
|
"C 3",
|
|
"N 3 530",
|
|
"T 4",
|
|
"A 6 66 2779097057",
|
|
"A 7 21 2779096770",
|
|
"N 2 221",
|
|
"A 7 55 2779096380",
|
|
"Q 8",
|
|
"T 0",
|
|
"T 1",
|
|
"T 2147483648",
|
|
"N 3 779",
|
|
"A 6 5 2779096572",
|
|
"Q 11",
|
|
"A 7 16 2779096689",
|
|
"T 100",
|
|
"A 3 31 2779096306",
|
|
"T 4",
|
|
"T 2147483648",
|
|
"T 6",
|
|
"A 7 46 2779096811",
|
|
"N 7 653",
|
|
"N 2 859",
|
|
"A 2 24 2779096372",
|
|
"N 9 732",
|
|
"N 7 18",
|
|
"A 3 87 2779096077",
|
|
"T 5",
|
|
"A 1 75 2779096969",
|
|
"T 5",
|
|
"Q 10",
|
|
"C 2",
|
|
"N 4 156",
|
|
"A 5 18 2779096149",
|
|
"N 5 849",
|
|
"N 6 900",
|
|
"A 0 9 2779096448",
|
|
"A 0 27 2779097062",
|
|
"A 0 82 2779096918",
|
|
"T 5",
|
|
"A 11 55 2779096619",
|
|
"A 4 22 2779096485",
|
|
"Q 5",
|
|
"A 0 55 2779096130",
|
|
"T 1",
|
|
"C 10",
|
|
"Q 10",
|
|
"N 1 593",
|
|
"Q 1",
|
|
"Q 6",
|
|
"T 2147483648",
|
|
"T 4",
|
|
"A 7 16 2779096807",
|
|
"C 9",
|
|
"C 0",
|
|
"A 2 45 2779096498",
|
|
"N 5 607",
|
|
"N 4 327",
|
|
"T 5",
|
|
"T 6",
|
|
"N 0 954",
|
|
"N 0 597",
|
|
"T 0",
|
|
"Q 10",
|
|
"A 0 30 2779096991",
|
|
"N 2 647",
|
|
"N 1 803",
|
|
"Q 0",
|
|
"Q 11",
|
|
"N 4 979",
|
|
"N 5 731",
|
|
"T 2147483648",
|
|
"A 0 83 2779096216",
|
|
"Q 0",
|
|
"N 9 364",
|
|
"A 10 31 2779096580",
|
|
"C 4",
|
|
"N 4 324",
|
|
"T 100",
|
|
"A 1 78 2779096741",
|
|
"T 2147483648",
|
|
"T 2147483648",
|
|
"A 9 93 2779096835",
|
|
"N 1 563",
|
|
"T 4",
|
|
"T 2147483648",
|
|
"N 7 248",
|
|
"T 4",
|
|
"A 2 25 2779096538",
|
|
"T 4",
|
|
"A 7 27 2779096777",
|
|
"N 9 302",
|
|
"N 10 416",
|
|
"N 10 469",
|
|
"A 6 85 2779096556",
|
|
"Q 6",
|
|
"N 0 393",
|
|
"A 7 29 2779096380",
|
|
"N 7 198",
|
|
"Q 1",
|
|
"A 0 57 2779096365",
|
|
"N 11 763",
|
|
"C 2",
|
|
"N 0 459",
|
|
"N 9 617",
|
|
"N 8 249",
|
|
"T 2147483648",
|
|
"A 0 10 2779096302",
|
|
"A 8 70 2779096099",
|
|
"Q 9",
|
|
"C 10",
|
|
"A 4 42 2779096250",
|
|
"A 1 33 2779097008",
|
|
"N 4 450",
|
|
"A 10 74 2779096331",
|
|
"A 11 68 2779096064",
|
|
"T 1",
|
|
"N 6 836",
|
|
"N 7 28",
|
|
"Q 8",
|
|
"C 1",
|
|
"C 8",
|
|
"A 11 62 2779096089",
|
|
"C 4",
|
|
"N 2 291",
|
|
"A 11 11 2779096091",
|
|
"N 2 222",
|
|
"C 6",
|
|
"C 9",
|
|
"A 9 94 2779096535",
|
|
"Z 0 0 2",
|
|
"Z 1 2 4",
|
|
"A 1 2",
|
|
"Q nope",
|
|
"T -1",
|
|
"X 0"
|
|
]
|
|
}
|