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 )
18 #define RESULT_MISMATCH( act, exp ) \
20 result_buffer[ fread( result_buffer, 1, strlen( exp ) + 1, act ) ] = '\0', \
22 memcmp( result_buffer, exp, strlen( exp ) )
23 #define RESULT_STRING( tgt ) result_buffer
24 #elif defined( _PDCLIB_STRINGIO )
25 #define RESULT_MISMATCH( act, exp ) strcmp( act, exp ) != 0
26 #define RESULT_STRING( tgt ) tgt
30 #define PREP_RESULT_BUFFER char result_buffer[100];
32 #define PREP_RESULT_BUFFER
35 #define PRINTF_TEST( expected_rc, expected_string, format, ... ) do { \
37 int actual_rc = testprintf( target, format, __VA_ARGS__ ); \
38 if ( ( actual_rc != expected_rc ) || \
39 ( RESULT_MISMATCH( target, expected_string ) ) ) \
42 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 ) ); \
46 /* ...scanf() tests */
47 #if defined( _PDCLIB_FILEIO )
48 #define PREPARE_SOURCE( input_string ) \
50 fwrite( input_string, 1, sizeof( input_string ), source ); \
52 #elif defined( _PDCLIB_STRINGIO )
53 #define PREPARE_SOURCE( input_string ) \
54 memcpy( source, input_string, sizeof( input_string ) );
57 #define SCANF_TEST( expected_rc, input_string, format, ... ) do { \
59 PREPARE_SOURCE( input_string ); \
60 actual_rc = testscanf( source, format, __VA_ARGS__ ); \
61 if ( actual_rc != expected_rc ) \
64 fprintf( stderr, "FAILED: " __FILE__ " (" _PDCLIB_FILEID "), line %d\n expected %2d, actual %2d\n", __LINE__, expected_rc, actual_rc ); \
68 /* Virtually everything in the printf() / scanf() test drivers is heavily
69 depending on the platform, i.e. the width of the integer values. To do
70 proper domain tests, we need the limits of the integers (largest and
71 smallest value), which we can get from <limits.h>. But we also need the
72 string representations of these numbers, to the various bases, which of
73 course vary depending on how the platform defines 'int' and 'long'.
77 #define sym2s( x ) sym2v( x )
79 #if INT_MAX >> 15 == 1
83 #define INT_DIG_LESS1 "4"
84 #define INT_DIG_PLUS1 "6"
85 #define INT_DIG_PLUS2 "7"
86 #define INT_HEXDIG "FFF"
87 #define INT_hexdig "fff"
88 #define INT_OCTDIG "177777"
89 #define INT_MAX_DEZ_STR "32767"
90 #define INT_MIN_DEZ_STR "32768"
91 #define UINT_MAX_DEZ_STR "65535"
92 #define INT_MAX_OCT_STR
93 #define INT_MIN_OCT_STR
94 #define UINT_MAX_OCT_STR
95 #define INT_MAX_HEX_STR
96 #define INT_MIN_HEX_STR
97 #define UINT_MAX_HEX_STR
99 #elif UINT_MAX >> 31 == 1
103 #define INT_DIG_LESS1 "9"
104 #define INT_DIG_PLUS1 "11"
105 #define INT_DIG_PLUS2 "12"
106 #define INT_HEXDIG "FFFFFFF"
107 #define INT_hexdig "fffffff"
108 #define INT_OCTDIG "37777777777"
109 #define INT_MAX_DEZ_STR "2147483647"
110 #define INT_MIN_DEZ_STR "2147483648"
111 #define UINT_MAX_DEZ_STR "4294967295"
112 #define INT_MAX_OCT_STR
113 #define INT_MIN_OCT_STR
114 #define UINT_MAX_OCT_STR
115 #define INT_MAX_HEX_STR
116 #define INT_MIN_HEX_STR
117 #define UINT_MAX_HEX_STR
119 #elif UINT_MAX >> 63 == 1
123 #define INT_DIG_LESS1 "18"
124 #define INT_DIG_PLUS1 "20"
125 #define INT_DIG_PLUS2 "21"
126 #define INT_HEXDIG "FFFFFFFFFFFFFFF"
127 #define INT_hexdig "fffffffffffffff"
128 #define INT_OCTDIG "1777777777777777777777"
129 #define INT_MAX_DEZ_STR "9223372036854775807"
130 #define INT_MIN_DEZ_STR "9223372036854775808"
131 #define UINT_MAX_DEZ_STR "18446744073709551615"
132 #define INT_MAX_OCT_STR
133 #define INT_MIN_OCT_STR
134 #define UINT_MAX_OCT_STR
135 #define INT_MAX_HEX_STR
136 #define INT_MIN_HEX_STR
137 #define UINT_MAX_HEX_STR
141 #error Unsupported width of 'int' (neither 16, 32, nor 64 bit).
146 #if ULONG_MAX >> 31 == 1
150 #define LONG_MAX_DEZ_STR "2147483647"
151 #define LONG_MIN_DEZ_STR "2147483648"
152 #define ULONG_MAX_DEZ_STR "4294967295"
153 #define LONG_MAX_OCT_STR
154 #define LONG_MIN_OCT_STR
155 #define ULONG_MAX_OCT_STR
156 #define LONG_MAX_HEX_STR
157 #define LONG_MIN_HEX_STR
158 #define ULONG_MAX_HEX_STR
160 #elif ULONG_MAX >> 63 == 1
164 #define LONG_MAX_DEZ_STR "9223372036854775807"
165 #define LONG_MIN_DEZ_STR "9223372036854775808"
166 #define ULONG_MAX_DEZ_STR "18446744073709551615"
167 #define LONG_MAX_OCT_STR
168 #define LONG_MIN_OCT_STR
169 #define ULONG_MAX_OCT_STR
170 #define LONG_MAX_HEX_STR
171 #define LONG_MIN_HEX_STR
172 #define ULONG_MAX_HEX_STR
176 #error Unsupported width of 'long' (neither 32 nor 64 bit).
181 #if ULLONG_MAX >> 63 == 1
183 #define ULLONG_DIG 20
185 #define LLONG_MAX_DEZ_STR "9223372036854775807"
186 #define LLONG_MIN_DEZ_STR "9223372036854775808"
187 #define ULLONG_MAX_DEZ_STR "18446744073709551615"
188 #define LLONG_MAX_OCT_STR
189 #define LLONG_MIN_OCT_STR
190 #define ULLONG_MAX_OCT_STR
191 #define LLONG_MAX_HEX_STR
192 #define LLONG_MIN_HEX_STR
193 #define ULLONG_MAX_HEX_STR
195 #elif ULLONG_MAX >> 127 == 1
197 #define ULLONG_DIG 38
199 #define LLONG_MAX_DEZ_STR "170141183460469231731687303715884105727"
200 #define LLONG_MIN_DEZ_STR "170141183460469231731687303715884105728"
201 #define ULLONG_MAX_DEZ_STR "340282366920938463463374607431768211455"
202 #define LLONG_MAX_OCT_STR
203 #define LLONG_MIN_OCT_STR
204 #define ULLONG_MAX_OCT_STR
205 #define LLONG_MAX_HEX_STR
206 #define LLONG_MIN_HEX_STR
207 #define ULLONG_MAX_HEX_STR
211 #error Unsupported width of 'long long' (neither 64 nor 128 bit).