about summary refs log tree commit diff
path: root/stdlib/tst-stdbit.h
blob: 39b765beaa0d1925eecf9ebf7c81de72b9a5efd7 (plain) (blame)
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
/* Common test support for <stdbit.h> tests.
   Copyright (C) 2024 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

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

   The GNU C Library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#ifndef _TST_STDBIT_H
#define _TST_STDBIT_H

#include <stdbit.h>
#include <stdbool.h>

#include <array_length.h>
#include <support/check.h>

struct stdbit_test
{
  /* The test input.  */
  uint64_t x;
  /* Expected results if that test input is converted to 8, 16, 32 or
     64 bits and then passed to the function under test for that
     width.  */
  uint64_t res_8, res_16, res_32, res_64;
};

#define TEST_TYPE(EXPR, TYPE)						\
  _Static_assert (_Generic ((EXPR), TYPE: 1, default: 0), "bad type")

/* Test a <stdbit.h> function / macro.  For each function family, and
   each input type, we test both with and without macros from the
   header being used, both with a possibly wider argument being passed
   (that must be truncated by the prototype) and with the argument
   truncated in the caller, as well as testing the type-generic macro
   (with the argument truncated in the caller).  Also test that the
   results have the correct type; also test truncation from
   floating-point arguments (valid for functions, including with macro
   expansion, because the prototype must implicitly convert to integer
   type; not valid for the type-generic macros).  Also test that the
   argument is evaluated exactly once.  Also test the macros are
   usable (e.g. in typeof) at top level (GCC doesn't allow ({})
   outside functions: bug 93239).  */

#define TEST_STDBIT_T(FUNC, X, RES, TTYPE, TYPE, SUFFIX)		\
  do									\
    {									\
      TEST_COMPARE (FUNC ## SUFFIX (X), (RES));				\
      TEST_TYPE (FUNC ## SUFFIX (X), TTYPE);				\
      TEST_COMPARE ((FUNC ## SUFFIX) (X), (RES));			\
      TEST_TYPE ((FUNC ## SUFFIX) (X), TTYPE);				\
      TEST_COMPARE (FUNC ## SUFFIX ((TYPE) (X)), (RES));		\
      TEST_TYPE (FUNC ## SUFFIX ((TYPE) (X)), TTYPE);			\
      TEST_COMPARE ((FUNC ## SUFFIX) ((TYPE) (X)), (RES));		\
      TEST_TYPE ((FUNC ## SUFFIX) ((TYPE) (X)), TTYPE);			\
      TEST_COMPARE (FUNC ((TYPE) (X)), (RES));				\
      TEST_TYPE (FUNC ((TYPE) (X)), TTYPE);				\
      if (sizeof (TYPE) <= 2)						\
	{								\
	  TEST_COMPARE (FUNC ## SUFFIX ((float) (TYPE) (X)), (RES));	\
	  TEST_TYPE (FUNC ## SUFFIX ((float) (TYPE) (X)), TTYPE);	\
	  TEST_COMPARE ((FUNC ## SUFFIX) ((float) (TYPE) (X)), (RES));	\
	  TEST_TYPE ((FUNC ## SUFFIX) ((float) (TYPE) (X)), TTYPE);	\
	}								\
      if (sizeof (TYPE) <= 4)						\
	{								\
	  TEST_COMPARE (FUNC ## SUFFIX ((double) (TYPE) (X)), (RES));	\
	  TEST_TYPE (FUNC ## SUFFIX ((double) (TYPE) (X)), TTYPE);	\
	  TEST_COMPARE ((FUNC ## SUFFIX) ((double) (TYPE) (X)), (RES));	\
	  TEST_TYPE ((FUNC ## SUFFIX) ((double) (TYPE) (X)), TTYPE);	\
	  TEST_COMPARE (FUNC ## SUFFIX ((long double) (TYPE) (X)), (RES)); \
	  TEST_TYPE (FUNC ## SUFFIX ((long double) (TYPE) (X)), TTYPE); \
	  TEST_COMPARE ((FUNC ## SUFFIX) ((long double) (TYPE) (X)), (RES)); \
	  TEST_TYPE ((FUNC ## SUFFIX) ((long double) (TYPE) (X)), TTYPE); \
	}								\
      TYPE xt = (X);							\
      TEST_COMPARE (FUNC ## SUFFIX (xt++), (RES));			\
      TEST_COMPARE (xt, (TYPE) ((X) + 1));				\
      xt = (X);								\
      TEST_COMPARE (FUNC (xt++), (RES));				\
      TEST_COMPARE (xt, (TYPE) ((X) + 1));				\
    }									\
  while (0)

#define TEST_STDBIT_UI(FUNC, INPUTS)			\
  do							\
    for (int i = 0; i < array_length (INPUTS); i++)	\
      {							\
	uint64_t x = (INPUTS)[i].x;			\
	unsigned int res_8 = (INPUTS)[i].res_8;		\
	unsigned int res_16 = (INPUTS)[i].res_16;	\
	unsigned int res_32 = (INPUTS)[i].res_32;	\
	unsigned int res_64 = (INPUTS)[i].res_64;	\
	unsigned int res_l = (sizeof (long int) == 4	\
			      ? res_32 : res_64);	\
	TEST_STDBIT_T (FUNC, x, res_8, unsigned int,	\
		       unsigned char, _uc);		\
	TEST_STDBIT_T (FUNC, x, res_16, unsigned int,	\
		       unsigned short, _us);		\
	TEST_STDBIT_T (FUNC, x, res_32, unsigned int,	\
		       unsigned int, _ui);		\
	TEST_STDBIT_T (FUNC, x, res_l, unsigned int,	\
		       unsigned long int, _ul);		\
	TEST_STDBIT_T (FUNC, x, res_64, unsigned int,	\
		       unsigned long long int, _ull);	\
      }							\
  while (0)

#define TEST_STDBIT_BOOL(FUNC, INPUTS)					\
  do									\
    for (int i = 0; i < array_length (INPUTS); i++)			\
      {									\
	uint64_t x = (INPUTS)[i].x;					\
	bool res_8 = (INPUTS)[i].res_8;					\
	bool res_16 = (INPUTS)[i].res_16;				\
	bool res_32 = (INPUTS)[i].res_32;				\
	bool res_64 = (INPUTS)[i].res_64;				\
	bool res_l = (sizeof (long int) == 4 ? res_32 : res_64);	\
	TEST_STDBIT_T (FUNC, x, res_8, _Bool, unsigned char, _uc);	\
	TEST_STDBIT_T (FUNC, x, res_16, _Bool, unsigned short, _us);	\
	TEST_STDBIT_T (FUNC, x, res_32, _Bool, unsigned int, _ui);	\
	TEST_STDBIT_T (FUNC, x, res_l, _Bool, unsigned long int, _ul);	\
	TEST_STDBIT_T (FUNC, x, res_64, _Bool,				\
		       unsigned long long int, _ull);			\
      }									\
  while (0)

#define TEST_STDBIT_SAME(FUNC, INPUTS)				\
  do								\
    for (int i = 0; i < array_length (INPUTS); i++)		\
      {								\
	uint64_t x = (INPUTS)[i].x;				\
	unsigned char res_8 = (INPUTS)[i].res_8;		\
	unsigned short res_16 = (INPUTS)[i].res_16;		\
	unsigned int res_32 = (INPUTS)[i].res_32;		\
	unsigned long long int res_64 = (INPUTS)[i].res_64;	\
	unsigned long int res_l = (sizeof (long int) == 4	\
				   ? res_32 : res_64);		\
	TEST_STDBIT_T (FUNC, x, res_8, unsigned char,		\
		       unsigned char, _uc);			\
	TEST_STDBIT_T (FUNC, x, res_16, unsigned short,		\
		       unsigned short, _us);			\
	TEST_STDBIT_T (FUNC, x, res_32, unsigned int,		\
		       unsigned int, _ui);			\
	TEST_STDBIT_T (FUNC, x, res_l, unsigned long int,	\
		       unsigned long int, _ul);			\
	TEST_STDBIT_T (FUNC, x, res_64, unsigned long long int,	\
		       unsigned long long int, _ull);		\
      }								\
  while (0)

#define TEST_STDBIT_UI_TOPLEVEL(FUNC)				\
  TEST_TYPE (FUNC ## _uc ((unsigned char) 0), unsigned int);	\
  TEST_TYPE (FUNC ((unsigned char) 0), unsigned int);		\
  TEST_TYPE (FUNC ## _us ((unsigned short) 0), unsigned int);	\
  TEST_TYPE (FUNC ((unsigned short) 0), unsigned int);		\
  TEST_TYPE (FUNC ## _ui (0U), unsigned int);			\
  TEST_TYPE (FUNC (0U), unsigned int);				\
  TEST_TYPE (FUNC ## _ul (0UL), unsigned int);			\
  TEST_TYPE (FUNC (0UL), unsigned int);				\
  TEST_TYPE (FUNC ## _ull (0ULL), unsigned int);		\
  TEST_TYPE (FUNC (0ULL), unsigned int)

#define TEST_STDBIT_BOOL_TOPLEVEL(FUNC)			\
  TEST_TYPE (FUNC ## _uc ((unsigned char) 0), _Bool);	\
  TEST_TYPE (FUNC ((unsigned char) 0), _Bool);		\
  TEST_TYPE (FUNC ## _us ((unsigned short) 0), _Bool);	\
  TEST_TYPE (FUNC ((unsigned short) 0), _Bool);		\
  TEST_TYPE (FUNC ## _ui (0U), _Bool);			\
  TEST_TYPE (FUNC (0U), _Bool);				\
  TEST_TYPE (FUNC ## _ul (0UL), _Bool);			\
  TEST_TYPE (FUNC (0UL), _Bool);			\
  TEST_TYPE (FUNC ## _ull (0ULL), _Bool);		\
  TEST_TYPE (FUNC (0ULL), _Bool)

#define TEST_STDBIT_SAME_TOPLEVEL(FUNC)				\
  TEST_TYPE (FUNC ## _uc ((unsigned char) 0), unsigned char);	\
  TEST_TYPE (FUNC ((unsigned char) 0), unsigned char);		\
  TEST_TYPE (FUNC ## _us ((unsigned short) 0), unsigned short);	\
  TEST_TYPE (FUNC ((unsigned short) 0), unsigned short);	\
  TEST_TYPE (FUNC ## _ui (0U), unsigned int);			\
  TEST_TYPE (FUNC (0U), unsigned int);				\
  TEST_TYPE (FUNC ## _ul (0UL), unsigned long int);		\
  TEST_TYPE (FUNC (0UL), unsigned long int);			\
  TEST_TYPE (FUNC ## _ull (0ULL), unsigned long long int);	\
  TEST_TYPE (FUNC (0ULL), unsigned long long int)

#endif