Skip to content

Passing external state to a Test #548

@nishanthkarthik

Description

@nishanthkarthik

How can I pass a runtime determined state from the main test process to the forked worker?

#include <stdio.h>
#include "criterion/criterion.h"

static char *STATE = NULL;

void load_state() {
    // STATE is (null) here
    criterion_current_test->data->data = STATE;
}

Test(s, t, .init = load_state) {
    // criterion_current_test->data->data is (nil)
}

int main(int argc, char *argv[]) {
    char *state = "foobar"; // some runtime state

    STATE = state;
    fprintf(stderr, "STATE %p\n", STATE);

    struct criterion_test_set *tests = criterion_initialize();
    if (!criterion_handle_args(argc, argv, false)) {
        return EXIT_FAILURE;
    }

    criterion_run_all_tests(tests);
    criterion_finalize(tests);
    return EXIT_SUCCESS;
}

I understand using malloc is UB. However using cr_malloc requires cr_init to have been called already and that does not happen until the tests begin to run. cr_init is not idempotent, so invoking it in main manually before the tests begin would simply create a new arena again.

Here, state could be a random seed passed from the command line to the individual tests.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions