diff options
author | shmibs <shmibs@gmail.com> | 2015-12-21 23:23:56 -0700 |
---|---|---|
committer | shmibs <shmibs@gmail.com> | 2015-12-21 23:23:56 -0700 |
commit | deb22aa99c20c029f4db4f5c5ddeca5b2e6d9bec (patch) | |
tree | e32d153010f5f42d26d650e010c6e37c7c5d7702 /src | |
parent | a44c8ce0f213a0891713691ee342a5502548db31 (diff) | |
download | simple-test-deb22aa99c20c029f4db4f5c5ddeca5b2e6d9bec.tar.gz |
add bool assertions
Diffstat (limited to 'src')
-rw-r--r-- | src/simple-test.h | 101 |
1 files changed, 83 insertions, 18 deletions
diff --git a/src/simple-test.h b/src/simple-test.h index 12401f5..d6656ab 100644 --- a/src/simple-test.h +++ b/src/simple-test.h @@ -18,6 +18,7 @@ ****************************/ typedef enum { + SIMPLE_TEST_BOOL, SIMPLE_TEST_INT, SIMPLE_TEST_LINT, SIMPLE_TEST_UINT, @@ -129,6 +130,20 @@ void simple_test_get_escape_wide(wchar_t c, wchar_t *buf) #define SIMPLE_TEST_LEN_DIFF(arg1, arg2) \ ( simple_test_mbslen(arg1) - simple_test_mbslen(arg2) ) +#define SIMPLE_TEST_FAIL_ONE(summary, file, line, fmt, sarg, arg) \ +do { \ + { \ + printf("\e[1m :: "); \ + printf(file); \ + printf(":%i: \e[31;1mfail:\e[m ", line); \ + printf(summary); \ + printf("\e[m\n"); \ + printf("\e[1m :: "); \ + printf("%s == \e[m" fmt "\n", sarg, arg); \ + exit(1); \ + } \ +} while(0) + #define SIMPLE_TEST_FAIL(summary, file, line, fmt, sarg1, sarg2, arg1, arg2) \ do { \ { \ @@ -156,6 +171,41 @@ do { \ exit(1); \ } while(0) +void simple_test_assert_bool(char *sarg1, char *sarg2, + bool arg1, bool arg2, + const char *file, const int line, simple_test_cond_t cond) +{ + switch(cond) { + case SIMPLE_TEST_TRUE: + if(!(arg2)) + SIMPLE_TEST_FAIL_ONE("unexpected false value", file, line, + "%s", sarg2, arg2 ? "true" : "false"); + break; + case SIMPLE_TEST_EQ: + if(arg1 != arg2) + SIMPLE_TEST_FAIL("bool values do not match", file, line, + "%s", sarg1, sarg2, + arg1 ? "true" : "false", arg2 ? "true" : "false"); + break; + case SIMPLE_TEST_NEQ: + if(arg1 == arg2) + SIMPLE_TEST_FAIL("bool values match", file, line, + "%s", sarg1, sarg2, + arg1 ? "true" : "false", arg2 ? "true" : "false"); + break; + case SIMPLE_TEST_G: + SIMPLE_TEST_INTERN_ERR(); + case SIMPLE_TEST_GEQ: + SIMPLE_TEST_INTERN_ERR(); + case SIMPLE_TEST_L: + SIMPLE_TEST_INTERN_ERR(); + case SIMPLE_TEST_LEQ: + SIMPLE_TEST_INTERN_ERR(); + default: + SIMPLE_TEST_INTERN_ERR(); + } +} + void simple_test_assert_int(char *sarg1, char *sarg2, intmax_t arg1, intmax_t arg2, const char *file, const int line, simple_test_cond_t cond) @@ -163,8 +213,8 @@ void simple_test_assert_int(char *sarg1, char *sarg2, switch(cond) { case SIMPLE_TEST_TRUE: if(!(arg2)) - SIMPLE_TEST_FAIL("unexpected 0 value", file, line, - "%" PRIiMAX, sarg1, sarg2, arg1, arg2); + SIMPLE_TEST_FAIL_ONE("unexpected 0 value", file, line, + "%" PRIiMAX, sarg2, arg2); break; case SIMPLE_TEST_EQ: if(arg1 != arg2) @@ -208,8 +258,8 @@ void simple_test_assert_uint(char *sarg1, char *sarg2, switch(cond) { case SIMPLE_TEST_TRUE: if(!(arg2)) - SIMPLE_TEST_FAIL("unexpected 0 value", file, line, - "%" PRIuMAX, sarg1, sarg2, arg1, arg2); + SIMPLE_TEST_FAIL_ONE("unexpected 0 value", file, line, + "%" PRIuMAX, sarg2, arg2); break; case SIMPLE_TEST_EQ: if(arg1 != arg2) @@ -253,8 +303,8 @@ void simple_test_assert_hex(char *sarg1, char *sarg2, switch(cond) { case SIMPLE_TEST_TRUE: if(!(arg2)) - SIMPLE_TEST_FAIL("unexpected 0 value", file, line, - "%" PRIxMAX, sarg1, sarg2, arg1, arg2); + SIMPLE_TEST_FAIL_ONE("unexpected 0 value", file, line, + "%" PRIxMAX, sarg2, arg2); break; case SIMPLE_TEST_EQ: if(arg1 != arg2) @@ -298,8 +348,8 @@ void simple_test_assert_float(char *sarg1, char *sarg2, switch(cond) { case SIMPLE_TEST_TRUE: if(!(arg2)) - SIMPLE_TEST_FAIL("unexpected 0 value", file, line, - "%Lg", sarg1, sarg2, arg1, arg2); + SIMPLE_TEST_FAIL_ONE("unexpected 0 value", file, line, + "%Lg", sarg2, arg2); break; case SIMPLE_TEST_EQ: if(arg1 != arg2) @@ -343,8 +393,8 @@ void simple_test_assert_ptr(char *sarg1, char *sarg2, switch(cond) { case SIMPLE_TEST_TRUE: if(arg2 == NULL) - SIMPLE_TEST_FAIL("unexpected NULL value", file, line, - "%p", sarg1, sarg2, arg1, arg2); + SIMPLE_TEST_FAIL_ONE("unexpected NULL value", file, line, + "%p", sarg2, arg2); break; case SIMPLE_TEST_EQ: if(arg1 != arg2) @@ -392,8 +442,8 @@ void simple_test_assert_char(char *sarg1, char *sarg2, switch(cond) { case SIMPLE_TEST_TRUE: if(arg2 == '\0') - SIMPLE_TEST_FAIL("unexpected '\\0' value", file, line, - "‘%s’", sarg1, sarg2, buf1, buf2); + SIMPLE_TEST_FAIL_ONE("unexpected '\\0' value", file, line, + "‘%s’", sarg2, buf2); break; case SIMPLE_TEST_EQ: if(arg1 != arg2) @@ -437,8 +487,8 @@ void simple_test_assert_str(char *sarg1, char *sarg2, switch(cond) { case SIMPLE_TEST_TRUE: if(arg2 == NULL) - SIMPLE_TEST_FAIL("unexpected NULL string", file, line, - "‘%s’", sarg1, sarg2, arg1, arg2); + SIMPLE_TEST_FAIL_ONE("unexpected NULL string", file, line, + "‘%s’", sarg2, arg2); break; case SIMPLE_TEST_EQ: if( strcmp(arg1, arg2) ) @@ -486,8 +536,8 @@ void simple_test_assert_wchar(char *sarg1, char *sarg2, switch(cond) { case SIMPLE_TEST_TRUE: if(arg2 == L'\0') - SIMPLE_TEST_FAIL("unexpected '\\0' value", file, line, - "‘%ls’", sarg1, sarg2, buf1, buf2); + SIMPLE_TEST_FAIL_ONE("unexpected '\\0' value", file, line, + "‘%ls’", sarg2, buf2); break; case SIMPLE_TEST_EQ: if(arg1 != arg2) @@ -531,8 +581,8 @@ void simple_test_assert_wstr(char *sarg1, char *sarg2, switch(cond) { case SIMPLE_TEST_TRUE: if(arg2 == NULL) - SIMPLE_TEST_FAIL("unexpected NULL string", file, line, - "‘%ls’", sarg1, sarg2, arg1, arg2); + SIMPLE_TEST_FAIL_ONE("unexpected NULL string", file, line, + "‘%ls’", sarg2, arg2); break; case SIMPLE_TEST_EQ: if( wcscmp(arg1, arg2) ) @@ -675,6 +725,21 @@ do { \ * TYPE-SPECIFIC TESTS * *************************/ +#define ASSERT_BOOL(arg) \ + simple_test_assert_bool("false", #arg, \ + false, (bool)(arg), \ + __FILE__, __LINE__, SIMPLE_TEST_TRUE) + +#define ASSERT_BOOL_EQ(arg1, arg2) \ + simple_test_assert_bool(#arg1, #arg2, \ + (bool)(arg1), (bool)(arg2), \ + __FILE__, __LINE__, SIMPLE_TEST_EQ) + +#define ASSERT_BOOL_NEQ(arg1, arg2) \ + simple_test_assert_bool(#arg1, #arg2, \ + (bool)(arg1), (bool)(arg2), \ + __FILE__, __LINE__, SIMPLE_TEST_NEQ) + #define ASSERT_INT(arg) \ simple_test_assert_int("0", #arg, \ (intmax_t)0, (intmax_t)(arg), \ |