3 /* PDCLib testing suite <_PDCLIB_test.h>
5 This file is part of the Public Domain C Library (PDCLib).
6 Permission is granted to use, modify, and / or redistribute at will.
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 /* -------------------------------------------------------------------------- */
16 /* ...printf() tests */
17 #if defined( _PDCLIB_FILEIO )
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
26 #define RESULT_STRING( tgt ) tgt
30 #define PREP_RESULT_BUFFER char result_buffer[100] = { 0 }; rewind( target );
32 #define PREP_RESULT_BUFFER
35 #define GETFMT( fmt, ... ) (fmt)
36 #define PRINTF_TEST( expected_rc, expected_string, ... ) do { \
38 int actual_rc = testprintf( target, __VA_ARGS__ ); \
40 if ( ( actual_rc != expected_rc ) || \
41 ( RESULT_MISMATCH( target, expected_string ) ) ) \
45 "FAILED: " __FILE__ " (" _PDCLIB_FILEID "), line %d\n" \
46 " format string \"%s\"\n" \
47 " expected %2d, \"%s\"\n" \
48 " actual %2d, \"%s\"\n", \
49 __LINE__, GETFMT(__VA_ARGS__, 0), expected_rc, \
50 expected_string, actual_rc, RESULT_STRING( target ) ); \
54 /* ...scanf() tests */
55 #if defined( _PDCLIB_FILEIO )
56 #define PREPARE_SOURCE( input_string ) \
58 fwrite( input_string, 1, sizeof( input_string ), source ); \
60 #elif defined( _PDCLIB_STRINGIO )
61 #define PREPARE_SOURCE( input_string ) \
62 memcpy( source, input_string, sizeof( input_string ) );
65 #define SCANF_TEST( expected_rc, input_string, ... ) do { \
67 PREPARE_SOURCE( input_string ); \
68 actual_rc = testscanf( source, __VA_ARGS__ ); \
69 if ( actual_rc != expected_rc ) \
72 fprintf( stderr, "FAILED: " __FILE__ " (" _PDCLIB_FILEID "), line %d\n expected %2d, actual %2d\n", __LINE__, expected_rc, actual_rc ); \
76 /* Virtually everything in the printf() / scanf() test drivers is heavily
77 depending on the platform, i.e. the width of the integer values. To do
78 proper domain tests, we need the limits of the integers (largest and
79 smallest value), which we can get from <limits.h>. But we also need the
80 string representations of these numbers, to the various bases, which of
81 course vary depending on how the platform defines 'int' and 'long'.
85 #define sym2s( x ) sym2v( x )
87 #if INT_MAX >> 15 == 1
91 #define INT_DIG_LESS1 "4"
92 #define INT_DIG_PLUS1 "6"
93 #define INT_DIG_PLUS2 "7"
94 #define INT_HEXDIG "FFF"
95 #define INT_hexdig "fff"
96 #define INT_OCTDIG "177777"
97 #define INT_MAX_DEZ_STR "32767"
98 #define INT_MIN_DEZ_STR "32768"
99 #define UINT_MAX_DEZ_STR "65535"
100 #define INT_MAX_OCT_STR
101 #define INT_MIN_OCT_STR
102 #define UINT_MAX_OCT_STR
103 #define INT_MAX_HEX_STR
104 #define INT_MIN_HEX_STR
105 #define UINT_MAX_HEX_STR
107 #elif UINT_MAX >> 31 == 1
111 #define INT_DIG_LESS1 "9"
112 #define INT_DIG_PLUS1 "11"
113 #define INT_DIG_PLUS2 "12"
114 #define INT_HEXDIG "FFFFFFF"
115 #define INT_hexdig "fffffff"
116 #define INT_OCTDIG "37777777777"
117 #define INT_MAX_DEZ_STR "2147483647"
118 #define INT_MIN_DEZ_STR "2147483648"
119 #define UINT_MAX_DEZ_STR "4294967295"
120 #define INT_MAX_OCT_STR
121 #define INT_MIN_OCT_STR
122 #define UINT_MAX_OCT_STR
123 #define INT_MAX_HEX_STR
124 #define INT_MIN_HEX_STR
125 #define UINT_MAX_HEX_STR
127 #elif UINT_MAX >> 63 == 1
131 #define INT_DIG_LESS1 "18"
132 #define INT_DIG_PLUS1 "20"
133 #define INT_DIG_PLUS2 "21"
134 #define INT_HEXDIG "FFFFFFFFFFFFFFF"
135 #define INT_hexdig "fffffffffffffff"
136 #define INT_OCTDIG "1777777777777777777777"
137 #define INT_MAX_DEZ_STR "9223372036854775807"
138 #define INT_MIN_DEZ_STR "9223372036854775808"
139 #define UINT_MAX_DEZ_STR "18446744073709551615"
140 #define INT_MAX_OCT_STR
141 #define INT_MIN_OCT_STR
142 #define UINT_MAX_OCT_STR
143 #define INT_MAX_HEX_STR
144 #define INT_MIN_HEX_STR
145 #define UINT_MAX_HEX_STR
149 #error Unsupported width of 'int' (neither 16, 32, nor 64 bit).
154 #if ULONG_MAX >> 31 == 1
158 #define LONG_MAX_DEZ_STR "2147483647"
159 #define LONG_MIN_DEZ_STR "2147483648"
160 #define ULONG_MAX_DEZ_STR "4294967295"
161 #define LONG_MAX_OCT_STR
162 #define LONG_MIN_OCT_STR
163 #define ULONG_MAX_OCT_STR
164 #define LONG_MAX_HEX_STR
165 #define LONG_MIN_HEX_STR
166 #define ULONG_MAX_HEX_STR
168 #elif ULONG_MAX >> 63 == 1
172 #define LONG_MAX_DEZ_STR "9223372036854775807"
173 #define LONG_MIN_DEZ_STR "9223372036854775808"
174 #define ULONG_MAX_DEZ_STR "18446744073709551615"
175 #define LONG_MAX_OCT_STR
176 #define LONG_MIN_OCT_STR
177 #define ULONG_MAX_OCT_STR
178 #define LONG_MAX_HEX_STR
179 #define LONG_MIN_HEX_STR
180 #define ULONG_MAX_HEX_STR
184 #error Unsupported width of 'long' (neither 32 nor 64 bit).
189 #if ULLONG_MAX >> 63 == 1
191 #define ULLONG_DIG 20
193 #define LLONG_MAX_DEZ_STR "9223372036854775807"
194 #define LLONG_MIN_DEZ_STR "9223372036854775808"
195 #define ULLONG_MAX_DEZ_STR "18446744073709551615"
196 #define LLONG_MAX_OCT_STR
197 #define LLONG_MIN_OCT_STR
198 #define ULLONG_MAX_OCT_STR
199 #define LLONG_MAX_HEX_STR
200 #define LLONG_MIN_HEX_STR
201 #define ULLONG_MAX_HEX_STR
203 #elif ULLONG_MAX >> 127 == 1
205 #define ULLONG_DIG 38
207 #define LLONG_MAX_DEZ_STR "170141183460469231731687303715884105727"
208 #define LLONG_MIN_DEZ_STR "170141183460469231731687303715884105728"
209 #define ULLONG_MAX_DEZ_STR "340282366920938463463374607431768211455"
210 #define LLONG_MAX_OCT_STR
211 #define LLONG_MIN_OCT_STR
212 #define ULLONG_MAX_OCT_STR
213 #define LLONG_MAX_HEX_STR
214 #define LLONG_MIN_HEX_STR
215 #define ULLONG_MAX_HEX_STR
219 #error Unsupported width of 'long long' (neither 64 nor 128 bit).