Add new test to verify that the array index, limit, and stride are
[official-gcc.git] / gcc / testsuite / gcc.dg / c99-stdint-1.c
blob37e1203f0aeaad44ce3b928063fd151d3e3b9462
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" } */
13 #include <limits.h>
14 #include <stdint.h>
15 /* This and the later SIG_ATOMIC_* tests should be appropriately
16 conditioned for any freestanding targets with no <signal.h>. */
17 #ifndef SIGNAL_SUPPRESS
18 #include <signal.h>
19 #endif
21 /* Note that some of these conditions assume two's complement and no
22 padding bits; GCC only supports two's complement, and no supported
23 target has padding bits in any integer type of the standard
24 widths. */
25 #define CHECK_SIGNED(TYPE) \
26 do { TYPE a; int b[(TYPE)-1 < 0 ? 1 : -1]; } while (0)
27 #define CHECK_UNSIGNED(TYPE) \
28 do { TYPE a; int b[(TYPE)-1 < 0 ? -1 : 1]; } while (0)
29 #define CHECK_WIDTH_EQUALS(TYPE, WIDTH) \
30 do { int a[sizeof(TYPE) * CHAR_BIT == (WIDTH) ? 1 : -1]; } while (0)
31 #define CHECK_WIDTH_AT_LEAST(TYPE, WIDTH) \
32 do { int a[sizeof(TYPE) * CHAR_BIT >= (WIDTH) ? 1 : -1]; } while (0)
33 #define CHECK_WIDTH_ORDER(TYPE1, TYPE2) \
34 do { int a[sizeof(TYPE2) >= sizeof(TYPE1) ? 1 : -1]; } while (0)
35 #define CHECK_EXPR_TYPE(TYPE, EXPR) \
36 do { __typeof__(EXPR) a; __typeof__((TYPE)0 + 0) *b = &a; } while (0)
37 #define UNSIGNED_MAX_COND(TYPE, EXPR) \
38 ((EXPR) == (TYPE)-1)
39 #define SIGNED_MIN_MAX_COND(TYPE, MIN, MAX) \
40 ((MIN) == -(MAX)-1 \
41 && ((MAX) & 1) \
42 && ((((MAX) >> 1) + 1) >> (sizeof(TYPE) * CHAR_BIT - 2)) == 1)
43 #define MIN_MAX_COND(TYPE, MIN, MAX) \
44 ((TYPE)-1 < 0 \
45 ? SIGNED_MIN_MAX_COND(TYPE, (MIN), (MAX)) \
46 : ((MIN) == 0 && UNSIGNED_MAX_COND(TYPE, (MAX))))
47 #define CHECK_SIGNED_LIMITS(TYPE, MIN, MAX) \
48 CHECK_SIGNED(TYPE); \
49 CHECK_EXPR_TYPE(TYPE, (MIN)); \
50 CHECK_EXPR_TYPE(TYPE, (MAX)); \
51 do { int a[SIGNED_MIN_MAX_COND(TYPE, (MIN), (MAX)) ? 1 : -1]; } while (0)
52 #define CHECK_SIGNED_LIMITS_2(TYPE, MIN, MAX, MINBD, MAXBD) \
53 CHECK_SIGNED(TYPE); \
54 CHECK_EXPR_TYPE(TYPE, (MIN)); \
55 CHECK_EXPR_TYPE(TYPE, (MAX)); \
56 do { int a[(SIGNED_MIN_MAX_COND(TYPE, (MIN), (MAX)) \
57 && (MIN) <= (MINBD) \
58 && (MAX) >= (MAXBD)) ? 1 : -1]; } while (0)
59 #define CHECK_UNSIGNED_LIMITS(TYPE, MAX) \
60 CHECK_UNSIGNED(TYPE); \
61 CHECK_EXPR_TYPE(TYPE, (MAX)); \
62 do { int a[UNSIGNED_MAX_COND(TYPE, (MAX)) ? 1 : -1]; } while (0)
63 #define CHECK_UNSIGNED_LIMITS_2(TYPE, MAX, MAXBD) \
64 CHECK_UNSIGNED(TYPE); \
65 CHECK_EXPR_TYPE(TYPE, (MAX)); \
66 do { int a[(UNSIGNED_MAX_COND(TYPE, (MAX)) \
67 && (MAX) >= (MAXBD)) ? 1 : -1]; } while (0)
68 #define CHECK_LIMITS_2(TYPE, MIN, MAX, SMINBD, SMAXBD, UMAXBD) \
69 do { int a[(MIN_MAX_COND(TYPE, (MIN), (MAX)) \
70 && ((TYPE)-1 < 0 \
71 ? ((MIN) <= (SMINBD) && (MAX) >= (SMAXBD)) \
72 : (MAX) >= (UMAXBD))) ? 1 : -1]; } while (0)
73 #define CHECK_CONSTS(TYPE, MACRO) \
74 CHECK_EXPR_TYPE(TYPE, MACRO(01)); \
75 CHECK_EXPR_TYPE(TYPE, MACRO(2)); \
76 CHECK_EXPR_TYPE(TYPE, MACRO(0x3)); \
77 do { int a[(MACRO(12) == 12 \
78 && MACRO(012) == 012 \
79 && MACRO(0x12) == 0x12) ? 1 : -1]; } while (0);
81 void
82 test_exact (void)
84 #ifdef INT8_MIN
85 CHECK_WIDTH_EQUALS(int8_t, 8);
86 CHECK_SIGNED_LIMITS(int8_t, INT8_MIN, INT8_MAX);
87 #else
88 CHECK_WIDTH_AT_LEAST(int_least8_t, 9);
89 #endif
90 #ifdef INT16_MIN
91 CHECK_WIDTH_EQUALS(int16_t, 16);
92 CHECK_SIGNED_LIMITS(int16_t, INT16_MIN, INT16_MAX);
93 #else
94 CHECK_WIDTH_AT_LEAST(int_least16_t, 17);
95 #endif
96 #ifdef INT32_MIN
97 CHECK_WIDTH_EQUALS(int32_t, 32);
98 CHECK_SIGNED_LIMITS(int32_t, INT32_MIN, INT32_MAX);
99 #else
100 CHECK_WIDTH_AT_LEAST(int_least32_t, 33);
101 #endif
102 #ifdef INT64_MIN
103 CHECK_WIDTH_EQUALS(int64_t, 64);
104 CHECK_SIGNED_LIMITS(int64_t, INT64_MIN, INT64_MAX);
105 #else
106 CHECK_WIDTH_AT_LEAST(int_least64_t, 65);
107 #endif
108 #ifdef UINT8_MAX
109 CHECK_WIDTH_EQUALS(uint8_t, 8);
110 CHECK_UNSIGNED_LIMITS(uint8_t, UINT8_MAX);
111 #else
112 CHECK_WIDTH_AT_LEAST(uint_least8_t, 9);
113 #endif
114 #ifdef UINT16_MAX
115 CHECK_WIDTH_EQUALS(uint16_t, 16);
116 CHECK_UNSIGNED_LIMITS(uint16_t, UINT16_MAX);
117 #else
118 CHECK_WIDTH_AT_LEAST(uint_least16_t, 17);
119 #endif
120 #ifdef UINT32_MAX
121 CHECK_WIDTH_EQUALS(uint32_t, 32);
122 CHECK_UNSIGNED_LIMITS(uint32_t, UINT32_MAX);
123 #else
124 CHECK_WIDTH_AT_LEAST(uint_least32_t, 33);
125 #endif
126 #ifdef UINT64_MAX
127 CHECK_WIDTH_EQUALS(uint64_t, 64);
128 CHECK_UNSIGNED_LIMITS(uint64_t, UINT64_MAX);
129 #else
130 CHECK_WIDTH_AT_LEAST(uint_least64_t, 65);
131 #endif
134 void
135 test_least (void)
137 CHECK_WIDTH_AT_LEAST(int_least8_t, 8);
138 CHECK_WIDTH_ORDER(int_least8_t, int_fast8_t);
139 CHECK_SIGNED_LIMITS(int_least8_t, INT_LEAST8_MIN, INT_LEAST8_MAX);
140 CHECK_WIDTH_AT_LEAST(int_least16_t, 16);
141 CHECK_WIDTH_ORDER(int_least16_t, int_fast16_t);
142 CHECK_SIGNED_LIMITS(int_least16_t, INT_LEAST16_MIN, INT_LEAST16_MAX);
143 CHECK_WIDTH_AT_LEAST(int_least32_t, 32);
144 CHECK_WIDTH_ORDER(int_least32_t, int_fast32_t);
145 CHECK_SIGNED_LIMITS(int_least32_t, INT_LEAST32_MIN, INT_LEAST32_MAX);
146 CHECK_WIDTH_AT_LEAST(int_least64_t, 64);
147 CHECK_WIDTH_ORDER(int_least64_t, int_fast64_t);
148 CHECK_SIGNED_LIMITS(int_least64_t, INT_LEAST64_MIN, INT_LEAST64_MAX);
149 CHECK_WIDTH_AT_LEAST(uint_least8_t, 8);
150 CHECK_WIDTH_ORDER(uint_least8_t, uint_fast8_t);
151 CHECK_UNSIGNED_LIMITS(uint_least8_t, UINT_LEAST8_MAX);
152 CHECK_WIDTH_AT_LEAST(uint_least16_t, 16);
153 CHECK_WIDTH_ORDER(uint_least16_t, uint_fast16_t);
154 CHECK_UNSIGNED_LIMITS(uint_least16_t, UINT_LEAST16_MAX);
155 CHECK_WIDTH_AT_LEAST(uint_least32_t, 32);
156 CHECK_WIDTH_ORDER(uint_least32_t, uint_fast32_t);
157 CHECK_UNSIGNED_LIMITS(uint_least32_t, UINT_LEAST32_MAX);
158 CHECK_WIDTH_AT_LEAST(uint_least64_t, 64);
159 CHECK_WIDTH_ORDER(uint_least64_t, uint_fast64_t);
160 CHECK_UNSIGNED_LIMITS(uint_least64_t, UINT_LEAST64_MAX);
163 void
164 test_fast (void)
166 CHECK_WIDTH_AT_LEAST(int_fast8_t, 8);
167 CHECK_SIGNED_LIMITS(int_fast8_t, INT_FAST8_MIN, INT_FAST8_MAX);
168 CHECK_WIDTH_AT_LEAST(int_fast16_t, 16);
169 CHECK_SIGNED_LIMITS(int_fast16_t, INT_FAST16_MIN, INT_FAST16_MAX);
170 CHECK_WIDTH_AT_LEAST(int_fast32_t, 32);
171 CHECK_SIGNED_LIMITS(int_fast32_t, INT_FAST32_MIN, INT_FAST32_MAX);
172 CHECK_WIDTH_AT_LEAST(int_fast64_t, 64);
173 CHECK_SIGNED_LIMITS(int_fast64_t, INT_FAST64_MIN, INT_FAST64_MAX);
174 CHECK_WIDTH_AT_LEAST(uint_fast8_t, 8);
175 CHECK_UNSIGNED_LIMITS(uint_fast8_t, UINT_FAST8_MAX);
176 CHECK_WIDTH_AT_LEAST(uint_fast16_t, 16);
177 CHECK_UNSIGNED_LIMITS(uint_fast16_t, UINT_FAST16_MAX);
178 CHECK_WIDTH_AT_LEAST(uint_fast32_t, 32);
179 CHECK_UNSIGNED_LIMITS(uint_fast32_t, UINT_FAST32_MAX);
180 CHECK_WIDTH_AT_LEAST(uint_fast64_t, 64);
181 CHECK_UNSIGNED_LIMITS(uint_fast64_t, UINT_FAST64_MAX);
184 void
185 test_ptr (void)
187 #ifdef INTPTR_MIN
188 CHECK_SIGNED_LIMITS_2(intptr_t, INTPTR_MIN, INTPTR_MAX, -0x7fff, 0x7fff);
189 #endif
190 #ifdef UINTPTR_MAX
191 CHECK_UNSIGNED_LIMITS_2(uintptr_t, UINTPTR_MAX, 0xffffU);
192 #endif
195 void
196 test_max (void)
198 CHECK_WIDTH_AT_LEAST(intmax_t, 64);
199 CHECK_WIDTH_ORDER(long long, intmax_t);
200 CHECK_WIDTH_ORDER(int_fast8_t, intmax_t);
201 CHECK_WIDTH_ORDER(int_fast16_t, intmax_t);
202 CHECK_WIDTH_ORDER(int_fast32_t, intmax_t);
203 CHECK_WIDTH_ORDER(int_fast64_t, intmax_t);
204 CHECK_SIGNED_LIMITS(intmax_t, INTMAX_MIN, INTMAX_MAX);
205 CHECK_WIDTH_AT_LEAST(uintmax_t, 64);
206 CHECK_WIDTH_ORDER(unsigned long long, uintmax_t);
207 CHECK_WIDTH_ORDER(uint_fast8_t, uintmax_t);
208 CHECK_WIDTH_ORDER(uint_fast16_t, uintmax_t);
209 CHECK_WIDTH_ORDER(uint_fast32_t, uintmax_t);
210 CHECK_WIDTH_ORDER(uint_fast64_t, uintmax_t);
211 CHECK_UNSIGNED_LIMITS(uintmax_t, UINTMAX_MAX);
214 void
215 test_misc_limits (void)
217 /* { dg-bogus "size" "ptrdiff is 16bits" { xfail avr-*-* } 218 } */
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