From 7c93fe6bc9f061484d28115e9fa1ec5f89ce5924 Mon Sep 17 00:00:00 2001 From: sanine Date: Sat, 4 Feb 2023 11:23:37 -0600 Subject: fix segfault when comparing NULL strings --- tests/assertions.c | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) (limited to 'tests/assertions.c') diff --git a/tests/assertions.c b/tests/assertions.c index 6951f58..280bb97 100644 --- a/tests/assertions.c +++ b/tests/assertions.c @@ -149,6 +149,33 @@ const char * test_CHECK_EQS() return 0; } +const char * test_CHECK_EQS_NULL() +{ + lily_g.HEAD.next = NULL; + lily_g.TAIL = &(lily_g.HEAD); + lily_g.failed = 0; + + const char *a = "hi"; + const char *b = "bye"; + CHECK_EQS(a, NULL); + CHECK_EQS(a, "hi"); + + if (lily_g.HEAD.next == NULL) + return "CHECK_EQS did not append any failure message"; + + if (lily_g.HEAD.next->next != NULL) + return "CHECK_EQS appended more than one message"; + + if (strcmp( + lily_g.HEAD.next->msg, + "CHECK failed: a == NULL\n a = \"hi\"\n NULL = \"(null)\"" + ) != 0) + return "incorrect message"; + + lily_msg_destroy(lily_g.HEAD.next); + return 0; +} + @@ -303,3 +330,40 @@ const char * test_REQUIRE_EQS() return "test continued to run!"; } } + +void f_test_REQUIRE_EQS_NULL() +{ + const char *a = "hi"; + const char *b = "bye"; + REQUIRE_EQS(a, NULL); + REQUIRE_EQS("hi hi"+3, a); +} +const char * test_REQUIRE_EQS_NULL() +{ + 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 == NULL\n a = \"hi\"\n NULL = \"(null)\"" + ) != 0) + return "incorrect message"; + + lily_msg_destroy(lily_g.HEAD.next); + return 0; + } + else { + f_test_REQUIRE_EQS_NULL(); + return "test continued to run!"; + } + +} -- cgit v1.2.1