Merge branch 'stable'
[gnulib.git] / tests / test-stdint.c
1 /* Test of <stdint.h> substitute.
2    Copyright (C) 2006-2011 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 <http://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 #define __STDC_LIMIT_MACROS 1 /* to make it work also in C++ mode */
25 #define __STDC_CONSTANT_MACROS 1 /* likewise */
26 #include <stdint.h>
27
28 #include "verify.h"
29 #include "intprops.h"
30
31 #if __GNUC__ >= 2 && DO_PEDANTIC
32 # define verify_same_types(expr1,expr2)  \
33     extern void _verify_func(__LINE__) (__typeof__ (expr1) *); \
34     extern void _verify_func(__LINE__) (__typeof__ (expr2) *);
35 # define _verify_func(line) _verify_func2(line)
36 # define _verify_func2(line) verify_func_ ## line
37 #else
38 # define verify_same_types(expr1,expr2) extern void verify_func (int)
39 #endif
40
41 /* 7.18.1.1. Exact-width integer types */
42 /* 7.18.2.1. Limits of exact-width integer types */
43
44 int8_t a1[3] = { INT8_C (17), INT8_MIN, INT8_MAX };
45 verify (TYPE_MINIMUM (int8_t) == INT8_MIN);
46 verify (TYPE_MAXIMUM (int8_t) == INT8_MAX);
47 verify_same_types (INT8_MIN, (int8_t) 0 + 0);
48 verify_same_types (INT8_MAX, (int8_t) 0 + 0);
49
50 int16_t a2[3] = { INT16_C (17), INT16_MIN, INT16_MAX };
51 verify (TYPE_MINIMUM (int16_t) == INT16_MIN);
52 verify (TYPE_MAXIMUM (int16_t) == INT16_MAX);
53 verify_same_types (INT16_MIN, (int16_t) 0 + 0);
54 verify_same_types (INT16_MAX, (int16_t) 0 + 0);
55
56 int32_t a3[3] = { INT32_C (17), INT32_MIN, INT32_MAX };
57 verify (TYPE_MINIMUM (int32_t) == INT32_MIN);
58 verify (TYPE_MAXIMUM (int32_t) == INT32_MAX);
59 verify_same_types (INT32_MIN, (int32_t) 0 + 0);
60 verify_same_types (INT32_MAX, (int32_t) 0 + 0);
61
62 #ifdef INT64_MAX
63 int64_t a4[3] = { INT64_C (17), INT64_MIN, INT64_MAX };
64 verify (TYPE_MINIMUM (int64_t) == INT64_MIN);
65 verify (TYPE_MAXIMUM (int64_t) == INT64_MAX);
66 verify_same_types (INT64_MIN, (int64_t) 0 + 0);
67 verify_same_types (INT64_MAX, (int64_t) 0 + 0);
68 #endif
69
70 uint8_t b1[2] = { UINT8_C (17), UINT8_MAX };
71 verify (TYPE_MAXIMUM (uint8_t) == UINT8_MAX);
72 verify_same_types (UINT8_MAX, (uint8_t) 0 + 0);
73
74 uint16_t b2[2] = { UINT16_C (17), UINT16_MAX };
75 verify (TYPE_MAXIMUM (uint16_t) == UINT16_MAX);
76 verify_same_types (UINT16_MAX, (uint16_t) 0 + 0);
77
78 uint32_t b3[2] = { UINT32_C (17), UINT32_MAX };
79 verify (TYPE_MAXIMUM (uint32_t) == UINT32_MAX);
80 verify_same_types (UINT32_MAX, (uint32_t) 0 + 0);
81
82 #ifdef UINT64_MAX
83 uint64_t b4[2] = { UINT64_C (17), UINT64_MAX };
84 verify (TYPE_MAXIMUM (uint64_t) == UINT64_MAX);
85 verify_same_types (UINT64_MAX, (uint64_t) 0 + 0);
86 #endif
87
88 #if INT8_MIN && INT8_MAX && INT16_MIN && INT16_MAX && INT32_MIN && INT32_MAX
89 /* ok */
90 #else
91 err or;
92 #endif
93
94 #if UINT8_MAX && UINT16_MAX && UINT32_MAX
95 /* ok */
96 #else
97 err or;
98 #endif
99
100 /* 7.18.1.2. Minimum-width integer types */
101 /* 7.18.2.2. Limits of minimum-width integer types */
102
103 int_least8_t c1[3] = { 17, INT_LEAST8_MIN, INT_LEAST8_MAX };
104 verify (TYPE_MINIMUM (int_least8_t) == INT_LEAST8_MIN);
105 verify (TYPE_MAXIMUM (int_least8_t) == INT_LEAST8_MAX);
106 verify_same_types (INT_LEAST8_MIN, (int_least8_t) 0 + 0);
107 verify_same_types (INT_LEAST8_MAX, (int_least8_t) 0 + 0);
108
109 int_least16_t c2[3] = { 17, INT_LEAST16_MIN, INT_LEAST16_MAX };
110 verify (TYPE_MINIMUM (int_least16_t) == INT_LEAST16_MIN);
111 verify (TYPE_MAXIMUM (int_least16_t) == INT_LEAST16_MAX);
112 verify_same_types (INT_LEAST16_MIN, (int_least16_t) 0 + 0);
113 verify_same_types (INT_LEAST16_MAX, (int_least16_t) 0 + 0);
114
115 int_least32_t c3[3] = { 17, INT_LEAST32_MIN, INT_LEAST32_MAX };
116 verify (TYPE_MINIMUM (int_least32_t) == INT_LEAST32_MIN);
117 verify (TYPE_MAXIMUM (int_least32_t) == INT_LEAST32_MAX);
118 verify_same_types (INT_LEAST32_MIN, (int_least32_t) 0 + 0);
119 verify_same_types (INT_LEAST32_MAX, (int_least32_t) 0 + 0);
120
121 #ifdef INT_LEAST64_MAX
122 int_least64_t c4[3] = { 17, INT_LEAST64_MIN, INT_LEAST64_MAX };
123 verify (TYPE_MINIMUM (int_least64_t) == INT_LEAST64_MIN);
124 verify (TYPE_MAXIMUM (int_least64_t) == INT_LEAST64_MAX);
125 verify_same_types (INT_LEAST64_MIN, (int_least64_t) 0 + 0);
126 verify_same_types (INT_LEAST64_MAX, (int_least64_t) 0 + 0);
127 #endif
128
129 uint_least8_t d1[2] = { 17, UINT_LEAST8_MAX };
130 verify (TYPE_MAXIMUM (uint_least8_t) == UINT_LEAST8_MAX);
131 verify_same_types (UINT_LEAST8_MAX, (uint_least8_t) 0 + 0);
132
133 uint_least16_t d2[2] = { 17, UINT_LEAST16_MAX };
134 verify (TYPE_MAXIMUM (uint_least16_t) == UINT_LEAST16_MAX);
135 verify_same_types (UINT_LEAST16_MAX, (uint_least16_t) 0 + 0);
136
137 uint_least32_t d3[2] = { 17, UINT_LEAST32_MAX };
138 verify (TYPE_MAXIMUM (uint_least32_t) == UINT_LEAST32_MAX);
139 verify_same_types (UINT_LEAST32_MAX, (uint_least32_t) 0 + 0);
140
141 #ifdef UINT_LEAST64_MAX
142 uint_least64_t d4[2] = { 17, UINT_LEAST64_MAX };
143 verify (TYPE_MAXIMUM (uint_least64_t) == UINT_LEAST64_MAX);
144 verify_same_types (UINT_LEAST64_MAX, (uint_least64_t) 0 + 0);
145 #endif
146
147 #if INT_LEAST8_MIN && INT_LEAST8_MAX && INT_LEAST16_MIN && INT_LEAST16_MAX && INT_LEAST32_MIN && INT_LEAST32_MAX
148 /* ok */
149 #else
150 err or;
151 #endif
152
153 #if UINT_LEAST8_MAX && UINT_LEAST16_MAX && UINT_LEAST32_MAX
154 /* ok */
155 #else
156 err or;
157 #endif
158
159 /* 7.18.1.3. Fastest minimum-width integer types */
160 /* 7.18.2.3. Limits of fastest minimum-width integer types */
161
162 int_fast8_t e1[3] = { 17, INT_FAST8_MIN, INT_FAST8_MAX };
163 verify (TYPE_MINIMUM (int_fast8_t) == INT_FAST8_MIN);
164 verify (TYPE_MAXIMUM (int_fast8_t) == INT_FAST8_MAX);
165 verify_same_types (INT_FAST8_MIN, (int_fast8_t) 0 + 0);
166 verify_same_types (INT_FAST8_MAX, (int_fast8_t) 0 + 0);
167
168 int_fast16_t e2[3] = { 17, INT_FAST16_MIN, INT_FAST16_MAX };
169 verify (TYPE_MINIMUM (int_fast16_t) == INT_FAST16_MIN);
170 verify (TYPE_MAXIMUM (int_fast16_t) == INT_FAST16_MAX);
171 verify_same_types (INT_FAST16_MIN, (int_fast16_t) 0 + 0);
172 verify_same_types (INT_FAST16_MAX, (int_fast16_t) 0 + 0);
173
174 int_fast32_t e3[3] = { 17, INT_FAST32_MIN, INT_FAST32_MAX };
175 verify (TYPE_MINIMUM (int_fast32_t) == INT_FAST32_MIN);
176 verify (TYPE_MAXIMUM (int_fast32_t) == INT_FAST32_MAX);
177 verify_same_types (INT_FAST32_MIN, (int_fast32_t) 0 + 0);
178 verify_same_types (INT_FAST32_MAX, (int_fast32_t) 0 + 0);
179
180 #ifdef INT_FAST64_MAX
181 int_fast64_t e4[3] = { 17, INT_FAST64_MIN, INT_FAST64_MAX };
182 verify (TYPE_MINIMUM (int_fast64_t) == INT_FAST64_MIN);
183 verify (TYPE_MAXIMUM (int_fast64_t) == INT_FAST64_MAX);
184 verify_same_types (INT_FAST64_MIN, (int_fast64_t) 0 + 0);
185 verify_same_types (INT_FAST64_MAX, (int_fast64_t) 0 + 0);
186 #endif
187
188 uint_fast8_t f1[2] = { 17, UINT_FAST8_MAX };
189 verify (TYPE_MAXIMUM (uint_fast8_t) == UINT_FAST8_MAX);
190 verify_same_types (UINT_FAST8_MAX, (uint_fast8_t) 0 + 0);
191
192 uint_fast16_t f2[2] = { 17, UINT_FAST16_MAX };
193 verify (TYPE_MAXIMUM (uint_fast16_t) == UINT_FAST16_MAX);
194 verify_same_types (UINT_FAST16_MAX, (uint_fast16_t) 0 + 0);
195
196 uint_fast32_t f3[2] = { 17, UINT_FAST32_MAX };
197 verify (TYPE_MAXIMUM (uint_fast32_t) == UINT_FAST32_MAX);
198 verify_same_types (UINT_FAST32_MAX, (uint_fast32_t) 0 + 0);
199
200 #ifdef UINT_FAST64_MAX
201 uint_fast64_t f4[2] = { 17, UINT_FAST64_MAX };
202 verify (TYPE_MAXIMUM (uint_fast64_t) == UINT_FAST64_MAX);
203 verify_same_types (UINT_FAST64_MAX, (uint_fast64_t) 0 + 0);
204 #endif
205
206 #if INT_FAST8_MIN && INT_FAST8_MAX && INT_FAST16_MIN && INT_FAST16_MAX && INT_FAST32_MIN && INT_FAST32_MAX
207 /* ok */
208 #else
209 err or;
210 #endif
211
212 #if UINT_FAST8_MAX && UINT_FAST16_MAX && UINT_FAST32_MAX
213 /* ok */
214 #else
215 err or;
216 #endif
217
218 /* 7.18.1.4. Integer types capable of holding object pointers */
219 /* 7.18.2.4. Limits of integer types capable of holding object pointers */
220
221 intptr_t g[3] = { 17, INTPTR_MIN, INTPTR_MAX };
222 verify (TYPE_MINIMUM (intptr_t) == INTPTR_MIN);
223 verify (TYPE_MAXIMUM (intptr_t) == INTPTR_MAX);
224 verify_same_types (INTPTR_MIN, (intptr_t) 0 + 0);
225 verify_same_types (INTPTR_MAX, (intptr_t) 0 + 0);
226
227 uintptr_t h[2] = { 17, UINTPTR_MAX };
228 verify (TYPE_MAXIMUM (uintptr_t) == UINTPTR_MAX);
229 verify_same_types (UINTPTR_MAX, (uintptr_t) 0 + 0);
230
231 #if INTPTR_MIN && INTPTR_MAX && UINTPTR_MAX
232 /* ok */
233 #else
234 err 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
240 intmax_t i[3] = { INTMAX_C (17), INTMAX_MIN, INTMAX_MAX };
241 verify (TYPE_MINIMUM (intmax_t) == INTMAX_MIN);
242 verify (TYPE_MAXIMUM (intmax_t) == INTMAX_MAX);
243 verify_same_types (INTMAX_MIN, (intmax_t) 0 + 0);
244 verify_same_types (INTMAX_MAX, (intmax_t) 0 + 0);
245
246 uintmax_t j[2] = { UINTMAX_C (17), UINTMAX_MAX };
247 verify (TYPE_MAXIMUM (uintmax_t) == UINTMAX_MAX);
248 verify_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
256 err or;
257 #endif
258 #endif
259
260 /* 7.18.3. Limits of other integer types */
261
262 #include <stddef.h>
263
264 verify (TYPE_MINIMUM (ptrdiff_t) == PTRDIFF_MIN);
265 verify (TYPE_MAXIMUM (ptrdiff_t) == PTRDIFF_MAX);
266 verify_same_types (PTRDIFF_MIN, (ptrdiff_t) 0 + 0);
267 verify_same_types (PTRDIFF_MAX, (ptrdiff_t) 0 + 0);
268
269 #if PTRDIFF_MIN && PTRDIFF_MAX
270 /* ok */
271 #else
272 err or;
273 #endif
274
275 #include <signal.h>
276
277 verify (TYPE_MINIMUM (sig_atomic_t) == SIG_ATOMIC_MIN);
278 verify (TYPE_MAXIMUM (sig_atomic_t) == SIG_ATOMIC_MAX);
279 verify_same_types (SIG_ATOMIC_MIN, (sig_atomic_t) 0 + 0);
280 verify_same_types (SIG_ATOMIC_MAX, (sig_atomic_t) 0 + 0);
281
282 #if SIG_ATOMIC_MIN != 17 && SIG_ATOMIC_MAX
283 /* ok */
284 #else
285 err or;
286 #endif
287
288 verify (TYPE_MAXIMUM (size_t) == SIZE_MAX);
289 verify_same_types (SIZE_MAX, (size_t) 0 + 0);
290
291 #if SIZE_MAX
292 /* ok */
293 #else
294 err or;
295 #endif
296
297 #if HAVE_WCHAR_T
298 verify (TYPE_MINIMUM (wchar_t) == WCHAR_MIN);
299 verify (TYPE_MAXIMUM (wchar_t) == WCHAR_MAX);
300 verify_same_types (WCHAR_MIN, (wchar_t) 0 + 0);
301 verify_same_types (WCHAR_MAX, (wchar_t) 0 + 0);
302
303 # if WCHAR_MIN != 17 && WCHAR_MAX
304 /* ok */
305 # else
306 err or;
307 # endif
308 #endif
309
310 #if HAVE_WINT_T
311 # include <wchar.h>
312
313 verify (TYPE_MINIMUM (wint_t) == WINT_MIN);
314 verify (TYPE_MAXIMUM (wint_t) == WINT_MAX);
315 verify_same_types (WINT_MIN, (wint_t) 0 + 0);
316 verify_same_types (WINT_MAX, (wint_t) 0 + 0);
317
318 # if WINT_MIN != 17 && WINT_MAX
319 /* ok */
320 # else
321 err or;
322 # endif
323 #endif
324
325 /* 7.18.4. Macros for integer constants */
326
327 verify (INT8_C (17) == 17);
328 verify_same_types (INT8_C (17), (int_least8_t)0 + 0);
329 verify (UINT8_C (17) == 17);
330 verify_same_types (UINT8_C (17), (uint_least8_t)0 + 0);
331
332 verify (INT16_C (17) == 17);
333 verify_same_types (INT16_C (17), (int_least16_t)0 + 0);
334 verify (UINT16_C (17) == 17);
335 verify_same_types (UINT16_C (17), (uint_least16_t)0 + 0);
336
337 verify (INT32_C (17) == 17);
338 verify_same_types (INT32_C (17), (int_least32_t)0 + 0);
339 verify (UINT32_C (17) == 17);
340 verify_same_types (UINT32_C (17), (uint_least32_t)0 + 0);
341
342 #ifdef INT64_C
343 verify (INT64_C (17) == 17);
344 verify_same_types (INT64_C (17), (int_least64_t)0 + 0);
345 #endif
346 #ifdef UINT64_C
347 verify (UINT64_C (17) == 17);
348 verify_same_types (UINT64_C (17), (uint_least64_t)0 + 0);
349 #endif
350
351 verify (INTMAX_C (17) == 17);
352 verify_same_types (INTMAX_C (17), (intmax_t)0 + 0);
353 verify (UINTMAX_C (17) == 17);
354 verify_same_types (UINTMAX_C (17), (uintmax_t)0 + 0);
355
356
357 int
358 main (void)
359 {
360   return 0;
361 }