Add support for messages within asserts

This commit is contained in:
Lorenzo Carletti 2021-11-16 20:51:53 +01:00
parent 131da4f142
commit 9154667efa
2 changed files with 21 additions and 4 deletions

View File

@ -1,6 +1,7 @@
#include "test_print.h" #include "test_print.h"
#include "mgba.h" #include "mgba.h"
#include <stdarg.h> #include <stdarg.h>
#include "printf.h"
void test_printf(const char* ptr, ...) { void test_printf(const char* ptr, ...) {
@ -10,10 +11,22 @@ void test_printf(const char* ptr, ...) {
va_end(args); 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) 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() void start_session()

View File

@ -1,9 +1,13 @@
#include "mgba.h" #include "mgba.h"
#include "stdbool.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 test_printf(const char* string, ...);
void start_session(void); void start_session(void);
void end_session(void); void end_session(void);