ZT_CHECK(3)

ZT_CHECK πŸ’– ZT_ASSERT β€” verify a test claim, bestie

okay so this is a page from the LOCAL manual, and it's about the two little icons zt_check and zt_assert from libzt. they're your vibe-checkers for test claims. no cap. πŸŽ€


✨ SYNOPSIS ✨

#include <zt.h>

void
zt_check(zt_t t, zt_claim claim);

void
zt_assert(zt_t t, zt_claim claim);

🌸 DESCRIPTION 🌸

so like, both zt_check() and zt_assert() verify a claim β€” a claim being a function bundled up with its arguments β€” and you're supposed to use them inside test functions. that's the whole gig. πŸ’…

the first argument is a pointer to an opaque type that encapsulates the test outcome. the second argument is usually built with a helper macro, like ZT_CMP_TRUE() or ZT_CMP_FALSE().

if the claim holds then execution continues normally. if the claim does not hold, the test gets marked as failed and a diagnostic message gets printed to stderr. literally printing receipts. 😭

the only difference between zt_check() and zt_assert() is that zt_check() lets the test keep executing, while zt_assert() does a non-local exit out of the test function. and before you panic β€” only the currently executing test function is affected. the other test cases and test suites still run normally, they're unbothered, they're thriving. πŸ‘‘

  • use zt_check() when it's safe to run the rest of the test even if the claim isn't true. this is the move because it lets every failure come with as much diagnostic output as possible, without yeeting the whole test program into a crash.
  • use zt_assert() when running the rest of the test after a failure would be unsafe or just straight up meaningless.
it's giving "check everything you can, assert only what you must." iykyk 🫢

🧚 IMPLEMENTATION NOTES 🧚

the non-local exit from zt_assert() is implemented with siglongjump(). so do not count on C++ destructors or the GCC cleanup function extension to manage your resources correctly. like, do not. fr fr. πŸ’€


⭐ RETURN VALUES ⭐

neither function returns anything. failures get remembered inside the opaque zt_test structure that's passed by pointer (aka zt_t) as the first argument. the receipts live in the struct, not in a return value. periodt. ✨


πŸ¦‹ EXAMPLES πŸ¦‹

a minimal test program that looks up the UNIX user root and measures several properties of the returned record:

#include <sys/types.h>
#include <pwd.h>

#include <zt.h>

static void test_root_user(zt_t t) {
    struct passwd *p = getpwnam("root");
    zt_assert(t, ZT_NOT_NULL(p));
    zt_check(t, ZT_CMP_CSTR(p->pw_name, ==, "root"));
    zt_check(t, ZT_CMP_INT(p->pw_uid, ==, 0));
    zt_check(t, ZT_CMP_INT(p->pw_gid, ==, 0));
}

static void test_suite(zt_visitor v) {
    ZT_VISIT_TEST_CASE(v, test_root_user);
}

int main(int argc, char** argv, char** envp) {
    return zt_main(argc, argv, envp, test_suite);
}

πŸ‘‘ SEE ALSO πŸ‘‘


πŸ’œ HISTORY πŸ’œ

the zt_check() and the zt_assert() functions first appeared in libzt 0.1. og behavior, honestly.


πŸŽ€ AUTHORS πŸŽ€

Zygmunt Krynicki <me@zygoon.pl> β€” the icon who wrote this. πŸ‘‘


and that's the whole page, bestie!! this beauty is from libzt 0.3.1, dated January 12, 2020, and it lives in section ZT_CHECK(3). go forth and assert responsibly πŸ’–βœ¨πŸŒΈπŸ¦‹