1 /* PDCLib testing suite <_PDCLIB_test.h>
3 This file is part of the Public Domain C Library (PDCLib).
4 Permission is granted to use, modify, and / or redistribute at will.
7 /* -------------------------------------------------------------------------- */
8 /* Helper macros for printf() / scanf() tests */
9 /* -------------------------------------------------------------------------- */
10 /* Tucked away in a seperate header because these are ugly, complex, and not */
11 /* needed in 95% of all test cases. */
12 /* -------------------------------------------------------------------------- */
14 /* ...printf() tests */
15 #if defined( _PDCLIB_FILEIO )
18 if ( (int)fread( result_buffer, 1, actual_rc, target ) != actual_rc ) \
20 fprintf( stderr, "GET_RESULT failed." ); \
22 #define RESULT_MISMATCH( act, exp ) strcmp( result_buffer, exp ) != 0
23 #define RESULT_STRING( tgt ) result_buffer
24 #elif defined( _PDCLIB_STRINGIO )
25 #define RESULT_MISMATCH( act, exp ) strcmp( act, exp ) != 0
27 #define RESULT_STRING( tgt ) tgt
31 #define PREP_RESULT_BUFFER char result_buffer[100] = { 0 }; rewind( target );
33 #define PREP_RESULT_BUFFER
36 #define GETFMT( fmt, ... ) (fmt)
37 #define PRINTF_TEST( expected_rc, expected_string, ... ) do { \
39 int actual_rc = testprintf( target, __VA_ARGS__ ); \
41 if ( ( actual_rc != expected_rc ) || \
42 ( RESULT_MISMATCH( target, expected_string ) ) ) \
46 "FAILED: " __FILE__ " (" _PDCLIB_FILEID "), line %d\n" \
47 " format string \"%s\"\n" \
48 " expected %2d, \"%s\"\n" \
49 " actual %2d, \"%s\"\n", \
50 __LINE__, GETFMT(__VA_ARGS__, 0), expected_rc, \
51 expected_string, actual_rc, RESULT_STRING( target ) ); \
55 /* ...scanf() tests */
56 #if defined( _PDCLIB_FILEIO )
57 #define PREPARE_SOURCE( input_string ) \
59 fwrite( input_string, 1, sizeof( input_string ), source ); \
61 #elif defined( _PDCLIB_STRINGIO )
62 #define PREPARE_SOURCE( input_string ) \
63 memcpy( source, input_string, sizeof( input_string ) );
66 #define SCANF_TEST( expected_rc, input_string, ... ) do { \
68 PREPARE_SOURCE( input_string ); \
69 actual_rc = testscanf( source, __VA_ARGS__ ); \
70 if ( actual_rc != expected_rc ) \
73 fprintf( stderr, "FAILED: " __FILE__ " (" _PDCLIB_FILEID "), line %d\n expected %2d, actual %2d\n", __LINE__, expected_rc, actual_rc ); \
77 /* Virtually everything in the printf() / scanf() test drivers is heavily
78 depending on the platform, i.e. the width of the integer values. To do
79 proper domain tests, we need the limits of the integers (largest and
80 smallest value), which we can get from <limits.h>. But we also need the
81 string representations of these numbers, to the various bases, which of
82 course vary depending on how the platform defines 'int' and 'long'.
86 #define sym2s( x ) sym2v( x )
88 #if INT_MAX >> 15 == 1
92 #define INT_DIG_LESS1 "4"
93 #define INT_DIG_PLUS1 "6"
94 #define INT_DIG_PLUS2 "7"
95 #define INT_HEXDIG "FFF"
96 #define INT_hexdig "fff"
97 #define INT_OCTDIG "177777"
98 #define INT_MAX_DEZ_STR "32767"
99 #define INT_MIN_DEZ_STR "32768"
100 #define UINT_MAX_DEZ_STR "65535"
101 #define INT_MAX_OCT_STR
102 #define INT_MIN_OCT_STR
103 #define UINT_MAX_OCT_STR
104 #define INT_MAX_HEX_STR
105 #define INT_MIN_HEX_STR
106 #define UINT_MAX_HEX_STR
108 #elif UINT_MAX >> 31 == 1
112 #define INT_DIG_LESS1 "9"
113 #define INT_DIG_PLUS1 "11"
114 #define INT_DIG_PLUS2 "12"
115 #define INT_HEXDIG "FFFFFFF"
116 #define INT_hexdig "fffffff"
117 #define INT_OCTDIG "37777777777"
118 #define INT_MAX_DEZ_STR "2147483647"
119 #define INT_MIN_DEZ_STR "2147483648"
120 #define UINT_MAX_DEZ_STR "4294967295"
121 #define INT_MAX_OCT_STR
122 #define INT_MIN_OCT_STR
123 #define UINT_MAX_OCT_STR
124 #define INT_MAX_HEX_STR
125 #define INT_MIN_HEX_STR
126 #define UINT_MAX_HEX_STR
128 #elif UINT_MAX >> 63 == 1
132 #define INT_DIG_LESS1 "18"
133 #define INT_DIG_PLUS1 "20"
134 #define INT_DIG_PLUS2 "21"
135 #define INT_HEXDIG "FFFFFFFFFFFFFFF"
136 #define INT_hexdig "fffffffffffffff"
137 #define INT_OCTDIG "1777777777777777777777"
138 #define INT_MAX_DEZ_STR "9223372036854775807"
139 #define INT_MIN_DEZ_STR "9223372036854775808"
140 #define UINT_MAX_DEZ_STR "18446744073709551615"
141 #define INT_MAX_OCT_STR
142 #define INT_MIN_OCT_STR
143 #define UINT_MAX_OCT_STR
144 #define INT_MAX_HEX_STR
145 #define INT_MIN_HEX_STR
146 #define UINT_MAX_HEX_STR
150 #error Unsupported width of 'int' (neither 16, 32, nor 64 bit).
155 #if ULONG_MAX >> 31 == 1
159 #define LONG_MAX_DEZ_STR "2147483647"
160 #define LONG_MIN_DEZ_STR "2147483648"
161 #define ULONG_MAX_DEZ_STR "4294967295"
162 #define LONG_MAX_OCT_STR
163 #define LONG_MIN_OCT_STR
164 #define ULONG_MAX_OCT_STR
165 #define LONG_MAX_HEX_STR
166 #define LONG_MIN_HEX_STR
167 #define ULONG_MAX_HEX_STR
169 #elif ULONG_MAX >> 63 == 1
173 #define LONG_MAX_DEZ_STR "9223372036854775807"
174 #define LONG_MIN_DEZ_STR "9223372036854775808"
175 #define ULONG_MAX_DEZ_STR "18446744073709551615"
176 #define LONG_MAX_OCT_STR
177 #define LONG_MIN_OCT_STR
178 #define ULONG_MAX_OCT_STR
179 #define LONG_MAX_HEX_STR
180 #define LONG_MIN_HEX_STR
181 #define ULONG_MAX_HEX_STR
185 #error Unsupported width of 'long' (neither 32 nor 64 bit).
190 #if ULLONG_MAX >> 63 == 1
192 #define ULLONG_DIG 20
194 #define LLONG_MAX_DEZ_STR "9223372036854775807"
195 #define LLONG_MIN_DEZ_STR "9223372036854775808"
196 #define ULLONG_MAX_DEZ_STR "18446744073709551615"
197 #define LLONG_MAX_OCT_STR
198 #define LLONG_MIN_OCT_STR
199 #define ULLONG_MAX_OCT_STR
200 #define LLONG_MAX_HEX_STR
201 #define LLONG_MIN_HEX_STR
202 #define ULLONG_MAX_HEX_STR
204 #elif ULLONG_MAX >> 127 == 1
206 #define ULLONG_DIG 38
208 #define LLONG_MAX_DEZ_STR "170141183460469231731687303715884105727"
209 #define LLONG_MIN_DEZ_STR "170141183460469231731687303715884105728"
210 #define ULLONG_MAX_DEZ_STR "340282366920938463463374607431768211455"
211 #define LLONG_MAX_OCT_STR
212 #define LLONG_MIN_OCT_STR
213 #define ULLONG_MAX_OCT_STR
214 #define LLONG_MAX_HEX_STR
215 #define LLONG_MIN_HEX_STR
216 #define ULLONG_MAX_HEX_STR
220 #error Unsupported width of 'long long' (neither 64 nor 128 bit).