]> pd.if.org Git - pdclib/blob - testing/_PDCLIB_iotest.h
_PDCLIB_iotest.h: the code for determining if the result matched or not was too cleve...
[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 GET_RESULT \
19       rewind( target ); \
20       fread( result_buffer, 1, actual_rc, target );
21    #define RESULT_MISMATCH( act, exp ) strcmp( result_buffer, exp ) != 0
22    #define RESULT_STRING( tgt ) result_buffer
23 #elif defined( _PDCLIB_STRINGIO )
24    #define RESULT_MISMATCH( act, exp ) strcmp( act, exp ) != 0
25    #define GET_RESULT
26    #define RESULT_STRING( tgt ) tgt
27 #endif
28
29 #ifdef _PDCLIB_FILEIO
30 #define PREP_RESULT_BUFFER char result_buffer[100] = { 0 }; rewind( target );
31 #else
32 #define PREP_RESULT_BUFFER
33 #endif
34
35 #define PRINTF_TEST( expected_rc, expected_string, ... ) do { \
36         PREP_RESULT_BUFFER \
37         int actual_rc = testprintf( target, __VA_ARGS__ ); \
38         GET_RESULT \
39         if ( ( actual_rc != expected_rc ) || \
40              ( RESULT_MISMATCH( target, expected_string ) ) ) \
41         { \
42             ++TEST_RESULTS; \
43             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 ) ); \
44         } \
45     } while ( 0 )
46
47 /* ...scanf() tests */
48 #if defined( _PDCLIB_FILEIO )
49     #define PREPARE_SOURCE( input_string ) \
50         rewind( source ); \
51         fwrite( input_string, 1, sizeof( input_string ), source ); \
52         rewind( source );
53 #elif defined( _PDCLIB_STRINGIO )
54     #define PREPARE_SOURCE( input_string ) \
55         memcpy( source, input_string, sizeof( input_string ) );
56 #endif
57
58 #define SCANF_TEST( expected_rc, input_string, ... ) do { \
59         int actual_rc; \
60         PREPARE_SOURCE( input_string ); \
61         actual_rc = testscanf( source, __VA_ARGS__ ); \
62         if ( actual_rc != expected_rc ) \
63         { \
64             ++TEST_RESULTS; \
65             fprintf( stderr, "FAILED: " __FILE__ " (" _PDCLIB_FILEID "), line %d\n        expected %2d,        actual   %2d\n", __LINE__, expected_rc, actual_rc ); \
66         } \
67     } while ( 0 )
68
69 /* Virtually everything in the printf() / scanf() test drivers is heavily
70    depending on the platform, i.e. the width of the integer values. To do
71    proper domain tests, we need the limits of the integers (largest and
72    smallest value), which we can get from <limits.h>. But we also need the
73    string representations of these numbers, to the various bases, which of
74    course vary depending on how the platform defines 'int' and 'long'.
75 */
76
77 #define sym2v( x ) #x
78 #define sym2s( x ) sym2v( x )
79
80 #if INT_MAX >> 15 == 1
81
82 #define UINT_DIG 5
83 #define INT_DIG  5
84 #define INT_DIG_LESS1 "4"
85 #define INT_DIG_PLUS1 "6"
86 #define INT_DIG_PLUS2 "7"
87 #define INT_HEXDIG "FFF"
88 #define INT_hexdig "fff"
89 #define INT_OCTDIG "177777"
90 #define INT_MAX_DEZ_STR  "32767"
91 #define INT_MIN_DEZ_STR  "32768"
92 #define UINT_MAX_DEZ_STR "65535"
93 #define INT_MAX_OCT_STR
94 #define INT_MIN_OCT_STR
95 #define UINT_MAX_OCT_STR
96 #define INT_MAX_HEX_STR
97 #define INT_MIN_HEX_STR
98 #define UINT_MAX_HEX_STR
99
100 #elif UINT_MAX >> 31 == 1
101
102 #define UINT_DIG 10
103 #define INT_DIG  10
104 #define INT_DIG_LESS1 "9"
105 #define INT_DIG_PLUS1 "11"
106 #define INT_DIG_PLUS2 "12"
107 #define INT_HEXDIG "FFFFFFF"
108 #define INT_hexdig "fffffff"
109 #define INT_OCTDIG "37777777777"
110 #define INT_MAX_DEZ_STR  "2147483647"
111 #define INT_MIN_DEZ_STR  "2147483648"
112 #define UINT_MAX_DEZ_STR "4294967295"
113 #define INT_MAX_OCT_STR
114 #define INT_MIN_OCT_STR
115 #define UINT_MAX_OCT_STR
116 #define INT_MAX_HEX_STR
117 #define INT_MIN_HEX_STR
118 #define UINT_MAX_HEX_STR
119
120 #elif UINT_MAX >> 63 == 1
121
122 #define UINT_DIG 20
123 #define INT_DIG  19
124 #define INT_DIG_LESS1 "18"
125 #define INT_DIG_PLUS1 "20"
126 #define INT_DIG_PLUS2 "21"
127 #define INT_HEXDIG "FFFFFFFFFFFFFFF"
128 #define INT_hexdig "fffffffffffffff"
129 #define INT_OCTDIG "1777777777777777777777"
130 #define INT_MAX_DEZ_STR   "9223372036854775807"
131 #define INT_MIN_DEZ_STR   "9223372036854775808"
132 #define UINT_MAX_DEZ_STR "18446744073709551615"
133 #define INT_MAX_OCT_STR
134 #define INT_MIN_OCT_STR
135 #define UINT_MAX_OCT_STR
136 #define INT_MAX_HEX_STR
137 #define INT_MIN_HEX_STR
138 #define UINT_MAX_HEX_STR
139
140 #else
141
142 #error Unsupported width of 'int' (neither 16, 32, nor 64 bit).
143
144 #endif
145
146
147 #if ULONG_MAX >> 31 == 1
148
149 #define ULONG_DIG 10
150 #define LONG_DIG  10
151 #define LONG_MAX_DEZ_STR  "2147483647"
152 #define LONG_MIN_DEZ_STR  "2147483648"
153 #define ULONG_MAX_DEZ_STR "4294967295"
154 #define LONG_MAX_OCT_STR
155 #define LONG_MIN_OCT_STR
156 #define ULONG_MAX_OCT_STR
157 #define LONG_MAX_HEX_STR
158 #define LONG_MIN_HEX_STR
159 #define ULONG_MAX_HEX_STR
160
161 #elif ULONG_MAX >> 63 == 1
162
163 #define ULONG_DIG 20
164 #define LONG_DIG  19
165 #define LONG_MAX_DEZ_STR   "9223372036854775807"
166 #define LONG_MIN_DEZ_STR   "9223372036854775808"
167 #define ULONG_MAX_DEZ_STR "18446744073709551615"
168 #define LONG_MAX_OCT_STR
169 #define LONG_MIN_OCT_STR
170 #define ULONG_MAX_OCT_STR
171 #define LONG_MAX_HEX_STR
172 #define LONG_MIN_HEX_STR
173 #define ULONG_MAX_HEX_STR
174
175 #else
176
177 #error Unsupported width of 'long' (neither 32 nor 64 bit).
178
179 #endif
180
181
182 #if ULLONG_MAX >> 63 == 1
183
184 #define ULLONG_DIG 20
185 #define LLONG_DIG  19
186 #define LLONG_MAX_DEZ_STR   "9223372036854775807"
187 #define LLONG_MIN_DEZ_STR   "9223372036854775808"
188 #define ULLONG_MAX_DEZ_STR "18446744073709551615"
189 #define LLONG_MAX_OCT_STR
190 #define LLONG_MIN_OCT_STR
191 #define ULLONG_MAX_OCT_STR
192 #define LLONG_MAX_HEX_STR
193 #define LLONG_MIN_HEX_STR
194 #define ULLONG_MAX_HEX_STR
195
196 #elif ULLONG_MAX >> 127 == 1
197
198 #define ULLONG_DIG 38
199 #define LLONG_DIG  38
200 #define LLONG_MAX_DEZ_STR  "170141183460469231731687303715884105727"
201 #define LLONG_MIN_DEZ_STR  "170141183460469231731687303715884105728"
202 #define ULLONG_MAX_DEZ_STR "340282366920938463463374607431768211455"
203 #define LLONG_MAX_OCT_STR
204 #define LLONG_MIN_OCT_STR
205 #define ULLONG_MAX_OCT_STR
206 #define LLONG_MAX_HEX_STR
207 #define LLONG_MIN_HEX_STR
208 #define ULLONG_MAX_HEX_STR
209
210 #else
211
212 #error Unsupported width of 'long long' (neither 64 nor 128 bit).
213
214 #endif