.. c:macro:: YMO_ASSERT_STREAM_OUT Output stream for assertions .. code-block:: c :caption: Definition #define YMO_ASSERT_STREAM_OUT stdout Assertions ============= -------------------------------------------------- Convenience Macros -------------------------------------------------- .. c:macro:: ymo_assert_test_abort Function used to abort test execution .. code-block:: c :caption: Definition #define ymo_assert_test_abort() exit(-1) .. c:macro:: ymo_assert_test_fail_fmt Function used to report assertion failure and abort .. code-block:: c :caption: Definition #define ymo_assert_test_fail_fmt(fmt, ...) \ fprintf(YMO_ASSERT_STREAM_OUT, \ " - \033[00;31mFAIL: "fmt " (%s:%s:%i)\033[00;m\n", \ __VA_ARGS__, YMO_SOURCE, __func__, __LINE__); \ ymo_assert_test_abort(); .. c:macro:: ymo_assert_test_fail Function used to report assertion failure and abort .. code-block:: c :caption: Definition #define ymo_assert_test_fail(test_desc) \ ymo_assert_test_fail_fmt("%s", test_desc); \ ymo_assert_test_abort(); .. c:macro:: ymo_assert_test_pass_fmt Function used to report assertion success, if verbose is defined .. code-block:: c :caption: Definition #define ymo_assert_test_pass_fmt(fmt, ...) \ if( YMO_ASSERT_VERBOSE ) { \ fprintf(YMO_ASSERT_STREAM_OUT, \ " - \033[00;32mPASS: "fmt " (%s:%s:%i)\033[00;m\n", \ __VA_ARGS__, YMO_SOURCE, __func__, __LINE__); \ } .. c:macro:: ymo_assert_test_pass Function used to report assertion success, if verbose is defined .. code-block:: c :caption: Definition #define ymo_assert_test_pass(test_desc) ymo_assert_test_pass_fmt("%s", test_desc) .. c:macro:: ymo_assert_test_fmt Convenience macro used to execute tests and report failure .. code-block:: c :caption: Definition #define ymo_assert_test_fmt(test_cond, fmt, ...) \ do { \ if( !(test_cond) ) { \ ymo_assert_test_fail_fmt(fmt, __VA_ARGS__); \ }; \ ymo_assert_test_pass_fmt(fmt, __VA_ARGS__); \ } while( 0 ) .. c:macro:: ymo_assert_test Convenience macro used to execute tests and report failure .. code-block:: c :caption: Definition #define ymo_assert_test(test_cond, test_desc) \ do { \ if( !(test_cond) ) { \ ymo_assert_test_fail(test_desc); \ }; \ ymo_assert_test_pass(test_desc); \ } while( 0 ) ------------------------------------- Assertions ------------------------------------- .. c:macro:: ymo_assert General true/false assertion. .. code-block:: c :caption: Definition #define ymo_assert(test_cond) ymo_assert_test(test_cond, #test_cond) String Assertions ................... .. c:macro:: ymo_assert_str_eq String equality: - pass if two non-null strings are lexicographically equal - pass if both strings are the NULL pointer .. code-block:: c :caption: Definition #define ymo_assert_str_eq(x, y) ymo_assert_test_fmt( \ (x && y \ && !strcmp((const char*)x,(const char*)y)) \ || ((x == NULL) && (y == NULL)), \ "%s:\n%s: >>%s<<\n%s: >>%s<<\n", \ #x " == " #y, \ #x, (const char*)x, \ #y, (const char*)y \ ) .. c:macro:: ymo_assert_str_ne String inequality - pass if two non-null strings are not lexicographically equal - pass if one string is NULL and the other isn't .. code-block:: c :caption: Definition #define ymo_assert_str_ne(x, y) ymo_assert_test_fmt( \ (x && y && strcmp((const char*)x,(const char*)y)) \ || ((x == NULL || y == NULL) && (x != y)), \ "%s:\n%s: >>%s<<\n%s: >>%s<<\n", \ #x " != " #y, #x, (const char*)x, #y, (const char*)y) .. c:macro:: ymo_assert_str_contains Substring matching: pass if x is a substring of y .. code-block:: c :caption: Definition #define ymo_assert_str_contains(x, y) ymo_assert_test( \ x && y && strstr(x, y) != NULL, #x " contains " #y) .. c:macro:: ymo_assert_str_startswith Prefix matching: pass if x starts with y .. code-block:: c :caption: Definition #define ymo_assert_str_startswith(x, y) ymo_assert_test( \ x && y && ((const char*)strstr(x, y)) == (const char*)x, \ #x " starts with " #y)