These are just a handful of assertion macro definitions from an old project
of mine. They don't do much beyond what you can do with assert()
. If
you're using a unit test library, you definitely don't need them! They're
just handy for small programs / check_PROGRAMS
targets.
(If you're looking for a lightweight unit test framework for C + a richer set of assertion macros, have a look at Unity).
Run make check
to see an example.
This project is licensed under the MIT license. See COPYING for more info.
ℹ️ Documentation is generated using pomd4c.
If set to 1, no output will be generated by any ymo_* assertions.
# define YMO_ASSERT_QUIET 0
If set to 1, output will also be generated for passing assertions.
# define YMO_ASSERT_VERBOSE 0
Default file stream for verbose output, if undefined.
# define YMO_ASSERT_FILE stderr
Default action to take for a failed assertion, if undefined.
# define ymo_assert_fail_action()
This is the basic assertion. It works just like assert()
save for the
fact that failure behavior can be customized by defining
ymo_assert_fail_action
prior to including ymo_assert.h.
#define ymo_assert( test_cond )
- pass if two non-null strings are lexicographically equal
- pass if both strings are the NULL pointer
#define ymo_assert_str_eq( x, y )
Pass if either:
- two non-null strings are not lexicographically equal
- one string is NULL and the other isn't
#define ymo_assert_str_ne( x, y )
Substring matching: pass if x is a substring of y.
#define ymo_assert_str_contains( x, y )
Prefix matching: pass if x starts with y.
#define ymo_assert_str_startswith( x, y )
When YMO_ASSERT_VERBOSE == 1
, the following will output:
PASS: two < three (example.c:basic_assertions:35)
PASS: 10 > 1 (example.c:basic_assertions:36)
PASS: 1 <= two (example.c:basic_assertions:37)
PASS: two <= two (example.c:basic_assertions:38)
PASS: three >= two (example.c:basic_assertions:39)
PASS: three >= three (example.c:basic_assertions:40)
PASS: ((164 % 100) & 0x40) == ((6-4) << 5) (example.c:basic_assertions:41)
PASS: two == two (example.c:basic_assertions:42)
PASS: two != three (example.c:basic_assertions:43)
void basic_assertions(void)
{
int two = 2;
int three = 3;
ymo_assert(two < three);
ymo_assert(10 > 1);
ymo_assert(1 <= two);
ymo_assert(two <= two);
ymo_assert(three >= two);
ymo_assert(three >= three);
ymo_assert( ((164 % 100) & 0x40) == ((6-4) << 5) );
ymo_assert(two == two);
ymo_assert(two != three);
return;
};
When YMO_ASSERT_VERBOSE == 1
, the following will output:
PASS: "This" == this_str (example.c:string_assertions:57)
PASS: that_str == strdup(that_str) (example.c:string_assertions:58)
PASS: this_str == this_str_cpy (example.c:string_assertions:59)
PASS: null_str == NULL (example.c:string_assertions:60)
PASS: this_str != that_str (example.c:string_assertions:61)
PASS: this_str != null_str (example.c:string_assertions:62)
PASS: this_str != NULL (example.c:string_assertions:63)
PASS: "the subway is neat" contains "sub" (example.c:string_assertions:64)
PASS: "prefix" starts with "pre" (example.c:string_assertions:96)
PASS: haystack starts with "pre" (example.c:string_assertions:97)
PASS: "prefix" starts with needle (example.c:string_assertions:98)
PASS: haystack starts with needle (example.c:string_assertions:99)
void string_assertions(void)
{
const char* this_str = "This";
const char* this_str_cpy = strdup(this_str);
const char* that_str = "That";
const char* null_str = NULL;
const char* haystack = "prefix string";
const char* needle = "pre";
ymo_assert_str_eq("This", this_str);
ymo_assert_str_eq(that_str, strdup(that_str));
ymo_assert_str_eq(this_str, this_str_cpy);
ymo_assert_str_eq(null_str, NULL);
ymo_assert_str_ne(this_str, that_str);
ymo_assert_str_ne(this_str, null_str);
ymo_assert_str_ne(this_str, NULL);
ymo_assert_str_contains("the subway is neat", "sub");
ymo_assert_str_startswith("prefix", "pre");
ymo_assert_str_startswith(haystack, "pre");
ymo_assert_str_startswith("prefix", needle);
ymo_assert_str_startswith(haystack, needle);
return;
};