In a project of mine (which for technical reasons must not depend on any external libraries or third-party sources) I have set up the following test.h
:
static int rc = 0;
#define TESTCASE( x ) if ( x ) {} \
else \
{ \
rc += 1; \
printf( "FAILED: " __FILE__ ", line %d - " #x "\n", __LINE__ ); \
}
This works fine most of the time - if the testcase evaluates to true, nothing happens. If the testcase evaluates to false, the testcase is printed including file name and line number.
However, when the testcase contains a string literal, the printf()
in the TESTCASE macro insists on interpreting the string literal as part of the format string (which proved fatal for a certain testcase).
Now I am pondering various approaches to get the printf()
to print whatever is in x
(the testcase) verbatim, but I've come up empty. The closest thing was to use %c
to print a fixed numbers of characters, and pass the textcase (#x
) as parameter to printf()
- but I couldn't figure out how to determine the size of #x
, especially if it contains zero bytes (which rules out any use of strlen()
).
Confused, and at a loss. Help?