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 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'tests/assertions.c') 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; +} -- cgit v1.2.1