Rebase.
[official-gcc.git] / gcc / testsuite / gcc.dg / c99-stdint-1.c
blob530d13025e7998f7fc417a536d8f89ef23ad1bea
1 /* Verify that <stdint.h> is present and follows the C99 requirements.
2 If this test fails because of the header being missing on a
3 particular target, this indicates GCC has not been correctly
4 configured regarding what version of <stdint.h> to install or what
5 the <stdint.h> types are on that target. If GCC is wrapping a
6 system copy of the header and some tests fail because of bugs in
7 that copy, they should be fixed with fixincludes (and the bugs
8 reported to maintainer of that copy if still present in the latest
9 version). */
10 /* { dg-do compile } */
11 /* { dg-options "-std=iso9899:1999 -pedantic-errors -fhosted" } */
12 /* { dg-require-effective-target ptr32plus } */
14 #include <limits.h>
15 #include <stdint.h>
16 /* This and the later SIG_ATOMIC_* tests should be appropriately
17 conditioned for any freestanding targets with no <signal.h>. */
18 #ifndef SIGNAL_SUPPRESS
19 #include <signal.h>
20 #endif
22 /* Note that some of these conditions assume two's complement and no
23 padding bits; GCC only supports two's complement, and no supported
24 target has padding bits in any integer type of the standard
25 widths. */
26 #define CHECK_SIGNED(TYPE) \
27 do { TYPE a; int b[(TYPE)-1 < 0 ? 1 : -1]; } while (0)
28 #define CHECK_UNSIGNED(TYPE) \
29 do { TYPE a; int b[(TYPE)-1 < 0 ? -1 : 1]; } while (0)
30 #define CHECK_WIDTH_EQUALS(TYPE, WIDTH) \
31 do { int a[sizeof(TYPE) * CHAR_BIT == (WIDTH) ? 1 : -1]; } while (0)
32 #define CHECK_WIDTH_AT_LEAST(TYPE, WIDTH) \
33 do { int a[sizeof(TYPE) * CHAR_BIT >= (WIDTH) ? 1 : -1]; } while (0)
34 #define CHECK_WIDTH_ORDER(TYPE1, TYPE2) \
35 do { int a[sizeof(TYPE2) >= sizeof(TYPE1) ? 1 : -1]; } while (0)
36 #define CHECK_EXPR_TYPE(TYPE, EXPR) \
37 do { __typeof__(EXPR) a; __typeof__((TYPE)0 + 0) *b = &a; } while (0)
38 #define UNSIGNED_MAX_COND(TYPE, EXPR) \
39 ((EXPR) == (TYPE)-1)
40 #define SIGNED_MIN_MAX_COND(TYPE, MIN, MAX) \
41 ((MIN) == -(MAX)-1 \
42 && ((MAX) & 1) \
43 && ((((MAX) >> 1) + 1) >> (sizeof(TYPE) * CHAR_BIT - 2)) == 1)
44 #define MIN_MAX_COND(TYPE, MIN, MAX) \
45 ((TYPE)-1 < 0 \
46 ? SIGNED_MIN_MAX_COND(TYPE, (MIN), (MAX)) \
47 : ((MIN) == 0 && UNSIGNED_MAX_COND(TYPE, (MAX))))
48 #define CHECK_SIGNED_LIMITS(TYPE, MIN, MAX) \
49 CHECK_SIGNED(TYPE); \
50 CHECK_EXPR_TYPE(TYPE, (MIN)); \
51 CHECK_EXPR_TYPE(TYPE, (MAX)); \
52 do { int a[SIGNED_MIN_MAX_COND(TYPE, (MIN), (MAX)) ? 1 : -1]; } while (0)
53 #define CHECK_SIGNED_LIMITS_2(TYPE, MIN, MAX, MINBD, MAXBD) \
54 CHECK_SIGNED(TYPE); \
55 CHECK_EXPR_TYPE(TYPE, (MIN)); \
56 CHECK_EXPR_TYPE(TYPE, (MAX)); \
57 do { int a[(SIGNED_MIN_MAX_COND(TYPE, (MIN), (MAX)) \
58 && (MIN) <= (MINBD) \
59 && (MAX) >= (MAXBD)) ? 1 : -1]; } while (0)
60 #define CHECK_UNSIGNED_LIMITS(TYPE, MAX) \
61 CHECK_UNSIGNED(TYPE); \
62 CHECK_EXPR_TYPE(TYPE, (MAX)); \
63 do { int a[UNSIGNED_MAX_COND(TYPE, (MAX)) ? 1 : -1]; } while (0)
64 #define CHECK_UNSIGNED_LIMITS_2(TYPE, MAX, MAXBD) \
65 CHECK_UNSIGNED(TYPE); \
66 CHECK_EXPR_TYPE(TYPE, (MAX)); \
67 do { int a[(UNSIGNED_MAX_COND(TYPE, (MAX)) \
68 && (MAX) >= (MAXBD)) ? 1 : -1]; } while (0)
69 #define CHECK_LIMITS_2(TYPE, MIN, MAX, SMINBD, SMAXBD, UMAXBD) \
70 do { int a[(MIN_MAX_COND(TYPE, (MIN), (MAX)) \
71 && ((TYPE)-1 < 0 \
72 ? ((MIN) <= (SMINBD) && (MAX) >= (SMAXBD)) \
73 : (MAX) >= (UMAXBD))) ? 1 : -1]; } while (0)
74 #define CHECK_CONSTS(TYPE, MACRO) \
75 CHECK_EXPR_TYPE(TYPE, MACRO(01)); \
76 CHECK_EXPR_TYPE(TYPE, MACRO(2)); \
77 CHECK_EXPR_TYPE(TYPE, MACRO(0x3)); \
78 do { int a[(MACRO(12) == 12 \
79 && MACRO(012) == 012 \
80 && MACRO(0x12) == 0x12) ? 1 : -1]; } while (0);
82 void
83 test_exact (void)
85 #ifdef INT8_MIN
86 CHECK_WIDTH_EQUALS(int8_t, 8);
87 CHECK_SIGNED_LIMITS(int8_t, INT8_MIN, INT8_MAX);
88 #else
89 CHECK_WIDTH_AT_LEAST(int_least8_t, 9);
90 #endif
91 #ifdef INT16_MIN
92 CHECK_WIDTH_EQUALS(int16_t, 16);
93 CHECK_SIGNED_LIMITS(int16_t, INT16_MIN, INT16_MAX);
94 #else
95 CHECK_WIDTH_AT_LEAST(int_least16_t, 17);
96 #endif
97 #ifdef INT32_MIN
98 CHECK_WIDTH_EQUALS(int32_t, 32);
99 CHECK_SIGNED_LIMITS(int32_t, INT32_MIN, INT32_MAX);
100 #else
101 CHECK_WIDTH_AT_LEAST(int_least32_t, 33);
102 #endif
103 #ifdef INT64_MIN
104 CHECK_WIDTH_EQUALS(int64_t, 64);
105 CHECK_SIGNED_LIMITS(int64_t, INT64_MIN, INT64_MAX);
106 #else
107 CHECK_WIDTH_AT_LEAST(int_least64_t, 65);
108 #endif
109 #ifdef UINT8_MAX
110 CHECK_WIDTH_EQUALS(uint8_t, 8);
111 CHECK_UNSIGNED_LIMITS(uint8_t, UINT8_MAX);
112 #else
113 CHECK_WIDTH_AT_LEAST(uint_least8_t, 9);
114 #endif
115 #ifdef UINT16_MAX
116 CHECK_WIDTH_EQUALS(uint16_t, 16);
117 CHECK_UNSIGNED_LIMITS(uint16_t, UINT16_MAX);
118 #else
119 CHECK_WIDTH_AT_LEAST(uint_least16_t, 17);
120 #endif
121 #ifdef UINT32_MAX
122 CHECK_WIDTH_EQUALS(uint32_t, 32);
123 CHECK_UNSIGNED_LIMITS(uint32_t, UINT32_MAX);
124 #else
125 CHECK_WIDTH_AT_LEAST(uint_least32_t, 33);
126 #endif
127 #ifdef UINT64_MAX
128 CHECK_WIDTH_EQUALS(uint64_t, 64);
129 CHECK_UNSIGNED_LIMITS(uint64_t, UINT64_MAX);
130 #else
131 CHECK_WIDTH_AT_LEAST(uint_least64_t, 65);
132 #endif
135 void
136 test_least (void)
138 CHECK_WIDTH_AT_LEAST(int_least8_t, 8);
139 CHECK_WIDTH_ORDER(int_least8_t, int_fast8_t);
140 CHECK_SIGNED_LIMITS(int_least8_t, INT_LEAST8_MIN, INT_LEAST8_MAX);
141 CHECK_WIDTH_AT_LEAST(int_least16_t, 16);
142 CHECK_WIDTH_ORDER(int_least16_t, int_fast16_t);
143 CHECK_SIGNED_LIMITS(int_least16_t, INT_LEAST16_MIN, INT_LEAST16_MAX);
144 CHECK_WIDTH_AT_LEAST(int_least32_t, 32);
145 CHECK_WIDTH_ORDER(int_least32_t, int_fast32_t);
146 CHECK_SIGNED_LIMITS(int_least32_t, INT_LEAST32_MIN, INT_LEAST32_MAX);
147 CHECK_WIDTH_AT_LEAST(int_least64_t, 64);
148 CHECK_WIDTH_ORDER(int_least64_t, int_fast64_t);
149 CHECK_SIGNED_LIMITS(int_least64_t, INT_LEAST64_MIN, INT_LEAST64_MAX);
150 CHECK_WIDTH_AT_LEAST(uint_least8_t, 8);
151 CHECK_WIDTH_ORDER(uint_least8_t, uint_fast8_t);
152 CHECK_UNSIGNED_LIMITS(uint_least8_t, UINT_LEAST8_MAX);
153 CHECK_WIDTH_AT_LEAST(uint_least16_t, 16);
154 CHECK_WIDTH_ORDER(uint_least16_t, uint_fast16_t);
155 CHECK_UNSIGNED_LIMITS(uint_least16_t, UINT_LEAST16_MAX);
156 CHECK_WIDTH_AT_LEAST(uint_least32_t, 32);
157 CHECK_WIDTH_ORDER(uint_least32_t, uint_fast32_t);
158 CHECK_UNSIGNED_LIMITS(uint_least32_t, UINT_LEAST32_MAX);
159 CHECK_WIDTH_AT_LEAST(uint_least64_t, 64);
160 CHECK_WIDTH_ORDER(uint_least64_t, uint_fast64_t);
161 CHECK_UNSIGNED_LIMITS(uint_least64_t, UINT_LEAST64_MAX);
164 void
165 test_fast (void)
167 CHECK_WIDTH_AT_LEAST(int_fast8_t, 8);
168 CHECK_SIGNED_LIMITS(int_fast8_t, INT_FAST8_MIN, INT_FAST8_MAX);
169 CHECK_WIDTH_AT_LEAST(int_fast16_t, 16);
170 CHECK_SIGNED_LIMITS(int_fast16_t, INT_FAST16_MIN, INT_FAST16_MAX);
171 CHECK_WIDTH_AT_LEAST(int_fast32_t, 32);
172 CHECK_SIGNED_LIMITS(int_fast32_t, INT_FAST32_MIN, INT_FAST32_MAX);
173 CHECK_WIDTH_AT_LEAST(int_fast64_t, 64);
174 CHECK_SIGNED_LIMITS(int_fast64_t, INT_FAST64_MIN, INT_FAST64_MAX);
175 CHECK_WIDTH_AT_LEAST(uint_fast8_t, 8);
176 CHECK_UNSIGNED_LIMITS(uint_fast8_t, UINT_FAST8_MAX);
177 CHECK_WIDTH_AT_LEAST(uint_fast16_t, 16);
178 CHECK_UNSIGNED_LIMITS(uint_fast16_t, UINT_FAST16_MAX);
179 CHECK_WIDTH_AT_LEAST(uint_fast32_t, 32);
180 CHECK_UNSIGNED_LIMITS(uint_fast32_t, UINT_FAST32_MAX);
181 CHECK_WIDTH_AT_LEAST(uint_fast64_t, 64);
182 CHECK_UNSIGNED_LIMITS(uint_fast64_t, UINT_FAST64_MAX);
185 void
186 test_ptr (void)
188 #ifdef INTPTR_MIN
189 CHECK_SIGNED_LIMITS_2(intptr_t, INTPTR_MIN, INTPTR_MAX, -0x7fff, 0x7fff);
190 #endif
191 #ifdef UINTPTR_MAX
192 CHECK_UNSIGNED_LIMITS_2(uintptr_t, UINTPTR_MAX, 0xffffU);
193 #endif
196 void
197 test_max (void)
199 CHECK_WIDTH_AT_LEAST(intmax_t, 64);
200 CHECK_WIDTH_ORDER(long long, intmax_t);
201 CHECK_WIDTH_ORDER(int_fast8_t, intmax_t);
202 CHECK_WIDTH_ORDER(int_fast16_t, intmax_t);
203 CHECK_WIDTH_ORDER(int_fast32_t, intmax_t);
204 CHECK_WIDTH_ORDER(int_fast64_t, intmax_t);
205 CHECK_SIGNED_LIMITS(intmax_t, INTMAX_MIN, INTMAX_MAX);
206 CHECK_WIDTH_AT_LEAST(uintmax_t, 64);
207 CHECK_WIDTH_ORDER(unsigned long long, uintmax_t);
208 CHECK_WIDTH_ORDER(uint_fast8_t, uintmax_t);
209 CHECK_WIDTH_ORDER(uint_fast16_t, uintmax_t);
210 CHECK_WIDTH_ORDER(uint_fast32_t, uintmax_t);
211 CHECK_WIDTH_ORDER(uint_fast64_t, uintmax_t);
212 CHECK_UNSIGNED_LIMITS(uintmax_t, UINTMAX_MAX);
215 void
216 test_misc_limits (void)
218 CHECK_SIGNED_LIMITS_2(__PTRDIFF_TYPE__, PTRDIFF_MIN, PTRDIFF_MAX, -65535L, 65535L);
219 #ifndef SIGNAL_SUPPRESS
220 CHECK_LIMITS_2(sig_atomic_t, SIG_ATOMIC_MIN, SIG_ATOMIC_MAX, -127, 127, 255);
221 #endif
222 CHECK_UNSIGNED_LIMITS_2(__SIZE_TYPE__, SIZE_MAX, 65535U);
223 CHECK_LIMITS_2(__WCHAR_TYPE__, WCHAR_MIN, WCHAR_MAX, -127, 127, 255);
224 CHECK_LIMITS_2(__WINT_TYPE__, WINT_MIN, WINT_MAX, -32767, 32767, 65535);
227 void
228 test_constants (void)
230 CHECK_CONSTS(int_least8_t, INT8_C);
231 CHECK_CONSTS(int_least16_t, INT16_C);
232 CHECK_CONSTS(int_least32_t, INT32_C);
233 CHECK_CONSTS(int_least64_t, INT64_C);
234 CHECK_CONSTS(intmax_t, INTMAX_C);
235 CHECK_CONSTS(uint_least8_t, UINT8_C);
236 CHECK_CONSTS(uint_least16_t, UINT16_C);
237 CHECK_CONSTS(uint_least32_t, UINT32_C);
238 CHECK_CONSTS(uint_least64_t, UINT64_C);
239 CHECK_CONSTS(uintmax_t, UINTMAX_C);
240 #if INT8_C(12) != 12
241 #error "INT8_C not usable in #if"
242 #endif
243 #if INT16_C(12) != 12
244 #error "INT16_C not usable in #if"
245 #endif
246 #if INT32_C(12) != 12
247 #error "INT32_C not usable in #if"
248 #endif
249 #if INT64_C(12) != 12
250 #error "INT64_C not usable in #if"
251 #endif
252 #if INTMAX_C(12) != 12
253 #error "INTMAX_C not usable in #if"
254 #endif
255 #if UINT8_C(12) != 12
256 #error "UINT8_C not usable in #if"
257 #endif
258 #if UINT16_C(12) != 12
259 #error "UINT16_C not usable in #if"
260 #endif
261 #if UINT32_C(12) != 12
262 #error "UINT32_C not usable in #if"
263 #endif
264 #if UINT64_C(12) != 12
265 #error "UINT64_C not usable in #if"
266 #endif
267 #if UINTMAX_C(12) != 12
268 #error "UINTMAX_C not usable in #if"
269 #endif