#include "array_report.h" #include #include #include typedef struct { void *blocks[2]; size_t sizes[2]; int fail_at; int calls; int releases; int live; int invalid; int publications; ra_report report; } observer; static void *allocate(void *context, size_t bytes) { observer *state = context; state->calls += 1; if (state->calls <= 2) { state->sizes[state->calls - 1] = bytes; } if (state->fail_at == state->calls || state->calls > 2) { return NULL; } void *block = malloc(bytes); if (block != NULL) { state->blocks[state->calls - 1] = block; state->live += 1; } return block; } static void release(void *context, void *block) { observer *state = context; state->releases += 1; for (int index = 0; index < 2; ++index) { if (block != NULL && block == state->blocks[index]) { free(block); state->blocks[index] = NULL; state->live -= 1; return; } } state->invalid += 1; } static void publish(void *context, const ra_report *report) { observer *state = context; state->publications += 1; if (state->live != 2) { state->invalid += 1; } state->report = *report; } int main(void) { for (;;) { ra_request request; int got = scanf("%d %u %d %d %d %d %d %d %d", &request.count, &request.seed, &request.base, &request.step, &request.mode, &request.cutoff, &request.window, &request.bins, &request.fail_at); if (got == EOF) { return 0; } if (got != 9) { return 2; } observer state; memset(&state, 0, sizeof(state)); state.fail_at = request.fail_at; ra_port port = {&state, allocate, release, publish}; const ra_request *selected_request = &request; const ra_port *selected_port = &port; int probe = request.count; if (probe <= -2 && probe >= -6) { request.count = 1; if (probe == -2) selected_request = NULL; if (probe == -3) selected_port = NULL; if (probe == -4) port.allocate = NULL; if (probe == -5) port.release = NULL; if (probe == -6) port.publish = NULL; } ra_request saved = request; ra_port saved_port = port; int rc = ra_process(selected_request, selected_port); if (request.count != saved.count || request.seed != saved.seed || request.base != saved.base || request.step != saved.step || request.mode != saved.mode || request.cutoff != saved.cutoff || request.window != saved.window || request.bins != saved.bins || request.fail_at != saved.fail_at || port.context != saved_port.context || port.allocate != saved_port.allocate || port.release != saved_port.release || port.publish != saved_port.publish) { state.invalid += 1; } printf("%d %d %d %d %d %zu %zu %d", rc, state.calls, state.releases, state.live, state.invalid, state.sizes[0], state.sizes[1], state.publications); for (int field = 0; field < RA_FIELDS; ++field) { printf(" %lld", state.report.v[field]); } putchar('\n'); for (int index = 0; index < 2; ++index) { free(state.blocks[index]); } } }