openat: don’t close (-1)
[gnulib.git] / tests / test-stdint.c
blobb8799eafa0956c56dedb5d46d789a6a4278f59a7
1 /* Test of <stdint.h> substitute.
2 Copyright (C) 2006-2024 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Bruno Haible <bruno@clisp.org>, 2006. */
19 #include <config.h>
21 /* Whether to enable pedantic checks. */
22 #define DO_PEDANTIC 0
24 #include <stdint.h>
26 #include "verify.h"
27 #include "intprops.h"
29 #if ((__GNUC__ >= 2) || (__clang_major__ >= 4)) && DO_PEDANTIC
30 # define verify_same_types(expr1,expr2) \
31 extern void _verify_func(__LINE__) (__typeof__ (expr1) *); \
32 extern void _verify_func(__LINE__) (__typeof__ (expr2) *);
33 # define _verify_func(line) _verify_func2(line)
34 # define _verify_func2(line) verify_func_ ## line
35 #else
36 # define verify_same_types(expr1,expr2) extern void verify_func (int)
37 #endif
39 /* 7.18.1.1. Exact-width integer types */
40 /* 7.18.2.1. Limits of exact-width integer types */
42 int8_t a1[3] = { INT8_C (17), INT8_MIN, INT8_MAX };
43 verify (TYPE_MINIMUM (int8_t) == INT8_MIN);
44 verify (TYPE_MAXIMUM (int8_t) == INT8_MAX);
45 verify_same_types (INT8_MIN, (int8_t) 0 + 0);
46 verify_same_types (INT8_MAX, (int8_t) 0 + 0);
48 int16_t a2[3] = { INT16_C (17), INT16_MIN, INT16_MAX };
49 verify (TYPE_MINIMUM (int16_t) == INT16_MIN);
50 verify (TYPE_MAXIMUM (int16_t) == INT16_MAX);
51 verify_same_types (INT16_MIN, (int16_t) 0 + 0);
52 verify_same_types (INT16_MAX, (int16_t) 0 + 0);
54 int32_t a3[3] = { INT32_C (17), INT32_MIN, INT32_MAX };
55 verify (TYPE_MINIMUM (int32_t) == INT32_MIN);
56 verify (TYPE_MAXIMUM (int32_t) == INT32_MAX);
57 verify_same_types (INT32_MIN, (int32_t) 0 + 0);
58 verify_same_types (INT32_MAX, (int32_t) 0 + 0);
60 #ifdef INT64_MAX
61 int64_t a4[3] = { INT64_C (17), INT64_MIN, INT64_MAX };
62 verify (TYPE_MINIMUM (int64_t) == INT64_MIN);
63 verify (TYPE_MAXIMUM (int64_t) == INT64_MAX);
64 verify_same_types (INT64_MIN, (int64_t) 0 + 0);
65 verify_same_types (INT64_MAX, (int64_t) 0 + 0);
66 #endif
68 uint8_t b1[2] = { UINT8_C (17), UINT8_MAX };
69 verify (TYPE_MAXIMUM (uint8_t) == UINT8_MAX);
70 verify_same_types (UINT8_MAX, (uint8_t) 0 + 0);
72 uint16_t b2[2] = { UINT16_C (17), UINT16_MAX };
73 verify (TYPE_MAXIMUM (uint16_t) == UINT16_MAX);
74 verify_same_types (UINT16_MAX, (uint16_t) 0 + 0);
76 uint32_t b3[2] = { UINT32_C (17), UINT32_MAX };
77 verify (TYPE_MAXIMUM (uint32_t) == UINT32_MAX);
78 verify_same_types (UINT32_MAX, (uint32_t) 0 + 0);
80 #ifdef UINT64_MAX
81 uint64_t b4[2] = { UINT64_C (17), UINT64_MAX };
82 verify (TYPE_MAXIMUM (uint64_t) == UINT64_MAX);
83 verify_same_types (UINT64_MAX, (uint64_t) 0 + 0);
84 #endif
86 #if INT8_MIN && INT8_MAX && INT16_MIN && INT16_MAX && INT32_MIN && INT32_MAX
87 /* ok */
88 #else
89 err or;
90 #endif
92 #if UINT8_MAX && UINT16_MAX && UINT32_MAX
93 /* ok */
94 #else
95 err or;
96 #endif
98 /* 7.18.1.2. Minimum-width integer types */
99 /* 7.18.2.2. Limits of minimum-width integer types */
101 int_least8_t c1[3] = { 17, INT_LEAST8_MIN, INT_LEAST8_MAX };
102 verify (TYPE_MINIMUM (int_least8_t) == INT_LEAST8_MIN);
103 verify (TYPE_MAXIMUM (int_least8_t) == INT_LEAST8_MAX);
104 verify_same_types (INT_LEAST8_MIN, (int_least8_t) 0 + 0);
105 verify_same_types (INT_LEAST8_MAX, (int_least8_t) 0 + 0);
107 int_least16_t c2[3] = { 17, INT_LEAST16_MIN, INT_LEAST16_MAX };
108 verify (TYPE_MINIMUM (int_least16_t) == INT_LEAST16_MIN);
109 verify (TYPE_MAXIMUM (int_least16_t) == INT_LEAST16_MAX);
110 verify_same_types (INT_LEAST16_MIN, (int_least16_t) 0 + 0);
111 verify_same_types (INT_LEAST16_MAX, (int_least16_t) 0 + 0);
113 int_least32_t c3[3] = { 17, INT_LEAST32_MIN, INT_LEAST32_MAX };
114 verify (TYPE_MINIMUM (int_least32_t) == INT_LEAST32_MIN);
115 verify (TYPE_MAXIMUM (int_least32_t) == INT_LEAST32_MAX);
116 verify_same_types (INT_LEAST32_MIN, (int_least32_t) 0 + 0);
117 verify_same_types (INT_LEAST32_MAX, (int_least32_t) 0 + 0);
119 #ifdef INT_LEAST64_MAX
120 int_least64_t c4[3] = { 17, INT_LEAST64_MIN, INT_LEAST64_MAX };
121 verify (TYPE_MINIMUM (int_least64_t) == INT_LEAST64_MIN);
122 verify (TYPE_MAXIMUM (int_least64_t) == INT_LEAST64_MAX);
123 verify_same_types (INT_LEAST64_MIN, (int_least64_t) 0 + 0);
124 verify_same_types (INT_LEAST64_MAX, (int_least64_t) 0 + 0);
125 #endif
127 uint_least8_t d1[2] = { 17, UINT_LEAST8_MAX };
128 verify (TYPE_MAXIMUM (uint_least8_t) == UINT_LEAST8_MAX);
129 verify_same_types (UINT_LEAST8_MAX, (uint_least8_t) 0 + 0);
131 uint_least16_t d2[2] = { 17, UINT_LEAST16_MAX };
132 verify (TYPE_MAXIMUM (uint_least16_t) == UINT_LEAST16_MAX);
133 verify_same_types (UINT_LEAST16_MAX, (uint_least16_t) 0 + 0);
135 uint_least32_t d3[2] = { 17, UINT_LEAST32_MAX };
136 verify (TYPE_MAXIMUM (uint_least32_t) == UINT_LEAST32_MAX);
137 verify_same_types (UINT_LEAST32_MAX, (uint_least32_t) 0 + 0);
139 #ifdef UINT_LEAST64_MAX
140 uint_least64_t d4[2] = { 17, UINT_LEAST64_MAX };
141 verify (TYPE_MAXIMUM (uint_least64_t) == UINT_LEAST64_MAX);
142 verify_same_types (UINT_LEAST64_MAX, (uint_least64_t) 0 + 0);
143 #endif
145 #if INT_LEAST8_MIN && INT_LEAST8_MAX && INT_LEAST16_MIN && INT_LEAST16_MAX && INT_LEAST32_MIN && INT_LEAST32_MAX
146 /* ok */
147 #else
148 err or;
149 #endif
151 #if UINT_LEAST8_MAX && UINT_LEAST16_MAX && UINT_LEAST32_MAX
152 /* ok */
153 #else
154 err or;
155 #endif
157 /* 7.18.1.3. Fastest minimum-width integer types */
158 /* 7.18.2.3. Limits of fastest minimum-width integer types */
160 int_fast8_t e1[3] = { 17, INT_FAST8_MIN, INT_FAST8_MAX };
161 verify (TYPE_MINIMUM (int_fast8_t) == INT_FAST8_MIN);
162 verify (TYPE_MAXIMUM (int_fast8_t) == INT_FAST8_MAX);
163 verify_same_types (INT_FAST8_MIN, (int_fast8_t) 0 + 0);
164 verify_same_types (INT_FAST8_MAX, (int_fast8_t) 0 + 0);
166 int_fast16_t e2[3] = { 17, INT_FAST16_MIN, INT_FAST16_MAX };
167 verify (TYPE_MINIMUM (int_fast16_t) == INT_FAST16_MIN);
168 verify (TYPE_MAXIMUM (int_fast16_t) == INT_FAST16_MAX);
169 verify_same_types (INT_FAST16_MIN, (int_fast16_t) 0 + 0);
170 verify_same_types (INT_FAST16_MAX, (int_fast16_t) 0 + 0);
172 int_fast32_t e3[3] = { 17, INT_FAST32_MIN, INT_FAST32_MAX };
173 verify (TYPE_MINIMUM (int_fast32_t) == INT_FAST32_MIN);
174 verify (TYPE_MAXIMUM (int_fast32_t) == INT_FAST32_MAX);
175 verify_same_types (INT_FAST32_MIN, (int_fast32_t) 0 + 0);
176 verify_same_types (INT_FAST32_MAX, (int_fast32_t) 0 + 0);
178 #ifdef INT_FAST64_MAX
179 int_fast64_t e4[3] = { 17, INT_FAST64_MIN, INT_FAST64_MAX };
180 verify (TYPE_MINIMUM (int_fast64_t) == INT_FAST64_MIN);
181 verify (TYPE_MAXIMUM (int_fast64_t) == INT_FAST64_MAX);
182 verify_same_types (INT_FAST64_MIN, (int_fast64_t) 0 + 0);
183 verify_same_types (INT_FAST64_MAX, (int_fast64_t) 0 + 0);
184 #endif
186 uint_fast8_t f1[2] = { 17, UINT_FAST8_MAX };
187 verify (TYPE_MAXIMUM (uint_fast8_t) == UINT_FAST8_MAX);
188 verify_same_types (UINT_FAST8_MAX, (uint_fast8_t) 0 + 0);
190 uint_fast16_t f2[2] = { 17, UINT_FAST16_MAX };
191 verify (TYPE_MAXIMUM (uint_fast16_t) == UINT_FAST16_MAX);
192 verify_same_types (UINT_FAST16_MAX, (uint_fast16_t) 0 + 0);
194 uint_fast32_t f3[2] = { 17, UINT_FAST32_MAX };
195 verify (TYPE_MAXIMUM (uint_fast32_t) == UINT_FAST32_MAX);
196 verify_same_types (UINT_FAST32_MAX, (uint_fast32_t) 0 + 0);
198 #ifdef UINT_FAST64_MAX
199 uint_fast64_t f4[2] = { 17, UINT_FAST64_MAX };
200 verify (TYPE_MAXIMUM (uint_fast64_t) == UINT_FAST64_MAX);
201 verify_same_types (UINT_FAST64_MAX, (uint_fast64_t) 0 + 0);
202 #endif
204 #if INT_FAST8_MIN && INT_FAST8_MAX && INT_FAST16_MIN && INT_FAST16_MAX && INT_FAST32_MIN && INT_FAST32_MAX
205 /* ok */
206 #else
207 err or;
208 #endif
210 #if UINT_FAST8_MAX && UINT_FAST16_MAX && UINT_FAST32_MAX
211 /* ok */
212 #else
213 err or;
214 #endif
216 /* 7.18.1.4. Integer types capable of holding object pointers */
217 /* 7.18.2.4. Limits of integer types capable of holding object pointers */
219 #ifdef INTPTR_MAX
220 intptr_t g[3] = { 17, INTPTR_MIN, INTPTR_MAX };
221 verify (sizeof (void *) <= sizeof (intptr_t));
222 # ifndef __CHERI_PURE_CAPABILITY__
223 verify (TYPE_MINIMUM (intptr_t) == INTPTR_MIN);
224 verify (TYPE_MAXIMUM (intptr_t) == INTPTR_MAX);
225 # endif
226 verify_same_types (INTPTR_MIN, (intptr_t) 0 + 0);
227 verify_same_types (INTPTR_MAX, (intptr_t) 0 + 0);
228 #endif
230 #ifdef UINTPTR_MAX
231 uintptr_t h[2] = { 17, UINTPTR_MAX };
232 verify (sizeof (void *) <= sizeof (uintptr_t));
233 # ifndef __CHERI_PURE_CAPABILITY__
234 verify (TYPE_MAXIMUM (uintptr_t) == UINTPTR_MAX);
235 # endif
236 verify_same_types (UINTPTR_MAX, (uintptr_t) 0 + 0);
237 #endif
239 /* 7.18.1.5. Greatest-width integer types */
240 /* 7.18.2.5. Limits of greatest-width integer types */
242 intmax_t i[3] = { INTMAX_C (17), INTMAX_MIN, INTMAX_MAX };
243 verify (TYPE_MINIMUM (intmax_t) == INTMAX_MIN);
244 verify (TYPE_MAXIMUM (intmax_t) == INTMAX_MAX);
245 verify_same_types (INTMAX_MIN, (intmax_t) 0 + 0);
246 verify_same_types (INTMAX_MAX, (intmax_t) 0 + 0);
248 uintmax_t j[2] = { UINTMAX_C (17), UINTMAX_MAX };
249 verify (TYPE_MAXIMUM (uintmax_t) == UINTMAX_MAX);
250 verify_same_types (UINTMAX_MAX, (uintmax_t) 0 + 0);
252 /* Older Sun C and HP-UX 10.20 cc don't support 'long long' constants in
253 the preprocessor. */
254 #if !((defined __SUNPRO_C && __SUNPRO_C < 0x5150) \
255 || (defined __hpux && !defined __GNUC__))
256 #if INTMAX_MIN && INTMAX_MAX && UINTMAX_MAX
257 /* ok */
258 #else
259 err or;
260 #endif
261 #endif
263 /* 7.18.3. Limits of other integer types */
265 #include <stddef.h>
267 verify (TYPE_MINIMUM (ptrdiff_t) == PTRDIFF_MIN);
268 verify (TYPE_MAXIMUM (ptrdiff_t) == PTRDIFF_MAX);
269 verify_same_types (PTRDIFF_MIN, (ptrdiff_t) 0 + 0);
270 verify_same_types (PTRDIFF_MAX, (ptrdiff_t) 0 + 0);
272 #if PTRDIFF_MIN && PTRDIFF_MAX
273 /* ok */
274 #else
275 err or;
276 #endif
278 #include <signal.h>
280 verify (TYPE_MINIMUM (sig_atomic_t) == SIG_ATOMIC_MIN);
281 verify (TYPE_MAXIMUM (sig_atomic_t) == SIG_ATOMIC_MAX);
282 verify_same_types (SIG_ATOMIC_MIN, (sig_atomic_t) 0 + 0);
283 verify_same_types (SIG_ATOMIC_MAX, (sig_atomic_t) 0 + 0);
285 #if SIG_ATOMIC_MIN != 17 && SIG_ATOMIC_MAX
286 /* ok */
287 #else
288 err or;
289 #endif
291 verify (TYPE_MAXIMUM (size_t) == SIZE_MAX);
292 verify_same_types (SIZE_MAX, (size_t) 0 + 0);
294 #if SIZE_MAX
295 /* ok */
296 #else
297 err or;
298 #endif
300 verify (TYPE_MINIMUM (wchar_t) == WCHAR_MIN);
301 verify (TYPE_MAXIMUM (wchar_t) == WCHAR_MAX);
302 verify_same_types (WCHAR_MIN, (wchar_t) 0 + 0);
303 verify_same_types (WCHAR_MAX, (wchar_t) 0 + 0);
305 #if WCHAR_MIN != 17 && WCHAR_MAX
306 /* ok */
307 #else
308 err or;
309 #endif
311 #if HAVE_WINT_T
312 # include <wchar.h>
314 verify (TYPE_MINIMUM (wint_t) == WINT_MIN);
315 verify (TYPE_MAXIMUM (wint_t) == WINT_MAX);
316 verify_same_types (WINT_MIN, (wint_t) 0 + 0);
317 verify_same_types (WINT_MAX, (wint_t) 0 + 0);
319 # if WINT_MIN != 17 && WINT_MAX
320 /* ok */
321 # else
322 err or;
323 # endif
324 #endif
326 /* 7.18.4. Macros for integer constants */
328 verify (INT8_C (17) == 17);
329 verify_same_types (INT8_C (17), (int_least8_t)0 + 0);
330 verify (UINT8_C (17) == 17);
331 verify_same_types (UINT8_C (17), (uint_least8_t)0 + 0);
333 verify (INT16_C (17) == 17);
334 verify_same_types (INT16_C (17), (int_least16_t)0 + 0);
335 verify (UINT16_C (17) == 17);
336 verify_same_types (UINT16_C (17), (uint_least16_t)0 + 0);
338 verify (INT32_C (17) == 17);
339 verify_same_types (INT32_C (17), (int_least32_t)0 + 0);
340 verify (UINT32_C (17) == 17);
341 verify_same_types (UINT32_C (17), (uint_least32_t)0 + 0);
343 #ifdef INT64_C
344 verify (INT64_C (17) == 17);
345 verify_same_types (INT64_C (17), (int_least64_t)0 + 0);
346 #endif
347 #ifdef UINT64_C
348 verify (UINT64_C (17) == 17);
349 verify_same_types (UINT64_C (17), (uint_least64_t)0 + 0);
350 #endif
352 verify (INTMAX_C (17) == 17);
353 verify_same_types (INTMAX_C (17), (intmax_t)0 + 0);
354 verify (UINTMAX_C (17) == 17);
355 verify_same_types (UINTMAX_C (17), (uintmax_t)0 + 0);
357 /* Use _GL_VERIFY (with a fixed-length diagnostic string) rather than verify,
358 because the latter would require forming each stringified expression, and
359 many of these would be so long as to trigger a warning/error like this:
361 test-stdint.c:407:1: error: string length '6980' is greater than the \
362 length '4095' ISO C99 compilers are required to support \
363 [-Werror=overlength-strings]
365 #define verify_width(width, min, max) \
366 _GL_VERIFY ((max) >> ((width) - 1 - ((min) < 0)) == 1, \
367 "verify_width check", -)
369 /* Macros specified by ISO/IEC TS 18661-1:2014. */
371 #ifdef INT8_MAX
372 verify_width (INT8_WIDTH, INT8_MIN, INT8_MAX);
373 #endif
374 #ifdef UINT8_MAX
375 verify_width (UINT8_WIDTH, 0, UINT8_MAX);
376 #endif
377 #ifdef INT16_MAX
378 verify_width (INT16_WIDTH, INT16_MIN, INT16_MAX);
379 #endif
380 #ifdef UINT16_MAX
381 verify_width (UINT16_WIDTH, 0, UINT16_MAX);
382 #endif
383 #ifdef INT32_MAX
384 verify_width (INT32_WIDTH, INT32_MIN, INT32_MAX);
385 #endif
386 #ifdef UINT32_MAX
387 verify_width (UINT32_WIDTH, 0, UINT32_MAX);
388 #endif
389 #ifdef INT64_MAX
390 verify_width (INT64_WIDTH, INT64_MIN, INT64_MAX);
391 #endif
392 #ifdef UINT64_MAX
393 verify_width (UINT64_WIDTH, 0, UINT64_MAX);
394 #endif
395 verify_width (INT_LEAST8_WIDTH, INT_LEAST8_MIN, INT_LEAST8_MAX);
396 verify_width (UINT_LEAST8_WIDTH, 0, UINT_LEAST8_MAX);
397 verify_width (INT_LEAST16_WIDTH, INT_LEAST16_MIN, INT_LEAST16_MAX);
398 verify_width (UINT_LEAST16_WIDTH, 0, UINT_LEAST16_MAX);
399 verify_width (INT_LEAST32_WIDTH, INT_LEAST32_MIN, INT_LEAST32_MAX);
400 verify_width (UINT_LEAST32_WIDTH, 0, UINT_LEAST32_MAX);
401 verify_width (INT_LEAST64_WIDTH, INT_LEAST64_MIN, INT_LEAST64_MAX);
402 verify_width (UINT_LEAST64_WIDTH, 0, UINT_LEAST64_MAX);
403 verify_width (INT_FAST8_WIDTH, INT_FAST8_MIN, INT_FAST8_MAX);
404 verify_width (UINT_FAST8_WIDTH, 0, UINT_FAST8_MAX);
405 verify_width (INT_FAST16_WIDTH, INT_FAST16_MIN, INT_FAST16_MAX);
406 verify_width (UINT_FAST16_WIDTH, 0, UINT_FAST16_MAX);
407 verify_width (INT_FAST32_WIDTH, INT_FAST32_MIN, INT_FAST32_MAX);
408 verify_width (UINT_FAST32_WIDTH, 0, UINT_FAST32_MAX);
409 verify_width (INT_FAST64_WIDTH, INT_FAST64_MIN, INT_FAST64_MAX);
410 verify_width (UINT_FAST64_WIDTH, 0, UINT_FAST64_MAX);
411 #ifndef __CHERI_PURE_CAPABILITY__
412 # ifdef INTPTR_WIDTH
413 verify_width (INTPTR_WIDTH, INTPTR_MIN, INTPTR_MAX);
414 # endif
415 # ifdef UINTPTR_WIDTH
416 verify_width (UINTPTR_WIDTH, 0, UINTPTR_MAX);
417 # endif
418 #endif
419 verify_width (INTMAX_WIDTH, INTMAX_MIN, INTMAX_MAX);
420 verify_width (UINTMAX_WIDTH, 0, UINTMAX_MAX);
421 verify_width (PTRDIFF_WIDTH, PTRDIFF_MIN, PTRDIFF_MAX);
422 verify_width (SIZE_WIDTH, 0, SIZE_MAX);
423 verify_width (WCHAR_WIDTH, WCHAR_MIN, WCHAR_MAX);
424 #ifdef WINT_MAX
425 verify_width (WINT_WIDTH, WINT_MIN, WINT_MAX);
426 #endif
427 #ifdef SIG_ATOMIC_MAX
428 verify_width (SIG_ATOMIC_WIDTH, SIG_ATOMIC_MIN, SIG_ATOMIC_MAX);
429 #endif
432 main (void)
434 return 0;