Reworked scanf() testing. General cleanups.
[fw/pdclib] / testing / _PDCLIB_test.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 test drivers                                             */
11 /* -------------------------------------------------------------------------- */
12
13 #include <stdio.h>
14 #include <limits.h>
15 #include <string.h>
16
17 /* Some strings used for <string.h> and <stdlib.h> testing. */
18 static char const abcde[] = "abcde";
19 static char const abcdx[] = "abcdx";
20 static char const teststring[] = "1234567890\nABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstuvwxyz\n";
21
22 /* Temporary file names */
23 static char const testfile[]="testing/testfile";
24 static char const testfile1[]="testing/testfile1";
25 static char const testfile2[]="testing/testfile2";
26
27 #define NO_TESTDRIVER 0
28
29 static int TEST_RESULTS = 0;
30
31 /* TESTCASE() - generic test */
32 #define TESTCASE( x ) if ( x ) {} \
33                       else { TEST_RESULTS += 1; printf( "FAILED: " __FILE__ ", line %d - %s\n", __LINE__, #x ); }
34
35 /* TESTCASE_NOREG() - PDCLib-only test */
36 #ifndef REGTEST
37     #define TESTCASE_NOREG( x ) TESTCASE( x )
38 #else
39     #define TESTCASE_NOREG( x )
40 #endif
41
42 /* ...printf() tests */
43 #if defined( _PDCLIB_FILEIO )
44     #define RESULT_MISMATCH( act, exp ) \
45         rewind( act ), \
46         result_buffer[ fread( result_buffer, 1, strlen( exp ) + 1, act ) ] = '\0', \
47         rewind( act ), \
48         memcmp( result_buffer, exp, strlen( exp ) )
49    #define RESULT_STRING( tgt ) result_buffer
50 #elif defined( _PDCLIB_STRINGIO )
51    #define RESULT_MISMATCH( act, exp ) strcmp( act, exp ) != 0
52    #define RESULT_STRING( tgt ) tgt
53 #endif
54
55 #ifdef _PDCLIB_FILEIO
56 #define PREP_RESULT_BUFFER char result_buffer[100];
57 #else
58 #define PREP_RESULT_BUFFER
59 #endif
60
61 #define PRINTF_TEST( expected_rc, expected_string, format, ... ) do { \
62         PREP_RESULT_BUFFER \
63         int actual_rc = testprintf( target, format, __VA_ARGS__ ); \
64         if ( ( actual_rc != expected_rc ) || \
65              ( RESULT_MISMATCH( target, expected_string ) ) ) \
66         { \
67             ++TEST_RESULTS; \
68             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 ) ); \
69         } \
70     } while ( 0 )
71
72 /* ...scanf() tests */
73 #if defined( _PDCLIB_FILEIO )
74     #define PREPARE_SOURCE( input_string ) \
75         rewind( source ); \
76         fwrite( input_string, 1, sizeof( input_string ), source ); \
77         rewind( source );
78 #elif defined( _PDCLIB_STRINGIO )
79     #define PREPARE_SOURCE( input_string ) \
80         memcpy( source, input_string, sizeof( input_string ) );
81 #endif
82
83 #define SCANF_TEST( expected_rc, input_string, format, ... ) do { \
84         int actual_rc; \
85         PREPARE_SOURCE( input_string ); \
86         actual_rc = testscanf( source, format, __VA_ARGS__ ); \
87         if ( actual_rc != expected_rc ) \
88         { \
89             ++TEST_RESULTS; \
90             fprintf( stderr, "FAILED: " __FILE__ " (" _PDCLIB_FILEID "), line %d\n        expected %2d,        actual   %2d\n", __LINE__, expected_rc, actual_rc ); \
91         } \
92     } while ( 0 )
93