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