forked from intel/cpp-std-extensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitset.cpp
360 lines (312 loc) · 12.7 KB
/
bitset.cpp
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
#include <stdx/bitset.hpp>
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp>
#include <cstdint>
#include <limits>
#include <type_traits>
TEST_CASE("bitset storage rounds up to nearest element size", "[bitset]") {
static_assert(sizeof(stdx::bitset<1, std::uint8_t>) == 1);
static_assert(sizeof(stdx::bitset<8, std::uint8_t>) == 1);
static_assert(sizeof(stdx::bitset<9, std::uint8_t>) == 2);
static_assert(sizeof(stdx::bitset<1, std::uint16_t>) == 2);
static_assert(sizeof(stdx::bitset<16, std::uint16_t>) == 2);
static_assert(sizeof(stdx::bitset<17, std::uint16_t>) == 4);
}
TEST_CASE("bitset with implicit storage element type", "[bitset]") {
static_assert(sizeof(stdx::bitset<1>) == 1);
static_assert(sizeof(stdx::bitset<8>) == 1);
static_assert(sizeof(stdx::bitset<9>) == 2);
static_assert(sizeof(stdx::bitset<16>) == 2);
static_assert(sizeof(stdx::bitset<17>) == 4);
static_assert(sizeof(stdx::bitset<32>) == 4);
static_assert(sizeof(stdx::bitset<33>) == 8);
static_assert(sizeof(stdx::bitset<64>) == 8);
}
TEMPLATE_TEST_CASE("bitset size", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
static_assert(stdx::bitset<1, TestType>{}.size() == 1);
static_assert(stdx::bitset<8, TestType>{}.size() == 8);
}
TEMPLATE_TEST_CASE("index operation", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
static_assert(not stdx::bitset<1, TestType>{}[0]);
}
TEMPLATE_TEST_CASE("set single bit", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
auto bs = stdx::bitset<1, TestType>{};
CHECK(not bs[0]);
bs.set(0);
CHECK(bs[0]);
bs.set(0, false);
CHECK(not bs[0]);
}
TEMPLATE_TEST_CASE("set all bits", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
auto bs = stdx::bitset<1, TestType>{};
CHECK(not bs[0]);
bs.set();
CHECK(bs[0]);
}
TEMPLATE_TEST_CASE("reset single bit", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
auto bs = stdx::bitset<1, TestType>{1ul};
CHECK(bs[0]);
bs.reset(0);
CHECK(not bs[0]);
}
TEMPLATE_TEST_CASE("reset all bits", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
auto bs = stdx::bitset<1, TestType>{1ul};
CHECK(bs[0]);
bs.reset();
CHECK(not bs[0]);
}
TEMPLATE_TEST_CASE("flip single bit", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
auto bs = stdx::bitset<3, TestType>{0b101ul};
CHECK(bs[0]);
CHECK(not bs[1]);
CHECK(bs[2]);
bs.flip(0);
CHECK(not bs[0]);
CHECK(not bs[1]);
CHECK(bs[2]);
}
TEMPLATE_TEST_CASE("flip all bits", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
auto bs = stdx::bitset<3, TestType>{0b101ul};
CHECK(bs[0]);
CHECK(not bs[1]);
CHECK(bs[2]);
bs.flip();
CHECK(not bs[0]);
CHECK(bs[1]);
CHECK(not bs[2]);
}
TEMPLATE_TEST_CASE("construct with a value", "[bitset]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
constexpr auto bs1 = stdx::bitset<1, TestType>{1ul};
static_assert(bs1[0]);
constexpr auto bs2 = stdx::bitset<3, TestType>{255ul};
static_assert(bs2[0]);
static_assert(bs2[1]);
}
TEMPLATE_TEST_CASE("construct with values for bits", "[bitset]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
constexpr auto bs = stdx::bitset<1, TestType>{stdx::place_bits, 1, 3, 5};
static_assert(not bs[0]);
static_assert(bs[1]);
static_assert(bs[3]);
static_assert(bs[5]);
}
TEMPLATE_TEST_CASE("construct with a string_view", "[bitset]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
using namespace std::string_view_literals;
static_assert(stdx::bitset<4, TestType>{"1010"sv} ==
stdx::bitset<4, TestType>{0b1010ul});
}
TEMPLATE_TEST_CASE("construct with a substring", "[bitset]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
using namespace std::string_view_literals;
static_assert(stdx::bitset<4, TestType>{"XOXOXO"sv, 2, 4, 'X'} ==
stdx::bitset<4, TestType>{0b1010ul});
}
TEMPLATE_TEST_CASE("convert to uint64_t", "[bitset]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
constexpr auto bs = stdx::bitset<3, TestType>{255ul};
constexpr auto val = bs.to_uint64_t();
static_assert(std::is_same_v<decltype(val), std::uint64_t const>);
static_assert(val == 7ul);
}
TEMPLATE_TEST_CASE("all", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
constexpr auto bs1 =
stdx::bitset<8, TestType>{std::numeric_limits<TestType>::max()};
static_assert(bs1.all());
constexpr auto bs2 = stdx::bitset<9, TestType>{0x1fful};
static_assert(bs2.all());
constexpr auto bs3 = stdx::bitset<8, TestType>{0xf7ul};
static_assert(not bs3.all());
}
TEMPLATE_TEST_CASE("any", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
constexpr auto bs1 = stdx::bitset<8, TestType>{8ul};
static_assert(bs1.any());
constexpr auto bs2 = stdx::bitset<9, TestType>{0xfful};
static_assert(bs2.any());
constexpr auto bs3 = stdx::bitset<8, TestType>{};
static_assert(not bs3.any());
}
TEMPLATE_TEST_CASE("none", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
constexpr auto bs1 = stdx::bitset<8, TestType>{};
static_assert(bs1.none());
constexpr auto bs2 = stdx::bitset<8, TestType>{8ul};
static_assert(not bs2.none());
}
TEMPLATE_TEST_CASE("count", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
constexpr auto bs1 = stdx::bitset<8, TestType>{};
static_assert(bs1.count() == 0u);
constexpr auto bs2 = stdx::bitset<8, TestType>{0b10101ul};
static_assert(bs2.count() == 3u);
}
TEMPLATE_TEST_CASE("or", "[bitset]", std::uint8_t, std::uint16_t, std::uint32_t,
std::uint64_t) {
constexpr auto bs1 = stdx::bitset<3, TestType>{0b101ul};
constexpr auto bs2 = stdx::bitset<3, TestType>{0b010ul};
static_assert((bs1 | bs2) == stdx::bitset<3, TestType>{0b111ul});
}
TEMPLATE_TEST_CASE("and", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
constexpr auto bs1 = stdx::bitset<3, TestType>{0b101ul};
constexpr auto bs2 = stdx::bitset<3, TestType>{0b100ul};
static_assert((bs1 & bs2) == stdx::bitset<3, TestType>{0b100ul});
}
TEMPLATE_TEST_CASE("xor", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
constexpr auto bs1 = stdx::bitset<3, TestType>{0b101ul};
constexpr auto bs2 = stdx::bitset<3, TestType>{0b010ul};
static_assert((bs1 ^ bs2) == stdx::bitset<3, TestType>{0b111ul});
}
TEMPLATE_TEST_CASE("not", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
constexpr auto bs = stdx::bitset<3, TestType>{0b101ul};
static_assert(~bs == stdx::bitset<3, TestType>{0b10ul});
}
TEMPLATE_TEST_CASE("left shift", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
constexpr auto bs = stdx::bitset<3, TestType>{0b101ul};
static_assert(bs << 1u == stdx::bitset<3, TestType>{0b10ul});
static_assert(bs << 2u == stdx::bitset<3, TestType>{0b100ul});
}
TEMPLATE_TEST_CASE("left shift (equal to type size)", "[bitset]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
constexpr auto type_size = std::numeric_limits<TestType>::digits;
constexpr auto sz = type_size * 3;
auto bs = stdx::bitset<sz, TestType>{};
bs.set();
bs <<= type_size;
CHECK(bs[type_size]);
CHECK(not bs[type_size - 1]);
CHECK(not bs[0]);
}
TEMPLATE_TEST_CASE("left shift (off end)", "[bitset]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
constexpr auto bs = stdx::bitset<3, TestType>{0b101ul};
static_assert(bs << 3u == stdx::bitset<3, TestType>{});
}
TEMPLATE_TEST_CASE("left shift (large shift)", "[bitset]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
constexpr auto sz = std::numeric_limits<TestType>::digits * 2;
constexpr auto shift_amount = std::numeric_limits<TestType>::digits + 1;
constexpr auto bs = stdx::bitset<sz, TestType>{0b101ul} << shift_amount;
static_assert(not bs[0]);
static_assert(bs[shift_amount]);
static_assert(not bs[shift_amount + 1]);
static_assert(bs[shift_amount + 2]);
}
TEMPLATE_TEST_CASE("right shift", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
constexpr auto bs = stdx::bitset<3, TestType>{0b101ul};
static_assert(bs >> 1u == stdx::bitset<3, TestType>{0b10ul});
static_assert(bs >> 2u == stdx::bitset<3, TestType>{0b1ul});
}
TEMPLATE_TEST_CASE("right shift (equal to type size)", "[bitset]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
constexpr auto type_size = std::numeric_limits<TestType>::digits;
constexpr auto sz = type_size * 3;
auto bs = stdx::bitset<sz, TestType>{};
bs.set();
bs >>= type_size;
CHECK(not bs[type_size * 2]);
CHECK(bs[type_size * 2 - 1]);
CHECK(bs[0]);
}
TEMPLATE_TEST_CASE("right shift (off end)", "[bitset]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
constexpr auto bs = stdx::bitset<3, TestType>{0b101ul};
static_assert(bs >> 3u == stdx::bitset<3, TestType>{});
}
TEMPLATE_TEST_CASE("right shift (large shift)", "[bitset]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
constexpr auto sz = std::numeric_limits<TestType>::digits * 2;
constexpr auto shift_amount = std::numeric_limits<TestType>::digits + 1;
auto bs = stdx::bitset<sz, TestType>{};
bs.set();
bs >>= shift_amount;
CHECK(not bs[std::numeric_limits<TestType>::digits]);
CHECK(not bs[std::numeric_limits<TestType>::digits - 1]);
CHECK(bs[std::numeric_limits<TestType>::digits - 2]);
CHECK(bs[0]);
}
TEMPLATE_TEST_CASE("for_each", "[bitset]", std::uint8_t, std::uint16_t,
std::uint32_t, std::uint64_t) {
constexpr auto bs = stdx::bitset<64, TestType>{0x01020304'05060708ul};
auto result = decltype(bs){};
for_each([&](auto i) { result.set(i); }, bs);
CHECK(result == bs);
}
TEMPLATE_TEST_CASE("for_each iterates in order lsb to msb", "[bitset]",
std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t) {
constexpr auto bs = stdx::bitset<5, TestType>{0b10101ul};
auto result = decltype(bs){};
std::size_t expected{};
for_each(
[&](auto i) {
CHECK(i == expected);
expected += 2;
result.set(i);
},
bs);
CHECK(result == bs);
}
TEMPLATE_TEST_CASE("set range of bits (lsb, length)", "[bitset]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
using namespace stdx::literals;
auto bs = stdx::bitset<64, TestType>{};
bs.set(6_lsb, 4_len);
CHECK(bs[6]);
CHECK(bs[7]);
CHECK(bs[8]);
CHECK(bs[9]);
CHECK(bs.count() == 4u);
}
TEMPLATE_TEST_CASE("set range of bits (lsb, msb)", "[bitset]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
using namespace stdx::literals;
auto bs = stdx::bitset<64, TestType>{};
bs.set(6_lsb, 9_msb);
CHECK(bs[6]);
CHECK(bs[7]);
CHECK(bs[8]);
CHECK(bs[9]);
CHECK(bs.count() == 4u);
}
TEMPLATE_TEST_CASE("construct with all bits set", "[bitset]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
constexpr auto bs = stdx::bitset<9, TestType>{stdx::all_bits};
static_assert(bs.all());
}
TEMPLATE_TEST_CASE("reset range of bits (lsb, length)", "[bitset]",
std::uint8_t, std::uint16_t, std::uint32_t, std::uint64_t) {
using namespace stdx::literals;
auto bs = stdx::bitset<64, TestType>{stdx::all_bits};
bs.reset(6_lsb, 4_len);
CHECK(not bs[6]);
CHECK(not bs[7]);
CHECK(not bs[8]);
CHECK(not bs[9]);
CHECK(bs.count() == 60);
}
TEMPLATE_TEST_CASE("reset range of bits (lsb, msb)", "[bitset]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
using namespace stdx::literals;
auto bs = stdx::bitset<64, TestType>{stdx::all_bits};
bs.reset(6_lsb, 9_msb);
CHECK(not bs[6]);
CHECK(not bs[7]);
CHECK(not bs[8]);
CHECK(not bs[9]);
CHECK(bs.count() == 60);
}