Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Passing external state to a Test #548

Open
nishanthkarthik opened this issue Jul 3, 2024 · 0 comments
Open

Passing external state to a Test #548

nishanthkarthik opened this issue Jul 3, 2024 · 0 comments

Comments

@nishanthkarthik
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant