Fix last ChangeLog entry.
[gnulib.git] / tests / test-intprops.c
blobe3f165f146c9f7c4c9430cc865cd8f59cb075d14
1 /* Test intprops.h.
2 Copyright (C) 2011-2025 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* Written by Paul Eggert. */
19 #include <config.h>
21 #if _GL_GNUC_PREREQ (4, 3)
23 /* Tell gcc not to warn about the long expressions that the overflow
24 macros expand to, or about the (X < 0) expressions. */
25 # pragma GCC diagnostic ignored "-Woverlength-strings"
26 # pragma GCC diagnostic ignored "-Wtype-limits"
28 /* Work around a bug in GCC 6.1 and earlier; see:
29 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68971 */
30 # pragma GCC diagnostic ignored "-Woverflow"
32 #endif
34 #ifdef TEST_STDCKDINT
35 # include <stdckdint.h>
36 #else
37 # include "intprops.h"
38 #endif
40 #include <inttypes.h>
41 #include <limits.h>
43 #include "macros.h"
45 /* Compile-time verification of expression X.
46 In this file, we need it as a statement, rather than as a declaration. */
47 #define verify_stmt(x) do { static_assert (x); } while (0)
49 /* VERIFY (X) uses a static assertion for compilers that are known to work,
50 and falls back on a dynamic assertion for other compilers.
51 But it ignores X if testing stdckdint.h.
52 These tests should be checkable via 'verify' rather than 'ASSERT', but
53 using 'verify' would run into a bug with HP-UX 11.23 cc; see
54 <https://lists.gnu.org/r/bug-gnulib/2011-05/msg00401.html>. */
55 #ifdef TEST_STDCKDINT
56 # define VERIFY(x) ((void) 0)
57 #elif __GNUC__ || __clang__ || __SUNPRO_C
58 # define VERIFY(x) verify_stmt (x)
59 #else
60 # define VERIFY(x) ASSERT (x)
61 #endif
63 #define DONTCARE __LINE__
65 int int_minus_2 = -2;
66 int int_1 = 1;
68 int
69 main (void)
71 /* Use VERIFY for tests that must be integer constant expressions,
72 ASSERT otherwise. */
74 #ifndef TEST_STDCKDINT
75 /* TYPE_IS_INTEGER. */
76 ASSERT (TYPE_IS_INTEGER (bool));
77 ASSERT (TYPE_IS_INTEGER (char));
78 ASSERT (TYPE_IS_INTEGER (signed char));
79 ASSERT (TYPE_IS_INTEGER (unsigned char));
80 ASSERT (TYPE_IS_INTEGER (short int));
81 ASSERT (TYPE_IS_INTEGER (unsigned short int));
82 ASSERT (TYPE_IS_INTEGER (int));
83 ASSERT (TYPE_IS_INTEGER (unsigned int));
84 ASSERT (TYPE_IS_INTEGER (long int));
85 ASSERT (TYPE_IS_INTEGER (unsigned long int));
86 ASSERT (TYPE_IS_INTEGER (intmax_t));
87 ASSERT (TYPE_IS_INTEGER (uintmax_t));
88 ASSERT (! TYPE_IS_INTEGER (float));
89 ASSERT (! TYPE_IS_INTEGER (double));
90 ASSERT (! TYPE_IS_INTEGER (long double));
92 /* TYPE_SIGNED. */
93 /* VERIFY (! TYPE_SIGNED (bool)); // not guaranteed by gnulib substitute */
94 VERIFY (TYPE_SIGNED (signed char));
95 VERIFY (! TYPE_SIGNED (unsigned char));
96 VERIFY (TYPE_SIGNED (short int));
97 VERIFY (! TYPE_SIGNED (unsigned short int));
98 VERIFY (TYPE_SIGNED (int));
99 VERIFY (! TYPE_SIGNED (unsigned int));
100 VERIFY (TYPE_SIGNED (long int));
101 VERIFY (! TYPE_SIGNED (unsigned long int));
102 VERIFY (TYPE_SIGNED (intmax_t));
103 VERIFY (! TYPE_SIGNED (uintmax_t));
104 ASSERT (TYPE_SIGNED (float));
105 ASSERT (TYPE_SIGNED (double));
106 ASSERT (TYPE_SIGNED (long double));
108 /* Integer representation. Check that it is two's complement. */
109 VERIFY (INT_MIN + INT_MAX < 0);
111 /* TYPE_MINIMUM, TYPE_MAXIMUM. */
112 VERIFY (TYPE_MINIMUM (char) == CHAR_MIN);
113 VERIFY (TYPE_MAXIMUM (char) == CHAR_MAX);
114 VERIFY (TYPE_MINIMUM (unsigned char) == 0);
115 VERIFY (TYPE_MAXIMUM (unsigned char) == UCHAR_MAX);
116 VERIFY (TYPE_MINIMUM (signed char) == SCHAR_MIN);
117 VERIFY (TYPE_MAXIMUM (signed char) == SCHAR_MAX);
118 VERIFY (TYPE_MINIMUM (short int) == SHRT_MIN);
119 VERIFY (TYPE_MAXIMUM (short int) == SHRT_MAX);
120 VERIFY (TYPE_MINIMUM (unsigned short int) == 0);
121 VERIFY (TYPE_MAXIMUM (unsigned short int) == USHRT_MAX);
122 VERIFY (TYPE_MINIMUM (int) == INT_MIN);
123 VERIFY (TYPE_MAXIMUM (int) == INT_MAX);
124 VERIFY (TYPE_MINIMUM (unsigned int) == 0);
125 VERIFY (TYPE_MAXIMUM (unsigned int) == UINT_MAX);
126 VERIFY (TYPE_MINIMUM (long int) == LONG_MIN);
127 VERIFY (TYPE_MAXIMUM (long int) == LONG_MAX);
128 VERIFY (TYPE_MINIMUM (unsigned long int) == 0);
129 VERIFY (TYPE_MAXIMUM (unsigned long int) == ULONG_MAX);
130 #ifdef LLONG_MAX
131 verify_stmt (TYPE_MINIMUM (long long int) == LLONG_MIN);
132 verify_stmt (TYPE_MAXIMUM (long long int) == LLONG_MAX);
133 verify_stmt (TYPE_MINIMUM (unsigned long long int) == 0);
134 verify_stmt (TYPE_MAXIMUM (unsigned long long int) == ULLONG_MAX);
135 #endif
136 VERIFY (TYPE_MINIMUM (intmax_t) == INTMAX_MIN);
137 VERIFY (TYPE_MAXIMUM (intmax_t) == INTMAX_MAX);
138 VERIFY (TYPE_MINIMUM (uintmax_t) == 0);
139 VERIFY (TYPE_MAXIMUM (uintmax_t) == UINTMAX_MAX);
141 /* TYPE_WIDTH. */
142 #ifdef CHAR_WIDTH
143 verify_stmt (TYPE_WIDTH (char) == CHAR_WIDTH);
144 verify_stmt (TYPE_WIDTH (signed char) == SCHAR_WIDTH);
145 verify_stmt (TYPE_WIDTH (unsigned char) == UCHAR_WIDTH);
146 verify_stmt (TYPE_WIDTH (short int) == SHRT_WIDTH);
147 verify_stmt (TYPE_WIDTH (unsigned short int) == USHRT_WIDTH);
148 verify_stmt (TYPE_WIDTH (int) == INT_WIDTH);
149 verify_stmt (TYPE_WIDTH (unsigned int) == UINT_WIDTH);
150 verify_stmt (TYPE_WIDTH (long int) == LONG_WIDTH);
151 verify_stmt (TYPE_WIDTH (unsigned long int) == ULONG_WIDTH);
152 #ifdef LLONG_WIDTH
153 verify_stmt (TYPE_WIDTH (long long int) == LLONG_WIDTH);
154 verify_stmt (TYPE_WIDTH (unsigned long long int) == ULLONG_WIDTH);
155 #endif
156 #endif
158 /* INT_BITS_STRLEN_BOUND. */
159 VERIFY (INT_BITS_STRLEN_BOUND (1) == 1);
160 VERIFY (INT_BITS_STRLEN_BOUND (2620) == 789);
162 /* INT_STRLEN_BOUND, INT_BUFSIZE_BOUND. */
163 #ifdef INT32_MAX /* POSIX guarantees int32_t; this ports to non-POSIX. */
164 VERIFY (INT_STRLEN_BOUND (int32_t) == sizeof ("-2147483648") - 1);
165 VERIFY (INT_BUFSIZE_BOUND (int32_t) == sizeof ("-2147483648"));
166 #endif
167 #ifdef INT64_MAX
168 VERIFY (INT_STRLEN_BOUND (int64_t) == sizeof ("-9223372036854775808") - 1);
169 VERIFY (INT_BUFSIZE_BOUND (int64_t) == sizeof ("-9223372036854775808"));
170 #endif
171 #endif
173 /* All the INT_<op>_RANGE_OVERFLOW tests are equally valid as
174 INT_<op>_OVERFLOW tests, so define macros to do both. OP is the
175 operation, OPNAME its symbolic name, A and B its operands, T the
176 result type, V the overflow flag, and VRES the result if V and if
177 two's complement. CHECK_BINOP is for most binary operations,
178 CHECK_SBINOP for binary +, -, * when the result type is signed,
179 and CHECK_UNOP for unary operations. */
180 #define CHECK_BINOP(op, opname, a, b, t, v, vres) \
181 VERIFY (INT_##opname##_RANGE_OVERFLOW (a, b, TYPE_MINIMUM (t), \
182 TYPE_MAXIMUM (t)) \
183 == (v)); \
184 VERIFY (INT_##opname##_OVERFLOW (a, b) == (v))
185 #define CHECK_SBINOP(op, opname, a, b, t, v, vres) \
186 CHECK_BINOP(op, opname, a, b, t, v, vres); \
188 t result; \
189 ASSERT (INT_##opname##_WRAPV (a, b, &result) == (v)); \
190 ASSERT (result == ((v) ? (vres) : ((a) op (b)))); \
192 #define CHECK_UNOP(op, opname, a, t, v) \
193 VERIFY (INT_##opname##_RANGE_OVERFLOW (a, TYPE_MINIMUM (t), \
194 TYPE_MAXIMUM (t)) \
195 == (v)); \
196 VERIFY (INT_##opname##_OVERFLOW (a) == (v))
198 /* INT_<op>_RANGE_OVERFLOW, INT_<op>_OVERFLOW. */
199 VERIFY (INT_ADD_RANGE_OVERFLOW (INT_MAX, 1, INT_MIN, INT_MAX));
200 VERIFY (INT_ADD_OVERFLOW (INT_MAX, 1));
202 CHECK_SBINOP (+, ADD, INT_MAX, 1, int, true, INT_MIN);
203 CHECK_SBINOP (+, ADD, INT_MAX, -1, int, false, INT_MAX - 1);
204 CHECK_SBINOP (+, ADD, INT_MIN, 1, int, false, INT_MIN + 1);
205 CHECK_SBINOP (+, ADD, INT_MIN, -1, int, true, INT_MAX);
206 CHECK_BINOP (+, ADD, UINT_MAX, 1u, unsigned int, true, 0u);
207 CHECK_BINOP (+, ADD, 0u, 1u, unsigned int, false, 1u);
209 CHECK_SBINOP (-, SUBTRACT, INT_MAX, 1, int, false, INT_MAX - 1);
210 CHECK_SBINOP (-, SUBTRACT, INT_MAX, -1, int, true, INT_MIN);
211 CHECK_SBINOP (-, SUBTRACT, INT_MIN, 1, int, true, INT_MAX);
212 CHECK_SBINOP (-, SUBTRACT, INT_MIN, -1, int, false, INT_MIN - -1);
213 CHECK_BINOP (-, SUBTRACT, UINT_MAX, 1u, unsigned int, false, UINT_MAX - 1u);
214 CHECK_BINOP (-, SUBTRACT, 0u, 1u, unsigned int, true, 0u - 1u);
216 CHECK_UNOP (-, NEGATE, INT_MIN, int, true);
217 CHECK_UNOP (-, NEGATE, 0, int, false);
218 CHECK_UNOP (-, NEGATE, INT_MAX, int, false);
219 CHECK_UNOP (-, NEGATE, 0u, unsigned int, false);
220 CHECK_UNOP (-, NEGATE, 1u, unsigned int, true);
221 CHECK_UNOP (-, NEGATE, UINT_MAX, unsigned int, true);
223 CHECK_SBINOP (*, MULTIPLY, INT_MAX, INT_MAX, int, true, 1);
224 CHECK_SBINOP (*, MULTIPLY, INT_MAX, INT_MIN, int, true, INT_MIN);
225 CHECK_SBINOP (*, MULTIPLY, INT_MIN, INT_MAX, int, true, INT_MIN);
226 CHECK_SBINOP (*, MULTIPLY, INT_MIN, INT_MIN, int, true, 0);
227 CHECK_SBINOP (*, MULTIPLY, -1, INT_MIN, int,
228 INT_NEGATE_OVERFLOW (INT_MIN), INT_MIN);
229 #if !defined __HP_cc
230 CHECK_SBINOP (*, MULTIPLY, LONG_MIN / INT_MAX, (long int) INT_MAX,
231 long int, false, LONG_MIN - LONG_MIN % INT_MAX);
232 #endif
234 CHECK_BINOP (/, DIVIDE, INT_MIN, -1, int,
235 INT_NEGATE_OVERFLOW (INT_MIN), INT_MIN);
236 CHECK_BINOP (/, DIVIDE, INT_MAX, 1, int, false, INT_MAX);
237 CHECK_BINOP (/, DIVIDE, (unsigned int) INT_MIN, -1u, unsigned int,
238 false, INT_MIN / -1u);
240 CHECK_BINOP (%, REMAINDER, INT_MIN, -1, int, INT_NEGATE_OVERFLOW (INT_MIN), 0);
241 CHECK_BINOP (%, REMAINDER, INT_MAX, 1, int, false, 0);
242 CHECK_BINOP (%, REMAINDER, (unsigned int) INT_MIN, -1u, unsigned int,
243 false, INT_MIN % -1u);
245 CHECK_BINOP (<<, LEFT_SHIFT, UINT_MAX, 1, unsigned int, true, UINT_MAX << 1);
246 CHECK_BINOP (<<, LEFT_SHIFT, UINT_MAX / 2 + 1, 1, unsigned int, true,
247 (UINT_MAX / 2 + 1) << 1);
248 CHECK_BINOP (<<, LEFT_SHIFT, UINT_MAX / 2, 1, unsigned int, false,
249 (UINT_MAX / 2) << 1);
251 /* INT_<op>_OVERFLOW and INT_<op>_WRAPV with mixed types. */
252 #define CHECK_SUM(a, b, t, v, vres) \
253 CHECK_SUM1 (a, b, t, v, vres); \
254 CHECK_SUM1 (b, a, t, v, vres)
255 #define CHECK_SUM_WRAPV(a, b, t, v, vres, okres) \
256 CHECK_SUM_WRAPV1 (a, b, t, v, vres, okres); \
257 CHECK_SUM_WRAPV1 (b, a, t, v, vres, okres)
258 #define CHECK_SUM1(a, b, t, v, vres) \
259 VERIFY (INT_ADD_OVERFLOW (a, b) == (v)); \
260 CHECK_SUM_WRAPV1 (a, b, t, v, vres, (a) + (b))
261 #define CHECK_SUM_WRAPV1(a, b, t, v, vres, okres) \
263 t result; \
264 ASSERT (INT_ADD_WRAPV (a, b, &result) == (v)); \
265 ASSERT (result == ((v) ? (vres) : (okres))); \
267 CHECK_SUM (-1, LONG_MIN, long int, true, LONG_MAX);
268 CHECK_SUM (-1, UINT_MAX, unsigned int, false, DONTCARE);
269 CHECK_SUM (-1L, INT_MIN, long int, INT_MIN == LONG_MIN,
270 INT_MIN == LONG_MIN ? INT_MAX : DONTCARE);
271 CHECK_SUM (0u, -1, unsigned int, true, 0u + -1);
272 CHECK_SUM (0u, 0, unsigned int, false, DONTCARE);
273 CHECK_SUM (0u, 1, unsigned int, false, DONTCARE);
274 CHECK_SUM (1, LONG_MAX, long int, true, LONG_MIN);
275 CHECK_SUM (1, UINT_MAX, unsigned int, true, 0u);
276 CHECK_SUM (1L, INT_MAX, long int, INT_MAX == LONG_MAX,
277 INT_MAX == LONG_MAX ? INT_MIN : DONTCARE);
278 CHECK_SUM (1u, INT_MAX, unsigned int, INT_MAX == UINT_MAX, 1u + INT_MAX);
279 CHECK_SUM (1u, INT_MIN, unsigned int, true, 1u + INT_MIN);
280 CHECK_SUM_WRAPV (-1, 1u, int, false, DONTCARE, 0);
281 CHECK_SUM_WRAPV (-1, 1ul, int, false, DONTCARE, 0);
282 CHECK_SUM_WRAPV (-1l, 1u, int, false, DONTCARE, 0);
283 CHECK_SUM_WRAPV (-100, 1000u, int, false, DONTCARE, 900);
284 CHECK_SUM_WRAPV (INT_MIN, UINT_MAX, int, false, DONTCARE, INT_MAX);
285 CHECK_SUM_WRAPV (1u, INT_MAX, int, true, INT_MIN, DONTCARE);
286 CHECK_SUM_WRAPV (INT_MAX, 1, long int, LONG_MAX <= INT_MAX, INT_MIN,
287 INT_MAX + 1L);
288 CHECK_SUM_WRAPV (UINT_MAX, 1, long int, LONG_MAX <= UINT_MAX, 0,
289 UINT_MAX + 1L);
290 CHECK_SUM_WRAPV (INT_MAX, 1, unsigned long int, ULONG_MAX <= INT_MAX, 0,
291 INT_MAX + 1uL);
292 CHECK_SUM_WRAPV (UINT_MAX, 1, unsigned long int, ULONG_MAX <= UINT_MAX, 0,
293 UINT_MAX + 1uL);
296 long int result;
297 ASSERT (INT_ADD_WRAPV (1, INT_MAX, &result) == (INT_MAX == LONG_MAX));
298 ASSERT (INT_ADD_WRAPV (-1, INT_MIN, &result) == (INT_MIN == LONG_MIN));
301 #define CHECK_DIFFERENCE(a, b, t, v, vres) \
302 VERIFY (INT_SUBTRACT_OVERFLOW (a, b) == (v))
303 #define CHECK_SDIFFERENCE(a, b, t, v, vres) \
304 CHECK_DIFFERENCE (a, b, t, v, vres); \
305 CHECK_SDIFFERENCE_WRAPV (a, b, t, v, vres)
306 #define CHECK_SDIFFERENCE_WRAPV(a, b, t, v, vres) \
308 t result; \
309 ASSERT (INT_SUBTRACT_WRAPV (a, b, &result) == (v)); \
310 ASSERT (result == ((v) ? (vres) : ((a) - (b)))); \
312 CHECK_DIFFERENCE (INT_MAX, 1u, unsigned int, UINT_MAX < INT_MAX - 1,
313 INT_MAX - 1u);
314 CHECK_DIFFERENCE (UINT_MAX, 1, unsigned int, false, UINT_MAX - 1);
315 CHECK_DIFFERENCE (0u, -1, unsigned int, false, 0u - -1);
316 CHECK_DIFFERENCE (UINT_MAX, -1, unsigned int, true, UINT_MAX - -1);
317 CHECK_DIFFERENCE (INT_MIN, 1u, unsigned int, true, INT_MIN - 1u);
318 CHECK_DIFFERENCE (-1, 0u, unsigned int, true, -1 - 0u);
319 CHECK_SDIFFERENCE (-1, INT_MIN, int, false, -1 - INT_MIN);
320 CHECK_SDIFFERENCE (-1, INT_MAX, int, false, -1 - INT_MAX);
321 CHECK_SDIFFERENCE (0, INT_MIN, int, INT_MIN < -INT_MAX, INT_MIN);
322 CHECK_SDIFFERENCE (0, INT_MAX, int, false, 0 - INT_MAX);
323 CHECK_SDIFFERENCE_WRAPV (-1, 1u, int, false, DONTCARE);
324 CHECK_SDIFFERENCE_WRAPV (-1, 1ul, int, false, DONTCARE);
325 CHECK_SDIFFERENCE_WRAPV (-1l, 1u, int, false, DONTCARE);
326 CHECK_SDIFFERENCE_WRAPV (0u, INT_MAX, int, false, DONTCARE);
327 CHECK_SDIFFERENCE_WRAPV (1u, INT_MIN, int, true, 1u - INT_MIN);
329 long int result;
330 ASSERT (INT_SUBTRACT_WRAPV (INT_MAX, -1, &result) == (INT_MAX == LONG_MAX));
331 ASSERT (INT_SUBTRACT_WRAPV (INT_MIN, 1, &result) == (INT_MAX == LONG_MAX));
334 #define CHECK_PRODUCT(a, b, t, v, vres) \
335 CHECK_PRODUCT1 (a, b, t, v, vres); \
336 CHECK_PRODUCT1 (b, a, t, v, vres)
337 #define CHECK_SPRODUCT(a, b, t, v, vres) \
338 CHECK_SPRODUCT1 (a, b, t, v, vres); \
339 CHECK_SPRODUCT1 (b, a, t, v, vres)
340 #define CHECK_SPRODUCT_WRAPV(a, b, t, v, vres) \
341 CHECK_SPRODUCT_WRAPV1 (a, b, t, v, vres); \
342 CHECK_SPRODUCT_WRAPV1 (b, a, t, v, vres)
343 #define CHECK_PRODUCT1(a, b, t, v, vres) \
344 VERIFY (INT_MULTIPLY_OVERFLOW (a, b) == (v))
345 #define CHECK_SPRODUCT1(a, b, t, v, vres) \
346 CHECK_PRODUCT1 (a, b, t, v, vres); \
347 CHECK_SPRODUCT_WRAPV1 (a, b, t, v, vres)
348 #define CHECK_SPRODUCT_WRAPV1(a, b, t, v, vres) \
350 t result; \
351 ASSERT (INT_MULTIPLY_WRAPV (a, b, &result) == (v)); \
352 ASSERT (result == ((v) ? (vres) : ((a) * (b)))); \
354 CHECK_PRODUCT (-1, 1u, unsigned int, true, -1 * 1u);
355 CHECK_SPRODUCT (-1, INT_MIN, int, INT_NEGATE_OVERFLOW (INT_MIN), INT_MIN);
356 CHECK_PRODUCT (-1, UINT_MAX, unsigned int, true, -1 * UINT_MAX);
357 CHECK_SPRODUCT (-32768, LONG_MAX / -32768 - 1, long int, true, LONG_MIN);
358 CHECK_SPRODUCT (-12345, LONG_MAX / -12345, long int, false, DONTCARE);
359 CHECK_SPRODUCT (0, -1, int, false, DONTCARE);
360 CHECK_SPRODUCT (0, 0, int, false, DONTCARE);
361 CHECK_PRODUCT (0, 0u, unsigned int, false, DONTCARE);
362 CHECK_SPRODUCT (0, 1, int, false, DONTCARE);
363 CHECK_SPRODUCT (0, INT_MAX, int, false, DONTCARE);
364 CHECK_SPRODUCT (0, INT_MIN, int, false, DONTCARE);
365 CHECK_PRODUCT (0, UINT_MAX, unsigned int, false, DONTCARE);
366 CHECK_PRODUCT (0u, -1, unsigned int, false, DONTCARE);
367 CHECK_PRODUCT (0u, 0, unsigned int, false, DONTCARE);
368 CHECK_PRODUCT (0u, 0u, unsigned int, false, DONTCARE);
369 CHECK_PRODUCT (0u, 1, unsigned int, false, DONTCARE);
370 CHECK_PRODUCT (0u, INT_MAX, unsigned int, false, DONTCARE);
371 CHECK_PRODUCT (0u, INT_MIN, unsigned int, false, DONTCARE);
372 CHECK_PRODUCT (0u, UINT_MAX, unsigned int, false, DONTCARE);
373 CHECK_SPRODUCT (1, INT_MAX, int, false, DONTCARE);
374 CHECK_SPRODUCT (1, INT_MIN, int, false, DONTCARE);
375 CHECK_PRODUCT (1, UINT_MAX, unsigned int, false, DONTCARE);
376 CHECK_PRODUCT (1u, INT_MIN, unsigned int, true, 1u * INT_MIN);
377 CHECK_PRODUCT (1u, INT_MAX, unsigned int, UINT_MAX < INT_MAX, 1u * INT_MAX);
378 CHECK_PRODUCT (INT_MAX, UINT_MAX, unsigned int, true, INT_MAX * UINT_MAX);
379 CHECK_PRODUCT (INT_MAX, ULONG_MAX, unsigned long int, true,
380 INT_MAX * ULONG_MAX);
381 #if !defined __HP_cc
382 CHECK_SPRODUCT (INT_MIN, LONG_MAX / INT_MIN - 1, long int, true, LONG_MIN);
383 CHECK_SPRODUCT (INT_MIN, LONG_MAX / INT_MIN, long int, false, DONTCARE);
384 #endif
385 CHECK_PRODUCT (INT_MIN, UINT_MAX, unsigned int, true, INT_MIN * UINT_MAX);
386 CHECK_PRODUCT (INT_MIN, ULONG_MAX, unsigned long int, true,
387 INT_MIN * ULONG_MAX);
388 CHECK_SPRODUCT_WRAPV (-1, INT_MAX + 1u, int, false, DONTCARE);
389 CHECK_SPRODUCT_WRAPV (-1, 1u, int, false, DONTCARE);
390 CHECK_SPRODUCT (0, ULONG_MAX, int, false, DONTCARE);
391 CHECK_SPRODUCT (0u, LONG_MIN, int, false, DONTCARE);
393 long int result;
394 ASSERT (INT_MULTIPLY_WRAPV (INT_MAX, INT_MAX, &result)
395 == (LONG_MAX / INT_MAX < INT_MAX));
396 ASSERT (INT_MULTIPLY_WRAPV (INT_MAX, INT_MAX, &result)
397 || result == INT_MAX * (long int) INT_MAX);
398 ASSERT (INT_MULTIPLY_WRAPV (INT_MIN, INT_MIN, &result)
399 || result == INT_MIN * (long int) INT_MIN);
402 # ifdef LLONG_MAX
404 long long int result;
405 ASSERT (INT_MULTIPLY_WRAPV (LONG_MAX, LONG_MAX, &result)
406 == (LLONG_MAX / LONG_MAX < LONG_MAX));
407 ASSERT (INT_MULTIPLY_WRAPV (LONG_MAX, LONG_MAX, &result)
408 || result == LONG_MAX * (long long int) LONG_MAX);
409 ASSERT (INT_MULTIPLY_WRAPV (LONG_MIN, LONG_MIN, &result)
410 || result == LONG_MIN * (long long int) LONG_MIN);
412 # endif
414 /* Check for GCC bug 91450. */
416 unsigned long long result;
417 ASSERT (INT_MULTIPLY_WRAPV (int_minus_2, int_1, &result) && result == -2);
420 #define CHECK_QUOTIENT(a, b, v) VERIFY (INT_DIVIDE_OVERFLOW (a, b) == (v))
422 CHECK_QUOTIENT (INT_MIN, -1L, INT_MIN == LONG_MIN);
423 CHECK_QUOTIENT (INT_MIN, UINT_MAX, false);
424 CHECK_QUOTIENT (INTMAX_MIN, UINTMAX_MAX, false);
425 CHECK_QUOTIENT (INTMAX_MIN, UINT_MAX, false);
426 CHECK_QUOTIENT (-11, 10u, true);
427 CHECK_QUOTIENT (-10, 10u, true);
428 CHECK_QUOTIENT (-9, 10u, false);
429 CHECK_QUOTIENT (11u, -10, true);
430 CHECK_QUOTIENT (10u, -10, true);
431 CHECK_QUOTIENT (9u, -10, false);
433 #define CHECK_REMAINDER(a, b, v) VERIFY (INT_REMAINDER_OVERFLOW (a, b) == (v))
435 CHECK_REMAINDER (INT_MIN, -1L, INT_MIN == LONG_MIN);
436 CHECK_REMAINDER (-1, UINT_MAX, true);
437 CHECK_REMAINDER ((intmax_t) -1, UINTMAX_MAX, true);
438 CHECK_REMAINDER (INTMAX_MIN, UINT_MAX,
439 (INTMAX_MAX < UINT_MAX
440 && - (unsigned int) INTMAX_MIN % UINT_MAX != 0));
441 CHECK_REMAINDER (INT_MIN, ULONG_MAX, INT_MIN % ULONG_MAX != 1);
442 CHECK_REMAINDER (1u, -1, false);
443 CHECK_REMAINDER (37*39u, -39, false);
444 CHECK_REMAINDER (37*39u + 1, -39, true);
445 CHECK_REMAINDER (37*39u - 1, -39, true);
446 CHECK_REMAINDER (LONG_MAX, -INT_MAX, false);
448 return test_exit_status;