source: trunk/src/sed/gnulib-tests/test-stdint.c@ 3669

Last change on this file since 3669 was 3611, checked in by bird, 10 months ago

vendor/sed/current: GNU sed 4.9 (sed-4.9.tar.xz sha256:6e226b732e1cd739464ad6862bd1a1aba42d7982922da7a53519631d24975181)

File size: 13.8 KB
Line 
1/* Test of <stdint.h> substitute.
2 Copyright (C) 2006-2022 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>. */
16
17/* Written by Bruno Haible <bruno@clisp.org>, 2006. */
18
19#include <config.h>
20
21/* Whether to enable pedantic checks. */
22#define DO_PEDANTIC 0
23
24#include <stdint.h>
25
26#include "verify.h"
27#include "intprops.h"
28
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
38
39/* 7.18.1.1. Exact-width integer types */
40/* 7.18.2.1. Limits of exact-width integer types */
41
42int8_t a1[3] = { INT8_C (17), INT8_MIN, INT8_MAX };
43verify (TYPE_MINIMUM (int8_t) == INT8_MIN);
44verify (TYPE_MAXIMUM (int8_t) == INT8_MAX);
45verify_same_types (INT8_MIN, (int8_t) 0 + 0);
46verify_same_types (INT8_MAX, (int8_t) 0 + 0);
47
48int16_t a2[3] = { INT16_C (17), INT16_MIN, INT16_MAX };
49verify (TYPE_MINIMUM (int16_t) == INT16_MIN);
50verify (TYPE_MAXIMUM (int16_t) == INT16_MAX);
51verify_same_types (INT16_MIN, (int16_t) 0 + 0);
52verify_same_types (INT16_MAX, (int16_t) 0 + 0);
53
54int32_t a3[3] = { INT32_C (17), INT32_MIN, INT32_MAX };
55verify (TYPE_MINIMUM (int32_t) == INT32_MIN);
56verify (TYPE_MAXIMUM (int32_t) == INT32_MAX);
57verify_same_types (INT32_MIN, (int32_t) 0 + 0);
58verify_same_types (INT32_MAX, (int32_t) 0 + 0);
59
60#ifdef INT64_MAX
61int64_t a4[3] = { INT64_C (17), INT64_MIN, INT64_MAX };
62verify (TYPE_MINIMUM (int64_t) == INT64_MIN);
63verify (TYPE_MAXIMUM (int64_t) == INT64_MAX);
64verify_same_types (INT64_MIN, (int64_t) 0 + 0);
65verify_same_types (INT64_MAX, (int64_t) 0 + 0);
66#endif
67
68uint8_t b1[2] = { UINT8_C (17), UINT8_MAX };
69verify (TYPE_MAXIMUM (uint8_t) == UINT8_MAX);
70verify_same_types (UINT8_MAX, (uint8_t) 0 + 0);
71
72uint16_t b2[2] = { UINT16_C (17), UINT16_MAX };
73verify (TYPE_MAXIMUM (uint16_t) == UINT16_MAX);
74verify_same_types (UINT16_MAX, (uint16_t) 0 + 0);
75
76uint32_t b3[2] = { UINT32_C (17), UINT32_MAX };
77verify (TYPE_MAXIMUM (uint32_t) == UINT32_MAX);
78verify_same_types (UINT32_MAX, (uint32_t) 0 + 0);
79
80#ifdef UINT64_MAX
81uint64_t b4[2] = { UINT64_C (17), UINT64_MAX };
82verify (TYPE_MAXIMUM (uint64_t) == UINT64_MAX);
83verify_same_types (UINT64_MAX, (uint64_t) 0 + 0);
84#endif
85
86#if INT8_MIN && INT8_MAX && INT16_MIN && INT16_MAX && INT32_MIN && INT32_MAX
87/* ok */
88#else
89err or;
90#endif
91
92#if UINT8_MAX && UINT16_MAX && UINT32_MAX
93/* ok */
94#else
95err or;
96#endif
97
98/* 7.18.1.2. Minimum-width integer types */
99/* 7.18.2.2. Limits of minimum-width integer types */
100
101int_least8_t c1[3] = { 17, INT_LEAST8_MIN, INT_LEAST8_MAX };
102verify (TYPE_MINIMUM (int_least8_t) == INT_LEAST8_MIN);
103verify (TYPE_MAXIMUM (int_least8_t) == INT_LEAST8_MAX);
104verify_same_types (INT_LEAST8_MIN, (int_least8_t) 0 + 0);
105verify_same_types (INT_LEAST8_MAX, (int_least8_t) 0 + 0);
106
107int_least16_t c2[3] = { 17, INT_LEAST16_MIN, INT_LEAST16_MAX };
108verify (TYPE_MINIMUM (int_least16_t) == INT_LEAST16_MIN);
109verify (TYPE_MAXIMUM (int_least16_t) == INT_LEAST16_MAX);
110verify_same_types (INT_LEAST16_MIN, (int_least16_t) 0 + 0);
111verify_same_types (INT_LEAST16_MAX, (int_least16_t) 0 + 0);
112
113int_least32_t c3[3] = { 17, INT_LEAST32_MIN, INT_LEAST32_MAX };
114verify (TYPE_MINIMUM (int_least32_t) == INT_LEAST32_MIN);
115verify (TYPE_MAXIMUM (int_least32_t) == INT_LEAST32_MAX);
116verify_same_types (INT_LEAST32_MIN, (int_least32_t) 0 + 0);
117verify_same_types (INT_LEAST32_MAX, (int_least32_t) 0 + 0);
118
119#ifdef INT_LEAST64_MAX
120int_least64_t c4[3] = { 17, INT_LEAST64_MIN, INT_LEAST64_MAX };
121verify (TYPE_MINIMUM (int_least64_t) == INT_LEAST64_MIN);
122verify (TYPE_MAXIMUM (int_least64_t) == INT_LEAST64_MAX);
123verify_same_types (INT_LEAST64_MIN, (int_least64_t) 0 + 0);
124verify_same_types (INT_LEAST64_MAX, (int_least64_t) 0 + 0);
125#endif
126
127uint_least8_t d1[2] = { 17, UINT_LEAST8_MAX };
128verify (TYPE_MAXIMUM (uint_least8_t) == UINT_LEAST8_MAX);
129verify_same_types (UINT_LEAST8_MAX, (uint_least8_t) 0 + 0);
130
131uint_least16_t d2[2] = { 17, UINT_LEAST16_MAX };
132verify (TYPE_MAXIMUM (uint_least16_t) == UINT_LEAST16_MAX);
133verify_same_types (UINT_LEAST16_MAX, (uint_least16_t) 0 + 0);
134
135uint_least32_t d3[2] = { 17, UINT_LEAST32_MAX };
136verify (TYPE_MAXIMUM (uint_least32_t) == UINT_LEAST32_MAX);
137verify_same_types (UINT_LEAST32_MAX, (uint_least32_t) 0 + 0);
138
139#ifdef UINT_LEAST64_MAX
140uint_least64_t d4[2] = { 17, UINT_LEAST64_MAX };
141verify (TYPE_MAXIMUM (uint_least64_t) == UINT_LEAST64_MAX);
142verify_same_types (UINT_LEAST64_MAX, (uint_least64_t) 0 + 0);
143#endif
144
145#if INT_LEAST8_MIN && INT_LEAST8_MAX && INT_LEAST16_MIN && INT_LEAST16_MAX && INT_LEAST32_MIN && INT_LEAST32_MAX
146/* ok */
147#else
148err or;
149#endif
150
151#if UINT_LEAST8_MAX && UINT_LEAST16_MAX && UINT_LEAST32_MAX
152/* ok */
153#else
154err or;
155#endif
156
157/* 7.18.1.3. Fastest minimum-width integer types */
158/* 7.18.2.3. Limits of fastest minimum-width integer types */
159
160int_fast8_t e1[3] = { 17, INT_FAST8_MIN, INT_FAST8_MAX };
161verify (TYPE_MINIMUM (int_fast8_t) == INT_FAST8_MIN);
162verify (TYPE_MAXIMUM (int_fast8_t) == INT_FAST8_MAX);
163verify_same_types (INT_FAST8_MIN, (int_fast8_t) 0 + 0);
164verify_same_types (INT_FAST8_MAX, (int_fast8_t) 0 + 0);
165
166int_fast16_t e2[3] = { 17, INT_FAST16_MIN, INT_FAST16_MAX };
167verify (TYPE_MINIMUM (int_fast16_t) == INT_FAST16_MIN);
168verify (TYPE_MAXIMUM (int_fast16_t) == INT_FAST16_MAX);
169verify_same_types (INT_FAST16_MIN, (int_fast16_t) 0 + 0);
170verify_same_types (INT_FAST16_MAX, (int_fast16_t) 0 + 0);
171
172int_fast32_t e3[3] = { 17, INT_FAST32_MIN, INT_FAST32_MAX };
173verify (TYPE_MINIMUM (int_fast32_t) == INT_FAST32_MIN);
174verify (TYPE_MAXIMUM (int_fast32_t) == INT_FAST32_MAX);
175verify_same_types (INT_FAST32_MIN, (int_fast32_t) 0 + 0);
176verify_same_types (INT_FAST32_MAX, (int_fast32_t) 0 + 0);
177
178#ifdef INT_FAST64_MAX
179int_fast64_t e4[3] = { 17, INT_FAST64_MIN, INT_FAST64_MAX };
180verify (TYPE_MINIMUM (int_fast64_t) == INT_FAST64_MIN);
181verify (TYPE_MAXIMUM (int_fast64_t) == INT_FAST64_MAX);
182verify_same_types (INT_FAST64_MIN, (int_fast64_t) 0 + 0);
183verify_same_types (INT_FAST64_MAX, (int_fast64_t) 0 + 0);
184#endif
185
186uint_fast8_t f1[2] = { 17, UINT_FAST8_MAX };
187verify (TYPE_MAXIMUM (uint_fast8_t) == UINT_FAST8_MAX);
188verify_same_types (UINT_FAST8_MAX, (uint_fast8_t) 0 + 0);
189
190uint_fast16_t f2[2] = { 17, UINT_FAST16_MAX };
191verify (TYPE_MAXIMUM (uint_fast16_t) == UINT_FAST16_MAX);
192verify_same_types (UINT_FAST16_MAX, (uint_fast16_t) 0 + 0);
193
194uint_fast32_t f3[2] = { 17, UINT_FAST32_MAX };
195verify (TYPE_MAXIMUM (uint_fast32_t) == UINT_FAST32_MAX);
196verify_same_types (UINT_FAST32_MAX, (uint_fast32_t) 0 + 0);
197
198#ifdef UINT_FAST64_MAX
199uint_fast64_t f4[2] = { 17, UINT_FAST64_MAX };
200verify (TYPE_MAXIMUM (uint_fast64_t) == UINT_FAST64_MAX);
201verify_same_types (UINT_FAST64_MAX, (uint_fast64_t) 0 + 0);
202#endif
203
204#if INT_FAST8_MIN && INT_FAST8_MAX && INT_FAST16_MIN && INT_FAST16_MAX && INT_FAST32_MIN && INT_FAST32_MAX
205/* ok */
206#else
207err or;
208#endif
209
210#if UINT_FAST8_MAX && UINT_FAST16_MAX && UINT_FAST32_MAX
211/* ok */
212#else
213err or;
214#endif
215
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 */
218
219intptr_t g[3] = { 17, INTPTR_MIN, INTPTR_MAX };
220verify (sizeof (void *) <= sizeof (intptr_t));
221verify (TYPE_MINIMUM (intptr_t) == INTPTR_MIN);
222verify (TYPE_MAXIMUM (intptr_t) == INTPTR_MAX);
223verify_same_types (INTPTR_MIN, (intptr_t) 0 + 0);
224verify_same_types (INTPTR_MAX, (intptr_t) 0 + 0);
225
226uintptr_t h[2] = { 17, UINTPTR_MAX };
227verify (sizeof (void *) <= sizeof (uintptr_t));
228verify (TYPE_MAXIMUM (uintptr_t) == UINTPTR_MAX);
229verify_same_types (UINTPTR_MAX, (uintptr_t) 0 + 0);
230
231#if INTPTR_MIN && INTPTR_MAX && UINTPTR_MAX
232/* ok */
233#else
234err or;
235#endif
236
237/* 7.18.1.5. Greatest-width integer types */
238/* 7.18.2.5. Limits of greatest-width integer types */
239
240intmax_t i[3] = { INTMAX_C (17), INTMAX_MIN, INTMAX_MAX };
241verify (TYPE_MINIMUM (intmax_t) == INTMAX_MIN);
242verify (TYPE_MAXIMUM (intmax_t) == INTMAX_MAX);
243verify_same_types (INTMAX_MIN, (intmax_t) 0 + 0);
244verify_same_types (INTMAX_MAX, (intmax_t) 0 + 0);
245
246uintmax_t j[2] = { UINTMAX_C (17), UINTMAX_MAX };
247verify (TYPE_MAXIMUM (uintmax_t) == UINTMAX_MAX);
248verify_same_types (UINTMAX_MAX, (uintmax_t) 0 + 0);
249
250/* Older Sun C and HP-UX 10.20 cc don't support 'long long' constants in
251 the preprocessor. */
252#if !((defined __SUNPRO_C && __SUNPRO_C < 0x5150) \
253 || (defined __hpux && !defined __GNUC__))
254#if INTMAX_MIN && INTMAX_MAX && UINTMAX_MAX
255/* ok */
256#else
257err or;
258#endif
259#endif
260
261/* 7.18.3. Limits of other integer types */
262
263#include <stddef.h>
264
265verify (TYPE_MINIMUM (ptrdiff_t) == PTRDIFF_MIN);
266verify (TYPE_MAXIMUM (ptrdiff_t) == PTRDIFF_MAX);
267verify_same_types (PTRDIFF_MIN, (ptrdiff_t) 0 + 0);
268verify_same_types (PTRDIFF_MAX, (ptrdiff_t) 0 + 0);
269
270#if PTRDIFF_MIN && PTRDIFF_MAX
271/* ok */
272#else
273err or;
274#endif
275
276#include <signal.h>
277
278verify (TYPE_MINIMUM (sig_atomic_t) == SIG_ATOMIC_MIN);
279verify (TYPE_MAXIMUM (sig_atomic_t) == SIG_ATOMIC_MAX);
280verify_same_types (SIG_ATOMIC_MIN, (sig_atomic_t) 0 + 0);
281verify_same_types (SIG_ATOMIC_MAX, (sig_atomic_t) 0 + 0);
282
283#if SIG_ATOMIC_MIN != 17 && SIG_ATOMIC_MAX
284/* ok */
285#else
286err or;
287#endif
288
289verify (TYPE_MAXIMUM (size_t) == SIZE_MAX);
290verify_same_types (SIZE_MAX, (size_t) 0 + 0);
291
292#if SIZE_MAX
293/* ok */
294#else
295err or;
296#endif
297
298#if HAVE_WCHAR_T
299verify (TYPE_MINIMUM (wchar_t) == WCHAR_MIN);
300verify (TYPE_MAXIMUM (wchar_t) == WCHAR_MAX);
301verify_same_types (WCHAR_MIN, (wchar_t) 0 + 0);
302verify_same_types (WCHAR_MAX, (wchar_t) 0 + 0);
303
304# if WCHAR_MIN != 17 && WCHAR_MAX
305/* ok */
306# else
307err or;
308# endif
309#endif
310
311#if HAVE_WINT_T
312# include <wchar.h>
313
314verify (TYPE_MINIMUM (wint_t) == WINT_MIN);
315verify (TYPE_MAXIMUM (wint_t) == WINT_MAX);
316verify_same_types (WINT_MIN, (wint_t) 0 + 0);
317verify_same_types (WINT_MAX, (wint_t) 0 + 0);
318
319# if WINT_MIN != 17 && WINT_MAX
320/* ok */
321# else
322err or;
323# endif
324#endif
325
326/* 7.18.4. Macros for integer constants */
327
328verify (INT8_C (17) == 17);
329verify_same_types (INT8_C (17), (int_least8_t)0 + 0);
330verify (UINT8_C (17) == 17);
331verify_same_types (UINT8_C (17), (uint_least8_t)0 + 0);
332
333verify (INT16_C (17) == 17);
334verify_same_types (INT16_C (17), (int_least16_t)0 + 0);
335verify (UINT16_C (17) == 17);
336verify_same_types (UINT16_C (17), (uint_least16_t)0 + 0);
337
338verify (INT32_C (17) == 17);
339verify_same_types (INT32_C (17), (int_least32_t)0 + 0);
340verify (UINT32_C (17) == 17);
341verify_same_types (UINT32_C (17), (uint_least32_t)0 + 0);
342
343#ifdef INT64_C
344verify (INT64_C (17) == 17);
345verify_same_types (INT64_C (17), (int_least64_t)0 + 0);
346#endif
347#ifdef UINT64_C
348verify (UINT64_C (17) == 17);
349verify_same_types (UINT64_C (17), (uint_least64_t)0 + 0);
350#endif
351
352verify (INTMAX_C (17) == 17);
353verify_same_types (INTMAX_C (17), (intmax_t)0 + 0);
354verify (UINTMAX_C (17) == 17);
355verify_same_types (UINTMAX_C (17), (uintmax_t)0 + 0);
356
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:
360
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]
364 */
365#define verify_width(width, min, max) \
366 _GL_VERIFY ((max) >> ((width) - 1 - ((min) < 0)) == 1, \
367 "verify_width check", -)
368
369/* Macros specified by ISO/IEC TS 18661-1:2014. */
370
371#ifdef INT8_MAX
372verify_width (INT8_WIDTH, INT8_MIN, INT8_MAX);
373#endif
374#ifdef UINT8_MAX
375verify_width (UINT8_WIDTH, 0, UINT8_MAX);
376#endif
377#ifdef INT16_MAX
378verify_width (INT16_WIDTH, INT16_MIN, INT16_MAX);
379#endif
380#ifdef UINT16_MAX
381verify_width (UINT16_WIDTH, 0, UINT16_MAX);
382#endif
383#ifdef INT32_MAX
384verify_width (INT32_WIDTH, INT32_MIN, INT32_MAX);
385#endif
386#ifdef UINT32_MAX
387verify_width (UINT32_WIDTH, 0, UINT32_MAX);
388#endif
389#ifdef INT64_MAX
390verify_width (INT64_WIDTH, INT64_MIN, INT64_MAX);
391#endif
392#ifdef UINT64_MAX
393verify_width (UINT64_WIDTH, 0, UINT64_MAX);
394#endif
395verify_width (INT_LEAST8_WIDTH, INT_LEAST8_MIN, INT_LEAST8_MAX);
396verify_width (UINT_LEAST8_WIDTH, 0, UINT_LEAST8_MAX);
397verify_width (INT_LEAST16_WIDTH, INT_LEAST16_MIN, INT_LEAST16_MAX);
398verify_width (UINT_LEAST16_WIDTH, 0, UINT_LEAST16_MAX);
399verify_width (INT_LEAST32_WIDTH, INT_LEAST32_MIN, INT_LEAST32_MAX);
400verify_width (UINT_LEAST32_WIDTH, 0, UINT_LEAST32_MAX);
401verify_width (INT_LEAST64_WIDTH, INT_LEAST64_MIN, INT_LEAST64_MAX);
402verify_width (UINT_LEAST64_WIDTH, 0, UINT_LEAST64_MAX);
403verify_width (INT_FAST8_WIDTH, INT_FAST8_MIN, INT_FAST8_MAX);
404verify_width (UINT_FAST8_WIDTH, 0, UINT_FAST8_MAX);
405verify_width (INT_FAST16_WIDTH, INT_FAST16_MIN, INT_FAST16_MAX);
406verify_width (UINT_FAST16_WIDTH, 0, UINT_FAST16_MAX);
407verify_width (INT_FAST32_WIDTH, INT_FAST32_MIN, INT_FAST32_MAX);
408verify_width (UINT_FAST32_WIDTH, 0, UINT_FAST32_MAX);
409verify_width (INT_FAST64_WIDTH, INT_FAST64_MIN, INT_FAST64_MAX);
410verify_width (UINT_FAST64_WIDTH, 0, UINT_FAST64_MAX);
411verify_width (INTPTR_WIDTH, INTPTR_MIN, INTPTR_MAX);
412verify_width (UINTPTR_WIDTH, 0, UINTPTR_MAX);
413verify_width (INTMAX_WIDTH, INTMAX_MIN, INTMAX_MAX);
414verify_width (UINTMAX_WIDTH, 0, UINTMAX_MAX);
415verify_width (PTRDIFF_WIDTH, PTRDIFF_MIN, PTRDIFF_MAX);
416verify_width (SIZE_WIDTH, 0, SIZE_MAX);
417verify_width (WCHAR_WIDTH, WCHAR_MIN, WCHAR_MAX);
418#ifdef WINT_MAX
419verify_width (WINT_WIDTH, WINT_MIN, WINT_MAX);
420#endif
421#ifdef SIG_ATOMIC_MAX
422verify_width (SIG_ATOMIC_WIDTH, SIG_ATOMIC_MIN, SIG_ATOMIC_MAX);
423#endif
424
425int
426main (void)
427{
428 return 0;
429}
Note: See TracBrowser for help on using the repository browser.