From 9154667efa633e5601d8546deb4635b87a3c06c7 Mon Sep 17 00:00:00 2001 From: Lorenzo Carletti Date: Tue, 16 Nov 2021 20:51:53 +0100 Subject: [PATCH] Add support for messages within asserts --- src/c/tests/debug_printf/test_print.c | 17 +++++++++++++++-- src/c/tests/debug_printf/test_print.h | 8 ++++++-- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/c/tests/debug_printf/test_print.c b/src/c/tests/debug_printf/test_print.c index 9c61681..a52be10 100644 --- a/src/c/tests/debug_printf/test_print.c +++ b/src/c/tests/debug_printf/test_print.c @@ -1,6 +1,7 @@ #include "test_print.h" #include "mgba.h" #include +#include "printf.h" void test_printf(const char* ptr, ...) { @@ -10,10 +11,22 @@ void test_printf(const char* ptr, ...) { va_end(args); } -void assert_print(bool condition, const char* file, int line) +void assert_print(bool condition, const char* file, int line, const char* message, ...) { + char str[MAX_STR_SIZE]; if(!condition) - test_printf("FAIL! File: %s - Line: %d", file, line); + { + if(message == NULL) + test_printf("FAIL! File: %s - Line: %d", file, line); + else + { + va_list args; + va_start(args, message); + vsnprintf(str, MAX_STR_SIZE, message, args); + va_end(args); + test_printf("FAIL! File: %s - Line: %d - Message: %s", file, line, str); + } + } } void start_session() diff --git a/src/c/tests/debug_printf/test_print.h b/src/c/tests/debug_printf/test_print.h index 4b050d7..bd9ee69 100644 --- a/src/c/tests/debug_printf/test_print.h +++ b/src/c/tests/debug_printf/test_print.h @@ -1,9 +1,13 @@ #include "mgba.h" #include "stdbool.h" -#define assert(condition) assert_print(condition, __FILE__, __LINE__) +#define MAX_STR_SIZE 0x100 +#define NULL 0 -void assert_print(bool condition, const char* file, int line); +#define assert(condition) assert_print(condition, __FILE__, __LINE__, NULL) +#define assert_message(condition, format, ...) assert_print(condition, __FILE__, __LINE__, format, __VA_ARGS__) + +void assert_print(bool condition, const char* file, int line, const char* message, ...); void test_printf(const char* string, ...); void start_session(void); void end_session(void);