diff options
author | sanine <sanine.not@pm.me> | 2022-12-30 14:03:26 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-12-30 14:03:26 -0600 |
commit | b8c647c9250b514a5daa11ea731691b19944aa4e (patch) | |
tree | f3074710bc4f99bc9cdd4f8fe33313a0e8890bfa | |
parent | e48761f7ca1dba622b54b7f2223854645996fc30 (diff) |
add CHECK and REQUIRE comparison assertions
-rw-r--r-- | .gitignore | 1 | ||||
-rw-r--r-- | lily-test.h | 27 | ||||
-rw-r--r-- | tests/assertions.c | 37 | ||||
-rw-r--r-- | tests/tests.h | 1 |
4 files changed, 62 insertions, 4 deletions
@@ -2,3 +2,4 @@ build img *.swp +example/a.out diff --git a/lily-test.h b/lily-test.h index f0d14e7..ac956aa 100644 --- a/lily-test.h +++ b/lily-test.h @@ -138,10 +138,17 @@ void lily_check(int x, const char *location, const char *fmt, ...); #define LILY_CHECK(x) LILY_CHECK_(STR(x), x, LILY_LOCATION) #define CHECK(x) LILY_CHECK(x) -#define LILY_CHECK_EQ_(test, location, fmt, x, y, xstr, ystr) \ - lily_check(test, location, "CHECK failed: %s == %s (" fmt " == " fmt ")", xstr, ystr, x, y) -#define LILY_CHECK_EQ(x, y, xstr, ystr, fmt) LILY_CHECK_EQ_(x == y, LILY_LOCATION, fmt, x, y, xstr, ystr) -#define CHECK_EQ(x, y, fmt) LILY_CHECK_EQ(x, y, #x, #y, fmt) +#define LILY_CHECK_CMP_(x, y, xstr, ystr, cmp, cmpstr, fmt) \ + lily_check(x cmp y, LILY_LOCATION, "CHECK failed: %s " cmpstr " %s (" fmt " " cmpstr " " fmt ")", xstr, ystr, x, y) +#define LILY_CHECK_CMP(x, cmp, y, fmt) LILY_CHECK_CMP_(x, y, #x, #y, cmp, #cmp, fmt) + +/* check comparision assertions */ +#define CHECK_EQ(x, y, fmt) LILY_CHECK_CMP(x, ==, y, fmt) +#define CHECK_NEQ(x, y, fmt) LILY_CHECK_CMP(x, !=, y, fmt) +#define CHECK_LT(x, y, fmt) LILY_CHECK_CMP(x, <, y, fmt) +#define CHECK_LE(x, y, fmt) LILY_CHECK_CMP(x, <=, y, fmt) +#define CHECK_GT(x, y, fmt) LILY_CHECK_CMP(x, >, y, fmt) +#define CHECK_GE(x, y, fmt) LILY_CHECK_CMP(x, >=, y, fmt) void lily_require(int x, const char *location, const char *fmt, ...); @@ -150,6 +157,18 @@ void lily_require(int x, const char *location, const char *fmt, ...); #define LILY_REQUIRE(x) LILY_REQUIRE_(STR(x), x, LILY_LOCATION) #define REQUIRE(x) LILY_REQUIRE(x) +#define LILY_REQUIRE_CMP_(x, y, xstr, ystr, cmp, cmpstr, fmt) \ + lily_require(x cmp y, LILY_LOCATION, "REQUIRE failed: %s " cmpstr " %s (" fmt " " cmpstr " " fmt ")", xstr, ystr, x, y) +#define LILY_REQUIRE_CMP(x, cmp, y, fmt) LILY_REQUIRE_CMP_(x, y, #x, #y, cmp, #cmp, fmt) + +/* require comparison assertions */ +#define REQUIRE_EQ(x, y, fmt) LILY_REQUIRE_CMP(x, ==, y, fmt) +#define REQUIRE_NEQ(x, y, fmt) LILY_REQUIRE_CMP(x, !=, y, fmt) +#define REQUIRE_LT(x, y, fmt) LILY_REQUIRE_CMP(x, <, y, fmt) +#define REQUIRE_LE(x, y, fmt) LILY_REQUIRE_CMP(x, <=, y, fmt) +#define REQUIRE_GT(x, y, fmt) LILY_REQUIRE_CMP(x, >, y, fmt) +#define REQUIRE_GE(x, y, fmt) LILY_REQUIRE_CMP(x, >=, y, fmt) + void lily_run_test(void (*test)()); diff --git a/tests/assertions.c b/tests/assertions.c index 5a32854..39db7bd 100644 --- a/tests/assertions.c +++ b/tests/assertions.c @@ -135,3 +135,40 @@ const char * test_REQUIRE() return "test function did not longjump!"; } } + + +void f_test_REQUIRE_EQ() +{ + REQUIRE_EQ(2, 2, "%d"); + REQUIRE_EQ(2, 4, "%d"); + REQUIRE_EQ(2, 1, "%d"); +} + +const char * test_REQUIRE_EQ() +{ + lily_g.HEAD.next = NULL; + lily_g.TAIL = &(lily_g.HEAD); + lily_g.failed = 0; + int test_failed = setjmp(lily_g.env); + + if (test_failed) { + if (!lily_g.failed) + return "test did not mark itself as failed"; + + if (lily_g.HEAD.next == NULL) + return "test did not generate any messages"; + + if (strcmp(lily_g.HEAD.next->msg, "REQUIRE failed: 2 == 4 (2 == 4)") != 0) + return "test generated incorrect message"; + + if (lily_g.HEAD.next->next != NULL) + return "test generated too many messages"; + + lily_msg_destroy(lily_g.HEAD.next); + return 0; + } + else { + f_test_REQUIRE_EQ(); + return "test function did not longjump!"; + } +} diff --git a/tests/tests.h b/tests/tests.h index 393da87..c450e13 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -16,6 +16,7 @@ int validate_message(const char* received, const char* expected, X(test_CHECK) \ X(test_CHECK_EQ) \ X(test_REQUIRE) \ + X(test_REQUIRE_EQ) \ #define X(test) const char * test(); TESTS |