summaryrefslogtreecommitdiff
path: root/gl/tests/test-intprops.c
blob: 06df5a7c1121a31f0481227bc04a28af1ef7fb5b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
/* Test intprops.h.
   Copyright (C) 2011-2015 Free Software Foundation, Inc.

   This program is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */

/* Written by Paul Eggert.  */

/* Tell gcc not to warn about the many (X < 0) expressions that
   the overflow macros expand to.  */
#if (__GNUC__ == 4 && 3 <= __GNUC_MINOR__) || 4 < __GNUC__
# pragma GCC diagnostic ignored "-Wtype-limits"
#endif

#include <config.h>

#include "intprops.h"
#include "verify.h"

#include <stdbool.h>
#include <inttypes.h>

#include "macros.h"

/* VERIFY (X) uses a static assertion for compilers that are known to work,
   and falls back on a dynamic assertion for other compilers.
   These tests should be checkable via 'verify' rather than 'ASSERT', but
   using 'verify' would run into a bug with HP-UX 11.23 cc; see
   <http://lists.gnu.org/archive/html/bug-gnulib/2011-05/msg00401.html>.  */
#if __GNUC__ || __SUNPRO_C
# define VERIFY(x) do { verify (x); } while (0)
#else
# define VERIFY(x) ASSERT (x)
#endif

int
main (void)
{
  /* Use VERIFY for tests that must be integer constant expressions,
     ASSERT otherwise.  */

  /* TYPE_IS_INTEGER.  */
  ASSERT (TYPE_IS_INTEGER (bool));
  ASSERT (TYPE_IS_INTEGER (char));
  ASSERT (TYPE_IS_INTEGER (signed char));
  ASSERT (TYPE_IS_INTEGER (unsigned char));
  ASSERT (TYPE_IS_INTEGER (short int));
  ASSERT (TYPE_IS_INTEGER (unsigned short int));
  ASSERT (TYPE_IS_INTEGER (int));
  ASSERT (TYPE_IS_INTEGER (unsigned int));
  ASSERT (TYPE_IS_INTEGER (long int));
  ASSERT (TYPE_IS_INTEGER (unsigned long int));
  ASSERT (TYPE_IS_INTEGER (intmax_t));
  ASSERT (TYPE_IS_INTEGER (uintmax_t));
  ASSERT (! TYPE_IS_INTEGER (float));
  ASSERT (! TYPE_IS_INTEGER (double));
  ASSERT (! TYPE_IS_INTEGER (long double));

  /* TYPE_SIGNED.  */
  /* VERIFY (! TYPE_SIGNED (bool)); // not guaranteed by gnulib substitute */
  VERIFY (TYPE_SIGNED (signed char));
  VERIFY (! TYPE_SIGNED (unsigned char));
  VERIFY (TYPE_SIGNED (short int));
  VERIFY (! TYPE_SIGNED (unsigned short int));
  VERIFY (TYPE_SIGNED (int));
  VERIFY (! TYPE_SIGNED (unsigned int));
  VERIFY (TYPE_SIGNED (long int));
  VERIFY (! TYPE_SIGNED (unsigned long int));
  VERIFY (TYPE_SIGNED (intmax_t));
  VERIFY (! TYPE_SIGNED (uintmax_t));
  ASSERT (TYPE_SIGNED (float));
  ASSERT (TYPE_SIGNED (double));
  ASSERT (TYPE_SIGNED (long double));

  /* Integer representation.  */
  VERIFY (INT_MIN + INT_MAX < 0
          ? (TYPE_TWOS_COMPLEMENT (int)
             && ! TYPE_ONES_COMPLEMENT (int) && ! TYPE_SIGNED_MAGNITUDE (int))
          : (! TYPE_TWOS_COMPLEMENT (int)
             && (TYPE_ONES_COMPLEMENT (int) || TYPE_SIGNED_MAGNITUDE (int))));

  /* TYPE_MINIMUM, TYPE_MAXIMUM.  */
  VERIFY (TYPE_MINIMUM (char) == CHAR_MIN);
  VERIFY (TYPE_MAXIMUM (char) == CHAR_MAX);
  VERIFY (TYPE_MINIMUM (unsigned char) == 0);
  VERIFY (TYPE_MAXIMUM (unsigned char) == UCHAR_MAX);
  VERIFY (TYPE_MINIMUM (signed char) == SCHAR_MIN);
  VERIFY (TYPE_MAXIMUM (signed char) == SCHAR_MAX);
  VERIFY (TYPE_MINIMUM (short int) == SHRT_MIN);
  VERIFY (TYPE_MAXIMUM (short int) == SHRT_MAX);
  VERIFY (TYPE_MINIMUM (unsigned short int) == 0);
  VERIFY (TYPE_MAXIMUM (unsigned short int) == USHRT_MAX);
  VERIFY (TYPE_MINIMUM (int) == INT_MIN);
  VERIFY (TYPE_MAXIMUM (int) == INT_MAX);
  VERIFY (TYPE_MINIMUM (unsigned int) == 0);
  VERIFY (TYPE_MAXIMUM (unsigned int) == UINT_MAX);
  VERIFY (TYPE_MINIMUM (long int) == LONG_MIN);
  VERIFY (TYPE_MAXIMUM (long int) == LONG_MAX);
  VERIFY (TYPE_MINIMUM (unsigned long int) == 0);
  VERIFY (TYPE_MAXIMUM (unsigned long int) == ULONG_MAX);
  VERIFY (TYPE_MINIMUM (intmax_t) == INTMAX_MIN);
  VERIFY (TYPE_MAXIMUM (intmax_t) == INTMAX_MAX);
  VERIFY (TYPE_MINIMUM (uintmax_t) == 0);
  VERIFY (TYPE_MAXIMUM (uintmax_t) == UINTMAX_MAX);

  /* INT_BITS_STRLEN_BOUND.  */
  VERIFY (INT_BITS_STRLEN_BOUND (1) == 1);
  VERIFY (INT_BITS_STRLEN_BOUND (2620) == 789);

  /* INT_STRLEN_BOUND, INT_BUFSIZE_BOUND.  */
  #ifdef INT32_MAX /* POSIX guarantees int32_t; this ports to non-POSIX.  */
  VERIFY (INT_STRLEN_BOUND (int32_t) == sizeof ("-2147483648") - 1);
  VERIFY (INT_BUFSIZE_BOUND (int32_t) == sizeof ("-2147483648"));
  #endif
  #ifdef INT64_MAX
  VERIFY (INT_STRLEN_BOUND (int64_t) == sizeof ("-9223372036854775808") - 1);
  VERIFY (INT_BUFSIZE_BOUND (int64_t) == sizeof ("-9223372036854775808"));
  #endif

  /* All the INT_<op>_RANGE_OVERFLOW tests are equally valid as
     INT_<op>_OVERFLOW tests, so define a single macro to do both.  */
  #define CHECK_BINOP(op, a, b, min, max, overflow)                      \
    (INT_##op##_RANGE_OVERFLOW (a, b, min, max) == (overflow)            \
     && INT_##op##_OVERFLOW (a, b) == (overflow))
  #define CHECK_UNOP(op, a, min, max, overflow)                          \
    (INT_##op##_RANGE_OVERFLOW (a, min, max) == (overflow)               \
     && INT_##op##_OVERFLOW (a) == (overflow))

  /* INT_<op>_RANGE_OVERFLOW, INT_<op>_OVERFLOW.  */
  VERIFY (INT_ADD_RANGE_OVERFLOW (INT_MAX, 1, INT_MIN, INT_MAX));
  VERIFY (INT_ADD_OVERFLOW (INT_MAX, 1));
  VERIFY (CHECK_BINOP (ADD, INT_MAX, 1, INT_MIN, INT_MAX, true));
  VERIFY (CHECK_BINOP (ADD, INT_MAX, -1, INT_MIN, INT_MAX, false));
  VERIFY (CHECK_BINOP (ADD, INT_MIN, 1, INT_MIN, INT_MAX, false));
  VERIFY (CHECK_BINOP (ADD, INT_MIN, -1, INT_MIN, INT_MAX, true));
  VERIFY (CHECK_BINOP (ADD, UINT_MAX, 1u, 0u, UINT_MAX, true));
  VERIFY (CHECK_BINOP (ADD, 0u, 1u, 0u, UINT_MAX, false));

  VERIFY (CHECK_BINOP (SUBTRACT, INT_MAX, 1, INT_MIN, INT_MAX, false));
  VERIFY (CHECK_BINOP (SUBTRACT, INT_MAX, -1, INT_MIN, INT_MAX, true));
  VERIFY (CHECK_BINOP (SUBTRACT, INT_MIN, 1, INT_MIN, INT_MAX, true));
  VERIFY (CHECK_BINOP (SUBTRACT, INT_MIN, -1, INT_MIN, INT_MAX, false));
  VERIFY (CHECK_BINOP (SUBTRACT, UINT_MAX, 1u, 0u, UINT_MAX, false));
  VERIFY (CHECK_BINOP (SUBTRACT, 0u, 1u, 0u, UINT_MAX, true));

  VERIFY (CHECK_UNOP (NEGATE, INT_MIN, INT_MIN, INT_MAX,
                      TYPE_TWOS_COMPLEMENT (int)));
  VERIFY (CHECK_UNOP (NEGATE, 0, INT_MIN, INT_MAX, false));
  VERIFY (CHECK_UNOP (NEGATE, INT_MAX, INT_MIN, INT_MAX, false));
  VERIFY (CHECK_UNOP (NEGATE, 0u, 0u, UINT_MAX, false));
  VERIFY (CHECK_UNOP (NEGATE, 1u, 0u, UINT_MAX, true));
  VERIFY (CHECK_UNOP (NEGATE, UINT_MAX, 0u, UINT_MAX, true));

  VERIFY (CHECK_BINOP (MULTIPLY, INT_MAX, INT_MAX, INT_MIN, INT_MAX, true));
  VERIFY (CHECK_BINOP (MULTIPLY, INT_MAX, INT_MIN, INT_MIN, INT_MAX, true));
  VERIFY (CHECK_BINOP (MULTIPLY, INT_MIN, INT_MAX, INT_MIN, INT_MAX, true));
  VERIFY (CHECK_BINOP (MULTIPLY, INT_MIN, INT_MIN, INT_MIN, INT_MAX, true));
  VERIFY (CHECK_BINOP (MULTIPLY, -1, INT_MIN, INT_MIN, INT_MAX,
                       INT_NEGATE_OVERFLOW (INT_MIN)));
  VERIFY (CHECK_BINOP (MULTIPLY, LONG_MIN / INT_MAX, (long int) INT_MAX,
                       LONG_MIN, LONG_MIN, false));

  VERIFY (CHECK_BINOP (DIVIDE, INT_MIN, -1, INT_MIN, INT_MAX,
                       INT_NEGATE_OVERFLOW (INT_MIN)));
  VERIFY (CHECK_BINOP (DIVIDE, INT_MAX, 1, INT_MIN, INT_MAX, false));
  VERIFY (CHECK_BINOP (DIVIDE, (unsigned int) INT_MIN,
                       -1u, 0u, UINT_MAX, false));

  VERIFY (CHECK_BINOP (REMAINDER, INT_MIN, -1, INT_MIN, INT_MAX,
                       INT_NEGATE_OVERFLOW (INT_MIN)));
  VERIFY (CHECK_BINOP (REMAINDER, INT_MAX, 1, INT_MIN, INT_MAX, false));
  VERIFY (CHECK_BINOP (REMAINDER, (unsigned int) INT_MIN,
                       -1u, 0u, UINT_MAX, false));

  VERIFY (CHECK_BINOP (LEFT_SHIFT, UINT_MAX, 1, 0u, UINT_MAX, true));
  VERIFY (CHECK_BINOP (LEFT_SHIFT, UINT_MAX / 2 + 1, 1, 0u, UINT_MAX, true));
  VERIFY (CHECK_BINOP (LEFT_SHIFT, UINT_MAX / 2, 1, 0u, UINT_MAX, false));

  /* INT_<op>_OVERFLOW with mixed types.  */
  #define CHECK_SUM(a, b, overflow)                       \
    VERIFY (INT_ADD_OVERFLOW (a, b) == (overflow));       \
    VERIFY (INT_ADD_OVERFLOW (b, a) == (overflow))
  CHECK_SUM (-1, LONG_MIN, true);
  CHECK_SUM (-1, UINT_MAX, false);
  CHECK_SUM (-1L, INT_MIN, INT_MIN == LONG_MIN);
  CHECK_SUM (0u, -1, true);
  CHECK_SUM (0u, 0, false);
  CHECK_SUM (0u, 1, false);
  CHECK_SUM (1, LONG_MAX, true);
  CHECK_SUM (1, UINT_MAX, true);
  CHECK_SUM (1L, INT_MAX, INT_MAX == LONG_MAX);
  CHECK_SUM (1u, INT_MAX, INT_MAX == UINT_MAX);
  CHECK_SUM (1u, INT_MIN, true);

  VERIFY (! INT_SUBTRACT_OVERFLOW (INT_MAX, 1u));
  VERIFY (! INT_SUBTRACT_OVERFLOW (UINT_MAX, 1));
  VERIFY (! INT_SUBTRACT_OVERFLOW (0u, -1));
  VERIFY (INT_SUBTRACT_OVERFLOW (UINT_MAX, -1));
  VERIFY (INT_SUBTRACT_OVERFLOW (INT_MIN, 1u));
  VERIFY (INT_SUBTRACT_OVERFLOW (-1, 0u));

  #define CHECK_PRODUCT(a, b, overflow)                   \
    VERIFY (INT_MULTIPLY_OVERFLOW (a, b) == (overflow));   \
    VERIFY (INT_MULTIPLY_OVERFLOW (b, a) == (overflow))

  CHECK_PRODUCT (-1, 1u, true);
  CHECK_PRODUCT (-1, INT_MIN, INT_NEGATE_OVERFLOW (INT_MIN));
  CHECK_PRODUCT (-1, UINT_MAX, true);
  CHECK_PRODUCT (-12345, LONG_MAX / -12345 - 1, true);
  CHECK_PRODUCT (-12345, LONG_MAX / -12345, false);
  CHECK_PRODUCT (0, -1, false);
  CHECK_PRODUCT (0, 0, false);
  CHECK_PRODUCT (0, 0u, false);
  CHECK_PRODUCT (0, 1, false);
  CHECK_PRODUCT (0, INT_MAX, false);
  CHECK_PRODUCT (0, INT_MIN, false);
  CHECK_PRODUCT (0, UINT_MAX, false);
  CHECK_PRODUCT (0u, -1, false);
  CHECK_PRODUCT (0u, 0, false);
  CHECK_PRODUCT (0u, 0u, false);
  CHECK_PRODUCT (0u, 1, false);
  CHECK_PRODUCT (0u, INT_MAX, false);
  CHECK_PRODUCT (0u, INT_MIN, false);
  CHECK_PRODUCT (0u, UINT_MAX, false);
  CHECK_PRODUCT (1, INT_MAX, false);
  CHECK_PRODUCT (1, INT_MIN, false);
  CHECK_PRODUCT (1, UINT_MAX, false);
  CHECK_PRODUCT (1u, INT_MIN, true);
  CHECK_PRODUCT (1u, INT_MAX, UINT_MAX < INT_MAX);
  CHECK_PRODUCT (INT_MAX, UINT_MAX, true);
  CHECK_PRODUCT (INT_MAX, ULONG_MAX, true);
  CHECK_PRODUCT (INT_MIN, LONG_MAX / INT_MIN - 1, true);
  CHECK_PRODUCT (INT_MIN, LONG_MAX / INT_MIN, false);
  CHECK_PRODUCT (INT_MIN, UINT_MAX, true);
  CHECK_PRODUCT (INT_MIN, ULONG_MAX, true);

  VERIFY (INT_DIVIDE_OVERFLOW (INT_MIN, -1L)
          == (TYPE_TWOS_COMPLEMENT (long int) && INT_MIN == LONG_MIN));
  VERIFY (! INT_DIVIDE_OVERFLOW (INT_MIN, UINT_MAX));
  VERIFY (! INT_DIVIDE_OVERFLOW (INTMAX_MIN, UINTMAX_MAX));
  VERIFY (! INT_DIVIDE_OVERFLOW (INTMAX_MIN, UINT_MAX));
  VERIFY (INT_DIVIDE_OVERFLOW (-11, 10u));
  VERIFY (INT_DIVIDE_OVERFLOW (-10, 10u));
  VERIFY (! INT_DIVIDE_OVERFLOW (-9, 10u));
  VERIFY (INT_DIVIDE_OVERFLOW (11u, -10));
  VERIFY (INT_DIVIDE_OVERFLOW (10u, -10));
  VERIFY (! INT_DIVIDE_OVERFLOW (9u, -10));

  VERIFY (INT_REMAINDER_OVERFLOW (INT_MIN, -1L)
          == (TYPE_TWOS_COMPLEMENT (long int) && INT_MIN == LONG_MIN));
  VERIFY (INT_REMAINDER_OVERFLOW (-1, UINT_MAX));
  VERIFY (INT_REMAINDER_OVERFLOW ((intmax_t) -1, UINTMAX_MAX));
  VERIFY (INT_REMAINDER_OVERFLOW (INTMAX_MIN, UINT_MAX)
          == (INTMAX_MAX < UINT_MAX
              && - (unsigned int) INTMAX_MIN % UINT_MAX != 0));
  VERIFY (INT_REMAINDER_OVERFLOW (INT_MIN, ULONG_MAX)
          == (INT_MIN % ULONG_MAX != 1));
  VERIFY (! INT_REMAINDER_OVERFLOW (1u, -1));
  VERIFY (! INT_REMAINDER_OVERFLOW (37*39u, -39));
  VERIFY (INT_REMAINDER_OVERFLOW (37*39u + 1, -39));
  VERIFY (INT_REMAINDER_OVERFLOW (37*39u - 1, -39));
  VERIFY (! INT_REMAINDER_OVERFLOW (LONG_MAX, -INT_MAX));

  return 0;
}