-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathutility.cpp
More file actions
454 lines (373 loc) · 15.4 KB
/
utility.cpp
File metadata and controls
454 lines (373 loc) · 15.4 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
#include <stdx/utility.hpp>
#include <catch2/catch_template_test_macros.hpp>
#include <catch2/catch_test_macros.hpp>
#include <array>
#include <cstdint>
#include <string_view>
#include <type_traits>
TEST_CASE("forward_like", "[utility]") {
STATIC_REQUIRE(std::is_same_v<stdx::forward_like_t<int, float>, float &&>);
STATIC_REQUIRE(
std::is_same_v<stdx::forward_like_t<int const, float>, float const &&>);
STATIC_REQUIRE(
std::is_same_v<stdx::forward_like_t<int &&, float>, float &&>);
STATIC_REQUIRE(std::is_same_v<stdx::forward_like_t<int const &&, float>,
float const &&>);
STATIC_REQUIRE(std::is_same_v<stdx::forward_like_t<int &, float>, float &>);
STATIC_REQUIRE(std::is_same_v<stdx::forward_like_t<int const &, float>,
float const &>);
}
TEST_CASE("as_unsigned (changed)", "[utility]") {
STATIC_REQUIRE(std::is_same_v<decltype(stdx::as_unsigned(std::int8_t{})),
std::uint8_t>);
STATIC_REQUIRE(std::is_same_v<decltype(stdx::as_unsigned(std::int16_t{})),
std::uint16_t>);
STATIC_REQUIRE(std::is_same_v<decltype(stdx::as_unsigned(std::int32_t{})),
std::uint32_t>);
STATIC_REQUIRE(std::is_same_v<decltype(stdx::as_unsigned(std::int64_t{})),
std::uint64_t>);
STATIC_REQUIRE(stdx::as_unsigned(std::int8_t{17}) == 17);
}
TEMPLATE_TEST_CASE("as_unsigned (unchanged)", "[utility]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
STATIC_REQUIRE(
std::is_same_v<decltype(stdx::as_unsigned(TestType{})), TestType>);
STATIC_REQUIRE(stdx::as_unsigned(TestType{17}) == 17);
}
TEST_CASE("as_signed (changed)", "[utility]") {
STATIC_REQUIRE(
std::is_same_v<decltype(stdx::as_signed(std::uint8_t{})), std::int8_t>);
STATIC_REQUIRE(std::is_same_v<decltype(stdx::as_signed(std::uint16_t{})),
std::int16_t>);
STATIC_REQUIRE(std::is_same_v<decltype(stdx::as_signed(std::uint32_t{})),
std::int32_t>);
STATIC_REQUIRE(std::is_same_v<decltype(stdx::as_signed(std::uint64_t{})),
std::int64_t>);
STATIC_REQUIRE(stdx::as_signed(std::uint8_t{17}) == 17);
}
TEMPLATE_TEST_CASE("as_signed (unchanged)", "[utility]", std::int8_t,
std::int16_t, std::int32_t, std::int64_t) {
STATIC_REQUIRE(
std::is_same_v<decltype(stdx::as_signed(TestType{})), TestType>);
STATIC_REQUIRE(stdx::as_signed(TestType{17}) == 17);
}
TEMPLATE_TEST_CASE("sized<T> in (uint8_t zero/one/two case)", "[utility]",
std::uint16_t, std::uint32_t, std::uint64_t) {
STATIC_REQUIRE(stdx::sized<std::uint8_t>{0}.in<TestType>() == 0);
STATIC_REQUIRE(stdx::sized<std::uint8_t>{1}.in<TestType>() == 1);
STATIC_REQUIRE(stdx::sized<std::uint8_t>{2}.in<TestType>() == 1);
}
TEMPLATE_TEST_CASE("sized<T> in (uint16_t zero/one/two case)", "[utility]",
std::uint32_t, std::uint64_t) {
STATIC_REQUIRE(stdx::sized<std::uint16_t>{0}.in<TestType>() == 0);
STATIC_REQUIRE(stdx::sized<std::uint16_t>{1}.in<TestType>() == 1);
STATIC_REQUIRE(stdx::sized<std::uint16_t>{2}.in<TestType>() == 1);
}
TEMPLATE_TEST_CASE("sized<T> in (uint32_t zero/one/two case)", "[utility]",
std::uint64_t) {
STATIC_REQUIRE(stdx::sized<std::uint32_t>{0}.in<TestType>() == 0);
STATIC_REQUIRE(stdx::sized<std::uint32_t>{1}.in<TestType>() == 1);
STATIC_REQUIRE(stdx::sized<std::uint32_t>{2}.in<TestType>() == 1);
}
TEMPLATE_TEST_CASE("sized<T> in (same type)", "[utility]", std::uint8_t,
std::uint16_t, std::uint32_t, std::uint64_t) {
STATIC_REQUIRE(stdx::sized<TestType>{3}.template in<TestType>() == 3);
}
TEMPLATE_TEST_CASE("sized<T> in (exact case)", "[utility]", std::uint16_t,
std::uint32_t, std::uint64_t) {
STATIC_REQUIRE(
stdx::sized<std::uint8_t>{3 * sizeof(TestType)}.in<TestType>() == 3);
}
TEMPLATE_TEST_CASE("sized<T> in defaults to bytes", "[utility]", std::uint16_t,
std::uint32_t, std::uint64_t) {
STATIC_REQUIRE(stdx::sized<TestType>{3}.in() == 3 * sizeof(TestType));
}
TEMPLATE_TEST_CASE("sized<T> in (downsize)", "[utility]", std::uint8_t,
std::uint16_t, std::uint32_t) {
STATIC_REQUIRE(stdx::sized<std::uint64_t>{3}.in<TestType>() ==
3 * sizeof(std::uint64_t) / sizeof(TestType));
}
TEST_CASE("sized<T> in (upsize uint8_t)", "[utility]") {
STATIC_REQUIRE(stdx::sized<std::uint8_t>{3}.in<std::uint16_t>() == 2);
STATIC_REQUIRE(stdx::sized<std::uint8_t>{17}.in<std::uint16_t>() == 9);
STATIC_REQUIRE(stdx::sized<std::uint8_t>{5}.in<std::uint32_t>() == 2);
STATIC_REQUIRE(stdx::sized<std::uint8_t>{17}.in<std::uint32_t>() == 5);
STATIC_REQUIRE(stdx::sized<std::uint8_t>{9}.in<std::uint64_t>() == 2);
STATIC_REQUIRE(stdx::sized<std::uint8_t>{17}.in<std::uint64_t>() == 3);
}
TEST_CASE("sized<T> in (upsize uint16_t)", "[utility]") {
STATIC_REQUIRE(stdx::sized<std::uint16_t>{3}.in<std::uint32_t>() == 2);
STATIC_REQUIRE(stdx::sized<std::uint16_t>{17}.in<std::uint32_t>() == 9);
STATIC_REQUIRE(stdx::sized<std::uint16_t>{5}.in<std::uint64_t>() == 2);
STATIC_REQUIRE(stdx::sized<std::uint16_t>{17}.in<std::uint64_t>() == 5);
}
TEST_CASE("sized<T> in (upsize uint32_t)", "[utility]") {
STATIC_REQUIRE(stdx::sized<std::uint32_t>{3}.in<std::uint64_t>() == 2);
STATIC_REQUIRE(stdx::sized<std::uint32_t>{17}.in<std::uint64_t>() == 9);
}
TEST_CASE("sized<T> aliases", "[utility]") {
STATIC_REQUIRE(stdx::sized8{1}.in<std::uint64_t>() == 1);
STATIC_REQUIRE(stdx::sized16{1}.in<std::uint64_t>() == 1);
STATIC_REQUIRE(stdx::sized32{1}.in<std::uint64_t>() == 1);
STATIC_REQUIRE(stdx::sized64{1}.in<std::uint64_t>() == 1);
}
TEST_CASE("sized<T> in (downsize not divisible)", "[utility]") {
using T = std::array<char, 3>;
STATIC_REQUIRE(sizeof(T) == 3);
STATIC_REQUIRE(stdx::sized<std::uint32_t>{2}.in<T>() == 3);
}
TEST_CASE("sized<T> in (upsize not divisible)", "[utility]") {
using T = std::array<char, 3>;
STATIC_REQUIRE(sizeof(T) == 3);
STATIC_REQUIRE(stdx::sized<T>{3}.in<std::uint32_t>() == 3);
}
TEST_CASE("sized<T> in (downsize, mod > 1)", "[utility]") {
using T = std::array<char, 6>;
using U = std::array<char, 4>;
STATIC_REQUIRE(stdx::sized<T>{1}.in<U>() == 2);
}
TEST_CASE("sized<T> in (upsize, mod > 1)", "[utility]") {
using T = std::array<char, 6>;
using U = std::array<char, 4>;
STATIC_REQUIRE(stdx::sized<U>{2}.in<T>() == 2);
}
TEST_CASE("terse size conversion with UDLs", "[utility]") {
using namespace stdx::literals;
STATIC_REQUIRE(1_z8->z8 == 1);
STATIC_REQUIRE(2_z8->z16 == 1);
STATIC_REQUIRE(4_z8->z32 == 1);
STATIC_REQUIRE(8_z8->z64 == 1);
STATIC_REQUIRE(1_z16->z8 == 2);
STATIC_REQUIRE(1_z32->z8 == 4);
STATIC_REQUIRE(1_z64->z8 == 8);
STATIC_REQUIRE(1_z64->in<std::uint16_t> == 4);
STATIC_REQUIRE(1_z64->in<std::uint32_t> == 2);
}
TEST_CASE("CX_VALUE structural value", "[utility]") {
auto x = CX_VALUE(42);
STATIC_REQUIRE(x() == 42);
auto y = CX_VALUE(17, 42);
STATIC_REQUIRE(y() == 42);
}
TEST_CASE("CX_VALUE non-structural value", "[utility]") {
auto x = CX_VALUE(std::string_view{"Hello"});
STATIC_REQUIRE(x() == std::string_view{"Hello"});
}
TEST_CASE("CX_VALUE type", "[utility]") {
auto x = CX_VALUE(int);
STATIC_REQUIRE(std::is_same_v<decltype(x()), stdx::type_identity<int>>);
auto y = CX_VALUE(std::pair<int, int>);
STATIC_REQUIRE(std::is_same_v<decltype(y()),
stdx::type_identity<std::pair<int, int>>>);
}
namespace {
template <std::size_t S> constexpr auto cx_value_test_nttp() {
return CX_VALUE(S);
}
template <typename T> constexpr auto cx_value_test_type() {
return CX_VALUE(T);
}
} // namespace
TEST_CASE("CX_VALUE on NTTP", "[utility]") {
auto x = cx_value_test_nttp<42>();
STATIC_REQUIRE(x() == 42);
}
TEST_CASE("CX_VALUE on type template argument", "[utility]") {
auto x = cx_value_test_type<int>();
STATIC_REQUIRE(std::is_same_v<decltype(x()), stdx::type_identity<int>>);
}
namespace {
struct alignas(16) over_aligned {};
} // namespace
TEST_CASE("is_aligned_with (integral)", "[utility]") {
STATIC_REQUIRE(stdx::is_aligned_with<std::uint8_t>(0b1111));
STATIC_REQUIRE(stdx::is_aligned_with<std::uint16_t>(0b1110));
STATIC_REQUIRE(not stdx::is_aligned_with<std::uint16_t>(0b1111));
STATIC_REQUIRE(stdx::is_aligned_with<std::uint32_t>(0b1100));
STATIC_REQUIRE(not stdx::is_aligned_with<std::uint32_t>(0b1110));
STATIC_REQUIRE(stdx::is_aligned_with<std::uint64_t>(0b1000));
STATIC_REQUIRE(not stdx::is_aligned_with<std::uint64_t>(0b1100));
STATIC_REQUIRE(stdx::is_aligned_with<over_aligned>(0b1'0000));
STATIC_REQUIRE(not stdx::is_aligned_with<over_aligned>(0b1000));
}
TEST_CASE("is_aligned_with (pointer)", "[utility]") {
std::int32_t i;
auto p = &i;
auto pc = reinterpret_cast<std::int8_t *>(p);
++pc;
CHECK(stdx::is_aligned_with<std::uint8_t>(pc));
CHECK(not stdx::is_aligned_with<std::uint16_t>(pc));
CHECK(stdx::is_aligned_with<std::uint8_t>(p));
CHECK(stdx::is_aligned_with<std::uint16_t>(p));
CHECK(stdx::is_aligned_with<std::uint32_t>(p));
}
#if __cplusplus >= 202002L
TEST_CASE("ct (integral)", "[utility]") {
constexpr auto vs = stdx::ct<42>();
STATIC_REQUIRE(
std::is_same_v<decltype(vs), std::integral_constant<int, 42> const>);
constexpr auto vu = stdx::ct<42u>();
STATIC_REQUIRE(
std::is_same_v<decltype(vu),
std::integral_constant<unsigned int, 42> const>);
}
TEST_CASE("ct (integral constant)", "[utility]") {
constexpr auto vs = stdx::ct<std::integral_constant<int, 42>{}>();
STATIC_REQUIRE(
std::is_same_v<decltype(vs), std::integral_constant<int, 42> const>);
}
TEST_CASE("ct (bool)", "[utility]") {
constexpr auto v = stdx::ct<true>();
STATIC_REQUIRE(std::is_same_v<decltype(v), std::bool_constant<true> const>);
}
TEST_CASE("ct (char)", "[utility]") {
constexpr auto v = stdx::ct<'A'>();
STATIC_REQUIRE(
std::is_same_v<decltype(v), std::integral_constant<char, 'A'> const>);
}
namespace {
enum struct E { A, B, C };
}
TEST_CASE("ct (enum)", "[utility]") {
constexpr auto v = stdx::ct<E::A>();
STATIC_REQUIRE(
std::is_same_v<decltype(v), std::integral_constant<E, E::A> const>);
}
TEST_CASE("ct (type)", "[utility]") {
constexpr auto v = stdx::ct<int>();
STATIC_REQUIRE(std::is_same_v<decltype(v), stdx::type_identity<int> const>);
}
TEST_CASE("is_ct", "[utility]") {
constexpr auto x1 = stdx::ct<42>();
STATIC_REQUIRE(stdx::is_ct_v<decltype(x1)>);
constexpr auto x2 = stdx::ct<int>();
STATIC_REQUIRE(stdx::is_ct_v<decltype(x2)>);
}
TEST_CASE("CT_WRAP", "[utility]") {
auto x1 = 17;
STATIC_REQUIRE(std::is_same_v<decltype(CT_WRAP(x1)), int>);
CHECK(CT_WRAP(x1) == 17);
auto x2 = stdx::ct<17>();
STATIC_REQUIRE(
std::is_same_v<decltype(CT_WRAP(x2)), std::integral_constant<int, 17>>);
STATIC_REQUIRE(CT_WRAP(x2).value == 17);
auto const x3 = 17;
STATIC_REQUIRE(
std::is_same_v<decltype(CT_WRAP(x3)), std::integral_constant<int, 17>>);
STATIC_REQUIRE(CT_WRAP(x3).value == 17);
constexpr static auto x4 = 17;
STATIC_REQUIRE(
std::is_same_v<decltype(CT_WRAP(x4)), std::integral_constant<int, 17>>);
STATIC_REQUIRE(CT_WRAP(x4).value == 17);
[]<auto X>() {
STATIC_REQUIRE(std::is_same_v<decltype(CT_WRAP(X)),
std::integral_constant<int, 17>>);
STATIC_REQUIRE(CT_WRAP(X).value == 17);
}.template operator()<17>();
}
TEST_CASE("CX_WRAP integer runtime arg", "[utility]") {
auto x = 17;
STATIC_REQUIRE(std::is_same_v<decltype(CX_WRAP(x)), int>);
CHECK(CX_WRAP(x) == 17);
}
TEST_CASE("CX_WRAP string_view runtime arg", "[utility]") {
auto x = std::string_view{"hello"};
STATIC_REQUIRE(std::is_same_v<decltype(CX_WRAP(x)), std::string_view>);
CHECK(CX_WRAP(x) == std::string_view{"hello"});
}
namespace {
auto at_init_time() { return 17; }
auto nc_var = at_init_time();
} // namespace
TEST_CASE("CX_WRAP static runtime arg", "[utility]") {
STATIC_REQUIRE(std::is_same_v<decltype(CX_WRAP(nc_var)), decltype(nc_var)>);
CHECK(CX_WRAP(nc_var) == 17);
}
namespace {
constexpr auto at_compile_time() { return 17; }
constexpr auto c_var = at_compile_time();
} // namespace
TEST_CASE("CX_WRAP static constexpr arg", "[utility]") {
STATIC_REQUIRE(stdx::is_cx_value_v<decltype(CX_WRAP(c_var))>);
STATIC_REQUIRE(CX_WRAP(c_var)() == 17);
}
TEST_CASE("CX_WRAP const integral type", "[utility]") {
auto const x = 17;
STATIC_REQUIRE(stdx::is_cx_value_v<decltype(CX_WRAP(x))>);
STATIC_REQUIRE(CX_WRAP(x)() == 17);
}
TEST_CASE("CX_WRAP constexpr integral type", "[utility]") {
constexpr auto x = 17;
STATIC_REQUIRE(stdx::is_cx_value_v<decltype(CX_WRAP(x))>);
STATIC_REQUIRE(CX_WRAP(x)() == 17);
}
TEST_CASE("CX_WRAP constexpr non-structural type", "[utility]") {
constexpr static auto x = std::string_view{"hello"};
STATIC_REQUIRE(stdx::is_cx_value_v<decltype(CX_WRAP(x))>);
STATIC_REQUIRE(CX_WRAP(x)() == std::string_view{"hello"});
}
TEST_CASE("CX_WRAP integer literal", "[utility]") {
STATIC_REQUIRE(stdx::is_cx_value_v<decltype(CX_WRAP(17))>);
STATIC_REQUIRE(CX_WRAP(17)() == 17);
}
TEST_CASE("CX_WRAP string literal", "[utility]") {
STATIC_REQUIRE(stdx::is_cx_value_v<decltype(CX_WRAP("hello"))>);
STATIC_REQUIRE(CX_WRAP("hello")() == std::string_view{"hello"});
}
TEST_CASE("CX_WRAP existing CX_VALUE", "[utility]") {
auto x = CX_VALUE(17);
STATIC_REQUIRE(stdx::is_cx_value_v<decltype(CX_WRAP(x))>);
STATIC_REQUIRE(CX_WRAP(x)() == 17);
}
TEST_CASE("CX_WRAP template argument", "[utility]") {
[]<int x> {
STATIC_REQUIRE(stdx::is_cx_value_v<decltype(CX_WRAP(x))>);
STATIC_REQUIRE(CX_WRAP(x)() == 17);
}.template operator()<17>();
}
TEST_CASE("CX_WRAP type argument", "[utility]") {
STATIC_REQUIRE(
std::is_same_v<decltype(CX_WRAP(int)), stdx::type_identity<int>>);
}
TEST_CASE("CX_WRAP empty type argument", "[utility]") {
using X = std::integral_constant<int, 17>;
STATIC_REQUIRE(
std::is_same_v<decltype(CX_WRAP(X)), stdx::type_identity<X>>);
}
TEST_CASE("CX_WRAP integral_constant arg", "[utility]") {
auto x = std::integral_constant<int, 17>{};
STATIC_REQUIRE(std::is_same_v<decltype(CX_WRAP(x)), decltype(x)>);
CHECK(CX_WRAP(x)() == 17);
}
#ifdef __clang__
namespace {
struct expression_test {
auto f(int x) -> int { return x; }
};
} // namespace
TEST_CASE("CX_WRAP non-constexpr expression", "[utility]") {
auto x = 17;
STATIC_REQUIRE(
std::is_same_v<decltype(CX_WRAP(expression_test{}.f(x))), int>);
CHECK(CX_WRAP(expression_test{}.f(x)) == 17);
}
#endif
#endif
STDX_PRAGMA(diagnostic push)
#ifdef __clang__
STDX_PRAGMA(diagnostic ignored "-Wunknown-warning-option")
STDX_PRAGMA(diagnostic ignored "-Wc++26-extensions")
#endif
#if __cpp_structured_bindings >= 202411L
namespace {
template <std::size_t N> constexpr auto sum() {
auto [... Is] = stdx::make_index_sequence<N>{};
constexpr auto sum = (0 + ... + decltype(Is)::value);
return sum;
}
} // namespace
TEST_CASE("destructurable integer_sequence", "[utility]") {
STATIC_CHECK(sum<4>() == 6);
}
#endif
STDX_PRAGMA(diagnostic pop)