f39ce8a0ea5fd9fd7fa7d8a413ac91801f268ed9
[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
15 static char const abcde[] = "abcde";
16 static char const abcdx[] = "abcdx";
17 static char const teststring[] = "1234567890\nABCDEFGHIJKLMNOPQRSTUVWXYZ\nabcdefghijklmnopqrstuvwxyz\n";
18 static char const testfile[]="testing/testfile";
19 static char const testfile1[]="testing/testfile1";
20 static char const testfile2[]="testing/testfile2";
21
22 #define NO_TESTDRIVER 0
23
24 static int TEST_RESULTS = 0;
25
26 #define TESTCASE( x ) if ( x ) {} \
27                       else { TEST_RESULTS += 1; printf( "FAILED: " __FILE__ ", line %d - %s\n", __LINE__, #x ); }
28
29 #if defined( FPRINTF_FUNCTION )
30 static char result_buffer[ 1000 ];
31 #define RESULT_MISMATCH( act, exp ) \
32     rewind( act ), \
33     result_buffer[ fread( result_buffer, 1, strlen( exp ) + 1, act ) ] = '\0', \
34     rewind( act ), \
35     memcmp( result_buffer, exp, strlen( exp ) )
36 #define RESULT_STRING( tgt ) result_buffer
37 #elif defined( SPRINTF_FUNCTION )
38 #define RESULT_MISMATCH( act, exp ) strcmp( act, exp ) != 0
39 #define RESULT_STRING( tgt ) tgt
40 #endif
41
42 #define PRINTF_TEST( expected_rc, expected_string, format, ... ) { \
43         int actual_rc = testprintf( target, format, __VA_ARGS__ ); \
44         if ( ( actual_rc != expected_rc ) || \
45              ( RESULT_MISMATCH( target, expected_string ) ) ) \
46         { \
47             ++TEST_RESULTS; \
48             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 ) ); \
49         } \
50     } while ( 0 )
51
52 #ifndef REGTEST
53 #define TESTCASE_NOREG( x ) TESTCASE( x )
54 #else
55 #define TESTCASE_NOREG( x )
56 #endif