]> pd.if.org Git - pdclib/blob - testing/_PDCLIB_iotest.h
Put printf() / scanf() test macros in seperate header.
[pdclib] / testing / _PDCLIB_iotest.h
1 /* $Id$ */
2
3 /* PDCLib testing suite <_PDCLIB_test.h>
4
5    This file is part of the Public Domain C Library (PDCLib).
6    Permission is granted to use, modify, and / or redistribute at will.
7 */
8
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 /* -------------------------------------------------------------------------- */
15
16 /* ...printf() tests */
17 #if defined( _PDCLIB_FILEIO )
18     #define RESULT_MISMATCH( act, exp ) \
19         rewind( act ), \
20         result_buffer[ fread( result_buffer, 1, strlen( exp ) + 1, act ) ] = '\0', \
21         rewind( act ), \
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
27 #endif
28
29 #ifdef _PDCLIB_FILEIO
30 #define PREP_RESULT_BUFFER char result_buffer[100];
31 #else
32 #define PREP_RESULT_BUFFER
33 #endif
34
35 #define PRINTF_TEST( expected_rc, expected_string, format, ... ) do { \
36         PREP_RESULT_BUFFER \
37         int actual_rc = testprintf( target, format, __VA_ARGS__ ); \
38         if ( ( actual_rc != expected_rc ) || \
39              ( RESULT_MISMATCH( target, expected_string ) ) ) \
40         { \
41             ++TEST_RESULTS; \
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 ) ); \
43         } \
44     } while ( 0 )
45
46 /* ...scanf() tests */
47 #if defined( _PDCLIB_FILEIO )
48     #define PREPARE_SOURCE( input_string ) \
49         rewind( source ); \
50         fwrite( input_string, 1, sizeof( input_string ), source ); \
51         rewind( source );
52 #elif defined( _PDCLIB_STRINGIO )
53     #define PREPARE_SOURCE( input_string ) \
54         memcpy( source, input_string, sizeof( input_string ) );
55 #endif
56
57 #define SCANF_TEST( expected_rc, input_string, format, ... ) do { \
58         int actual_rc; \
59         PREPARE_SOURCE( input_string ); \
60         actual_rc = testscanf( source, format, __VA_ARGS__ ); \
61         if ( actual_rc != expected_rc ) \
62         { \
63             ++TEST_RESULTS; \
64             fprintf( stderr, "FAILED: " __FILE__ " (" _PDCLIB_FILEID "), line %d\n        expected %2d,        actual   %2d\n", __LINE__, expected_rc, actual_rc ); \
65         } \
66     } while ( 0 )
67