OSDN Git Service

More rationalization of CRT_INLINE function implementations.
[mingw/mingw-org-wsl.git] / mingwrt / mingwex / sitest.c
1 /*
2         sitest -- exercise features of C99 <stdint.h> and <inttypes.h>
3
4         This source code has been placed into the PUBLIC DOMAIN by its author.
5
6         last edit:      1999/11/05      gwyn@arl.mil
7
8         Tries to accommodate pre-C99 versions of <inttypes.h>.
9
10         Takes advantage of __Q8_* symbols defined by a particular
11         implementation of <stdint.h>, but doesn't require them.
12
13         NOTE:   This is not a thorough validation test of the facilities.
14 */
15
16 #include        <errno.h>
17 #include        <limits.h>              /* for CHAR_BIT */
18 #include        <stdio.h>
19 #include        <stddef.h>              /* for ptrdiff_t */
20 #include        <stdlib.h>
21 #include        <string.h>
22
23 #include        <inttypes.h>            /* embeds <stdint.h> */
24 #include        <signal.h>              /* for sig_atomic_t */
25 #if     defined(INTMAX_MAX)             /* <inttypes.h> has C99 features */
26 #include        <wchar.h>
27 #endif
28
29 #include        <inttypes.h>            /* test idempotency */
30
31 #if     1 /* __STDC_VERSION__ >= 199901 */
32 #ifndef __Q8_QT
33 #define __Q8_QT long long
34 #endif
35 #endif
36
37 #ifdef  PRIdMAX
38 #define HAVE_PRIdMAX
39 #ifndef __Q8_MT
40 #define __Q8_MT intmax_t
41 #endif
42 #else
43 #ifdef  PRIdLEAST64
44 #ifndef __Q8_MT
45 #define __Q8_MT int_least64_t
46 #endif
47 #define PRIdMAX PRIdLEAST64
48 #else
49 #ifndef __Q8_MT
50 #define __Q8_MT long
51 #endif
52 #define PRIdMAX "ld"
53 #endif
54 #endif
55
56 #ifdef  PRIuMAX
57 #define HAVE_PRIuMAX
58 #define U__Q8_MT        uintmax_t
59 #else
60 #ifdef  PRIuLEAST64
61 #define U__Q8_MT        uint_least64_t
62 #define PRIuMAX PRIuLEAST64
63 #else
64 #define U__Q8_MT        unsigned long
65 #define PRIuMAX "lu"
66 #endif
67 #endif
68
69 #define STR_SUB(s)      #s
70 #define STRINGIZE(s)    STR_SUB(s)      /* extra level to expand argument */
71
72 #if     defined(SCNo32) || defined(PRIo32)
73 static int32_t          int32;
74 #endif
75 static int_least16_t    intl16;
76 static uint_least16_t   uintl16;
77 static uint_fast16_t    uintf16;
78 static intmax_t         intmax;
79 static uintmax_t        uintmax;
80
81 int
82 main()  {
83         int     status = 0;             /* exit status to be returned */
84
85         /* <stdint.h> features: */
86
87         printf("CHAR_BIT=%u\n", (unsigned)CHAR_BIT );
88         printf("sizeof(char)=%u\n", (unsigned)sizeof(char));    /* s.b. 1 */
89         printf("sizeof(short)=%u\n", (unsigned)sizeof(short));
90         printf("sizeof(int)=%u\n", (unsigned)sizeof(int));
91         printf("sizeof(long)=%u\n", (unsigned)sizeof(long));
92 #ifdef  __Q8_QT
93         printf("sizeof(long long)=%u\n", (unsigned)sizeof(__Q8_QT));
94 #endif
95         printf("sizeof(intmax_t)=%u\n", (unsigned)sizeof(intmax_t));
96         printf("sizeof(ptrdiff_t)=%u\n", (unsigned)sizeof(ptrdiff_t));
97         printf("sizeof(size_t)=%u\n", (unsigned)sizeof(size_t));
98         printf("sizeof(sig_atomic_t)=%u\n", (unsigned)sizeof(sig_atomic_t));
99         printf("sizeof(wchar_t)=%u\n", (unsigned)sizeof(wchar_t));
100 #if     defined(WINT_MAX) || __STDC_VERSION__ >= 199901
101         printf("sizeof(wint_t)=%u\n", (unsigned)sizeof(wint_t));
102 #else
103         printf("*** wint_t isn't defined ***\n");
104         status = EXIT_FAILURE;
105 #endif
106 #ifdef  INT8_MAX
107         printf("sizeof(int8_t)=%u\n", (unsigned)sizeof(int8_t));
108         printf("sizeof(uint8_t)=%u\n", (unsigned)sizeof(uint8_t));
109 #endif
110 #ifdef  INT9_MAX
111         printf("sizeof(int9_t)=%u\n", (unsigned)sizeof(int9_t));
112         printf("sizeof(uint9_t)=%u\n", (unsigned)sizeof(uint9_t));
113 #endif
114 #ifdef  INT12_MAX
115         printf("sizeof(int12_t)=%u\n", (unsigned)sizeof(int12_t));
116         printf("sizeof(uint12_t)=%u\n", (unsigned)sizeof(uint12_t));
117 #endif
118 #ifdef  INT16_MAX
119         printf("sizeof(int16_t)=%u\n", (unsigned)sizeof(int16_t));
120         printf("sizeof(uint16_t)=%u\n", (unsigned)sizeof(uint16_t));
121 #endif
122 #ifdef  INT18_MAX
123         printf("sizeof(int18_t)=%u\n", (unsigned)sizeof(int18_t));
124         printf("sizeof(uint18_t)=%u\n", (unsigned)sizeof(uint18_t));
125 #endif
126 #ifdef  INT24_MAX
127         printf("sizeof(int24_t)=%u\n", (unsigned)sizeof(int24_t));
128         printf("sizeof(uint24_t)=%u\n", (unsigned)sizeof(uint24_t));
129 #endif
130 #ifdef  INT32_MAX
131         printf("sizeof(int32_t)=%u\n", (unsigned)sizeof(int32_t));
132         printf("sizeof(uint32_t)=%u\n", (unsigned)sizeof(uint32_t));
133 #endif
134 #ifdef  INT36_MAX
135         printf("sizeof(int36_t)=%u\n", (unsigned)sizeof(int36_t));
136         printf("sizeof(uint36_t)=%u\n", (unsigned)sizeof(uint36_t));
137 #endif
138 #ifdef  INT40_MAX
139         printf("sizeof(int40_t)=%u\n", (unsigned)sizeof(int40_t));
140         printf("sizeof(uint40_t)=%u\n", (unsigned)sizeof(uint40_t));
141 #endif
142 #ifdef  INT48_MAX
143         printf("sizeof(int48_t)=%u\n", (unsigned)sizeof(int48_t));
144         printf("sizeof(uint48_t)=%u\n", (unsigned)sizeof(uint48_t));
145 #endif
146 #ifdef  INT60_MAX
147         printf("sizeof(int60_t)=%u\n", (unsigned)sizeof(int60_t));
148         printf("sizeof(uint60_t)=%u\n", (unsigned)sizeof(uint60_t));
149 #endif
150 #ifdef  INT64_MAX
151         printf("sizeof(int64_t)=%u\n", (unsigned)sizeof(int64_t));
152         printf("sizeof(uint64_t)=%u\n", (unsigned)sizeof(uint64_t));
153 #endif
154 #ifdef  INT72_MAX
155         printf("sizeof(int72_t)=%u\n", (unsigned)sizeof(int72_t));
156         printf("sizeof(uint72_t)=%u\n", (unsigned)sizeof(uint72_t));
157 #endif
158 #ifdef  INT128_MAX
159         printf("sizeof(int128_t)=%u\n", (unsigned)sizeof(int128_t));
160         printf("sizeof(uint128_t)=%u\n", (unsigned)sizeof(uint128_t));
161 #endif
162         printf("sizeof(int_least8_t)=%u\n", (unsigned)sizeof(int_least8_t));
163         printf("sizeof(uint_least8_t)=%u\n", (unsigned)sizeof(uint_least8_t));
164         printf("sizeof(int_least16_t)=%u\n", (unsigned)sizeof(int_least16_t));
165         printf("sizeof(uint_least16_t)=%u\n", (unsigned)sizeof(uint_least16_t));
166         printf("sizeof(int_least32_t)=%u\n", (unsigned)sizeof(int_least32_t));
167         printf("sizeof(uint_least32_t)=%u\n", (unsigned)sizeof(uint_least32_t));
168 #ifdef  INT_LEAST64_MAX
169         printf("sizeof(int_least64_t)=%u\n", (unsigned)sizeof(int_least64_t));
170         printf("sizeof(uint_least64_t)=%u\n", (unsigned)sizeof(uint_least64_t));
171 #else
172         printf("*** uint_least64_t isn't defined ***\n");
173         status = EXIT_FAILURE;
174 #endif
175 #ifdef  INT_LEAST128_MAX
176         printf("sizeof(int_least128_t)=%u\n", (unsigned)sizeof(int_least128_t));
177         printf("sizeof(uint_least128_t)=%u\n",
178                 (unsigned)sizeof(uint_least128_t));
179 #endif
180         printf("sizeof(int_fast8_t)=%u\n", (unsigned)sizeof(int_fast8_t));
181         printf("sizeof(uint_fast8_t)=%u\n", (unsigned)sizeof(uint_fast8_t));
182         printf("sizeof(int_fast16_t)=%u\n", (unsigned)sizeof(int_fast16_t));
183         printf("sizeof(uint_fast16_t)=%u\n", (unsigned)sizeof(uint_fast16_t));
184         printf("sizeof(int_fast32_t)=%u\n", (unsigned)sizeof(int_fast32_t));
185         printf("sizeof(uint_fast32_t)=%u\n", (unsigned)sizeof(uint_fast32_t));
186 #ifdef  INT_FAST64_MAX
187         printf("sizeof(int_fast64_t)=%u\n", (unsigned)sizeof(int_fast64_t));
188         printf("sizeof(uint_fast64_t)=%u\n", (unsigned)sizeof(uint_fast64_t));
189 #else
190         printf("*** int_fast64_t isn't defined ***\n");
191         status = EXIT_FAILURE;
192 #endif
193 #ifdef  INT_FAST128_MAX
194         printf("sizeof(int_fast128_t)=%u\n", (unsigned)sizeof(int_fast128_t));
195         printf("sizeof(uint_fast128_t)=%u\n", (unsigned)sizeof(uint_fast128_t));
196 #endif
197 #if     defined(INTPTR_MAX)
198         printf("sizeof(intptr_t)=%u\n", (unsigned)sizeof(intptr_t));
199 #if     defined(UINTPTR_MAX)
200         printf("sizeof(uintptr_t)=%u\n", (unsigned)sizeof(uintptr_t));
201 #else
202         printf("*** intptr_t is defined but uintptr_t isn't ***\n");
203         status = EXIT_FAILURE;
204 #endif
205 #elif   defined(UINTPTR_MAX)
206         printf("sizeof(uintptr_t)=%u\n", (unsigned)sizeof(uintptr_t));
207         printf("*** uintptr_t is defined but intptr_t isn't ***\n");
208         status = EXIT_FAILURE;
209 #else
210         printf("*** neither intptr_t nor uintptr_t is defined ***\n");
211         status = EXIT_FAILURE;
212 #endif
213 #ifdef  INTMAX_MAX
214         printf("sizeof(intmax_t)=%u\n", (unsigned)sizeof(intmax_t));
215         printf("sizeof(uintmax_t)=%u\n", (unsigned)sizeof(uintmax_t));
216 #else
217         printf("*** intmax_t isn't defined ***\n");
218         status = EXIT_FAILURE;
219 #endif
220
221 #ifdef  INT8_MAX
222         printf("INT8_MIN=%"PRIdMAX"\n", (__Q8_MT)INT8_MIN);
223         printf("INT8_MAX=%"PRIdMAX"\n", (__Q8_MT)INT8_MAX);
224         printf("UINT8_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINT8_MAX);
225 #endif
226 #ifdef  INT9_MAX
227         printf("INT9_MIN=%"PRIdMAX"\n", (__Q8_MT)INT9_MIN);
228         printf("INT9_MAX=%"PRIdMAX"\n", (__Q8_MT)INT9_MAX);
229         printf("UINT9_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINT9_MAX);
230 #endif
231 #ifdef  INT12_MAX
232         printf("INT12_MIN=%"PRIdMAX"\n", (__Q8_MT)INT12_MIN);
233         printf("INT12_MAX=%"PRIdMAX"\n", (__Q8_MT)INT12_MAX);
234         printf("UINT12_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINT12_MAX);
235 #endif
236 #ifdef  INT16_MAX
237         printf("INT16_MIN=%"PRIdMAX"\n", (__Q8_MT)INT16_MIN);
238         printf("INT16_MAX=%"PRIdMAX"\n", (__Q8_MT)INT16_MAX);
239         printf("UINT16_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINT16_MAX);
240 #endif
241 #ifdef  INT18_MAX
242         printf("INT18_MIN=%"PRIdMAX"\n", (__Q8_MT)INT18_MIN);
243         printf("INT18_MAX=%"PRIdMAX"\n", (__Q8_MT)INT18_MAX);
244         printf("UINT18_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINT18_MAX);
245 #endif
246 #ifdef  INT24_MAX
247         printf("INT24_MIN=%"PRIdMAX"\n", (__Q8_MT)INT24_MIN);
248         printf("INT24_MAX=%"PRIdMAX"\n", (__Q8_MT)INT24_MAX);
249         printf("UINT24_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINT24_MAX);
250 #endif
251 #ifdef  INT32_MAX
252         printf("INT32_MIN=%"PRIdMAX"\n", (__Q8_MT)INT32_MIN);
253         printf("INT32_MAX=%"PRIdMAX"\n", (__Q8_MT)INT32_MAX);
254         printf("UINT32_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINT32_MAX);
255 #endif
256 #ifdef  INT36_MAX
257         printf("INT36_MIN=%"PRIdMAX"\n", (__Q8_MT)INT36_MIN);
258         printf("INT36_MAX=%"PRIdMAX"\n", (__Q8_MT)INT36_MAX);
259         printf("UINT36_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINT36_MAX);
260 #endif
261 #ifdef  INT40_MAX
262         printf("INT40_MIN=%"PRIdMAX"\n", (__Q8_MT)INT40_MIN);
263         printf("INT40_MAX=%"PRIdMAX"\n", (__Q8_MT)INT40_MAX);
264         printf("UINT40_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINT40_MAX);
265 #endif
266 #ifdef  INT48_MAX
267         printf("INT48_MIN=%"PRIdMAX"\n", (__Q8_MT)INT48_MIN);
268         printf("INT48_MAX=%"PRIdMAX"\n", (__Q8_MT)INT48_MAX);
269         printf("UINT48_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINT48_MAX);
270 #endif
271 #ifdef  INT60_MAX
272         printf("INT60_MIN=%"PRIdMAX"\n", (__Q8_MT)INT60_MIN);
273         printf("INT60_MAX=%"PRIdMAX"\n", (__Q8_MT)INT60_MAX);
274         printf("UINT60_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINT60_MAX);
275 #endif
276 #ifdef  INT64_MAX
277         printf("INT64_MIN=%"PRIdMAX"\n", (__Q8_MT)INT64_MIN);
278         printf("INT64_MAX=%"PRIdMAX"\n", (__Q8_MT)INT64_MAX);
279         printf("UINT64_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINT64_MAX);
280 #endif
281 #ifdef  INT72_MAX
282         printf("INT72_MIN=%"PRIdMAX"\n", (__Q8_MT)INT72_MIN);
283         printf("INT72_MAX=%"PRIdMAX"\n", (__Q8_MT)INT72_MAX);
284         printf("UINT72_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINT72_MAX);
285 #endif
286 #ifdef  INT128_MAX
287         printf("INT128_MIN=%"PRIdMAX"\n", (__Q8_MT)INT128_MIN);
288         printf("INT128_MAX=%"PRIdMAX"\n", (__Q8_MT)INT128_MAX);
289         printf("UINT128_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINT128_MAX);
290 #endif
291         printf("INT_LEAST8_MIN=%"PRIdMAX"\n", (__Q8_MT)INT_LEAST8_MIN);
292         printf("INT_LEAST8_MAX=%"PRIdMAX"\n", (__Q8_MT)INT_LEAST8_MAX);
293         printf("UINT_LEAST8_MAX=%"PRIuMAX"\n",
294                 (U__Q8_MT)UINT_LEAST8_MAX);
295         printf("INT_LEAST16_MIN=%"PRIdMAX"\n", (__Q8_MT)INT_LEAST16_MIN);
296         printf("INT_LEAST16_MAX=%"PRIdMAX"\n", (__Q8_MT)INT_LEAST16_MAX);
297         printf("UINT_LEAST16_MAX=%"PRIuMAX"\n",
298                 (U__Q8_MT)UINT_LEAST16_MAX);
299         printf("INT_LEAST32_MIN=%"PRIdMAX"\n", (__Q8_MT)INT_LEAST32_MIN);
300         printf("INT_LEAST32_MAX=%"PRIdMAX"\n", (__Q8_MT)INT_LEAST32_MAX);
301         printf("UINT_LEAST32_MAX=%"PRIuMAX"\n",
302                 (U__Q8_MT)UINT_LEAST32_MAX);
303 #ifdef  INT_LEAST64_MAX
304         printf("INT_LEAST64_MIN=%"PRIdMAX"\n", (__Q8_MT)INT_LEAST64_MIN);
305         printf("INT_LEAST64_MAX=%"PRIdMAX"\n", (__Q8_MT)INT_LEAST64_MAX);
306         printf("UINT_LEAST64_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINT_LEAST64_MAX);
307 #endif
308 #ifdef  INT_LEAST128_MAX
309         printf("INT_LEAST128_MIN=%"PRIdMAX"\n", (__Q8_MT)INT_LEAST128_MIN);
310         printf("INT_LEAST128_MAX=%"PRIdMAX"\n", (__Q8_MT)INT_LEAST128_MAX);
311         printf("UINT_LEAST128_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINT_LEAST128_MAX);
312 #endif
313         printf("INT_FAST8_MIN=%"PRIdMAX"\n", (__Q8_MT)INT_FAST8_MIN);
314         printf("INT_FAST8_MAX=%"PRIdMAX"\n", (__Q8_MT)INT_FAST8_MAX);
315         printf("UINT_FAST8_MAX=%"PRIuMAX"\n",
316                 (U__Q8_MT)UINT_FAST8_MAX);
317         printf("INT_FAST16_MIN=%"PRIdMAX"\n", (__Q8_MT)INT_FAST16_MIN);
318         printf("INT_FAST16_MAX=%"PRIdMAX"\n", (__Q8_MT)INT_FAST16_MAX);
319         printf("UINT_FAST16_MAX=%"PRIuMAX"\n",
320                 (U__Q8_MT)UINT_FAST16_MAX);
321         printf("INT_FAST32_MIN=%"PRIdMAX"\n", (__Q8_MT)INT_FAST32_MIN);
322         printf("INT_FAST32_MAX=%"PRIdMAX"\n", (__Q8_MT)INT_FAST32_MAX);
323         printf("UINT_FAST32_MAX=%"PRIuMAX"\n",
324                 (U__Q8_MT)UINT_FAST32_MAX);
325 #ifdef  INT_FAST64_MAX
326         printf("INT_FAST64_MIN=%"PRIdMAX"\n", (__Q8_MT)INT_FAST64_MIN);
327         printf("INT_FAST64_MAX=%"PRIdMAX"\n", (__Q8_MT)INT_FAST64_MAX);
328         printf("UINT_FAST64_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINT_FAST64_MAX);
329 #endif
330 #ifdef  INT_FAST128_MAX
331         printf("INT_FAST128_MIN=%"PRIdMAX"\n", (__Q8_MT)INT_FAST128_MIN);
332         printf("INT_FAST128_MAX=%"PRIdMAX"\n", (__Q8_MT)INT_FAST128_MAX);
333         printf("UINT_FAST128_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINT_FAST128_MAX);
334 #endif
335 #ifdef  INTPTR_MAX
336         printf("INTPTR_MIN=%"PRIdMAX"\n", (__Q8_MT)INTPTR_MIN);
337         printf("INTPTR_MAX=%"PRIdMAX"\n", (__Q8_MT)INTPTR_MAX);
338 #endif
339 #ifdef  UINTPTR_MAX
340         printf("UINTPTR_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINTPTR_MAX);
341 #endif
342 #ifdef  INTMAX_MAX
343         printf("INTMAX_MIN=%"PRIdMAX"\n", (__Q8_MT)INTMAX_MIN);
344         printf("INTMAX_MAX=%"PRIdMAX"\n", (__Q8_MT)INTMAX_MAX);
345         printf("UINTMAX_MAX=%"PRIuMAX"\n", (U__Q8_MT)UINTMAX_MAX);
346 #endif
347 #ifdef  PTRDIFF_MAX
348         printf("PTRDIFF_MIN=%"PRIdMAX"\n", (__Q8_MT)PTRDIFF_MIN);
349         printf("PTRDIFF_MAX=%"PRIdMAX"\n", (__Q8_MT)PTRDIFF_MAX);
350 #endif
351 #ifdef  SIG_ATOMIC_MAX
352 #if     SIG_ATOMIC_MIN < 0
353         printf("SIG_ATOMIC_MIN=%"PRIdMAX"\n", (__Q8_MT)SIG_ATOMIC_MIN);
354         printf("SIG_ATOMIC_MAX=%"PRIdMAX"\n", (__Q8_MT)SIG_ATOMIC_MAX);
355 #else
356         printf("SIG_ATOMIC_MIN=%"PRIuMAX"\n", (U__Q8_MT)SIG_ATOMIC_MIN);
357         printf("SIG_ATOMIC_MAX=%"PRIuMAX"\n", (U__Q8_MT)SIG_ATOMIC_MAX);
358 #endif
359 #endif
360 #ifdef  SIZE_MAX
361         printf("SIZE_MAX=%"PRIuMAX"\n", (U__Q8_MT)SIZE_MAX);
362 #endif
363
364 #ifdef  WCHAR_MAX
365 #if     WCHAR_MIN < 0
366         printf("WCHAR_MIN=%"PRIdMAX"\n", (__Q8_MT)WCHAR_MIN);
367         printf("WCHAR_MAX=%"PRIdMAX"\n", (__Q8_MT)WCHAR_MAX);
368 #else
369         printf("WCHAR_MIN=%"PRIuMAX"\n", (U__Q8_MT)WCHAR_MIN);
370         printf("WCHAR_MAX=%"PRIuMAX"\n", (U__Q8_MT)WCHAR_MAX);
371 #endif
372 #endif
373 #ifdef  WINT_MAX
374 #if     WINT_MIN < 0
375         printf("WINT_MIN=%"PRIdMAX"\n", (__Q8_MT)WINT_MIN);
376         printf("WINT_MAX=%"PRIdMAX"\n", (__Q8_MT)WINT_MAX);
377 #else
378         printf("WINT_MIN=%"PRIuMAX"\n", (U__Q8_MT)WINT_MIN);
379         printf("WINT_MAX=%"PRIuMAX"\n", (U__Q8_MT)WINT_MAX);
380 #endif
381 #endif
382
383         /*
384                 7.18.4  Macros for integer constants
385         */
386
387         /* INTn_C for n=8 and 16 were at one point unimplementable
388            on most platforms, so they're treated as "optional": */
389 #ifdef  INT8_C
390         if ( INT8_C(-123) != -123 )
391                 printf("*** INT8_C(-123) produced %"PRIdMAX" ***\n",
392                        (__Q8_MT)INT8_C(-123)
393                       );
394         if ( UINT8_C(123) != 123 )
395                 printf("*** UINT8_C(123) produced %"PRIuMAX" ***\n",
396                        (U__Q8_MT)UINT8_C(123)
397                       );
398 #endif
399 #ifdef  INT16_C
400         if ( INT16_C(-12345) != -12345 )
401                 printf("*** INT16_C(-12345) produced %"PRIdMAX" ***\n",
402                        (__Q8_MT)INT16_C(-12345)
403                       );
404         if ( UINT16_C(12345) != 12345 )
405                 printf("*** UINT16_C(12345) produced %"PRIuMAX" ***\n",
406                        (U__Q8_MT)UINT16_C(12345)
407                       );
408 #endif
409         if ( INT32_C(-123456789) != -123456789 )
410                 printf("*** INT32_C(-123456789) produced %"PRIdMAX" ***\n",
411                        (__Q8_MT)INT32_C(-123456789)
412                       );
413         if ( UINT32_C(123456789) != 123456789 )
414                 printf("*** UINT32_C(123456789) produced %"PRIuMAX" ***\n",
415                        (U__Q8_MT)UINT32_C(123456789)
416                       );
417 #ifdef  INT_LEAST64_MAX
418         if ( INT64_C(-1234567890123456789) != -1234567890123456789 )
419                 printf("*** INT64_C(-1234567890123456789) produced %"PRIdMAX
420                        " ***\n",
421                        (__Q8_MT)INT64_C(-1234567890123456789)
422                       );
423         if ( UINT64_C(1234567890123456789) != 1234567890123456789 )
424                 printf("*** UINT64_C(1234567890123456789) produced %"PRIuMAX
425                        " ***\n",
426                        (U__Q8_MT)UINT64_C(1234567890123456789)
427                       );
428 #endif
429 #ifdef  INTMAX_MAX
430         if ( INTMAX_C(-1234567890123456789) != -1234567890123456789 )
431                 printf("*** INTMAX_C(-1234567890123456789) produced %"PRIdMAX
432                        " ***\n",
433                        (__Q8_MT)INTMAX_C(-1234567890123456789)
434                       );
435         if ( UINTMAX_C(1234567890123456789) != 1234567890123456789 )
436                 printf("*** UINTMAX_C(1234567890123456789) produced %"PRIuMAX
437                        " ***\n",
438                        (U__Q8_MT)UINTMAX_C(1234567890123456789)
439                       );
440 #endif
441
442         /* <inttypes.h> features: */
443
444 #if     __STDC_VERSION__ >= 199901
445         printf("sizeof(imaxdiv_t)=%u\n", (unsigned)sizeof(imaxdiv_t));
446 #endif
447
448         /*
449                 7.8.1   Macros for format specifiers
450         */
451
452         {
453         /* scanf these strings */
454         static const char       in_dn[] = "Z119bZ";
455         static const char       in_dmo[] = "Z-0119bZ";
456         static const char       in_dspx[] = "Z \t\n +0X119bZ";
457         static const char       in_dsmx[] = "Z \t\n -0x119bZ";
458         static const char       in_dsn[] = "Z \t\n 119bZ";
459         static const char       in_dp[] = "Z+119bZ";
460         static const char       in_dpx[] = "Z+0X119bz";
461
462         /* sprintf into this */
463         static char             buffer[1024];
464
465 #define SCAN(buf,fs,var,exp)    if ( sscanf(buf, "Z%" fs, &var) != 1 ) \
466                                         { \
467                                         printf("*** " #fs "=" STR_SUB(fs) \
468                                                " failed ***\n" \
469                                               ); \
470                                         status = EXIT_FAILURE; \
471                                         } \
472                                 else if ( var != (exp) ) \
473                                         { \
474                                         printf("*** " #fs "="  STR_SUB(fs) \
475                                                " should be: " STR_SUB(exp) \
476                                                ", was: %" fs " ***\n", var \
477                                               ); \
478                                         status = EXIT_FAILURE; \
479                                         } \
480                                 else
481
482 #define PRINT(fs,var,exp)       if ( sprintf(buffer, "%" fs, var ) <= 0 ) \
483                                         { \
484                                         printf("*** " #fs "=" STR_SUB(fs) \
485                                                " failed ***\n" \
486                                               ); \
487                                         status = EXIT_FAILURE; \
488                                         } \
489                                 else if ( strcmp(buffer, STR_SUB(exp)) != 0 ) \
490                                         { \
491                                         printf("*** " #fs "="  STR_SUB(fs) \
492                                                " should be: " STR_SUB(exp) \
493                                                ", was: %s ***\n", buffer \
494                                               ); \
495                                         status = EXIT_FAILURE; \
496                                         } \
497                                 else
498
499 #ifdef  SCNo32
500         SCAN(in_dn, SCNo32, int32, 9);
501 #endif
502 #ifdef  PRIo32
503         PRINT(PRIo32, int32, 11);
504 #endif
505         SCAN(in_dmo, SCNiLEAST16, intl16, -9);
506         SCAN(in_dspx, SCNdLEAST16, intl16, 0);
507         SCAN(in_dsmx, SCNiLEAST16, intl16, -4507);
508         PRINT(PRIdLEAST16, intl16, -4507);
509         PRINT(PRIiLEAST16, intl16, -4507);
510         SCAN(in_dsn, SCNxLEAST16, uintl16, 4507);
511         PRINT(PRIoLEAST16, uintl16, 10633);
512         PRINT(PRIuLEAST16, uintl16, 4507);
513         PRINT(PRIxLEAST16, uintl16, 119b);
514         PRINT(PRIXLEAST16, uintl16, 119B);
515         SCAN(in_dp, SCNxFAST16, uintf16, 4507);
516         PRINT(PRIxFAST16, uintf16, 119b);
517 #ifdef  SCNdMAX
518         SCAN(in_dp, SCNdMAX, intmax, 119);
519 #endif
520 #ifdef  PRIiMAX
521         PRINT(PRIiMAX, intmax, 119);
522 #endif
523 #ifdef  SCNoMAX
524         SCAN(in_dpx, SCNoMAX, uintmax, 0);
525 #endif
526 #ifdef  PRIxMAX
527         PRINT(PRIxMAX, uintmax, 0);
528 #endif
529         /* Obviously there should be a much larger battery of such tests. */
530         }
531
532 #if     defined(INTMAX_MAX)             /* <inttypes.h> has C99 features */
533         /*
534                 7.8.2   Functions for greatest-width integer types
535         */
536
537         {
538         static struct
539                 {
540                 intmax_t        input;
541                 intmax_t        expect;
542                 }       abs_data[] =
543         {
544 #ifdef  INT8_MAX
545                 {INT8_MAX,              INT8_MAX},
546                 {-INT8_MAX,             INT8_MAX},
547                 {UINT8_MAX,             UINT8_MAX},
548 #endif
549
550 #ifdef  INT16_MAX
551         {       INT16_MAX,              INT16_MAX},
552         {       -INT16_MAX,             INT16_MAX},
553         {       UINT16_MAX,             UINT16_MAX},
554 #endif
555 #ifdef  INT32_MAX
556         {       INT32_MAX,              INT32_MAX},
557         {       -INT32_MAX,             INT32_MAX},
558 #ifdef  INT_LEAST64_MAX
559         {       UINT32_MAX,             UINT32_MAX},
560 #endif
561 #endif
562 #ifdef  INT64_MAX
563         {       INT64_MAX,              INT64_MAX},
564         {       -INT64_MAX,             INT64_MAX},
565 #endif
566         {       INT_LEAST8_MAX,         INT_LEAST8_MAX},
567         {       -INT_LEAST8_MAX,        INT_LEAST8_MAX},
568         {       UINT_LEAST8_MAX,        UINT_LEAST8_MAX},
569         {       INT_LEAST16_MAX,        INT_LEAST16_MAX},
570         {       -INT_LEAST16_MAX,       INT_LEAST16_MAX},
571         {       UINT_LEAST16_MAX,       UINT_LEAST16_MAX},
572         {       INT_LEAST32_MAX,        INT_LEAST32_MAX},
573         {       -INT_LEAST32_MAX,       INT_LEAST32_MAX},
574 #ifdef  INT_LEAST64_MAX
575         {       UINT_LEAST32_MAX,       UINT_LEAST32_MAX},
576         {       INT_LEAST64_MAX,        INT_LEAST64_MAX},
577         {       -INT_LEAST64_MAX,       INT_LEAST64_MAX},
578 #endif
579         {       INT_FAST8_MAX,          INT_FAST8_MAX},
580         {       -INT_FAST8_MAX, INT_FAST8_MAX},
581         {       UINT_FAST8_MAX, UINT_FAST8_MAX},
582         {       INT_FAST16_MAX, INT_FAST16_MAX},
583         {       -INT_FAST16_MAX,        INT_FAST16_MAX},
584         {       UINT_FAST16_MAX,        UINT_FAST16_MAX},
585         {       INT_FAST32_MAX, INT_FAST32_MAX},
586         {       -INT_FAST32_MAX,        INT_FAST32_MAX},
587 #ifdef  INT_FAST64_MAX
588         {       UINT_FAST32_MAX,        UINT_FAST32_MAX},
589         {       INT_FAST64_MAX,     INT_FAST64_MAX},
590         {       -INT_FAST64_MAX,        INT_FAST64_MAX},
591 #endif
592 #ifdef  INTPTR_MAX
593         {       INTPTR_MAX,                 INTPTR_MAX},
594         {       -INTPTR_MAX,            INTPTR_MAX},
595 #endif
596 #ifdef  UINTPTR_MAX
597         {       UINTPTR_MAX,            UINTPTR_MAX},
598 #endif
599         {       INTMAX_MAX,                 INTMAX_MAX},
600 #ifdef  PTRDIFF_MAX
601         {       PTRDIFF_MAX,            PTRDIFF_MAX},
602 #endif
603 #ifdef  SIG_ATOMIC_MAX
604         {       SIG_ATOMIC_MAX,         SIG_ATOMIC_MAX},
605 #if     SIG_ATOMIC_MIN < 0
606         {       -SIG_ATOMIC_MAX,        SIG_ATOMIC_MAX},
607 #endif
608 #endif
609 #ifdef  SIZE_MAX
610         {       SIZE_MAX,               SIZE_MAX},
611 #endif
612 #ifdef  WCHAR_MAX
613         {       WCHAR_MAX,              WCHAR_MAX},
614 #if     WCHAR_MIN < 0
615         {       -WCHAR_MAX,             WCHAR_MAX},
616 #endif
617 #endif
618 #ifdef  WINT_MAX
619         {       WINT_MAX,               WINT_MAX},
620 #if     WINT_MIN < 0
621         {       -WINT_MAX,              WINT_MAX},
622 #endif
623 #endif
624         {       127,                            127},
625         {       -127,                           127},
626         {       128,                            128},
627         {       -127-1,                         128},
628         {       255,                            255},
629         {       -256+1,                         255},
630         {       256,                            256},
631         {       -256,                           256},
632         {       32767,                          32767},
633         {       -32767,                         32767},
634         {       32768,                          32768},
635         {       -32767-1,                       32768},
636         {       65535,                          65535},
637         {       -65536+1,                       65535},
638         {       65536,                          65536},
639         {       -65536,                         65536},
640         {       2147483647,                     2147483647},
641         {       -2147483647,                    2147483647},
642         {       2147483648LL,                   2147483648LL},
643         {       -2147483647LL-1,                        2147483648LL},
644 #ifdef  INT_LEAST64_MAX
645         {       4294967295LL,                   4294967295LL},
646         {       -4294967296LL+1,                        4294967295LL},
647         {       4294967296LL,                   4294967296LL},
648         {       -4294967296LL,                  4294967296LL},
649         {       9223372036854775807LL,          9223372036854775807LL},
650         {       -9223372036854775807LL,         9223372036854775807LL},
651         {       1234567890123456789LL,          1234567890123456789LL},
652         {       -1234567890123456789LL,         1234567890123456789LL},
653 #endif
654         {       1,                              1},
655         {       -1,                             1},
656         {       2,                              2},
657         {       -2,                             2},
658         {       10,                             10},
659         {       -10,                    10},
660         {       16,                             16},
661         {       -16,                            16},
662                 /* Other test cases can be added here. */
663                 {0,             0}      /* terminates the list */
664                 },      *adp=abs_data;
665     int count=0;
666         do{
667     intmax = imaxabs(adp->input);
668     count++;
669     if (intmax  != adp->expect )
670                         {
671
672                         printf("*** imaxabs(%"PRIdMAX") failed; should be: %"
673                                PRIdMAX", was: %"PRIdMAX" ***\n",
674                                adp->input, adp->expect, intmax
675                               );
676                         status = EXIT_FAILURE;
677                         }
678         }
679     while ( adp++->input != 0 );
680         }
681
682         {
683         imaxdiv_t       result;
684         static struct
685                 {
686                 intmax_t        numer;
687                 intmax_t        denom;
688                 intmax_t        exp_quot;
689                 intmax_t        exp_rem;
690                 }       div_data[] =
691                 {
692         {       0, 1,                           0, 0},
693         {       0, -1,                          0, 0},
694         {       0, 2,                           0, 0},
695         {       0, -2,                          0, 0},
696         {       0, 5,                           0, 0},
697         {       0, -5,                          0, 0},
698         {       1, 1,                           1, 0},
699         {       1, -1,                          -1, 0},
700         {       1, 2,                           0, 1},
701         {       1, -2,                          0, 1},
702         {       1, 5,                           0, 1},
703         {       1, -5,                          0, 1},
704         {       -1, 1,                          -1, 0},
705         {       -1, -1,                         1, 0},
706         {       -1, 2,                          0, -1},
707         {       -1, -2,                         0, -1},
708         {       -1, 5,                          0, -1},
709         {       -1, -5,                         0, -1},
710         {       2, 1,                           2, 0},
711         {       2, -1,                          -2, 0},
712         {       2, 2,                           1, 0},
713         {       2, -2,                          -1, 0},
714         {       2, 5,                           0, 2},
715         {       2, -5,                          0, 2},
716         {       -2, 1,                          -2, 0},
717         {       -2, -1,                         2, 0},
718         {       -2, 2,                          -1, 0},
719         {       -2, -2,                         1, 0},
720         {       -2, 5,                          0, -2},
721         {       -2, -5,                         0, -2},
722         {       17, 5,                          3, 2},
723         {       -17, -5,                        3, -2},
724         {       17, -5,                         -3, 2},
725         {       -17, 5,                         -3, -2},
726         {       2147483647, 1,                  2147483647, 0},
727         {       -2147483647LL, 1,                       -2147483647LL, 0},
728         {       2147483648LL, 1LL,                      2147483648LL, 0LL},
729         {       -2147483647-1LL, 1LL,           -2147483647-1LL, 0LL},
730         {       2147483647LL, 2LL,                      1073741823LL, 1LL},
731         {       -2147483647LL, 2LL,                     -1073741823LL, -1LL},
732         {       2147483648LL, 2LL,                      1073741824LL, 0LL},
733         {       -2147483647-1LL, 2LL,           -1073741824LL, 0LL},
734 #ifdef  INT_LEAST64_MAX                 /* else might support only 32 bits */
735         {       4294967295LL, 1LL,                      4294967295LL, 0LL},
736         {       -4294967296LL+1LL, 1LL,         -4294967296LL+1, 0},
737         {       4294967296LL, 1,                        4294967296LL, 0},
738         {       -4294967296LL, 1LL,                     -4294967296LL, 0LL},
739         {       4294967295LL, -1LL,                     -4294967296+1LL, 0LL},
740         {       -4294967296+1LL, -1LL,          4294967295LL, 0LL},
741         {       4294967296LL, -1LL,                     -4294967296LL, 0LL},
742         {       -4294967296LL, -1LL,            4294967296LL, 0LL},
743         {       4294967295LL, 2LL,                      2147483647LL, 1LL},
744         {       -4294967296+1LL, 2LL,           -2147483647LL, -1LL},
745         {       4294967296LL, 2LL,                      2147483648LL, 0LL},
746         {       -4294967296LL, 2LL,                     -2147483647-1LL, 0LL},
747         {       4294967295LL, 2147483647LL,             2LL, 1LL},
748         {       -4294967296+1LL, 2147483647LL,  -2LL, -1LL},
749         {       4294967296LL, 2147483647LL,             2LL, 2LL},
750         {       -4294967296LL, 2147483647LL,    -2LL, -2LL},
751         {       4294967295LL, -2147483647LL,    -2LL, 1LL},
752         {       -4294967296+1LL, -2147483647LL, 2LL, -1LL},
753         {       4294967296LL, -2147483647LL,    -2LL, 2LL},
754         {       -4294967296LL, -2147483647LL,   2LL, -2LL},
755         {       4294967295LL, 2147483648LL,             1LL, 2147483647LL},
756         {       -4294967296+1LL, 2147483648LL,  -1LL, -2147483647LL},
757         {       4294967296LL, 2147483648LL,             2LL, 0LL},
758         {       -4294967296LL, 2147483648LL,    -2LL, 0LL},
759         {       4294967295LL, -2147483647-1LL,  -1LL, 2147483647LL},
760         {       -4294967296+1LL, -2147483647-1LL,       1LL, -2147483647LL},
761         {       4294967296LL, -2147483647-1LL,  -2LL, 0LL},
762         {       -4294967296LL, -2147483647-1LL, 2LL, 0LL},
763         {       9223372036854775807LL, 1LL,             9223372036854775807LL, 0LL},
764         {       -9223372036854775807LL, 1LL,    -9223372036854775807LL, 0LL},
765         {       9223372036854775807LL, 2LL,             4611686018427387903LL, 1LL},
766         {       -9223372036854775807LL, 2LL,    -4611686018427387903LL, -1LL},
767 #endif
768                 /* There should be a much larger battery of such tests. */
769     {     0, 0,         0, 0}   /* 0 denom terminates the list */
770                 },      *ddp;
771
772         for ( ddp = div_data; ddp->denom != 0; ++ddp )
773                 if ( (result = imaxdiv(ddp->numer, ddp->denom)).quot
774                      != ddp->exp_quot || result.rem != ddp->exp_rem
775                    )    {
776                         printf("*** imaxdiv(%"PRIdMAX",%"PRIdMAX
777                                ") failed; should be: (%"PRIdMAX",%"PRIdMAX
778                                "), was: (%"PRIdMAX",%"PRIdMAX") ***\n",
779                                ddp->numer, ddp->denom, ddp->exp_quot,
780                                ddp->exp_rem, result.quot, result.rem
781                               );
782                         status = EXIT_FAILURE;
783                         }
784         }
785
786         {
787         char            *endptr;
788         wchar_t         *wendptr;
789         static char     saved[64];      /* holds copy of input string */
790         static wchar_t  wnptr[64];      /* holds wide copy of test string */
791         static int      warned;         /* "warned for null endptr" flag */
792         register int    i;
793         static struct
794                 {
795                 char *          nptr;
796                 int             base;
797                 intmax_t        exp_val;
798                 int             exp_len;
799                 }       str_data[] =
800                 {
801         {       "", 0,                          0, 0},
802         {       "", 2,                          0, 0},
803         {       "", 8,                          0, 0},
804         {       "", 9,                          0, 0},
805         {       "", 10,                         0, 0},
806         {       "", 16,                         0, 0},
807         {       "", 36,                         0, 0},
808         {       "0", 0,                         0, 1},
809         {       "0", 2,                         0, 1},
810         {       "0", 8,                         0, 1},
811         {       "0", 9,                         0, 1},
812         {       "0", 10,                        0, 1},
813         {       "0", 16,                        0, 1},
814         {       "0", 36,                        0, 1},
815         {       "+0", 0,                        0, 2},
816         {       "+0", 2,                        0, 2},
817         {       "+0", 8,                        0, 2},
818         {       "+0", 9,                        0, 2},
819         {       "+0", 10,                       0, 2},
820         {       "+0", 16,                       0, 2},
821         {       "+0", 36,                       0, 2},
822         {       "-0", 0,                        0, 2},
823         {       "-0", 2,                        0, 2},
824         {       "-0", 8,                        0, 2},
825         {       "-0", 9,                        0, 2},
826         {       "-0", 10,                       0, 2},
827         {       "-0", 16,                       0, 2},
828         {       "-0", 36,                       0, 2},
829         {       "Inf", 0,                       0, 0},
830         {       "Inf", 2,                       0, 0},
831         {       "Inf", 8,                       0, 0},
832         {       "Inf", 9,                       0, 0},
833         {       "Inf", 10,                      0, 0},
834         {       "Inf", 16,                      0, 0},
835         {       "Inf", 36,                      24171, 3},
836         {       "+Inf", 0,                      0, 0},
837         {       "+Inf", 2,                      0, 0},
838         {       "+Inf", 8,                      0, 0},
839         {       "+Inf", 9,                      0, 0},
840         {       "+Inf", 10,                     0, 0},
841         {       "+Inf", 16,                     0, 0},
842         {       "+Inf", 36,                     24171, 4},
843         {       "-Inf", 0,                      0, 0},
844         {       "-Inf", 2,                      0, 0},
845         {       "-Inf", 8,                      0, 0},
846         {       "-Inf", 9,                      0, 0},
847         {       "-Inf", 10,                     0, 0},
848         {       "-Inf", 16,                     0, 0},
849         {       "-Inf", 36,                     -24171, 4},
850         {       "inf", 0,                       0, 0},
851         {       "inf", 2,                       0, 0},
852         {       "inf", 8,                       0, 0},
853         {       "inf", 9,                       0, 0},
854         {       "inf", 10,                      0, 0},
855         {       "inf", 16,                      0, 0},
856         {       "inf", 36,                      24171, 3},
857         {       "+inf", 0,                      0, 0},
858         {       "+inf", 2,                      0, 0},
859         {       "+inf", 8,                      0, 0},
860         {       "+inf", 9,                      0, 0},
861         {       "+inf", 10,                     0, 0},
862         {       "+inf", 16,                     0, 0},
863         {       "+inf", 36,                     24171, 4},
864         {       "-inf", 0,                      0, 0},
865         {       "-inf", 2,                      0, 0},
866         {       "-inf", 8,                      0, 0},
867         {       "-inf", 9,                      0, 0},
868         {       "-inf", 10,                     0, 0},
869         {       "-inf", 16,                     0, 0},
870         {       "-inf", 36,                     -24171, 4},
871         {       "119b8Z", 0,                    119, 3},
872         {       "119bZ", 0,                     119, 3},
873         {       "-0119bZ", 0,                   -9, 4},
874         {       " \t\n 0X119bZ", 0,             4507, 10},
875         {       " \t\n +0X119bZ", 0,            4507, 11},
876         {       " \t\n -0x119bZ", 0,            -4507, 11},
877         {       " \t\n 119bZ", 0,               119, 7},
878         {       "+119bZ", 0,                    119, 4},
879         {       "+0X119bz", 0,                  4507, 7},
880         {       "119b8Z", 2,                    3, 2},
881         {       "119bZ", 2,                     3, 2},
882         {       "-0119bZ", 2,                   -3, 4},
883         {       " \t\n 0X119bZ", 2,             0, 5},
884         {       " \t\n +0X119bZ", 2,            0, 6},
885         {       " \t\n -0x119bZ", 2,            0, 6},
886         {       " \t\n 119bZ", 2,               3, 6},
887         {       "+119bZ", 2,                    3, 3},
888         {       "+0X119bz", 2,                  0, 2},
889         {       "119b8Z", 8,                    9, 2},
890         {       "119bZ", 8,                     9, 2},
891         {       "-0119bZ", 8,                   -9, 4},
892         {       " \t\n 0X119bZ", 8,             0, 5},
893         {       " \t\n +0X119bZ", 8,            0, 6},
894         {       " \t\n -0x119bZ", 8,            0, 6},
895         {       " \t\n 119bZ", 8,               9, 6},
896         {       "+119bZ", 8,                    9, 3},
897         {       "+0X119bz", 8,                  0, 2},
898         {       "119b8Z", 9,                    10, 2},
899         {       "119bZ", 9,                     10, 2},
900         {       "-0119bZ", 9,                   -10, 4},
901         {       " \t\n 0X119bZ", 9,             0, 5},
902         {       " \t\n +0X119bZ", 9,            0, 6},
903         {       " \t\n -0x119bZ", 9,            0, 6},
904         {       " \t\n 119bZ", 9,               10, 6},
905         {       "+119bZ", 9,                    10, 3},
906         {       "+0X119bz", 9,                  0, 2},
907         {       "119b8Z", 10,                   119, 3},
908         {       "119bZ", 10,                    119, 3},
909         {       "-0119bZ", 10,                  -119, 5},
910         {       " \t\n 0X119bZ", 10,            0, 5},
911         {       " \t\n +0X119bZ", 10,           0, 6},
912         {       " \t\n -0x119bZ", 10,           0, 6},
913         {       " \t\n 119bZ", 10,              119, 7},
914         {       "+119bZ", 10,                   119, 4},
915         {       "+0X119bz", 10,                 0, 2},
916         {       "119b8Z", 16,                   72120, 5},
917         {       "119bZ", 16,                    4507, 4},
918         {       "-0119bZ", 16,                  -4507, 6},
919         {       " \t\n 0X119bZ", 16,            4507, 10},
920         {       " \t\n +0X119bZ", 16,           4507, 11},
921         {       " \t\n -0x119bZ", 16,           -4507, 11},
922         {       " \t\n 119bZ", 16,              4507,8},
923         {       "+119bZ", 16,                   4507, 5},
924         {       "+0X119bz", 16,                 4507, 7},
925         {       "119b8Z", 36,                   62580275, 6},
926         {       "119bZ", 36,                    1738367, 5},
927         {       "-0119bZ", 36,                  -1738367, 7},
928         {       " \t\n 0X119bZ", 36,            1997122175, 11},
929         {       " \t\n +0X119bZ", 36,           1997122175, 12},
930         {       " \t\n -0x119bZ", 36,           -1997122175, 12},
931         {       " \t\n 119bZ", 36,              1738367, 9},
932         {       "+119bZ", 36,                   1738367, 6},
933         {       "+0X119bz", 36,                 1997122175, 8},
934                 /* There should be a much larger battery of such tests. */
935         {       "127", 0,                       127, 3},
936         {       "-127", 0,                      -127, 4},
937         {       "128", 0,                       128, 3},
938         {       "-128", 0,                      -127-1, 4},
939         {       "255", 0,                       255, 3},
940         {       "-255", 0,                      -255, 4},
941         {       "256", 0,                       256, 3},
942         {       "-256", 0,                      -255-1, 4},
943         {       "32767", 0,                     32767, 5},
944         {       "-32767", 0,                    -32767, 6},
945         {       "32768", 0,                     32768, 5},
946         {       "-32768", 0,                    -32767-1, 6},
947         {       "65535", 0,                     65535, 5},
948         {       "-65535", 0,                    -65536+1, 6},
949         {       "65536", 0,                     65536, 5},
950         {       "-65536", 0,                    -65536, 6},
951         {       "2147483647", 0,                2147483647, 10},
952         {       "-2147483647", 0,               -2147483647, 11},
953         {       "2147483648", 0,                2147483648LL, 10},
954         {       "-2147483648", 0,               -2147483647LL-1, 11},
955         {       "4294967295", 0,                4294967295LL, 10},
956         {       "-4294967295", 0,               -4294967296LL+1, 11},
957         {       "4294967296", 0,                4294967296LL, 10},
958         {       "-4294967296", 0,               -4294967296L, 11},
959         {       "9223372036854775807", 0,       9223372036854775807LL, 19},
960         {       "-9223372036854775807", 0,      -9223372036854775807LL, 20},
961         {       "1234567890123456789", 0,       1234567890123456789LL, 19},
962         {       "-1234567890123456789", 0,      -1234567890123456789LL, 20},
963         {       "1", 0,                         1, 1},
964         {       "-1", 0,                        -1, 2},
965         {       "2", 0,                         2, 1},
966         {       "-2", 0,                        -2, 2},
967         {       "10", 0,                        10, 2},
968         {       "-10", 0,                       -10, 3},
969         {       "16", 0,                        16, 2},
970         {       "-16", 0,                       -16, 3},
971                 /* Other test cases can be added here. */
972         {       NULL, 0,        0, 0 }  /* terminates the list */
973                 },      *sdp;
974
975         for ( sdp = str_data; sdp->nptr != NULL ; ++sdp )
976                 {
977                 /*
978                         7.8.2.3 The strtoimax and strtoumax functions
979                 */
980
981                 strcpy(saved, sdp->nptr);
982
983                 errno = 0;              /* shouldn't be changed */
984
985                 if ( (intmax = strtoimax(sdp->nptr, &endptr, sdp->base))
986                   != sdp->exp_val
987                    )    {
988                         int     save = errno;
989
990                         printf("*** strtoimax(%s,,%d) failed; should be: %"
991                                PRIdMAX", was: %"PRIdMAX" ***\n", sdp->nptr,
992                                sdp->base, sdp->exp_val, intmax
993                               );
994                         status = EXIT_FAILURE;
995                         errno = save;
996                         }
997                 else if ( endptr != sdp->nptr + sdp->exp_len )
998                         {
999                         int     save = errno;
1000
1001                         printf("*** strtoimax(%s,,%d) returned wrong endptr"
1002                                " ***\n", sdp->nptr, sdp->base
1003                               );
1004                         status = EXIT_FAILURE;
1005                         errno = save;
1006                         }
1007
1008                 if ( errno != 0 )
1009                         {
1010                         printf("*** strtoimax modified errno ***\n");
1011                         status = EXIT_FAILURE;
1012                         }
1013
1014                 if ( strcmp(sdp->nptr, saved) != 0 )
1015                         {
1016                         printf("*** strtoimax modified its input ***\n");
1017                         status = EXIT_FAILURE;
1018                         strcpy(saved, sdp->nptr);
1019                         }
1020
1021                 if ( sdp->exp_val >= 0 )        /* else some sign extension */
1022                         {
1023                         errno = 0;      /* shouldn't be changed */
1024
1025                         if ( (uintmax = strtoumax(sdp->nptr, &endptr, sdp->base
1026                                                  )
1027                              ) != sdp->exp_val
1028                            )    {
1029                                 int     save = errno;
1030
1031                                 printf("*** strtoumax(%s,,%d) failed; "
1032                                        "should be: %"PRIuMAX", was: %"PRIuMAX
1033                                        " ***\n", sdp->nptr, sdp->base,
1034                                        sdp->exp_val, uintmax
1035                                       );
1036                                 status = EXIT_FAILURE;
1037                                 errno = save;
1038                                 }
1039                         else if ( endptr != sdp->nptr + sdp->exp_len )
1040                                 {
1041                                 int     save = errno;
1042
1043                                 printf("*** strtoumax(%s,,%d) returned wrong "
1044                                        "endptr ***\n", sdp->nptr, sdp->base
1045                                       );
1046                                 status = EXIT_FAILURE;
1047                                 errno = save;
1048                                 }
1049
1050                         if ( errno != 0 )
1051                                 {
1052                                 printf("*** strtoumax modified errno ***\n");
1053                                 status = EXIT_FAILURE;
1054                                 }
1055
1056                         if ( strcmp(sdp->nptr, saved) != 0 )
1057                                 {
1058                                 printf("*** strtoumax"
1059                                        " modified its input ***\n"
1060                                       );
1061                                 status = EXIT_FAILURE;
1062                                 strcpy(saved, sdp->nptr);
1063                                 }
1064                         }
1065
1066                 /* tests for null endptr */
1067
1068 #define WARN()  if (!warned) warned = 1, printf("*** Using null endptr: ***\n")
1069
1070                 warned = 0;
1071                 errno = 0;              /* shouldn't be changed */
1072
1073                 if ( (intmax = strtoimax(sdp->nptr, (char **)NULL, sdp->base))
1074                   != sdp->exp_val
1075                    )    {
1076                         int     save = errno;
1077
1078                         WARN();
1079                         printf("*** strtoimax(%s,NULL,%d) failed; "
1080                                "should be: %"PRIdMAX", was: %"PRIdMAX" ***\n",
1081                                sdp->nptr, sdp->base, sdp->exp_val, intmax
1082                               );
1083                         status = EXIT_FAILURE;
1084                         errno = save;
1085                         }
1086
1087                 if ( errno != 0 )
1088                         {
1089                         WARN();
1090                         printf("*** strtoimax modified errno ***\n");
1091                         status = EXIT_FAILURE;
1092                         }
1093
1094                 if ( strcmp(sdp->nptr, saved) != 0 )
1095                         {
1096                         WARN();
1097                         printf("*** strtoimax modified its input ***\n");
1098                         status = EXIT_FAILURE;
1099                         strcpy(saved, sdp->nptr);
1100                         }
1101
1102                 if ( sdp->exp_val >= 0 )        /* else some sign extension */
1103                         {
1104                         errno = 0;      /* shouldn't be changed */
1105
1106                         if ( (uintmax = strtoumax(sdp->nptr, (char **)NULL,
1107                                                   sdp->base
1108                                                  )
1109                              ) != sdp->exp_val
1110                            )    {
1111                                 int     save = errno;
1112
1113                                 WARN();
1114                                 printf("*** strtoumax(%s,NULL,%d) failed; "
1115                                        "should be: %"PRIuMAX", was: %"PRIuMAX
1116                                        " ***\n", sdp->nptr, sdp->base,
1117                                        sdp->exp_val, uintmax
1118                                       );
1119                                 status = EXIT_FAILURE;
1120                                 errno = save;
1121                                 }
1122
1123                          if ( errno != 0 )
1124                                 {
1125                                 WARN();
1126                                 printf("*** strtoumax modified errno ***\n");
1127                                 status = EXIT_FAILURE;
1128                                 }
1129
1130                          if ( strcmp(sdp->nptr, saved) != 0 )
1131                                 {
1132                                 WARN();
1133                                 printf("*** strtoumax"
1134                                        " modified its input ***\n"
1135                                       );
1136                                 status = EXIT_FAILURE;
1137                                 strcpy(saved, sdp->nptr);
1138                                 }
1139                         }
1140
1141                 /*
1142                         7.8.2.4 The wcstoimax and wcstoumax functions
1143                 */
1144
1145                 for ( i = 0; i < 64; ++i )
1146                         if ( (wnptr[i] = sdp->nptr[i]) == '\0' )
1147                                 break;
1148
1149                 errno = 0;              /* shouldn't be changed */
1150
1151                 if ( (intmax = wcstoimax(wnptr, &wendptr, sdp->base))
1152                   != sdp->exp_val
1153                    )    {
1154                         int     save = errno;
1155
1156                         printf("*** wcstoimax(%s,,%d) failed; should be: %"
1157                                PRIdMAX", was: %"PRIdMAX" ***\n", sdp->nptr,
1158                                sdp->base, sdp->exp_val, intmax
1159                               );
1160                         status = EXIT_FAILURE;
1161                         errno = save;
1162                         }
1163                 else if ( wendptr != wnptr + sdp->exp_len )
1164                         {
1165                         int     save = errno;
1166
1167                         printf("*** wcstoimax(%s,,%d) returned wrong endptr"
1168                                " ***\n", sdp->nptr, sdp->base
1169                               );
1170                         status = EXIT_FAILURE;
1171                         errno = save;
1172                         }
1173
1174                 if ( errno != 0 )
1175                         {
1176                         printf("*** wcstoimax modified errno ***\n");
1177                         status = EXIT_FAILURE;
1178                         }
1179
1180                 for ( i = 0; i < 64; ++i )
1181                         if ( wnptr[i] != sdp->nptr[i] )
1182                                 {
1183                                 printf("*** wcstoimax modified its input ***\n"
1184                                       );
1185                                 status = EXIT_FAILURE;
1186
1187                                 for ( ; i < 64; ++i )
1188                                         if ( (wnptr[i] = sdp->nptr[i]) == '\0' )
1189                                                 break;
1190
1191                                 break;
1192                                 }
1193                         else if ( wnptr[i] == '\0' )
1194                                 break;
1195
1196                 if ( sdp->exp_val >= 0 )        /* else some sign extension */
1197                         {
1198                         errno = 0;      /* shouldn't be changed */
1199
1200                         if ( (uintmax = wcstoumax(wnptr, &wendptr, sdp->base)
1201                              ) != sdp->exp_val
1202                            )    {
1203                                 int     save = errno;
1204
1205                                 printf("*** wcstoumax(%s,,%d) failed; "
1206                                        "should be: %"PRIuMAX", was: %"PRIuMAX
1207                                        " ***\n", sdp->nptr, sdp->base,
1208                                        sdp->exp_val, uintmax
1209                                       );
1210                                 status = EXIT_FAILURE;
1211                                 errno = save;
1212                                 }
1213                         else if ( wendptr != wnptr + sdp->exp_len )
1214                                 {
1215                                 int     save = errno;
1216
1217                                 printf("*** wcstoumax(%s,,%d) returned wrong "
1218                                        "endptr ***\n", sdp->nptr, sdp->base
1219                                       );
1220                                 status = EXIT_FAILURE;
1221                                 errno = save;
1222                                 }
1223
1224                         if ( errno != 0 )
1225                                 {
1226                                 printf("*** wcstoumax modified errno ***\n");
1227                                 status = EXIT_FAILURE;
1228                                 }
1229
1230                         for ( i = 0; i < 64; ++i )
1231                                 if ( wnptr[i] != sdp->nptr[i] )
1232                                         {
1233                                         printf("*** wcstoumax"
1234                                                " modified its input ***\n"
1235                                               );
1236                                         status = EXIT_FAILURE;
1237
1238                                         for ( ; i < 64; ++i )
1239                                                 if ( (wnptr[i] = sdp->nptr[i])
1240                                                   == '\0'
1241                                                    )
1242                                                         break;
1243
1244                                         break;
1245                                         }
1246                                 else if ( wnptr[i] == '\0' )
1247                                         break;
1248                         }
1249
1250                 /* tests for null endptr */
1251
1252                 warned = 0;
1253                 errno = 0;              /* shouldn't be changed */
1254
1255                 if ( (intmax = wcstoimax(wnptr, (wchar_t **)NULL, sdp->base))
1256                   != sdp->exp_val
1257                    )    {
1258                         int     save = errno;
1259
1260                         WARN();
1261                         printf("*** wcstoimax(%s,NULL,%d) failed; should be: %"
1262                                PRIdMAX", was: %"PRIdMAX" ***\n", sdp->nptr,
1263                                sdp->base, sdp->exp_val, intmax
1264                               );
1265                         status = EXIT_FAILURE;
1266                         errno = save;
1267                         }
1268
1269                 if ( errno != 0 )
1270                         {
1271                         WARN();
1272                         printf("*** wcstoimax modified errno ***\n");
1273                         status = EXIT_FAILURE;
1274                         }
1275
1276                 for ( i = 0; i < 64; ++i )
1277                         if ( wnptr[i] != sdp->nptr[i] )
1278                                 {
1279                                 WARN();
1280                                 printf("*** wcstoimax modified its input ***\n"
1281                                       );
1282                                 status = EXIT_FAILURE;
1283
1284                                 for ( ; i < 64; ++i )
1285                                         if ( (wnptr[i] = sdp->nptr[i])
1286                                           == '\0'
1287                                            )
1288                                                 break;
1289
1290                                 break;
1291                                 }
1292                         else if ( wnptr[i] == '\0' )
1293                                 break;
1294
1295                 if ( sdp->exp_val >= 0 )        /* else some sign extension */
1296                         {
1297                         errno = 0;      /* shouldn't be changed */
1298
1299                         if ( (uintmax = wcstoumax(wnptr, (wchar_t **)NULL,
1300                                                   sdp->base
1301                                                  )
1302                              ) != sdp->exp_val
1303                            )    {
1304                                 int     save = errno;
1305
1306                                 WARN();
1307                                 printf("*** wcstoumax(%s,NULL,%d) failed; "
1308                                        "should be: %"PRIuMAX", was: %"PRIuMAX
1309                                        " ***\n", sdp->nptr, sdp->base,
1310                                        sdp->exp_val, uintmax
1311                                       );
1312                                 status = EXIT_FAILURE;
1313                                 errno = save;
1314                                 }
1315
1316                          if ( errno != 0 )
1317                                 {
1318                                 WARN();
1319                                 printf("*** wcstoumax modified errno ***\n");
1320                                 status = EXIT_FAILURE;
1321                                 }
1322
1323                         for ( i = 0; i < 64; ++i )
1324                                 if ( wnptr[i] != sdp->nptr[i] )
1325                                         {
1326                                         WARN();
1327                                         printf("*** wcstoumax"
1328                                                " modified its input ***\n"
1329                                               );
1330                                         status = EXIT_FAILURE;
1331
1332                                         for ( ; i < 64; ++i )
1333                                                 if ( (wnptr[i] = sdp->nptr[i])
1334                                                   == '\0'
1335                                                    )
1336                                                         break;
1337
1338                                         break;
1339                                         }
1340                                 else if ( wnptr[i] == '\0' )
1341                                         break;
1342                         }
1343                 }
1344
1345         /*
1346                 7.8.2.3 The strtoimax and strtoumax functions (continued)
1347         */
1348
1349         if ( (intmax = strtoimax("1234567890123456789012345678901234567890"
1350                                  "1234567890123456789012345678901234567890"
1351                                  "1234567890123456789012345678901234567890"
1352                                  "1234567890123456789012345678901234567890"
1353                                  "1234567890123456789012345678901234567890"
1354                                  "1234567890123456789012345678901234567890",
1355                                  &endptr, 0
1356                                 )
1357              ) != INTMAX_MAX || errno != ERANGE
1358            )    {
1359                 printf("*** strtoimax failed overflow test ***\n");
1360                 status = EXIT_FAILURE;
1361                 }
1362
1363         if ( (intmax = strtoimax("+1234567890123456789012345678901234567890"
1364                                  "1234567890123456789012345678901234567890"
1365                                  "1234567890123456789012345678901234567890"
1366                                  "1234567890123456789012345678901234567890"
1367                                  "1234567890123456789012345678901234567890"
1368                                  "1234567890123456789012345678901234567890",
1369                                  &endptr, 0
1370                                 )
1371              ) != INTMAX_MAX || errno != ERANGE
1372            )    {
1373                 printf("*** strtoimax failed +overflow test ***\n");
1374                 status = EXIT_FAILURE;
1375                 }
1376
1377         if ( (intmax = strtoimax("-1234567890123456789012345678901234567890"
1378                                  "1234567890123456789012345678901234567890"
1379                                  "1234567890123456789012345678901234567890"
1380                                  "1234567890123456789012345678901234567890"
1381                                  "1234567890123456789012345678901234567890"
1382                                  "1234567890123456789012345678901234567890",
1383                                  &endptr, 0
1384                                 )
1385              ) != INTMAX_MIN || errno != ERANGE
1386            )    {
1387                 printf("*** strtoimax failed -overflow test ***\n");
1388                 status = EXIT_FAILURE;
1389                 }
1390
1391         if ( (uintmax = strtoumax("1234567890123456789012345678901234567890"
1392                                  "1234567890123456789012345678901234567890"
1393                                  "1234567890123456789012345678901234567890"
1394                                  "1234567890123456789012345678901234567890"
1395                                  "1234567890123456789012345678901234567890"
1396                                  "1234567890123456789012345678901234567890",
1397                                  &endptr, 0
1398                                 )
1399              ) != UINTMAX_MAX || errno != ERANGE
1400            )    {
1401                 printf("*** strtoumax failed overflow test ***\n");
1402                 status = EXIT_FAILURE;
1403                 }
1404
1405         if ( (uintmax = strtoumax("+1234567890123456789012345678901234567890"
1406                                  "1234567890123456789012345678901234567890"
1407                                  "1234567890123456789012345678901234567890"
1408                                  "1234567890123456789012345678901234567890"
1409                                  "1234567890123456789012345678901234567890"
1410                                  "1234567890123456789012345678901234567890",
1411                                  &endptr, 0
1412                                 )
1413              ) != UINTMAX_MAX || errno != ERANGE
1414            )    {
1415                 printf("*** strtoumax failed +overflow test ***\n");
1416                 status = EXIT_FAILURE;
1417                 }
1418
1419         if ( (uintmax = strtoumax("-1234567890123456789012345678901234567890"
1420                                  "1234567890123456789012345678901234567890"
1421                                  "1234567890123456789012345678901234567890"
1422                                  "1234567890123456789012345678901234567890"
1423                                  "1234567890123456789012345678901234567890"
1424                                  "1234567890123456789012345678901234567890",
1425                                  &endptr, 0
1426                                 )
1427              ) != UINTMAX_MAX || errno != ERANGE
1428            )    {
1429                 printf("*** strtoumax failed -overflow test ***\n");
1430                 status = EXIT_FAILURE;
1431                 }
1432
1433         /*
1434                 7.8.2.4 The wcstoimax and wcstoumax functions (continued)
1435         */
1436
1437         if ( (intmax = wcstoimax(L"1234567890123456789012345678901234567890"
1438                                  L"1234567890123456789012345678901234567890"
1439                                  L"1234567890123456789012345678901234567890"
1440                                  L"1234567890123456789012345678901234567890"
1441                                  L"1234567890123456789012345678901234567890"
1442                                  L"1234567890123456789012345678901234567890",
1443                                  &wendptr, 0
1444                                 )
1445              ) != INTMAX_MAX || errno != ERANGE
1446            )    {
1447                 printf("*** wcstoimax failed overflow test ***\n");
1448                 status = EXIT_FAILURE;
1449                 }
1450
1451         if ( (intmax = wcstoimax(L"+1234567890123456789012345678901234567890"
1452                                  L"1234567890123456789012345678901234567890"
1453                                  L"1234567890123456789012345678901234567890"
1454                                  L"1234567890123456789012345678901234567890"
1455                                  L"1234567890123456789012345678901234567890"
1456                                  L"1234567890123456789012345678901234567890",
1457                                  &wendptr, 0
1458                                 )
1459              ) != INTMAX_MAX || errno != ERANGE
1460            )    {
1461                 printf("*** wcstoimax failed +overflow test ***\n");
1462                 status = EXIT_FAILURE;
1463                 }
1464
1465         if ( (intmax = wcstoimax(L"-1234567890123456789012345678901234567890"
1466                                  L"1234567890123456789012345678901234567890"
1467                                  L"1234567890123456789012345678901234567890"
1468                                  L"1234567890123456789012345678901234567890"
1469                                  L"1234567890123456789012345678901234567890"
1470                                  L"1234567890123456789012345678901234567890",
1471                                  &wendptr, 0
1472                                 )
1473              ) != INTMAX_MIN || errno != ERANGE
1474            )    {
1475                 printf("*** wcstoimax failed -overflow test ***\n");
1476                 status = EXIT_FAILURE;
1477                 }
1478
1479         if ( (uintmax = wcstoumax(L"1234567890123456789012345678901234567890"
1480                                  L"1234567890123456789012345678901234567890"
1481                                  L"1234567890123456789012345678901234567890"
1482                                  L"1234567890123456789012345678901234567890"
1483                                  L"1234567890123456789012345678901234567890"
1484                                  L"1234567890123456789012345678901234567890",
1485                                  &wendptr, 0
1486                                 )
1487              ) != UINTMAX_MAX || errno != ERANGE
1488            )    {
1489                 printf("*** wcstoumax failed overflow test ***\n");
1490                 status = EXIT_FAILURE;
1491                 }
1492
1493         if ( (uintmax = wcstoumax(L"+1234567890123456789012345678901234567890"
1494                                  L"1234567890123456789012345678901234567890"
1495                                  L"1234567890123456789012345678901234567890"
1496                                  L"1234567890123456789012345678901234567890"
1497                                  L"1234567890123456789012345678901234567890"
1498                                  L"1234567890123456789012345678901234567890",
1499                                  &wendptr, 0
1500                                 )
1501              ) != UINTMAX_MAX || errno != ERANGE
1502            )    {
1503                 printf("*** wcstoumax failed +overflow test ***\n");
1504                 status = EXIT_FAILURE;
1505                 }
1506
1507         if ( (uintmax = wcstoumax(L"-1234567890123456789012345678901234567890"
1508                                  L"1234567890123456789012345678901234567890"
1509                                  L"1234567890123456789012345678901234567890"
1510                                  L"1234567890123456789012345678901234567890"
1511                                  L"1234567890123456789012345678901234567890"
1512                                  L"1234567890123456789012345678901234567890",
1513                                  &wendptr, 0
1514                                 )
1515              ) != UINTMAX_MAX || errno != ERANGE
1516            )    {
1517                 printf("*** wcstoumax failed -overflow test ***\n");
1518                 status = EXIT_FAILURE;
1519                 }
1520         }
1521 #endif  /* defined(INTMAX_MAX) */
1522
1523         if ( status != 0 )
1524                 fprintf(stderr, "sitest failed; see stdout for details\n");
1525
1526         return status;
1527         }