From 662c7c620e6db915c5705bd0afa84925bb84704d Mon Sep 17 00:00:00 2001 From: Yaroslav de la Peña Smirnov Date: Thu, 15 Oct 2026 22:51:45 +0300 Subject: utest: v2, now with more isolation * C23 required. * Less macro voodoo magic. * The test declaration API changed. * Now there's a test type with the test data. * Now it's possible to set a time out for each test. * It's now also possible to initialize data for each test separately. * More importantly, each test is now isolated in its own process, so that we beautifully report a crash and continue with the rest. * Still less than 500loc even with all the comments! --- utest/utest-test.c | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) (limited to 'utest/utest-test.c') diff --git a/utest/utest-test.c b/utest/utest-test.c index 591ee32..df7d51b 100644 --- a/utest/utest-test.c +++ b/utest/utest-test.c @@ -2,23 +2,28 @@ static int a, b; -/* This should pass */ -TEST_BEGIN(test_pass) +void test_pass_run(utest *t) { - asserteq(a, 0); - assertneq(a, b); - expect(b > a, "expected b > a"); - TEST_OUT + asserteq(t, a, 0); + assertneq(t, a, b); + expect(t, b > a, "expected b > a"); } -TEST_END -/* This should fail */ -TEST_BEGIN(test_fail) +void test_fail_run(utest *t) { - expect(0, "oopsie!"); - TEST_OUT + expect(t, 0, "oopsie!"); +} + +void test_crash_run(utest *t) +{ + int *x = NULL; + *x = 8; +} + +void test_timeout_run(utest *t) +{ + sleep(10); } -TEST_END void test_init(void) { @@ -26,9 +31,16 @@ void test_init(void) printf("hello, world!\n\n"); } -void test_destroy(void) +void test_cleanup(void) { printf("\ngood bye!\n"); } -RUN_TEST_SUITE(test_init, test_destroy, test_pass, test_fail); +utest tests[] = { + UTEST_INIT_S(test_pass, 0), + UTEST_INIT_S(test_fail, 0), + UTEST_INIT_S(test_crash, 0), + UTEST_INIT_S(test_timeout, 1), +}; + +RUN_TEST_SUITE(test_init, test_cleanup, tests); -- cgit v1.3.1