From 0fa7b599ed8061a9233ce19716a730731ebefc9b Mon Sep 17 00:00:00 2001 From: Sebastiaan de Schaetzen Date: Fri, 24 Apr 2026 14:57:24 +0200 Subject: [PATCH] Implement assert_str and assert_not_null and update tests --- v0/test.c | 13 +++++++++++++ v0/test.h | 21 +++++++++++++++++++++ v0/test_parser.c | 4 ++-- v0/test_token.c | 14 ++++++++++---- 4 files changed, 46 insertions(+), 6 deletions(-) diff --git a/v0/test.c b/v0/test.c index e308a27..fd60d0c 100644 --- a/v0/test.c +++ b/v0/test.c @@ -1,6 +1,7 @@ #include "test.h" #include #include +#include static jmp_buf s_testJmp; static const char* s_failMsg; @@ -10,6 +11,18 @@ void fail(const char* msg) { longjmp(s_testJmp, 1); } +void assert_not_null(void* ptr, const char* msg) { + if (ptr == NULL) { + fail(msg); + } +} + +void assert_str(const char* expected, const char* actual, const char* msg) { + if (expected == NULL || actual == NULL || strcmp(expected, actual) != 0) { + fail(msg); + } +} + typedef struct { const char* name; Test func; diff --git a/v0/test.h b/v0/test.h index 92ba2de..b3151d2 100644 --- a/v0/test.h +++ b/v0/test.h @@ -12,4 +12,25 @@ typedef void (*Test)(void); */ void fail(const char* msg); +/** + * Asserts that a pointer is not null. + * + * Calls `fail` if the assertion does not hold. + * + * @param ptr The pointer to test. + * @param msg The message to print if the pointer is null. + */ +void assert_not_null(void* ptr, const char* msg); + +/** + * Asserts that a string has the expected value. + * + * Calls `fail` if the assertion does not hold. + * + * @param expected The expected value. This is typically a string literal. + * @param actual The actual value. This is typically an expression. + * @param msg The message to print if these do not match. + */ +void assert_str(const char* expected, const char* actual, const char* msg); + #endif diff --git a/v0/test_parser.c b/v0/test_parser.c index f35366b..c4f323c 100644 --- a/v0/test_parser.c +++ b/v0/test_parser.c @@ -6,8 +6,8 @@ static void test_parser_module_name(void) { TokenStream* ts = tokenstream_open("module my_module;"); Module* m = parser_parse(ts); - if (m == NULL) fail("expected module to be parsed"); - if (strcmp(m->name, "my_module") != 0) fail("expected name 'my_module'"); + assert_not_null(m, "expected module to be parsed"); + assert_str("my_module", m->name, "expected name 'my_module'"); parser_free(m); tokenstream_close(ts); diff --git a/v0/test_token.c b/v0/test_token.c index 2a2bcf9..2e2c96e 100644 --- a/v0/test_token.c +++ b/v0/test_token.c @@ -89,16 +89,22 @@ static void test_tokenstream_info(void) { tokenstream_info(ts, &info1); if (t1 != TOKEN_MODULE) fail("expected TOKEN_MODULE"); if (info1.token != TOKEN_MODULE) fail("info: expected TOKEN_MODULE"); - if (info1.text_length != 6) fail("info: expected length 6"); - if (strncmp(info1.text, "module", 6) != 0) fail("info: expected 'module'"); + + char buf1[32]; + memcpy(buf1, info1.text, info1.text_length); + buf1[info1.text_length] = '\0'; + assert_str("module", buf1, "info: expected 'module'"); Token t2 = tokenstream_next(ts); TokenInfo info2; tokenstream_info(ts, &info2); if (t2 != TOKEN_IDENTIFIER) fail("expected TOKEN_IDENTIFIER"); if (info2.token != TOKEN_IDENTIFIER) fail("info: expected TOKEN_IDENTIFIER"); - if (info2.text_length != 4) fail("info: expected length 4"); - if (strncmp(info2.text, "main", 4) != 0) fail("info: expected 'main'"); + + char buf2[32]; + memcpy(buf2, info2.text, info2.text_length); + buf2[info2.text_length] = '\0'; + assert_str("main", buf2, "info: expected 'main'"); tokenstream_close(ts); }