avoid unnecessary newline
[gnutls.git] / gl / tests / test-intprops.c
blobaeb116813fd7b520bc70096eeac7fde7901bd466
1 /* Test intprops.h.
2 Copyright (C) 2011-2012 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 many (X < 0) expressions that
20 the overflow macros expand to. */
21 #if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__
22 # pragma GCC diagnostic ignored "-Wtype-limits"
23 #endif
25 #include <config.h>
27 #include "intprops.h"
28 #include "verify.h"
30 #include <stdbool.h>
31 #include <inttypes.h>
33 #include "macros.h"
35 /* VERIFY (X) uses a static assertion for compilers that are known to work,
36 and falls back on a dynamic assertion for other compilers.
37 These tests should be checkable via 'verify' rather than 'ASSERT', but
38 using 'verify' would run into a bug with HP-UX 11.23 cc; see
39 <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>. */
40 #if __GNUC__ || __SUNPRO_C
41 # define VERIFY(x) do { verify (x); } while (0)
42 #else
43 # define VERIFY(x) ASSERT (x)
44 #endif
46 int
47 main (void)
49 /* Use VERIFY for tests that must be integer constant expressions,
50 ASSERT otherwise. */
52 /* TYPE_IS_INTEGER. */
53 ASSERT (TYPE_IS_INTEGER (bool));
54 ASSERT (TYPE_IS_INTEGER (char));
55 ASSERT (TYPE_IS_INTEGER (signed char));
56 ASSERT (TYPE_IS_INTEGER (unsigned char));
57 ASSERT (TYPE_IS_INTEGER (short int));
58 ASSERT (TYPE_IS_INTEGER (unsigned short int));
59 ASSERT (TYPE_IS_INTEGER (int));
60 ASSERT (TYPE_IS_INTEGER (unsigned int));
61 ASSERT (TYPE_IS_INTEGER (long int));
62 ASSERT (TYPE_IS_INTEGER (unsigned long int));
63 ASSERT (TYPE_IS_INTEGER (intmax_t));
64 ASSERT (TYPE_IS_INTEGER (uintmax_t));
65 ASSERT (! TYPE_IS_INTEGER (float));
66 ASSERT (! TYPE_IS_INTEGER (double));
67 ASSERT (! TYPE_IS_INTEGER (long double));
69 /* TYPE_SIGNED. */
70 /* VERIFY (! TYPE_SIGNED (bool)); // not guaranteed by gnulib substitute */
71 VERIFY (TYPE_SIGNED (signed char));
72 VERIFY (! TYPE_SIGNED (unsigned char));
73 VERIFY (TYPE_SIGNED (short int));
74 VERIFY (! TYPE_SIGNED (unsigned short int));
75 VERIFY (TYPE_SIGNED (int));
76 VERIFY (! TYPE_SIGNED (unsigned int));
77 VERIFY (TYPE_SIGNED (long int));
78 VERIFY (! TYPE_SIGNED (unsigned long int));
79 VERIFY (TYPE_SIGNED (intmax_t));
80 VERIFY (! TYPE_SIGNED (uintmax_t));
81 ASSERT (TYPE_SIGNED (float));
82 ASSERT (TYPE_SIGNED (double));
83 ASSERT (TYPE_SIGNED (long double));
85 /* Integer representation. */
86 VERIFY (INT_MIN + INT_MAX < 0
87 ? (TYPE_TWOS_COMPLEMENT (int)
88 && ! TYPE_ONES_COMPLEMENT (int) && ! TYPE_SIGNED_MAGNITUDE (int))
89 : (! TYPE_TWOS_COMPLEMENT (int)
90 && (TYPE_ONES_COMPLEMENT (int) || TYPE_SIGNED_MAGNITUDE (int))));
92 /* TYPE_MINIMUM, TYPE_MAXIMUM. */
93 VERIFY (TYPE_MINIMUM (char) == CHAR_MIN);
94 VERIFY (TYPE_MAXIMUM (char) == CHAR_MAX);
95 VERIFY (TYPE_MINIMUM (unsigned char) == 0);
96 VERIFY (TYPE_MAXIMUM (unsigned char) == UCHAR_MAX);
97 VERIFY (TYPE_MINIMUM (signed char) == SCHAR_MIN);
98 VERIFY (TYPE_MAXIMUM (signed char) == SCHAR_MAX);
99 VERIFY (TYPE_MINIMUM (short int) == SHRT_MIN);
100 VERIFY (TYPE_MAXIMUM (short int) == SHRT_MAX);
101 VERIFY (TYPE_MINIMUM (unsigned short int) == 0);
102 VERIFY (TYPE_MAXIMUM (unsigned short int) == USHRT_MAX);
103 VERIFY (TYPE_MINIMUM (int) == INT_MIN);
104 VERIFY (TYPE_MAXIMUM (int) == INT_MAX);
105 VERIFY (TYPE_MINIMUM (unsigned int) == 0);
106 VERIFY (TYPE_MAXIMUM (unsigned int) == UINT_MAX);
107 VERIFY (TYPE_MINIMUM (long int) == LONG_MIN);
108 VERIFY (TYPE_MAXIMUM (long int) == LONG_MAX);
109 VERIFY (TYPE_MINIMUM (unsigned long int) == 0);
110 VERIFY (TYPE_MAXIMUM (unsigned long int) == ULONG_MAX);
111 VERIFY (TYPE_MINIMUM (intmax_t) == INTMAX_MIN);
112 VERIFY (TYPE_MAXIMUM (intmax_t) == INTMAX_MAX);
113 VERIFY (TYPE_MINIMUM (uintmax_t) == 0);
114 VERIFY (TYPE_MAXIMUM (uintmax_t) == UINTMAX_MAX);
116 /* INT_BITS_STRLEN_BOUND. */
117 VERIFY (INT_BITS_STRLEN_BOUND (1) == 1);
118 VERIFY (INT_BITS_STRLEN_BOUND (2620) == 789);
120 /* INT_STRLEN_BOUND, INT_BUFSIZE_BOUND. */
121 #ifdef INT32_MAX /* POSIX guarantees int32_t; this ports to non-POSIX. */
122 VERIFY (INT_STRLEN_BOUND (int32_t) == sizeof ("-2147483648") - 1);
123 VERIFY (INT_BUFSIZE_BOUND (int32_t) == sizeof ("-2147483648"));
124 #endif
125 #ifdef INT64_MAX
126 VERIFY (INT_STRLEN_BOUND (int64_t) == sizeof ("-9223372036854775808") - 1);
127 VERIFY (INT_BUFSIZE_BOUND (int64_t) == sizeof ("-9223372036854775808"));
128 #endif
130 /* All the INT_<op>_RANGE_OVERFLOW tests are equally valid as
131 INT_<op>_OVERFLOW tests, so define a single macro to do both. */
132 #define CHECK_BINOP(op, a, b, min, max, overflow) \
133 (INT_##op##_RANGE_OVERFLOW (a, b, min, max) == (overflow) \
134 && INT_##op##_OVERFLOW (a, b) == (overflow))
135 #define CHECK_UNOP(op, a, min, max, overflow) \
136 (INT_##op##_RANGE_OVERFLOW (a, min, max) == (overflow) \
137 && INT_##op##_OVERFLOW (a) == (overflow))
139 /* INT_<op>_RANGE_OVERFLOW, INT_<op>_OVERFLOW. */
140 VERIFY (INT_ADD_RANGE_OVERFLOW (INT_MAX, 1, INT_MIN, INT_MAX));
141 VERIFY (INT_ADD_OVERFLOW (INT_MAX, 1));
142 VERIFY (CHECK_BINOP (ADD, INT_MAX, 1, INT_MIN, INT_MAX, true));
143 VERIFY (CHECK_BINOP (ADD, INT_MAX, -1, INT_MIN, INT_MAX, false));
144 VERIFY (CHECK_BINOP (ADD, INT_MIN, 1, INT_MIN, INT_MAX, false));
145 VERIFY (CHECK_BINOP (ADD, INT_MIN, -1, INT_MIN, INT_MAX, true));
146 VERIFY (CHECK_BINOP (ADD, UINT_MAX, 1u, 0u, UINT_MAX, true));
147 VERIFY (CHECK_BINOP (ADD, 0u, 1u, 0u, UINT_MAX, false));
149 VERIFY (CHECK_BINOP (SUBTRACT, INT_MAX, 1, INT_MIN, INT_MAX, false));
150 VERIFY (CHECK_BINOP (SUBTRACT, INT_MAX, -1, INT_MIN, INT_MAX, true));
151 VERIFY (CHECK_BINOP (SUBTRACT, INT_MIN, 1, INT_MIN, INT_MAX, true));
152 VERIFY (CHECK_BINOP (SUBTRACT, INT_MIN, -1, INT_MIN, INT_MAX, false));
153 VERIFY (CHECK_BINOP (SUBTRACT, UINT_MAX, 1u, 0u, UINT_MAX, false));
154 VERIFY (CHECK_BINOP (SUBTRACT, 0u, 1u, 0u, UINT_MAX, true));
156 VERIFY (CHECK_UNOP (NEGATE, INT_MIN, INT_MIN, INT_MAX,
157 TYPE_TWOS_COMPLEMENT (int)));
158 VERIFY (CHECK_UNOP (NEGATE, 0, INT_MIN, INT_MAX, false));
159 VERIFY (CHECK_UNOP (NEGATE, INT_MAX, INT_MIN, INT_MAX, false));
160 VERIFY (CHECK_UNOP (NEGATE, 0u, 0u, UINT_MAX, false));
161 VERIFY (CHECK_UNOP (NEGATE, 1u, 0u, UINT_MAX, true));
162 VERIFY (CHECK_UNOP (NEGATE, UINT_MAX, 0u, UINT_MAX, true));
164 VERIFY (CHECK_BINOP (MULTIPLY, INT_MAX, INT_MAX, INT_MIN, INT_MAX, true));
165 VERIFY (CHECK_BINOP (MULTIPLY, INT_MAX, INT_MIN, INT_MIN, INT_MAX, true));
166 VERIFY (CHECK_BINOP (MULTIPLY, INT_MIN, INT_MAX, INT_MIN, INT_MAX, true));
167 VERIFY (CHECK_BINOP (MULTIPLY, INT_MIN, INT_MIN, INT_MIN, INT_MAX, true));
168 VERIFY (CHECK_BINOP (MULTIPLY, -1, INT_MIN, INT_MIN, INT_MAX,
169 INT_NEGATE_OVERFLOW (INT_MIN)));
170 VERIFY (CHECK_BINOP (MULTIPLY, LONG_MIN / INT_MAX, (long int) INT_MAX,
171 LONG_MIN, LONG_MIN, false));
173 VERIFY (CHECK_BINOP (DIVIDE, INT_MIN, -1, INT_MIN, INT_MAX,
174 INT_NEGATE_OVERFLOW (INT_MIN)));
175 VERIFY (CHECK_BINOP (DIVIDE, INT_MAX, 1, INT_MIN, INT_MAX, false));
176 VERIFY (CHECK_BINOP (DIVIDE, (unsigned int) INT_MIN,
177 -1u, 0u, UINT_MAX, false));
179 VERIFY (CHECK_BINOP (REMAINDER, INT_MIN, -1, INT_MIN, INT_MAX,
180 INT_NEGATE_OVERFLOW (INT_MIN)));
181 VERIFY (CHECK_BINOP (REMAINDER, INT_MAX, 1, INT_MIN, INT_MAX, false));
182 VERIFY (CHECK_BINOP (REMAINDER, (unsigned int) INT_MIN,
183 -1u, 0u, UINT_MAX, false));
185 VERIFY (CHECK_BINOP (LEFT_SHIFT, UINT_MAX, 1, 0u, UINT_MAX, true));
186 VERIFY (CHECK_BINOP (LEFT_SHIFT, UINT_MAX / 2 + 1, 1, 0u, UINT_MAX, true));
187 VERIFY (CHECK_BINOP (LEFT_SHIFT, UINT_MAX / 2, 1, 0u, UINT_MAX, false));
189 /* INT_<op>_OVERFLOW with mixed types. */
190 #define CHECK_SUM(a, b, overflow) \
191 VERIFY (INT_ADD_OVERFLOW (a, b) == (overflow)); \
192 VERIFY (INT_ADD_OVERFLOW (b, a) == (overflow))
193 CHECK_SUM (-1, LONG_MIN, true);
194 CHECK_SUM (-1, UINT_MAX, false);
195 CHECK_SUM (-1L, INT_MIN, INT_MIN == LONG_MIN);
196 CHECK_SUM (0u, -1, true);
197 CHECK_SUM (0u, 0, false);
198 CHECK_SUM (0u, 1, false);
199 CHECK_SUM (1, LONG_MAX, true);
200 CHECK_SUM (1, UINT_MAX, true);
201 CHECK_SUM (1L, INT_MAX, INT_MAX == LONG_MAX);
202 CHECK_SUM (1u, INT_MAX, INT_MAX == UINT_MAX);
203 CHECK_SUM (1u, INT_MIN, true);
205 VERIFY (! INT_SUBTRACT_OVERFLOW (INT_MAX, 1u));
206 VERIFY (! INT_SUBTRACT_OVERFLOW (UINT_MAX, 1));
207 VERIFY (! INT_SUBTRACT_OVERFLOW (0u, -1));
208 VERIFY (INT_SUBTRACT_OVERFLOW (UINT_MAX, -1));
209 VERIFY (INT_SUBTRACT_OVERFLOW (INT_MIN, 1u));
210 VERIFY (INT_SUBTRACT_OVERFLOW (-1, 0u));
212 #define CHECK_PRODUCT(a, b, overflow) \
213 VERIFY (INT_MULTIPLY_OVERFLOW (a, b) == (overflow)); \
214 VERIFY (INT_MULTIPLY_OVERFLOW (b, a) == (overflow))
216 CHECK_PRODUCT (-1, 1u, true);
217 CHECK_PRODUCT (-1, INT_MIN, INT_NEGATE_OVERFLOW (INT_MIN));
218 CHECK_PRODUCT (-1, UINT_MAX, true);
219 CHECK_PRODUCT (-12345, LONG_MAX / -12345 - 1, true);
220 CHECK_PRODUCT (-12345, LONG_MAX / -12345, false);
221 CHECK_PRODUCT (0, -1, false);
222 CHECK_PRODUCT (0, 0, false);
223 CHECK_PRODUCT (0, 0u, false);
224 CHECK_PRODUCT (0, 1, false);
225 CHECK_PRODUCT (0, INT_MAX, false);
226 CHECK_PRODUCT (0, INT_MIN, false);
227 CHECK_PRODUCT (0, UINT_MAX, false);
228 CHECK_PRODUCT (0u, -1, false);
229 CHECK_PRODUCT (0u, 0, false);
230 CHECK_PRODUCT (0u, 0u, false);
231 CHECK_PRODUCT (0u, 1, false);
232 CHECK_PRODUCT (0u, INT_MAX, false);
233 CHECK_PRODUCT (0u, INT_MIN, false);
234 CHECK_PRODUCT (0u, UINT_MAX, false);
235 CHECK_PRODUCT (1, INT_MAX, false);
236 CHECK_PRODUCT (1, INT_MIN, false);
237 CHECK_PRODUCT (1, UINT_MAX, false);
238 CHECK_PRODUCT (1u, INT_MIN, true);
239 CHECK_PRODUCT (1u, INT_MAX, UINT_MAX < INT_MAX);
240 CHECK_PRODUCT (INT_MAX, UINT_MAX, true);
241 CHECK_PRODUCT (INT_MAX, ULONG_MAX, true);
242 CHECK_PRODUCT (INT_MIN, LONG_MAX / INT_MIN - 1, true);
243 CHECK_PRODUCT (INT_MIN, LONG_MAX / INT_MIN, false);
244 CHECK_PRODUCT (INT_MIN, UINT_MAX, true);
245 CHECK_PRODUCT (INT_MIN, ULONG_MAX, true);
247 VERIFY (INT_DIVIDE_OVERFLOW (INT_MIN, -1L)
248 == (TYPE_TWOS_COMPLEMENT (long int) && INT_MIN == LONG_MIN));
249 VERIFY (! INT_DIVIDE_OVERFLOW (INT_MIN, UINT_MAX));
250 VERIFY (! INT_DIVIDE_OVERFLOW (INTMAX_MIN, UINTMAX_MAX));
251 VERIFY (! INT_DIVIDE_OVERFLOW (INTMAX_MIN, UINT_MAX));
252 VERIFY (INT_DIVIDE_OVERFLOW (-11, 10u));
253 VERIFY (INT_DIVIDE_OVERFLOW (-10, 10u));
254 VERIFY (! INT_DIVIDE_OVERFLOW (-9, 10u));
255 VERIFY (INT_DIVIDE_OVERFLOW (11u, -10));
256 VERIFY (INT_DIVIDE_OVERFLOW (10u, -10));
257 VERIFY (! INT_DIVIDE_OVERFLOW (9u, -10));
259 VERIFY (INT_REMAINDER_OVERFLOW (INT_MIN, -1L)
260 == (TYPE_TWOS_COMPLEMENT (long int) && INT_MIN == LONG_MIN));
261 VERIFY (INT_REMAINDER_OVERFLOW (-1, UINT_MAX));
262 VERIFY (INT_REMAINDER_OVERFLOW ((intmax_t) -1, UINTMAX_MAX));
263 VERIFY (INT_REMAINDER_OVERFLOW (INTMAX_MIN, UINT_MAX)
264 == (INTMAX_MAX < UINT_MAX
265 && - (unsigned int) INTMAX_MIN % UINT_MAX != 0));
266 VERIFY (INT_REMAINDER_OVERFLOW (INT_MIN, ULONG_MAX)
267 == (INT_MIN % ULONG_MAX != 1));
268 VERIFY (! INT_REMAINDER_OVERFLOW (1u, -1));
269 VERIFY (! INT_REMAINDER_OVERFLOW (37*39u, -39));
270 VERIFY (INT_REMAINDER_OVERFLOW (37*39u + 1, -39));
271 VERIFY (INT_REMAINDER_OVERFLOW (37*39u - 1, -39));
272 VERIFY (! INT_REMAINDER_OVERFLOW (LONG_MAX, -INT_MAX));
274 return 0;