3 /* PDCLib testing suite <_PDCLIB_test.h>
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
9 /* -------------------------------------------------------------------------- */
10 /* Helper macros for printf() / scanf() tests */
11 /* -------------------------------------------------------------------------- */
12 /* Tucked away in a seperate header because these are ugly, complex, and not */
13 /* needed in 95% of all test cases. */
14 /* -------------------------------------------------------------------------- */
16 /* ...printf() tests */
17 #if defined( _PDCLIB_FILEIO )
18 #define RESULT_MISMATCH( act, exp ) \
20 result_buffer[ fread( result_buffer, 1, strlen( exp ) + 1, act ) ] = '\0', \
22 memcmp( result_buffer, exp, strlen( exp ) )
23 #define RESULT_STRING( tgt ) result_buffer
24 #elif defined( _PDCLIB_STRINGIO )
25 #define RESULT_MISMATCH( act, exp ) strcmp( act, exp ) != 0
26 #define RESULT_STRING( tgt ) tgt
30 #define PREP_RESULT_BUFFER char result_buffer[100];
32 #define PREP_RESULT_BUFFER
35 #define PRINTF_TEST( expected_rc, expected_string, format, ... ) do { \
37 int actual_rc = testprintf( target, format, __VA_ARGS__ ); \
38 if ( ( actual_rc != expected_rc ) || \
39 ( RESULT_MISMATCH( target, expected_string ) ) ) \
42 fprintf( stderr, "FAILED: " __FILE__ " (" _PDCLIB_FILEID "), line %d\n expected %2d, \"%s\"\n actual %2d, \"%s\"\n", __LINE__, expected_rc, expected_string, actual_rc, RESULT_STRING( target ) ); \
46 /* ...scanf() tests */
47 #if defined( _PDCLIB_FILEIO )
48 #define PREPARE_SOURCE( input_string ) \
50 fwrite( input_string, 1, sizeof( input_string ), source ); \
52 #elif defined( _PDCLIB_STRINGIO )
53 #define PREPARE_SOURCE( input_string ) \
54 memcpy( source, input_string, sizeof( input_string ) );
57 #define SCANF_TEST( expected_rc, input_string, format, ... ) do { \
59 PREPARE_SOURCE( input_string ); \
60 actual_rc = testscanf( source, format, __VA_ARGS__ ); \
61 if ( actual_rc != expected_rc ) \
64 fprintf( stderr, "FAILED: " __FILE__ " (" _PDCLIB_FILEID "), line %d\n expected %2d, actual %2d\n", __LINE__, expected_rc, actual_rc ); \