VirtualBox

source: kBuild/vendor/grep/3.7/gnulib-tests/test-stdint.c@ 3529

Last change on this file since 3529 was 3529, checked in by bird, 3 years ago

Imported grep 3.7 from grep-3.7.tar.gz (sha256: c22b0cf2d4f6bbe599c902387e8058990e1eee99aef333a203829e5fd3dbb342), applying minimal auto-props.

  • Property svn:eol-style set to native
File size: 13.8 KB
Line 
1/* Test of <stdint.h> substitute.
2 Copyright (C) 2006-2021 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 <[email protected]>, 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/* As of 2007, Sun C and HP-UX 10.20 cc don't support 'long long' constants in
251 the preprocessor. */
252#if !(defined __SUNPRO_C || (defined __hpux && !defined __GNUC__))
253#if INTMAX_MIN && INTMAX_MAX && UINTMAX_MAX
254/* ok */
255#else
256err or;
257#endif
258#endif
259
260/* 7.18.3. Limits of other integer types */
261
262#include <stddef.h>
263
264verify (TYPE_MINIMUM (ptrdiff_t) == PTRDIFF_MIN);
265verify (TYPE_MAXIMUM (ptrdiff_t) == PTRDIFF_MAX);
266verify_same_types (PTRDIFF_MIN, (ptrdiff_t) 0 + 0);
267verify_same_types (PTRDIFF_MAX, (ptrdiff_t) 0 + 0);
268
269#if PTRDIFF_MIN && PTRDIFF_MAX
270/* ok */
271#else
272err or;
273#endif
274
275#include <signal.h>
276
277verify (TYPE_MINIMUM (sig_atomic_t) == SIG_ATOMIC_MIN);
278verify (TYPE_MAXIMUM (sig_atomic_t) == SIG_ATOMIC_MAX);
279verify_same_types (SIG_ATOMIC_MIN, (sig_atomic_t) 0 + 0);
280verify_same_types (SIG_ATOMIC_MAX, (sig_atomic_t) 0 + 0);
281
282#if SIG_ATOMIC_MIN != 17 && SIG_ATOMIC_MAX
283/* ok */
284#else
285err or;
286#endif
287
288verify (TYPE_MAXIMUM (size_t) == SIZE_MAX);
289verify_same_types (SIZE_MAX, (size_t) 0 + 0);
290
291#if SIZE_MAX
292/* ok */
293#else
294err or;
295#endif
296
297#if HAVE_WCHAR_T
298verify (TYPE_MINIMUM (wchar_t) == WCHAR_MIN);
299verify (TYPE_MAXIMUM (wchar_t) == WCHAR_MAX);
300verify_same_types (WCHAR_MIN, (wchar_t) 0 + 0);
301verify_same_types (WCHAR_MAX, (wchar_t) 0 + 0);
302
303# if WCHAR_MIN != 17 && WCHAR_MAX
304/* ok */
305# else
306err or;
307# endif
308#endif
309
310#if HAVE_WINT_T
311# include <wchar.h>
312
313verify (TYPE_MINIMUM (wint_t) == WINT_MIN);
314verify (TYPE_MAXIMUM (wint_t) == WINT_MAX);
315verify_same_types (WINT_MIN, (wint_t) 0 + 0);
316verify_same_types (WINT_MAX, (wint_t) 0 + 0);
317
318# if WINT_MIN != 17 && WINT_MAX
319/* ok */
320# else
321err or;
322# endif
323#endif
324
325/* 7.18.4. Macros for integer constants */
326
327verify (INT8_C (17) == 17);
328verify_same_types (INT8_C (17), (int_least8_t)0 + 0);
329verify (UINT8_C (17) == 17);
330verify_same_types (UINT8_C (17), (uint_least8_t)0 + 0);
331
332verify (INT16_C (17) == 17);
333verify_same_types (INT16_C (17), (int_least16_t)0 + 0);
334verify (UINT16_C (17) == 17);
335verify_same_types (UINT16_C (17), (uint_least16_t)0 + 0);
336
337verify (INT32_C (17) == 17);
338verify_same_types (INT32_C (17), (int_least32_t)0 + 0);
339verify (UINT32_C (17) == 17);
340verify_same_types (UINT32_C (17), (uint_least32_t)0 + 0);
341
342#ifdef INT64_C
343verify (INT64_C (17) == 17);
344verify_same_types (INT64_C (17), (int_least64_t)0 + 0);
345#endif
346#ifdef UINT64_C
347verify (UINT64_C (17) == 17);
348verify_same_types (UINT64_C (17), (uint_least64_t)0 + 0);
349#endif
350
351verify (INTMAX_C (17) == 17);
352verify_same_types (INTMAX_C (17), (intmax_t)0 + 0);
353verify (UINTMAX_C (17) == 17);
354verify_same_types (UINTMAX_C (17), (uintmax_t)0 + 0);
355
356/* Use _GL_VERIFY (with a fixed-length diagnostic string) rather than verify,
357 because the latter would require forming each stringified expression, and
358 many of these would be so long as to trigger a warning/error like this:
359
360 test-stdint.c:407:1: error: string length '6980' is greater than the \
361 length '4095' ISO C99 compilers are required to support \
362 [-Werror=overlength-strings]
363 */
364#define verify_width(width, min, max) \
365 _GL_VERIFY ((max) >> ((width) - 1 - ((min) < 0)) == 1, \
366 "verify_width check", -)
367
368/* Macros specified by ISO/IEC TS 18661-1:2014. */
369
370#ifdef INT8_MAX
371verify_width (INT8_WIDTH, INT8_MIN, INT8_MAX);
372#endif
373#ifdef UINT8_MAX
374verify_width (UINT8_WIDTH, 0, UINT8_MAX);
375#endif
376#ifdef INT16_MAX
377verify_width (INT16_WIDTH, INT16_MIN, INT16_MAX);
378#endif
379#ifdef UINT16_MAX
380verify_width (UINT16_WIDTH, 0, UINT16_MAX);
381#endif
382#ifdef INT32_MAX
383verify_width (INT32_WIDTH, INT32_MIN, INT32_MAX);
384#endif
385#ifdef UINT32_MAX
386verify_width (UINT32_WIDTH, 0, UINT32_MAX);
387#endif
388#ifdef INT64_MAX
389verify_width (INT64_WIDTH, INT64_MIN, INT64_MAX);
390#endif
391#ifdef UINT64_MAX
392verify_width (UINT64_WIDTH, 0, UINT64_MAX);
393#endif
394verify_width (INT_LEAST8_WIDTH, INT_LEAST8_MIN, INT_LEAST8_MAX);
395verify_width (UINT_LEAST8_WIDTH, 0, UINT_LEAST8_MAX);
396verify_width (INT_LEAST16_WIDTH, INT_LEAST16_MIN, INT_LEAST16_MAX);
397verify_width (UINT_LEAST16_WIDTH, 0, UINT_LEAST16_MAX);
398verify_width (INT_LEAST32_WIDTH, INT_LEAST32_MIN, INT_LEAST32_MAX);
399verify_width (UINT_LEAST32_WIDTH, 0, UINT_LEAST32_MAX);
400verify_width (INT_LEAST64_WIDTH, INT_LEAST64_MIN, INT_LEAST64_MAX);
401verify_width (UINT_LEAST64_WIDTH, 0, UINT_LEAST64_MAX);
402verify_width (INT_FAST8_WIDTH, INT_FAST8_MIN, INT_FAST8_MAX);
403verify_width (UINT_FAST8_WIDTH, 0, UINT_FAST8_MAX);
404verify_width (INT_FAST16_WIDTH, INT_FAST16_MIN, INT_FAST16_MAX);
405verify_width (UINT_FAST16_WIDTH, 0, UINT_FAST16_MAX);
406verify_width (INT_FAST32_WIDTH, INT_FAST32_MIN, INT_FAST32_MAX);
407verify_width (UINT_FAST32_WIDTH, 0, UINT_FAST32_MAX);
408verify_width (INT_FAST64_WIDTH, INT_FAST64_MIN, INT_FAST64_MAX);
409verify_width (UINT_FAST64_WIDTH, 0, UINT_FAST64_MAX);
410verify_width (INTPTR_WIDTH, INTPTR_MIN, INTPTR_MAX);
411verify_width (UINTPTR_WIDTH, 0, UINTPTR_MAX);
412verify_width (INTMAX_WIDTH, INTMAX_MIN, INTMAX_MAX);
413verify_width (UINTMAX_WIDTH, 0, UINTMAX_MAX);
414verify_width (PTRDIFF_WIDTH, PTRDIFF_MIN, PTRDIFF_MAX);
415verify_width (SIZE_WIDTH, 0, SIZE_MAX);
416verify_width (WCHAR_WIDTH, WCHAR_MIN, WCHAR_MAX);
417#ifdef WINT_MAX
418verify_width (WINT_WIDTH, WINT_MIN, WINT_MAX);
419#endif
420#ifdef SIG_ATOMIC_MAX
421verify_width (SIG_ATOMIC_WIDTH, SIG_ATOMIC_MIN, SIG_ATOMIC_MAX);
422#endif
423
424int
425main (void)
426{
427 return 0;
428}
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette