Snaipe / Criterion

A cross-platform C and C++ unit testing framework for the 21st century

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Passing external state to a Test

nishanthkarthik opened this issue · comments

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.