From 70fe832f3bfcf3fdff04772e1e9a96b4422ff2b3 Mon Sep 17 00:00:00 2001 From: sanine Date: Sun, 16 Jan 2022 10:48:17 -0600 Subject: add tests for lily_assert_null and lily_assert_ptr_equal --- tests/assertions.c | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ tests/main.c | 2 ++ tests/tests.h | 2 ++ 3 files changed, 74 insertions(+) diff --git a/tests/assertions.c b/tests/assertions.c index d1ba141..b19e9b3 100644 --- a/tests/assertions.c +++ b/tests/assertions.c @@ -135,3 +135,73 @@ const char *test_assert_not_null() return 0; } + + +const char *test_assert_null() +{ + int a = 5; + int *ptr = NULL; + + int val = setjmp(_lily_globals.env); + if (val != 0) + return "true assertion failed incorrectly!"; + + lily_assert_null(ptr); + + int passed_thru = 0; + _lily_globals.error_msg = NULL; + _lily_globals.error_location = ""; + val = setjmp(_lily_globals.env); + + if (passed_thru == 0) { + passed_thru = 1; + lily_assert_null(&a); + return "false assertion incorrectly succeeded!"; + } + else { + char buf[256]; + sprintf(buf, "&a (%p) is not NULL", &a); + if (strcmp(_lily_globals.error_msg, buf) != 0) + return "false assertion produced incorrect error message!"; + if (strcmp(_lily_globals.error_location, "tests/assertions.c:158") != 0) + return "false assertion produced incorrect error location!"; + } + + return 0; +} + + +const char *test_assert_ptr_equal() +{ + int a = 0; + int b = 0; + int *ptr = &a; + + int val = setjmp(_lily_globals.env); + if (val != 0) + return "true assertion failed incorrectly!"; + + lily_assert_ptr_equal(&a, ptr); + + int passed_thru = 0; + _lily_globals.error_msg = NULL; + _lily_globals.error_location = ""; + val = setjmp(_lily_globals.env); + ptr = &b; + + if (passed_thru == 0) { + passed_thru = 1; + lily_assert_ptr_equal(&a, ptr); + return "false assertion incorrectly succeeded!"; + } + else { + char buf[256]; + sprintf(buf, "&a (%p) is not equal to ptr (%p)", &a, &b); + if (strcmp(_lily_globals.error_msg, buf) != 0) + return "false assertion produced incorrect error message!"; + if (strcmp(_lily_globals.error_location, "tests/assertions.c:194") != 0) + return "false assertion produced incorrect error location!"; + } + + return 0; +} diff --git a/tests/main.c b/tests/main.c index 32c6944..6aaf1ed 100644 --- a/tests/main.c +++ b/tests/main.c @@ -15,6 +15,8 @@ int main() run_test(test_assert_true); run_test(test_assert_false); run_test(test_assert_not_null); + run_test(test_assert_null); + run_test(test_assert_ptr_equal); return 0; } diff --git a/tests/tests.h b/tests/tests.h index 46cfb2e..970d360 100644 --- a/tests/tests.h +++ b/tests/tests.h @@ -12,4 +12,6 @@ const char* test_assert_msg(); const char* test_assert_true(); const char* test_assert_false(); const char* test_assert_not_null(); +const char* test_assert_null(); +const char* test_assert_ptr_equal(); #endif -- cgit v1.2.1