Debian Codegraph @

gnulib-tests/from-glibc/tst-stdbit.h

raw
1 /* Common test support for <stdbit.h> tests.
2 Copyright (C) 2024-2025 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
18
19 #ifndef _TST_STDBIT_H
20 #define _TST_STDBIT_H
21
22 #if GNULIB_TEST_STDBIT
23 # include <libc-config.h>
24 #endif
25
26 #include <stdbit.h>
27 #include <stdbool.h>
28
29 #if !GNULIB_TEST_STDBIT
30 # include <array_length.h>
31 # include <support/check.h>
32 #else
33 # include <stdint.h>
34 # include <macros.h>
35 # define array_length(a) (sizeof (a) / sizeof *(a))
36 # define TEST_COMPARE(a, b) ASSERT ((a) == (b))
37 #endif
38
39 struct stdbit_test
40 {
41 /* The test input. */
42 uint64_t x;
43 /* Expected results if that test input is converted to 8, 16, 32 or
44 64 bits and then passed to the function under test for that
45 width. */
46 uint64_t res_8, res_16, res_32, res_64;
47 };
48
49 #if !GNULIB_TEST_STDBIT
50 # define TEST_TYPE(EXPR, TYPE) \
51 _Static_assert (_Generic ((EXPR), TYPE: 1, default: 0), "bad type")
52 #elif ((defined __GNUC__ && 2 <= __GNUC__) \
53 || (defined __clang_major__ && 4 <= __clang_major__) \
54 || (defined __IBMC__ && 1210 <= __IBMC__ && defined __IBM__TYPEOF__) \
55 || (defined __SUNPRO_C && 0x5110 <= __SUNPRO_C && !__STDC__) \
56 || 202311 <= __STDC_VERSION__)
57 # if __HAVE_GENERIC_SELECTION
58 # define TEST_TYPE(EXPR, TYPE) \
59 static_assert (_Generic (EXPR, TYPE: 1, default: 0))
60 # else
61 # define TEST_TYPE(EXPR, TYPE) static_assert (sizeof (EXPR) == sizeof (TYPE))
62 # endif
63 #else
64 /* A pre-C11 platform that lacks typeof. */
65 # define TEST_TYPE(EXPR, TYPE) static_assert (sizeof (TYPE) <= sizeof (EXPR))
66 #endif
67
68 /* Test a <stdbit.h> function / macro. For each function family, and
69 each input type, we test both with and without macros from the
70 header being used, both with a possibly wider argument being passed
71 (that must be truncated by the prototype) and with the argument
72 truncated in the caller, as well as testing the type-generic macro
73 (with the argument truncated in the caller). Also test that the
74 results have the correct type; also test truncation from
75 floating-point arguments (valid for functions, including with macro
76 expansion, because the prototype must implicitly convert to integer
77 type; not valid for the type-generic macros). Also test that the
78 argument is evaluated exactly once. Also test the macros are
79 usable (e.g. in typeof) at top level (GCC doesn't allow ({})
80 outside functions: bug 93239). */
81
82 #define TEST_STDBIT_T(FUNC, X, RES, TTYPE, TYPE, SUFFIX) \
83 do \
84 { \
85 TEST_COMPARE (FUNC ## SUFFIX (X), (RES)); \
86 TEST_TYPE (FUNC ## SUFFIX (X), TTYPE); \
87 TEST_COMPARE ((FUNC ## SUFFIX) (X), (RES)); \
88 TEST_TYPE ((FUNC ## SUFFIX) (X), TTYPE); \
89 TEST_COMPARE (FUNC ## SUFFIX ((TYPE) (X)), (RES)); \
90 TEST_TYPE (FUNC ## SUFFIX ((TYPE) (X)), TTYPE); \
91 TEST_COMPARE ((FUNC ## SUFFIX) ((TYPE) (X)), (RES)); \
92 TEST_TYPE ((FUNC ## SUFFIX) ((TYPE) (X)), TTYPE); \
93 TEST_COMPARE (FUNC ((TYPE) (X)), (RES)); \
94 TEST_TYPE (FUNC ((TYPE) (X)), TTYPE); \
95 if (sizeof (TYPE) <= 2) \
96 { \
97 TEST_COMPARE (FUNC ## SUFFIX ((float) (TYPE) (X)), (RES)); \
98 TEST_TYPE (FUNC ## SUFFIX ((float) (TYPE) (X)), TTYPE); \
99 TEST_COMPARE ((FUNC ## SUFFIX) ((float) (TYPE) (X)), (RES)); \
100 TEST_TYPE ((FUNC ## SUFFIX) ((float) (TYPE) (X)), TTYPE); \
101 } \
102 if (sizeof (TYPE) <= 4) \
103 { \
104 TEST_COMPARE (FUNC ## SUFFIX ((double) (TYPE) (X)), (RES)); \
105 TEST_TYPE (FUNC ## SUFFIX ((double) (TYPE) (X)), TTYPE); \
106 TEST_COMPARE ((FUNC ## SUFFIX) ((double) (TYPE) (X)), (RES)); \
107 TEST_TYPE ((FUNC ## SUFFIX) ((double) (TYPE) (X)), TTYPE); \
108 TEST_COMPARE (FUNC ## SUFFIX ((long double) (TYPE) (X)), (RES)); \
109 TEST_TYPE (FUNC ## SUFFIX ((long double) (TYPE) (X)), TTYPE); \
110 TEST_COMPARE ((FUNC ## SUFFIX) ((long double) (TYPE) (X)), (RES)); \
111 TEST_TYPE ((FUNC ## SUFFIX) ((long double) (TYPE) (X)), TTYPE); \
112 } \
113 TYPE xt = (X); \
114 TEST_COMPARE (FUNC ## SUFFIX (xt++), (RES)); \
115 TEST_COMPARE (xt, (TYPE) ((X) + 1)); \
116 xt = (X); \
117 TEST_COMPARE (FUNC (xt++), (RES)); \
118 TEST_COMPARE (xt, (TYPE) ((X) + 1)); \
119 } \
120 while (0)
121
122 #define TEST_STDBIT_UI(FUNC, INPUTS) \
123 do \
124 for (int i = 0; i < array_length (INPUTS); i++) \
125 { \
126 uint64_t x = (INPUTS)[i].x; \
127 unsigned int res_8 = (INPUTS)[i].res_8; \
128 unsigned int res_16 = (INPUTS)[i].res_16; \
129 unsigned int res_32 = (INPUTS)[i].res_32; \
130 unsigned int res_64 = (INPUTS)[i].res_64; \
131 unsigned int res_l = (sizeof (long int) == 4 \
132 ? res_32 : res_64); \
133 TEST_STDBIT_T (FUNC, x, res_8, unsigned int, \
134 unsigned char, _uc); \
135 TEST_STDBIT_T (FUNC, x, res_16, unsigned int, \
136 unsigned short, _us); \
137 TEST_STDBIT_T (FUNC, x, res_32, unsigned int, \
138 unsigned int, _ui); \
139 TEST_STDBIT_T (FUNC, x, res_l, unsigned int, \
140 unsigned long int, _ul); \
141 TEST_STDBIT_T (FUNC, x, res_64, unsigned int, \
142 unsigned long long int, _ull); \
143 } \
144 while (0)
145
146 #define TEST_STDBIT_BOOL(FUNC, INPUTS) \
147 do \
148 for (int i = 0; i < array_length (INPUTS); i++) \
149 { \
150 uint64_t x = (INPUTS)[i].x; \
151 bool res_8 = (INPUTS)[i].res_8; \
152 bool res_16 = (INPUTS)[i].res_16; \
153 bool res_32 = (INPUTS)[i].res_32; \
154 bool res_64 = (INPUTS)[i].res_64; \
155 bool res_l = (sizeof (long int) == 4 ? res_32 : res_64); \
156 TEST_STDBIT_T (FUNC, x, res_8, _Bool, unsigned char, _uc); \
157 TEST_STDBIT_T (FUNC, x, res_16, _Bool, unsigned short, _us); \
158 TEST_STDBIT_T (FUNC, x, res_32, _Bool, unsigned int, _ui); \
159 TEST_STDBIT_T (FUNC, x, res_l, _Bool, unsigned long int, _ul); \
160 TEST_STDBIT_T (FUNC, x, res_64, _Bool, \
161 unsigned long long int, _ull); \
162 } \
163 while (0)
164
165 #define TEST_STDBIT_SAME(FUNC, INPUTS) \
166 do \
167 for (int i = 0; i < array_length (INPUTS); i++) \
168 { \
169 uint64_t x = (INPUTS)[i].x; \
170 unsigned char res_8 = (INPUTS)[i].res_8; \
171 unsigned short res_16 = (INPUTS)[i].res_16; \
172 unsigned int res_32 = (INPUTS)[i].res_32; \
173 unsigned long long int res_64 = (INPUTS)[i].res_64; \
174 unsigned long int res_l = (sizeof (long int) == 4 \
175 ? res_32 : res_64); \
176 TEST_STDBIT_T (FUNC, x, res_8, unsigned char, \
177 unsigned char, _uc); \
178 TEST_STDBIT_T (FUNC, x, res_16, unsigned short, \
179 unsigned short, _us); \
180 TEST_STDBIT_T (FUNC, x, res_32, unsigned int, \
181 unsigned int, _ui); \
182 TEST_STDBIT_T (FUNC, x, res_l, unsigned long int, \
183 unsigned long int, _ul); \
184 TEST_STDBIT_T (FUNC, x, res_64, unsigned long long int, \
185 unsigned long long int, _ull); \
186 } \
187 while (0)
188
189 #define TEST_STDBIT_UI_TOPLEVEL(FUNC) \
190 TEST_TYPE (FUNC ## _uc ((unsigned char) 0), unsigned int); \
191 TEST_TYPE (FUNC ((unsigned char) 0), unsigned int); \
192 TEST_TYPE (FUNC ## _us ((unsigned short) 0), unsigned int); \
193 TEST_TYPE (FUNC ((unsigned short) 0), unsigned int); \
194 TEST_TYPE (FUNC ## _ui (0U), unsigned int); \
195 TEST_TYPE (FUNC (0U), unsigned int); \
196 TEST_TYPE (FUNC ## _ul (0UL), unsigned int); \
197 TEST_TYPE (FUNC (0UL), unsigned int); \
198 TEST_TYPE (FUNC ## _ull (0ULL), unsigned int); \
199 TEST_TYPE (FUNC (0ULL), unsigned int)
200
201 #define TEST_STDBIT_BOOL_TOPLEVEL(FUNC) \
202 TEST_TYPE (FUNC ## _uc ((unsigned char) 0), _Bool); \
203 TEST_TYPE (FUNC ((unsigned char) 0), _Bool); \
204 TEST_TYPE (FUNC ## _us ((unsigned short) 0), _Bool); \
205 TEST_TYPE (FUNC ((unsigned short) 0), _Bool); \
206 TEST_TYPE (FUNC ## _ui (0U), _Bool); \
207 TEST_TYPE (FUNC (0U), _Bool); \
208 TEST_TYPE (FUNC ## _ul (0UL), _Bool); \
209 TEST_TYPE (FUNC (0UL), _Bool); \
210 TEST_TYPE (FUNC ## _ull (0ULL), _Bool); \
211 TEST_TYPE (FUNC (0ULL), _Bool)
212
213 #define TEST_STDBIT_SAME_TOPLEVEL(FUNC) \
214 TEST_TYPE (FUNC ## _uc ((unsigned char) 0), unsigned char); \
215 TEST_TYPE (FUNC ((unsigned char) 0), unsigned char); \
216 TEST_TYPE (FUNC ## _us ((unsigned short) 0), unsigned short); \
217 TEST_TYPE (FUNC ((unsigned short) 0), unsigned short); \
218 TEST_TYPE (FUNC ## _ui (0U), unsigned int); \
219 TEST_TYPE (FUNC (0U), unsigned int); \
220 TEST_TYPE (FUNC ## _ul (0UL), unsigned long int); \
221 TEST_TYPE (FUNC (0UL), unsigned long int); \
222 TEST_TYPE (FUNC ## _ull (0ULL), unsigned long long int); \
223 TEST_TYPE (FUNC (0ULL), unsigned long long int)
224
225 #endif