diff options
author | sanine <sanine.not@pm.me> | 2022-12-31 14:17:15 -0600 |
---|---|---|
committer | sanine <sanine.not@pm.me> | 2022-12-31 14:17:15 -0600 |
commit | c7bf6fcccd50ded889b7cfb853af51e7c5f28d45 (patch) | |
tree | eb39eabcef4f809bf8511118d3cdd631ddfb527d | |
parent | 590b2997f93006112be93807b7e7a36d56d52809 (diff) |
add REQUIRE_EQF and REQUIRE_EQS
-rw-r--r-- | lily-test.h | 18 | ||||
-rw-r--r-- | tests/assertions.c | 74 | ||||
-rw-r--r-- | tests/tests.h | 2 |
3 files changed, 94 insertions, 0 deletions
diff --git a/lily-test.h b/lily-test.h index 6aafb8f..e4f1676 100644 --- a/lily-test.h +++ b/lily-test.h @@ -187,6 +187,18 @@ void lily_require(int x, const char *location, const char *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) +#define LILY_REQUIRE_EQF(x, y, xstr, ystr, fmt) \ + lily_require(fabs(x-y) < lily_g.epsilon, LILY_LOCATION, \ + "REQUIRE failed: %s == %s\n %s = " fmt "\n %s = " fmt "\n epsilon: %f", \ + xstr, ystr, xstr, x, ystr, y, lily_g.epsilon) +#define REQUIRE_EQF(x, y, fmt) LILY_REQUIRE_EQF(x, y, #x, #y, fmt) + +#define LILY_REQUIRE_EQS(x, y, xstr, ystr) \ + lily_require(strcmp(x, y) == 0, LILY_LOCATION, \ + "REQUIRE failed: %s == %s\n %s = \"%s\"\n %s = \"%s\"", \ + xstr, ystr, xstr, x, ystr, y) +#define REQUIRE_EQS(x, y) LILY_REQUIRE_EQS(x, y, #x, #y) + void lily_begin(); void lily_finish(); @@ -331,6 +343,12 @@ void lily_require(int x, const char *location, const char *fmt, ...) void lily_begin() { + lily_g.n_tests = 0; + lily_g.n_tests_failed = 0; + lily_g.n_assertions_failed = 0; + lily_g.n_assertions_failed = 0; + lily_g.epsilon = 1e-3; + printf("================================================================================\n"); printf("lily-test version %d.%d.%d\n", LILY_VERSION_MAJOR, LILY_VERSION_MINOR, LILY_VERSION_PATCH); } diff --git a/tests/assertions.c b/tests/assertions.c index e79ae44..6951f58 100644 --- a/tests/assertions.c +++ b/tests/assertions.c @@ -229,3 +229,77 @@ const char * test_REQUIRE_EQ() return "test function did not longjump!"; } } + + +void f_test_REQUIRE_EQF() +{ + lily_set_epsilon(0.2); + REQUIRE_EQF(0.5, 0.0, "%0.1f"); + REQUIRE_EQF(0.5, 0.4, "%0.1f"); +} + +const char * test_REQUIRE_EQF() +{ + 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.HEAD.next == NULL) + return "REQUIRE_EQF did not append any failure message"; + + if (lily_g.HEAD.next->next != NULL) + return "REQUIRE_EQF appended more than one message"; + + if (strcmp( + lily_g.HEAD.next->msg, + "REQUIRE failed: 0.5 == 0.0\n 0.5 = 0.5\n 0.0 = 0.0\n epsilon: 0.200000" + ) != 0) + return "incorrect message"; + + lily_msg_destroy(lily_g.HEAD.next); + return 0; + } + else { + f_test_REQUIRE_EQF(); + return "test continued to run!"; + } +} + + +void f_test_REQUIRE_EQS() +{ + const char *a = "hi"; + const char *b = "bye"; + REQUIRE_EQS(a, b); + REQUIRE_EQS("hi hi"+3, a); +} +const char * test_REQUIRE_EQS() +{ + 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.HEAD.next == NULL) + return "REQUIRE_EQS did not append any failure message"; + + if (lily_g.HEAD.next->next != NULL) + return "REQUIRE_EQS appended more than one message"; + + if (strcmp( + lily_g.HEAD.next->msg, + "REQUIRE failed: a == b\n a = \"hi\"\n b = \"bye\"" + ) != 0) + return "incorrect message"; + + lily_msg_destroy(lily_g.HEAD.next); + return 0; + } + else { + f_test_REQUIRE_EQS(); + return "test continued to run!"; + } +} diff --git a/tests/tests.h b/tests/tests.h index d6d95d1..4a02f66 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -19,6 +19,8 @@ int validate_message(const char* received, const char* expected, X(test_CHECK_EQS) \ X(test_REQUIRE) \ X(test_REQUIRE_EQ) \ + X(test_REQUIRE_EQF) \ + X(test_REQUIRE_EQS) \ #define X(test) const char * test(); TESTS |