annotate tests/test-intprops.c @ 14741:eb6ac457b650

intprops-tests: new module * modules/intprops-tests, tests/test-intprops.c: New files.
author Paul Eggert <eggert@cs.ucla.edu>
date Sun, 15 May 2011 09:37:21 -0700
parents
children 6c5502aeb486
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
14741
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
1 /* Test intprops.h.
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
2 Copyright (C) 2011 Free Software Foundation, Inc.
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
3
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
4 This program is free software: you can redistribute it and/or modify
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
5 it under the terms of the GNU General Public License as published by
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
6 the Free Software Foundation; either version 3 of the License, or
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
7 (at your option) any later version.
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
8
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
9 This program is distributed in the hope that it will be useful,
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
12 GNU General Public License for more details.
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
13
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
14 You should have received a copy of the GNU General Public License
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
16
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
17 /* Written by Paul Eggert. */
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
18
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
19 #include <config.h>
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
20
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
21 #include "intprops.h"
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
22 #include "verify.h"
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
23
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
24 #include <stdbool.h>
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
25 #include <inttypes.h>
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
26
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
27 /* TYPE_IS_INTEGER. */
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
28 verify (TYPE_IS_INTEGER (bool));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
29 verify (TYPE_IS_INTEGER (char));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
30 verify (TYPE_IS_INTEGER (signed char));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
31 verify (TYPE_IS_INTEGER (unsigned char));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
32 verify (TYPE_IS_INTEGER (short int));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
33 verify (TYPE_IS_INTEGER (unsigned short int));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
34 verify (TYPE_IS_INTEGER (int));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
35 verify (TYPE_IS_INTEGER (unsigned int));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
36 verify (TYPE_IS_INTEGER (long int));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
37 verify (TYPE_IS_INTEGER (unsigned long int));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
38 verify (TYPE_IS_INTEGER (intmax_t));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
39 verify (TYPE_IS_INTEGER (uintmax_t));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
40 verify (! TYPE_IS_INTEGER (float));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
41 verify (! TYPE_IS_INTEGER (double));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
42 verify (! TYPE_IS_INTEGER (long double));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
43
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
44 /* Integer representation. */
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
45 verify (INT_MIN + INT_MAX < 0
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
46 ? (TYPE_TWOS_COMPLEMENT (int)
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
47 && ! TYPE_ONES_COMPLEMENT (int) && ! TYPE_SIGNED_MAGNITUDE (int))
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
48 : (! TYPE_TWOS_COMPLEMENT (int)
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
49 && (TYPE_ONES_COMPLEMENT (int) || TYPE_SIGNED_MAGNITUDE (int))));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
50
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
51 /* TYPE_SIGNED. */
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
52 /* verify (! TYPE_SIGNED (bool)); */ /* not guaranteed by gnulib substitute */
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
53 verify (TYPE_SIGNED (signed char));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
54 verify (! TYPE_SIGNED (unsigned char));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
55 verify (TYPE_SIGNED (short int));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
56 verify (! TYPE_SIGNED (unsigned short int));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
57 verify (TYPE_SIGNED (int));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
58 verify (! TYPE_SIGNED (unsigned int));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
59 verify (TYPE_SIGNED (long int));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
60 verify (! TYPE_SIGNED (unsigned long int));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
61 verify (TYPE_SIGNED (intmax_t));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
62 verify (! TYPE_SIGNED (uintmax_t));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
63 verify (TYPE_SIGNED (float));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
64 verify (TYPE_SIGNED (double));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
65 verify (TYPE_SIGNED (long double));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
66
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
67 /* TYPE_MINIMUM, TYPE_MAXIMUM. */
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
68 verify (TYPE_MINIMUM (char) == CHAR_MIN);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
69 verify (TYPE_MAXIMUM (char) == CHAR_MAX);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
70 verify (TYPE_MINIMUM (unsigned char) == 0);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
71 verify (TYPE_MAXIMUM (unsigned char) == UCHAR_MAX);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
72 verify (TYPE_MINIMUM (signed char) == SCHAR_MIN);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
73 verify (TYPE_MAXIMUM (signed char) == SCHAR_MAX);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
74 verify (TYPE_MINIMUM (short int) == SHRT_MIN);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
75 verify (TYPE_MAXIMUM (short int) == SHRT_MAX);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
76 verify (TYPE_MINIMUM (unsigned short int) == 0);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
77 verify (TYPE_MAXIMUM (unsigned short int) == USHRT_MAX);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
78 verify (TYPE_MINIMUM (int) == INT_MIN);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
79 verify (TYPE_MAXIMUM (int) == INT_MAX);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
80 verify (TYPE_MINIMUM (unsigned int) == 0);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
81 verify (TYPE_MAXIMUM (unsigned int) == UINT_MAX);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
82 verify (TYPE_MINIMUM (long int) == LONG_MIN);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
83 verify (TYPE_MAXIMUM (long int) == LONG_MAX);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
84 verify (TYPE_MINIMUM (unsigned long int) == 0);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
85 verify (TYPE_MAXIMUM (unsigned long int) == ULONG_MAX);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
86 verify (TYPE_MINIMUM (intmax_t) == INTMAX_MIN);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
87 verify (TYPE_MAXIMUM (intmax_t) == INTMAX_MAX);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
88 verify (TYPE_MINIMUM (uintmax_t) == 0);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
89 verify (TYPE_MAXIMUM (uintmax_t) == UINTMAX_MAX);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
90
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
91 /* INT_BITS_STRLEN_BOUND. */
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
92 verify (INT_BITS_STRLEN_BOUND (1) == 1);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
93 verify (INT_BITS_STRLEN_BOUND (2620) == 789);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
94
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
95 /* INT_STRLEN_BOUND, INT_BUFSIZE_BOUND. */
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
96 #ifdef INT32_MAX /* POSIX guarantees int32_t; this ports to non-POSIX hosts */
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
97 verify (INT_STRLEN_BOUND (int32_t) == sizeof ("-2147483648") - 1);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
98 verify (INT_BUFSIZE_BOUND (int32_t) == sizeof ("-2147483648"));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
99 #endif
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
100 #ifdef INT64_MAX
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
101 verify (INT_STRLEN_BOUND (int64_t) == sizeof ("-9223372036854775808") - 1);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
102 verify (INT_BUFSIZE_BOUND (int64_t) == sizeof ("-9223372036854775808"));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
103 #endif
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
104
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
105 /* All the INT_<op>_RANGE_OVERFLOW tests are equally valid as
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
106 INT_<op>_OVERFLOW tests, so define a single macro to do both. */
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
107 #define check_binop(op, a, b, min, max, overflow) \
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
108 (INT_##op##_RANGE_OVERFLOW (a, b, min, max) == (overflow) \
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
109 && INT_##op##_OVERFLOW (a, b) == (overflow))
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
110 #define check_unop(op, a, min, max, overflow) \
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
111 (INT_##op##_RANGE_OVERFLOW (a, min, max) == (overflow) \
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
112 && INT_##op##_OVERFLOW (a) == (overflow))
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
113
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
114 /* INT_<op>_RANGE_OVERFLOW, INT_<op>_OVERFLOW. */
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
115 verify (INT_ADD_RANGE_OVERFLOW (INT_MAX, 1, INT_MIN, INT_MAX));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
116 verify (INT_ADD_OVERFLOW (INT_MAX, 1));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
117 verify (check_binop (ADD, INT_MAX, 1, INT_MIN, INT_MAX, true));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
118 verify (check_binop (ADD, INT_MAX, -1, INT_MIN, INT_MAX, false));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
119 verify (check_binop (ADD, INT_MIN, 1, INT_MIN, INT_MAX, false));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
120 verify (check_binop (ADD, INT_MIN, -1, INT_MIN, INT_MAX, true));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
121 verify (check_binop (ADD, UINT_MAX, 1u, 0u, UINT_MAX, true));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
122 verify (check_binop (ADD, 0u, 1u, 0u, UINT_MAX, false));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
123
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
124 verify (check_binop (SUBTRACT, INT_MAX, 1, INT_MIN, INT_MAX, false));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
125 verify (check_binop (SUBTRACT, INT_MAX, -1, INT_MIN, INT_MAX, true));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
126 verify (check_binop (SUBTRACT, INT_MIN, 1, INT_MIN, INT_MAX, true));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
127 verify (check_binop (SUBTRACT, INT_MIN, -1, INT_MIN, INT_MAX, false));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
128 verify (check_binop (SUBTRACT, UINT_MAX, 1u, 0u, UINT_MAX, false));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
129 verify (check_binop (SUBTRACT, 0u, 1u, 0u, UINT_MAX, true));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
130
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
131 verify (check_unop (NEGATE, INT_MIN, INT_MIN, INT_MAX,
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
132 TYPE_TWOS_COMPLEMENT (int)));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
133 verify (check_unop (NEGATE, 0, INT_MIN, INT_MAX, false));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
134 verify (check_unop (NEGATE, INT_MAX, INT_MIN, INT_MAX, false));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
135 verify (check_unop (NEGATE, 0u, 0u, UINT_MAX, false));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
136 verify (check_unop (NEGATE, 1u, 0u, UINT_MAX, true));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
137 verify (check_unop (NEGATE, UINT_MAX, 0u, UINT_MAX, true));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
138
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
139 verify (check_binop (MULTIPLY, INT_MAX, INT_MAX, INT_MIN, INT_MAX, true));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
140 verify (check_binop (MULTIPLY, INT_MAX, INT_MIN, INT_MIN, INT_MAX, true));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
141 verify (check_binop (MULTIPLY, INT_MIN, INT_MAX, INT_MIN, INT_MAX, true));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
142 verify (check_binop (MULTIPLY, INT_MIN, INT_MIN, INT_MIN, INT_MAX, true));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
143 verify (check_binop (MULTIPLY, -1, INT_MIN, INT_MIN, INT_MAX,
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
144 INT_NEGATE_OVERFLOW (INT_MIN)));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
145 verify (check_binop (MULTIPLY, LONG_MIN / INT_MAX, (long int) INT_MAX,
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
146 LONG_MIN, LONG_MIN, false));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
147
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
148 verify (check_binop (DIVIDE, INT_MIN, -1, INT_MIN, INT_MAX,
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
149 INT_NEGATE_OVERFLOW (INT_MIN)));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
150 verify (check_binop (DIVIDE, INT_MAX, 1, INT_MIN, INT_MAX, false));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
151 verify (check_binop (DIVIDE, (unsigned int) INT_MIN,
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
152 -1u, 0u, UINT_MAX, false));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
153
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
154 verify (check_binop (REMAINDER, INT_MIN, -1, INT_MIN, INT_MAX,
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
155 INT_NEGATE_OVERFLOW (INT_MIN)));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
156 verify (check_binop (REMAINDER, INT_MAX, 1, INT_MIN, INT_MAX, false));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
157 verify (check_binop (REMAINDER, (unsigned int) INT_MIN,
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
158 -1u, 0u, UINT_MAX, false));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
159
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
160 verify (check_binop (LEFT_SHIFT, UINT_MAX, 1, 0u, UINT_MAX, true));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
161 verify (check_binop (LEFT_SHIFT, UINT_MAX / 2 + 1, 1, 0u, UINT_MAX, true));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
162 verify (check_binop (LEFT_SHIFT, UINT_MAX / 2, 1, 0u, UINT_MAX, false));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
163
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
164 /* INT_<op>_OVERFLOW with mixed types. */
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
165 #define check_sum(a, b, overflow) \
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
166 verify (INT_ADD_OVERFLOW (a, b) == (overflow)); \
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
167 verify (INT_ADD_OVERFLOW (b, a) == (overflow))
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
168 check_sum (-1, LONG_MIN, true);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
169 check_sum (-1, UINT_MAX, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
170 check_sum (-1L, INT_MIN, INT_MIN == LONG_MIN);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
171 check_sum (0u, -1, true);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
172 check_sum (0u, 0, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
173 check_sum (0u, 1, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
174 check_sum (1, LONG_MAX, true);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
175 check_sum (1, UINT_MAX, true);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
176 check_sum (1L, INT_MAX, INT_MAX == LONG_MAX);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
177 check_sum (1u, INT_MAX, INT_MAX == UINT_MAX);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
178 check_sum (1u, INT_MIN, true);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
179
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
180 verify (! INT_SUBTRACT_OVERFLOW (INT_MAX, 1u));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
181 verify (! INT_SUBTRACT_OVERFLOW (UINT_MAX, 1));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
182 verify (! INT_SUBTRACT_OVERFLOW (0u, -1));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
183 verify (INT_SUBTRACT_OVERFLOW (UINT_MAX, -1));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
184 verify (INT_SUBTRACT_OVERFLOW (INT_MIN, 1u));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
185 verify (INT_SUBTRACT_OVERFLOW (-1, 0u));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
186
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
187 #define check_product(a, b, overflow) \
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
188 verify (INT_MULTIPLY_OVERFLOW (a, b) == (overflow)); \
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
189 verify (INT_MULTIPLY_OVERFLOW (b, a) == (overflow))
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
190
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
191 check_product (-1, 1u, true);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
192 check_product (-1, INT_MIN, INT_NEGATE_OVERFLOW (INT_MIN));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
193 check_product (-1, UINT_MAX, true);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
194 check_product (-12345, LONG_MAX / -12345 - 1, true);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
195 check_product (-12345, LONG_MAX / -12345, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
196 check_product (0, -1, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
197 check_product (0, 0, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
198 check_product (0, 0u, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
199 check_product (0, 1, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
200 check_product (0, INT_MAX, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
201 check_product (0, INT_MIN, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
202 check_product (0, UINT_MAX, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
203 check_product (0u, -1, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
204 check_product (0u, 0, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
205 check_product (0u, 0u, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
206 check_product (0u, 1, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
207 check_product (0u, INT_MAX, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
208 check_product (0u, INT_MIN, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
209 check_product (0u, UINT_MAX, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
210 check_product (1, INT_MAX, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
211 check_product (1, INT_MIN, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
212 check_product (1, UINT_MAX, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
213 check_product (1u, INT_MIN, true);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
214 check_product (1u, INT_MAX, UINT_MAX < INT_MAX);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
215 check_product (INT_MAX, UINT_MAX, true);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
216 check_product (INT_MAX, ULONG_MAX, true);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
217 check_product (INT_MIN, LONG_MAX / INT_MIN - 1, true);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
218 check_product (INT_MIN, LONG_MAX / INT_MIN, false);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
219 check_product (INT_MIN, UINT_MAX, true);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
220 check_product (INT_MIN, ULONG_MAX, true);
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
221
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
222 verify (INT_DIVIDE_OVERFLOW (INT_MIN, -1L)
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
223 == (TYPE_TWOS_COMPLEMENT (long int) && INT_MIN == LONG_MIN));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
224 verify (! INT_DIVIDE_OVERFLOW (INT_MIN, UINT_MAX));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
225 verify (! INT_DIVIDE_OVERFLOW (INTMAX_MIN, UINTMAX_MAX));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
226 verify (! INT_DIVIDE_OVERFLOW (INTMAX_MIN, UINT_MAX));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
227 verify (INT_DIVIDE_OVERFLOW (-11, 10u));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
228 verify (INT_DIVIDE_OVERFLOW (-10, 10u));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
229 verify (! INT_DIVIDE_OVERFLOW (-9, 10u));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
230 verify (INT_DIVIDE_OVERFLOW (11u, -10));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
231 verify (INT_DIVIDE_OVERFLOW (10u, -10));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
232 verify (! INT_DIVIDE_OVERFLOW (9u, -10));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
233
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
234 verify (INT_REMAINDER_OVERFLOW (INT_MIN, -1L)
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
235 == (TYPE_TWOS_COMPLEMENT (long int) && INT_MIN == LONG_MIN));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
236 verify (INT_REMAINDER_OVERFLOW (-1, UINT_MAX));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
237 verify (INT_REMAINDER_OVERFLOW ((intmax_t) -1, UINTMAX_MAX));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
238 verify (INT_REMAINDER_OVERFLOW (INTMAX_MIN, UINT_MAX)
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
239 == (INTMAX_MAX < UINT_MAX
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
240 && - (unsigned int) INTMAX_MIN % UINT_MAX != 0));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
241 verify (INT_REMAINDER_OVERFLOW (INT_MIN, ULONG_MAX)
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
242 == (INT_MIN % ULONG_MAX != 1));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
243 verify (! INT_REMAINDER_OVERFLOW (1u, -1));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
244 verify (! INT_REMAINDER_OVERFLOW (37*39u, -39));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
245 verify (INT_REMAINDER_OVERFLOW (37*39u + 1, -39));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
246 verify (INT_REMAINDER_OVERFLOW (37*39u - 1, -39));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
247 verify (! INT_REMAINDER_OVERFLOW (LONG_MAX, -INT_MAX));
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
248
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
249 int
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
250 main (void)
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
251 {
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
252 return 0;
eb6ac457b650 intprops-tests: new module
Paul Eggert <eggert@cs.ucla.edu>
parents:
diff changeset
253 }