2 Copyright (C) 2011-2017 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 <http://www.gnu.org/licenses/>. */
17 /* Written by Paul Eggert. */
19 /* Tell gcc not to warn about the long expressions that the overflow
20 macros expand to, or about the (X < 0) expressions. */
21 #if 4 < __GNUC__ + (3 <= __GNUC_MINOR__)
22 # pragma GCC diagnostic ignored "-Woverlength-strings"
23 # pragma GCC diagnostic ignored "-Wtype-limits"
25 /* Work around a bug in GCC 6.1 and earlier; see:
26 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68971 */
27 # pragma GCC diagnostic ignored "-Woverflow"
42 /* VERIFY (X) uses a static assertion for compilers that are known to work,
43 and falls back on a dynamic assertion for other compilers.
44 These tests should be checkable via 'verify' rather than 'ASSERT', but
45 using 'verify' would run into a bug with HP-UX 11.23 cc; see
46 <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>. */
47 #if __GNUC__ || __SUNPRO_C
48 # define VERIFY(x) do { verify (x); } while (0)
50 # define VERIFY(x) ASSERT (x)
53 #define DONTCARE __LINE__
58 /* Use VERIFY for tests that must be integer constant expressions,
61 /* TYPE_IS_INTEGER. */
62 ASSERT (TYPE_IS_INTEGER (bool));
63 ASSERT (TYPE_IS_INTEGER (char));
64 ASSERT (TYPE_IS_INTEGER (signed char));
65 ASSERT (TYPE_IS_INTEGER (unsigned char));
66 ASSERT (TYPE_IS_INTEGER (short int));
67 ASSERT (TYPE_IS_INTEGER (unsigned short int));
68 ASSERT (TYPE_IS_INTEGER (int));
69 ASSERT (TYPE_IS_INTEGER (unsigned int));
70 ASSERT (TYPE_IS_INTEGER (long int));
71 ASSERT (TYPE_IS_INTEGER (unsigned long int));
72 ASSERT (TYPE_IS_INTEGER (intmax_t));
73 ASSERT (TYPE_IS_INTEGER (uintmax_t));
74 ASSERT (! TYPE_IS_INTEGER (float));
75 ASSERT (! TYPE_IS_INTEGER (double));
76 ASSERT (! TYPE_IS_INTEGER (long double));
79 /* VERIFY (! TYPE_SIGNED (bool)); // not guaranteed by gnulib substitute */
80 VERIFY (TYPE_SIGNED (signed char));
81 VERIFY (! TYPE_SIGNED (unsigned char));
82 VERIFY (TYPE_SIGNED (short int));
83 VERIFY (! TYPE_SIGNED (unsigned short int));
84 VERIFY (TYPE_SIGNED (int));
85 VERIFY (! TYPE_SIGNED (unsigned int));
86 VERIFY (TYPE_SIGNED (long int));
87 VERIFY (! TYPE_SIGNED (unsigned long int));
88 VERIFY (TYPE_SIGNED (intmax_t));
89 VERIFY (! TYPE_SIGNED (uintmax_t));
90 ASSERT (TYPE_SIGNED (float));
91 ASSERT (TYPE_SIGNED (double));
92 ASSERT (TYPE_SIGNED (long double));
94 /* Integer representation. Check that it is two's complement. */
95 VERIFY (INT_MIN
+ INT_MAX
< 0);
97 /* TYPE_MINIMUM, TYPE_MAXIMUM. */
98 VERIFY (TYPE_MINIMUM (char) == CHAR_MIN
);
99 VERIFY (TYPE_MAXIMUM (char) == CHAR_MAX
);
100 VERIFY (TYPE_MINIMUM (unsigned char) == 0);
101 VERIFY (TYPE_MAXIMUM (unsigned char) == UCHAR_MAX
);
102 VERIFY (TYPE_MINIMUM (signed char) == SCHAR_MIN
);
103 VERIFY (TYPE_MAXIMUM (signed char) == SCHAR_MAX
);
104 VERIFY (TYPE_MINIMUM (short int) == SHRT_MIN
);
105 VERIFY (TYPE_MAXIMUM (short int) == SHRT_MAX
);
106 VERIFY (TYPE_MINIMUM (unsigned short int) == 0);
107 VERIFY (TYPE_MAXIMUM (unsigned short int) == USHRT_MAX
);
108 VERIFY (TYPE_MINIMUM (int) == INT_MIN
);
109 VERIFY (TYPE_MAXIMUM (int) == INT_MAX
);
110 VERIFY (TYPE_MINIMUM (unsigned int) == 0);
111 VERIFY (TYPE_MAXIMUM (unsigned int) == UINT_MAX
);
112 VERIFY (TYPE_MINIMUM (long int) == LONG_MIN
);
113 VERIFY (TYPE_MAXIMUM (long int) == LONG_MAX
);
114 VERIFY (TYPE_MINIMUM (unsigned long int) == 0);
115 VERIFY (TYPE_MAXIMUM (unsigned long int) == ULONG_MAX
);
116 VERIFY (TYPE_MINIMUM (intmax_t) == INTMAX_MIN
);
117 VERIFY (TYPE_MAXIMUM (intmax_t) == INTMAX_MAX
);
118 VERIFY (TYPE_MINIMUM (uintmax_t) == 0);
119 VERIFY (TYPE_MAXIMUM (uintmax_t) == UINTMAX_MAX
);
121 /* INT_BITS_STRLEN_BOUND. */
122 VERIFY (INT_BITS_STRLEN_BOUND (1) == 1);
123 VERIFY (INT_BITS_STRLEN_BOUND (2620) == 789);
125 /* INT_STRLEN_BOUND, INT_BUFSIZE_BOUND. */
126 #ifdef INT32_MAX /* POSIX guarantees int32_t; this ports to non-POSIX. */
127 VERIFY (INT_STRLEN_BOUND (int32_t) == sizeof ("-2147483648") - 1);
128 VERIFY (INT_BUFSIZE_BOUND (int32_t) == sizeof ("-2147483648"));
131 VERIFY (INT_STRLEN_BOUND (int64_t) == sizeof ("-9223372036854775808") - 1);
132 VERIFY (INT_BUFSIZE_BOUND (int64_t) == sizeof ("-9223372036854775808"));
135 /* All the INT_<op>_RANGE_OVERFLOW tests are equally valid as
136 INT_<op>_OVERFLOW tests, so define macros to do both. OP is the
137 operation, OPNAME its symbolic name, A and B its operands, T the
138 result type, V the overflow flag, and VRES the result if V and if
139 two's complement. CHECK_BINOP is for most binary operatinos,
140 CHECK_SBINOP for binary +, -, * when the result type is signed,
141 and CHECK_UNOP for unary operations. */
142 #define CHECK_BINOP(op, opname, a, b, t, v, vres) \
143 VERIFY (INT_##opname##_RANGE_OVERFLOW (a, b, TYPE_MINIMUM (t), \
146 VERIFY (INT_##opname##_OVERFLOW (a, b) == (v))
147 #define CHECK_SBINOP(op, opname, a, b, t, v, vres) \
148 CHECK_BINOP(op, opname, a, b, t, v, vres); \
151 ASSERT (INT_##opname##_WRAPV (a, b, &result) == (v)); \
152 ASSERT (result == ((v) ? (vres) : ((a) op (b)))); \
154 #define CHECK_UNOP(op, opname, a, t, v) \
155 VERIFY (INT_##opname##_RANGE_OVERFLOW (a, TYPE_MINIMUM (t), \
158 VERIFY (INT_##opname##_OVERFLOW (a) == (v))
160 /* INT_<op>_RANGE_OVERFLOW, INT_<op>_OVERFLOW. */
161 VERIFY (INT_ADD_RANGE_OVERFLOW (INT_MAX
, 1, INT_MIN
, INT_MAX
));
162 VERIFY (INT_ADD_OVERFLOW (INT_MAX
, 1));
164 CHECK_SBINOP (+, ADD
, INT_MAX
, 1, int, true, INT_MIN
);
165 CHECK_SBINOP (+, ADD
, INT_MAX
, -1, int, false, INT_MAX
- 1);
166 CHECK_SBINOP (+, ADD
, INT_MIN
, 1, int, false, INT_MIN
+ 1);
167 CHECK_SBINOP (+, ADD
, INT_MIN
, -1, int, true, INT_MAX
);
168 CHECK_BINOP (+, ADD
, UINT_MAX
, 1u, unsigned int, true, 0u);
169 CHECK_BINOP (+, ADD
, 0u, 1u, unsigned int, false, 1u);
171 CHECK_SBINOP (-, SUBTRACT
, INT_MAX
, 1, int, false, INT_MAX
- 1);
172 CHECK_SBINOP (-, SUBTRACT
, INT_MAX
, -1, int, true, INT_MIN
);
173 CHECK_SBINOP (-, SUBTRACT
, INT_MIN
, 1, int, true, INT_MAX
);
174 CHECK_SBINOP (-, SUBTRACT
, INT_MIN
, -1, int, false, INT_MIN
- -1);
175 CHECK_BINOP (-, SUBTRACT
, UINT_MAX
, 1u, unsigned int, false, UINT_MAX
- 1u);
176 CHECK_BINOP (-, SUBTRACT
, 0u, 1u, unsigned int, true, 0u - 1u);
178 CHECK_UNOP (-, NEGATE
, INT_MIN
, int, true);
179 CHECK_UNOP (-, NEGATE
, 0, int, false);
180 CHECK_UNOP (-, NEGATE
, INT_MAX
, int, false);
181 CHECK_UNOP (-, NEGATE
, 0u, unsigned int, false);
182 CHECK_UNOP (-, NEGATE
, 1u, unsigned int, true);
183 CHECK_UNOP (-, NEGATE
, UINT_MAX
, unsigned int, true);
185 CHECK_SBINOP (*, MULTIPLY
, INT_MAX
, INT_MAX
, int, true, 1);
186 CHECK_SBINOP (*, MULTIPLY
, INT_MAX
, INT_MIN
, int, true, INT_MIN
);
187 CHECK_SBINOP (*, MULTIPLY
, INT_MIN
, INT_MAX
, int, true, INT_MIN
);
188 CHECK_SBINOP (*, MULTIPLY
, INT_MIN
, INT_MIN
, int, true, 0);
189 CHECK_SBINOP (*, MULTIPLY
, -1, INT_MIN
, int,
190 INT_NEGATE_OVERFLOW (INT_MIN
), INT_MIN
);
191 CHECK_SBINOP (*, MULTIPLY
, LONG_MIN
/ INT_MAX
, (long int) INT_MAX
,
192 long int, false, LONG_MIN
- LONG_MIN
% INT_MAX
);
194 CHECK_BINOP (/, DIVIDE
, INT_MIN
, -1, int,
195 INT_NEGATE_OVERFLOW (INT_MIN
), INT_MIN
);
196 CHECK_BINOP (/, DIVIDE
, INT_MAX
, 1, int, false, INT_MAX
);
197 CHECK_BINOP (/, DIVIDE
, (unsigned int) INT_MIN
, -1u, unsigned int,
198 false, INT_MIN
/ -1u);
200 CHECK_BINOP (%, REMAINDER
, INT_MIN
, -1, int, INT_NEGATE_OVERFLOW (INT_MIN
), 0);
201 CHECK_BINOP (%, REMAINDER
, INT_MAX
, 1, int, false, 0);
202 CHECK_BINOP (%, REMAINDER
, (unsigned int) INT_MIN
, -1u, unsigned int,
203 false, INT_MIN
% -1u);
205 CHECK_BINOP (<<, LEFT_SHIFT
, UINT_MAX
, 1, unsigned int, true, UINT_MAX
<< 1);
206 CHECK_BINOP (<<, LEFT_SHIFT
, UINT_MAX
/ 2 + 1, 1, unsigned int, true,
207 (UINT_MAX
/ 2 + 1) << 1);
208 CHECK_BINOP (<<, LEFT_SHIFT
, UINT_MAX
/ 2, 1, unsigned int, false,
209 (UINT_MAX
/ 2) << 1);
211 /* INT_<op>_OVERFLOW and INT_<op>_WRAPV with mixed types. */
212 #define CHECK_SUM(a, b, t, v, vres) \
213 CHECK_SUM1(a, b, t, v, vres); \
214 CHECK_SUM1(b, a, t, v, vres)
215 #define CHECK_SSUM(a, b, t, v, vres) \
216 CHECK_SSUM1(a, b, t, v, vres); \
217 CHECK_SSUM1(b, a, t, v, vres)
218 #define CHECK_SUM1(a, b, t, v, vres) \
219 VERIFY (INT_ADD_OVERFLOW (a, b) == (v))
220 #define CHECK_SSUM1(a, b, t, v, vres) \
221 CHECK_SUM1(a, b, t, v, vres); \
224 ASSERT (INT_ADD_WRAPV (a, b, &result) == (v)); \
225 ASSERT (result == ((v) ? (vres) : ((a) + (b)))); \
227 CHECK_SSUM (-1, LONG_MIN
, long int, true, LONG_MAX
);
228 CHECK_SUM (-1, UINT_MAX
, unsigned int, false, DONTCARE
);
229 CHECK_SSUM (-1L, INT_MIN
, long int, INT_MIN
== LONG_MIN
,
230 INT_MIN
== LONG_MIN
? INT_MAX
: DONTCARE
);
231 CHECK_SUM (0u, -1, unsigned int, true, 0u + -1);
232 CHECK_SUM (0u, 0, unsigned int, false, DONTCARE
);
233 CHECK_SUM (0u, 1, unsigned int, false, DONTCARE
);
234 CHECK_SSUM (1, LONG_MAX
, long int, true, LONG_MIN
);
235 CHECK_SUM (1, UINT_MAX
, unsigned int, true, 0u);
236 CHECK_SSUM (1L, INT_MAX
, long int, INT_MAX
== LONG_MAX
,
237 INT_MAX
== LONG_MAX
? INT_MIN
: DONTCARE
);
238 CHECK_SUM (1u, INT_MAX
, unsigned int, INT_MAX
== UINT_MAX
, 1u + INT_MAX
);
239 CHECK_SUM (1u, INT_MIN
, unsigned int, true, 1u + INT_MIN
);
242 ASSERT (INT_ADD_WRAPV (1, INT_MAX
, &result
) == (INT_MAX
== LONG_MAX
));
243 ASSERT (INT_ADD_WRAPV (-1, INT_MIN
, &result
) == (INT_MIN
== LONG_MIN
));
246 #define CHECK_DIFFERENCE(a, b, t, v, vres) \
247 VERIFY (INT_SUBTRACT_OVERFLOW (a, b) == (v))
248 #define CHECK_SDIFFERENCE(a, b, t, v, vres) \
249 CHECK_DIFFERENCE(a, b, t, v, vres); \
252 ASSERT (INT_SUBTRACT_WRAPV (a, b, &result) == (v)); \
253 ASSERT (result == ((v) ? (vres) : ((a) - (b)))); \
255 CHECK_DIFFERENCE (INT_MAX
, 1u, unsigned int, UINT_MAX
< INT_MAX
- 1,
257 CHECK_DIFFERENCE (UINT_MAX
, 1, unsigned int, false, UINT_MAX
- 1);
258 CHECK_DIFFERENCE (0u, -1, unsigned int, false, 0u - -1);
259 CHECK_DIFFERENCE (UINT_MAX
, -1, unsigned int, true, UINT_MAX
- -1);
260 CHECK_DIFFERENCE (INT_MIN
, 1u, unsigned int, true, INT_MIN
- 1u);
261 CHECK_DIFFERENCE (-1, 0u, unsigned int, true, -1 - 0u);
262 CHECK_SDIFFERENCE (-1, INT_MIN
, int, false, -1 - INT_MIN
);
263 CHECK_SDIFFERENCE (-1, INT_MAX
, int, false, -1 - INT_MAX
);
264 CHECK_SDIFFERENCE (0, INT_MIN
, int, INT_MIN
< -INT_MAX
, INT_MIN
);
265 CHECK_SDIFFERENCE (0, INT_MAX
, int, false, 0 - INT_MAX
);
268 ASSERT (INT_SUBTRACT_WRAPV (INT_MAX
, -1, &result
) == (INT_MAX
== LONG_MAX
));
269 ASSERT (INT_SUBTRACT_WRAPV (INT_MIN
, 1, &result
) == (INT_MAX
== LONG_MAX
));
272 #define CHECK_PRODUCT(a, b, t, v, vres) \
273 CHECK_PRODUCT1(a, b, t, v, vres); \
274 CHECK_PRODUCT1(b, a, t, v, vres)
275 #define CHECK_SPRODUCT(a, b, t, v, vres) \
276 CHECK_SPRODUCT1(a, b, t, v, vres); \
277 CHECK_SPRODUCT1(b, a, t, v, vres)
278 #define CHECK_PRODUCT1(a, b, t, v, vres) \
279 VERIFY (INT_MULTIPLY_OVERFLOW (a, b) == (v))
280 #define CHECK_SPRODUCT1(a, b, t, v, vres) \
281 CHECK_PRODUCT1(a, b, t, v, vres); \
284 ASSERT (INT_MULTIPLY_WRAPV (a, b, &result) == (v)); \
285 ASSERT (result == ((v) ? (vres) : ((a) * (b)))); \
287 CHECK_PRODUCT (-1, 1u, unsigned int, true, -1 * 1u);
288 CHECK_SPRODUCT (-1, INT_MIN
, int, INT_NEGATE_OVERFLOW (INT_MIN
), INT_MIN
);
289 CHECK_PRODUCT (-1, UINT_MAX
, unsigned int, true, -1 * UINT_MAX
);
290 CHECK_SPRODUCT (-32768, LONG_MAX
/ -32768 - 1, long int, true, LONG_MIN
);
291 CHECK_SPRODUCT (-12345, LONG_MAX
/ -12345, long int, false, DONTCARE
);
292 CHECK_SPRODUCT (0, -1, int, false, DONTCARE
);
293 CHECK_SPRODUCT (0, 0, int, false, DONTCARE
);
294 CHECK_PRODUCT (0, 0u, unsigned int, false, DONTCARE
);
295 CHECK_SPRODUCT (0, 1, int, false, DONTCARE
);
296 CHECK_SPRODUCT (0, INT_MAX
, int, false, DONTCARE
);
297 CHECK_SPRODUCT (0, INT_MIN
, int, false, DONTCARE
);
298 CHECK_PRODUCT (0, UINT_MAX
, unsigned int, false, DONTCARE
);
299 CHECK_PRODUCT (0u, -1, unsigned int, false, DONTCARE
);
300 CHECK_PRODUCT (0u, 0, unsigned int, false, DONTCARE
);
301 CHECK_PRODUCT (0u, 0u, unsigned int, false, DONTCARE
);
302 CHECK_PRODUCT (0u, 1, unsigned int, false, DONTCARE
);
303 CHECK_PRODUCT (0u, INT_MAX
, unsigned int, false, DONTCARE
);
304 CHECK_PRODUCT (0u, INT_MIN
, unsigned int, false, DONTCARE
);
305 CHECK_PRODUCT (0u, UINT_MAX
, unsigned int, false, DONTCARE
);
306 CHECK_SPRODUCT (1, INT_MAX
, int, false, DONTCARE
);
307 CHECK_SPRODUCT (1, INT_MIN
, int, false, DONTCARE
);
308 CHECK_PRODUCT (1, UINT_MAX
, unsigned int, false, DONTCARE
);
309 CHECK_PRODUCT (1u, INT_MIN
, unsigned int, true, 1u * INT_MIN
);
310 CHECK_PRODUCT (1u, INT_MAX
, unsigned int, UINT_MAX
< INT_MAX
, 1u * INT_MAX
);
311 CHECK_PRODUCT (INT_MAX
, UINT_MAX
, unsigned int, true, INT_MAX
* UINT_MAX
);
312 CHECK_PRODUCT (INT_MAX
, ULONG_MAX
, unsigned long int, true,
313 INT_MAX
* ULONG_MAX
);
314 CHECK_SPRODUCT (INT_MIN
, LONG_MAX
/ INT_MIN
- 1, long int, true, LONG_MIN
);
315 CHECK_SPRODUCT (INT_MIN
, LONG_MAX
/ INT_MIN
, long int, false, DONTCARE
);
316 CHECK_PRODUCT (INT_MIN
, UINT_MAX
, unsigned int, true, INT_MIN
* UINT_MAX
);
317 CHECK_PRODUCT (INT_MIN
, ULONG_MAX
, unsigned long int, true,
318 INT_MIN
* ULONG_MAX
);
321 ASSERT (INT_MULTIPLY_WRAPV (INT_MAX
, INT_MAX
, &result
)
322 == (LONG_MAX
/ INT_MAX
< INT_MAX
));
323 ASSERT (INT_MULTIPLY_WRAPV (INT_MAX
, INT_MAX
, &result
)
324 || result
== INT_MAX
* (long int) INT_MAX
);
325 ASSERT (INT_MULTIPLY_WRAPV (INT_MIN
, INT_MIN
, &result
)
326 || result
== INT_MIN
* (long int) INT_MIN
);
331 long long int result
;
332 ASSERT (INT_MULTIPLY_WRAPV (LONG_MAX
, LONG_MAX
, &result
)
333 == (LLONG_MAX
/ LONG_MAX
< LONG_MAX
));
334 ASSERT (INT_MULTIPLY_WRAPV (LONG_MAX
, LONG_MAX
, &result
)
335 || result
== LONG_MAX
* (long long int) LONG_MAX
);
336 ASSERT (INT_MULTIPLY_WRAPV (LONG_MIN
, LONG_MIN
, &result
)
337 || result
== LONG_MIN
* (long long int) LONG_MIN
);
341 #define CHECK_QUOTIENT(a, b, v) VERIFY (INT_DIVIDE_OVERFLOW (a, b) == (v))
343 CHECK_QUOTIENT (INT_MIN
, -1L, INT_MIN
== LONG_MIN
);
344 CHECK_QUOTIENT (INT_MIN
, UINT_MAX
, false);
345 CHECK_QUOTIENT (INTMAX_MIN
, UINTMAX_MAX
, false);
346 CHECK_QUOTIENT (INTMAX_MIN
, UINT_MAX
, false);
347 CHECK_QUOTIENT (-11, 10u, true);
348 CHECK_QUOTIENT (-10, 10u, true);
349 CHECK_QUOTIENT (-9, 10u, false);
350 CHECK_QUOTIENT (11u, -10, true);
351 CHECK_QUOTIENT (10u, -10, true);
352 CHECK_QUOTIENT (9u, -10, false);
354 #define CHECK_REMAINDER(a, b, v) VERIFY (INT_REMAINDER_OVERFLOW (a, b) == (v))
356 CHECK_REMAINDER (INT_MIN
, -1L, INT_MIN
== LONG_MIN
);
357 CHECK_REMAINDER (-1, UINT_MAX
, true);
358 CHECK_REMAINDER ((intmax_t) -1, UINTMAX_MAX
, true);
359 CHECK_REMAINDER (INTMAX_MIN
, UINT_MAX
,
360 (INTMAX_MAX
< UINT_MAX
361 && - (unsigned int) INTMAX_MIN
% UINT_MAX
!= 0));
362 CHECK_REMAINDER (INT_MIN
, ULONG_MAX
, INT_MIN
% ULONG_MAX
!= 1);
363 CHECK_REMAINDER (1u, -1, false);
364 CHECK_REMAINDER (37*39u, -39, false);
365 CHECK_REMAINDER (37*39u + 1, -39, true);
366 CHECK_REMAINDER (37*39u - 1, -39, true);
367 CHECK_REMAINDER (LONG_MAX
, -INT_MAX
, false);