Reputation: 44715
I want to extend minunit to be more useful, with the macro.
#define mu_assert_equal(actual, expected) do { \
if (actual != expected) { \
char *message = malloc(MAX_ERROR_MESSAGE_LENGTH); \
if (message == NULL) { printf("malloc failed"); exit(1); } \
snprintf(message, MAX_ERROR_MESSAGE_LENGTH, "required: %s != %s, reality: %s == %lu", \
#actual, #expected, #actual, actual); \
return message; \
} \
} while (0)
invoked with:
mu_assert_equal(bytes_parsed, 1);
but the macro above only works for unsigned long values.
How I can I find the type of the macro arguments, and more importantly, their printf specifiers.
Upvotes: 1
Views: 354
Reputation: 2960
Without generics, perhaps the best would be different macros for your different types:
#define mu_assert_equal(actual, expected, test, fmt) do { \
if ( test ) { \
char *message = malloc(MAX_ERROR_MESSAGE_LENGTH); \
if (message == NULL) { printf("malloc failed"); exit(1); } \
snprintf(message, MAX_ERROR_MESSAGE_LENGTH, "required: %s != %s, reality: %s == " fmt,\
#actual, #expected, #actual, actual); \
return message; \
} \
} while (0)
#define mu_assert_equal_int(actual, expected) \
mu_assert_equal(actual, expected, actual != expected, "%lu")
#define mu_assert_equal_str(actual, expected) \
mu_assert_equal(actual, expected, strcmp( actual, expected ), "%s")
and invoke as:
mu_assert_equal_str( test_str, "abcde" ) ;
mu_assert_equal_int( test_int, 12345 ) ;
(Edited in light of comment to also pass the test to the "generic" test).
Upvotes: 1