-
Notifications
You must be signed in to change notification settings - Fork 120
/
ut.hpp
3345 lines (2936 loc) · 103 KB
/
ut.hpp
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
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// Copyright (c) 2019-2021 Kris Jusiak (kris at jusiak dot net)
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
#if defined(__cpp_modules) && !defined(BOOST_UT_DISABLE_MODULE)
export module boost.ut;
export import std;
#define BOOST_UT_EXPORT export
#else
#pragma once
#define BOOST_UT_EXPORT
#endif
#if __has_include(<iso646.h>)
#include <iso646.h> // and, or, not, ...
#endif
#include <version>
#if defined(_MSC_VER)
#pragma push_macro("min")
#pragma push_macro("max")
#undef min
#undef max
#endif
// Before libc++ 17 had experimental support for format and it required a
// special build flag. Currently libc++ has not implemented all C++20 chrono
// improvements. Therefore doesn't define __cpp_lib_format, instead query the
// library version to detect the support status.
//
// MSVC STL and libstdc++ provide __cpp_lib_format.
#if defined(__cpp_lib_format) or \
(defined(_LIBCPP_VERSION) and _LIBCPP_VERSION >= 170000)
#define BOOST_UT_HAS_FORMAT
#endif
#if not defined(__cpp_rvalue_references)
#error "[Boost::ext].UT requires support for rvalue references";
#elif not defined(__cpp_decltype)
#error "[Boost::ext].UT requires support for decltype";
#elif not defined(__cpp_return_type_deduction)
#error "[Boost::ext].UT requires support for return type deduction";
#elif not defined(__cpp_deduction_guides)
#error "[Boost::ext].UT requires support for return deduction guides";
#elif not defined(__cpp_generic_lambdas)
#error "[Boost::ext].UT requires support for generic lambdas";
#elif not defined(__cpp_constexpr)
#error "[Boost::ext].UT requires support for constexpr";
#elif not defined(__cpp_alias_templates)
#error "[Boost::ext].UT requires support for alias templates";
#elif not defined(__cpp_variadic_templates)
#error "[Boost::ext].UT requires support for variadic templates";
#elif not defined(__cpp_fold_expressions)
#error "[Boost::ext].UT requires support for return fold expressions";
#elif not defined(__cpp_static_assert)
#error "[Boost::ext].UT requires support for static assert";
#else
#define BOOST_UT_VERSION 2'1'0
#if defined(__has_builtin) and defined(__GNUC__) and (__GNUC__ < 10) and \
not defined(__clang__)
#undef __has_builtin
#endif
#if not defined(__has_builtin)
#if defined(__GNUC__) and (__GNUC__ >= 9)
#define __has___builtin_FILE 1
#define __has___builtin_LINE 1
#endif
#define __has_builtin(...) __has_##__VA_ARGS__
#endif
#include <algorithm>
#include <array>
#include <chrono>
#include <concepts>
#include <cstdint>
#include <fstream>
#include <functional>
#include <iostream>
#include <memory>
#include <optional>
#include <sstream>
#include <stack>
#include <string_view>
#include <type_traits>
#include <unordered_map>
#include <utility>
#include <variant>
#include <vector>
#if __has_include(<unistd.h>) and __has_include(<sys/wait.h>)
#include <sys/wait.h>
#include <unistd.h>
#endif
#if defined(__cpp_exceptions)
#include <exception>
#endif
#if __has_include(<format>)
#include <format>
#endif
#if __has_include(<source_location>)
#include <source_location>
#endif
struct unique_name_for_auto_detect_prefix_and_suffix_length_0123456789_struct_ {
};
BOOST_UT_EXPORT
namespace boost::inline ext::ut::inline v2_1_0 {
namespace utility {
template <class>
class function;
template <class R, class... TArgs>
class function<R(TArgs...)> {
public:
constexpr function() = default;
template <class T>
constexpr /*explicit(false)*/ function(T data)
: invoke_{invoke_impl<T>},
destroy_{destroy_impl<T>},
data_{new T{static_cast<T&&>(data)}} {}
constexpr function(function&& other) noexcept
: invoke_{static_cast<decltype(other.invoke_)&&>(other.invoke_)},
destroy_{static_cast<decltype(other.destroy_)&&>(other.destroy_)},
data_{static_cast<decltype(other.data_)&&>(other.data_)} {
other.data_ = {};
}
constexpr function(const function&) = delete;
~function() { destroy_(data_); }
constexpr function& operator=(const function&) = delete;
constexpr function& operator=(function&&) = delete;
[[nodiscard]] constexpr auto operator()(TArgs... args) -> R {
return invoke_(data_, args...);
}
[[nodiscard]] constexpr auto operator()(TArgs... args) const -> R {
return invoke_(data_, args...);
}
private:
template <class T>
[[nodiscard]] static auto invoke_impl(void* data, TArgs... args) -> R {
return (*static_cast<T*>(data))(args...);
}
template <class T>
static auto destroy_impl(void* data) -> void {
delete static_cast<T*>(data);
}
R (*invoke_)(void*, TArgs...){};
void (*destroy_)(void*){};
void* data_{};
};
[[nodiscard]] inline auto is_match(std::string_view input,
std::string_view pattern) -> bool {
if (std::empty(pattern)) {
return std::empty(input);
}
if (std::empty(input)) {
return pattern[0] == '*' ? is_match(input, pattern.substr(1)) : false;
}
if (pattern[0] != '?' and pattern[0] != '*' and pattern[0] != input[0]) {
return false;
}
if (pattern[0] == '*') {
for (decltype(std::size(input)) i = 0u; i <= std::size(input); ++i) {
if (is_match(input.substr(i), pattern.substr(1))) {
return true;
}
}
return false;
}
return is_match(input.substr(1), pattern.substr(1));
}
template <class TPattern, class TStr>
[[nodiscard]] constexpr auto match(const TPattern& pattern,
const TStr& str) -> std::vector<TStr> {
std::vector<TStr> groups{};
auto pi = 0u;
auto si = 0u;
const auto matcher = [&](char b, char e, char c = 0) {
const auto match = si;
while (str[si] and str[si] != b and str[si] != c) {
++si;
}
groups.emplace_back(str.substr(match, si - match));
while (pattern[pi] and pattern[pi] != e) {
++pi;
}
pi++;
};
while (pi < std::size(pattern) && si < std::size(str)) {
if (pattern[pi] == '\'' and str[si] == '\'' and pattern[pi + 1] == '{') {
++si;
matcher('\'', '}');
} else if (pattern[pi] == '{') {
matcher(' ', '}', ',');
} else if (pattern[pi] != str[si]) {
return {};
}
++pi;
++si;
}
if (si < str.size() or pi < std::size(pattern)) {
return {};
}
return groups;
}
template <class T = std::string_view, class TDelim>
[[nodiscard]] inline auto split(T input, TDelim delim) -> std::vector<T> {
std::vector<T> output{};
std::size_t first{};
while (first < std::size(input)) {
const auto second = input.find_first_of(delim, first);
if (first != second) {
output.emplace_back(input.substr(first, second - first));
}
if (second == T::npos) {
break;
}
first = second + 1;
}
return output;
}
constexpr auto regex_match(const char* str, const char* pattern) -> bool {
if (*pattern == '\0' && *str == '\0') return true;
if (*pattern == '\0' && *str != '\0') return false;
if (*str == '\0' && *pattern != '\0') return false;
if (*pattern == '.') {
return regex_match(str + 1, pattern + 1);
}
if (*pattern == *str) {
return regex_match(str + 1, pattern + 1);
}
return false;
}
} // namespace utility
namespace reflection {
#if defined(__cpp_lib_source_location) && !defined(_LIBCPP_APPLE_CLANG_VER)
using source_location = std::source_location;
#else
class source_location {
public:
[[nodiscard]] static constexpr auto current(
#if (__has_builtin(__builtin_FILE) and __has_builtin(__builtin_LINE))
const char* file = __builtin_FILE(), int line = __builtin_LINE()
#else
const char* file = "unknown", int line = {}
#endif
) noexcept {
source_location sl{};
sl.file_ = file;
sl.line_ = line;
return sl;
}
[[nodiscard]] constexpr auto file_name() const noexcept { return file_; }
[[nodiscard]] constexpr auto line() const noexcept { return line_; }
private:
const char* file_{"unknown"};
int line_{};
};
#endif
namespace detail {
template <typename TargetType>
[[nodiscard]] constexpr auto get_template_function_name_use_type()
-> const std::string_view {
// for over compiler need over macros
#if defined(_MSC_VER) && !defined(__clang__)
return {&__FUNCSIG__[0], sizeof(__FUNCSIG__)};
#else
return {&__PRETTY_FUNCTION__[0], sizeof(__PRETTY_FUNCTION__)};
#endif
}
// decay allows you to highlight a cleaner name
template <typename TargetType>
[[nodiscard]] constexpr auto get_template_function_name_use_decay_type()
-> const std::string_view {
return get_template_function_name_use_type<std::decay_t<TargetType>>();
}
inline constexpr const std::string_view raw_type_name =
get_template_function_name_use_decay_type<
unique_name_for_auto_detect_prefix_and_suffix_length_0123456789_struct_>();
inline constexpr const std::size_t raw_length = raw_type_name.length();
inline constexpr const std::string_view need_name =
#if defined(_MSC_VER) and not defined(__clang__)
"struct "
"unique_name_for_auto_detect_prefix_and_suffix_length_0123456789_struct_";
#else
"unique_name_for_auto_detect_prefix_and_suffix_length_0123456789_struct_";
#endif
inline constexpr const std::size_t need_length = need_name.length();
static_assert(need_length <= raw_length,
"Auto find prefix and suffix length broken error 1");
inline constexpr const std::size_t prefix_length =
raw_type_name.find(need_name);
static_assert(prefix_length != std::string_view::npos,
"Auto find prefix and suffix length broken error 2");
static_assert(prefix_length <= raw_length,
"Auto find prefix and suffix length broken error 3");
inline constexpr const std::size_t tail_length = raw_length - prefix_length;
static_assert(need_length <= tail_length,
"Auto find prefix and suffix length broken error 4");
inline constexpr const std::size_t suffix_length = tail_length - need_length;
} // namespace detail
template <typename TargetType>
[[nodiscard]] constexpr auto type_name() -> const std::string_view {
const std::string_view raw_type_name =
detail::get_template_function_name_use_type<TargetType>();
const std::size_t end = raw_type_name.length() - detail::suffix_length;
const std::size_t len = end - detail::prefix_length;
std::string_view result = raw_type_name.substr(detail::prefix_length, len);
return result;
}
// decay allows you to highlight a cleaner name
template <typename TargetType>
[[nodiscard]] constexpr auto decay_type_name() -> const std::string_view {
const std::string_view raw_type_name =
detail::get_template_function_name_use_decay_type<TargetType>();
const std::size_t end = raw_type_name.length() - detail::suffix_length;
const std::size_t len = end - detail::prefix_length;
std::string_view result = raw_type_name.substr(detail::prefix_length, len);
return result;
}
} // namespace reflection
namespace math {
template <class T>
[[nodiscard]] constexpr auto abs(const T t) -> T {
return t < T{} ? -t : t;
}
template <class T, class U>
[[nodiscard]] constexpr auto abs_diff(const T t,
const U u) -> decltype(t < u ? u - t
: t - u) {
return t < u ? u - t : t - u;
}
template <class T>
[[nodiscard]] constexpr auto min_value(const T& lhs, const T& rhs) -> const T& {
return (rhs < lhs) ? rhs : lhs;
}
template <class T, class TExp>
[[nodiscard]] constexpr auto pow(const T base, const TExp exp) -> T {
return exp ? T(base * pow(base, exp - TExp(1))) : T(1);
}
template <class T, char... Cs>
[[nodiscard]] constexpr auto num() -> T {
static_assert(
((Cs == '.' or Cs == '\'' or (Cs >= '0' and Cs <= '9')) and ...));
T result{};
for (const char c : std::array{Cs...}) {
if (c == '.') {
break;
}
if (c >= '0' and c <= '9') {
result = result * T(10) + T(c - '0');
}
}
return result;
}
template <class T, char... Cs>
[[nodiscard]] constexpr auto den() -> T {
constexpr const std::array cs{Cs...};
T result{};
auto i = 0u;
while (cs[i++] != '.') {
}
for (auto j = i; j < sizeof...(Cs); ++j) {
result += pow(T(10), sizeof...(Cs) - j) * T(cs[j] - '0');
}
return result;
}
template <class T, char... Cs>
[[nodiscard]] constexpr auto den_size() -> T {
constexpr const std::array cs{Cs...};
T i{};
while (cs[i++] != '.') {
}
return T(sizeof...(Cs)) - i + T(1);
}
template <class T, class TValue>
[[nodiscard]] constexpr auto den_size(TValue value) -> T {
constexpr auto precision = TValue(1e-7);
T result{};
TValue tmp{};
do {
value *= 10;
tmp = value - T(value);
++result;
} while (tmp > precision);
return result;
}
} // namespace math
namespace type_traits {
template <class...>
struct list {};
template <class T, class...>
struct identity {
using type = T;
};
template <class T>
struct function_traits : function_traits<decltype(&T::operator())> {};
template <class R, class... TArgs>
struct function_traits<R (*)(TArgs...)> {
using result_type = R;
using args = list<TArgs...>;
};
template <class R, class... TArgs>
struct function_traits<R(TArgs...)> {
using result_type = R;
using args = list<TArgs...>;
};
template <class R, class T, class... TArgs>
struct function_traits<R (T::*)(TArgs...)> {
using result_type = R;
using args = list<TArgs...>;
};
template <class R, class T, class... TArgs>
struct function_traits<R (T::*)(TArgs...) const> {
using result_type = R;
using args = list<TArgs...>;
};
template <class T>
T&& declval();
template <class... Ts, class TExpr>
constexpr auto is_valid(TExpr expr) -> decltype(expr(declval<Ts...>()),
bool()) {
return true;
}
template <class...>
constexpr auto is_valid(...) -> bool {
return false;
}
template <class T>
inline constexpr auto is_range_v =
is_valid<T>([](auto t) -> decltype(t.begin(), t.end(), void()) {});
template <class T>
inline constexpr auto has_user_print = is_valid<T>(
[](auto t) -> decltype(void(declval<std::ostringstream&>() << t)) {});
template <class T, class = void>
struct has_static_member_object_value : std::false_type {};
template <class T>
struct has_static_member_object_value<T,
std::void_t<decltype(declval<T>().value)>>
: std::bool_constant<!std::is_member_pointer_v<decltype(&T::value)> &&
!std::is_function_v<decltype(T::value)>> {};
template <class T>
inline constexpr bool has_static_member_object_value_v =
has_static_member_object_value<T>::value;
template <class T, class = void>
struct has_static_member_object_epsilon : std::false_type {};
template <class T>
struct has_static_member_object_epsilon<
T, std::void_t<decltype(declval<T>().epsilon)>>
: std::bool_constant<!std::is_member_pointer_v<decltype(&T::epsilon)> &&
!std::is_function_v<decltype(T::epsilon)>> {};
template <class T>
inline constexpr bool has_static_member_object_epsilon_v =
has_static_member_object_epsilon<T>::value;
template <class T>
inline constexpr auto is_floating_point_v = false;
template <>
inline constexpr auto is_floating_point_v<float> = true;
template <>
inline constexpr auto is_floating_point_v<double> = true;
template <>
inline constexpr auto is_floating_point_v<long double> = true;
#if defined(__clang__) or defined(_MSC_VER)
template <class From, class To>
inline constexpr auto is_convertible_v = __is_convertible_to(From, To);
#else
template <class From, class To>
constexpr auto is_convertible(int) -> decltype(bool(To(declval<From>()))) {
return true;
}
template <class...>
constexpr auto is_convertible(...) {
return false;
}
template <class From, class To>
constexpr auto is_convertible_v = is_convertible<From, To>(0);
#endif
template <bool>
struct requires_ {};
template <>
struct requires_<true> {
using type = int;
};
template <bool Cond>
using requires_t = typename requires_<Cond>::type;
} // namespace type_traits
template <typename CharT, std::size_t SIZE>
struct fixed_string {
constexpr static std::size_t N = SIZE;
CharT _data[N + 1] = {};
constexpr explicit(false) fixed_string(const CharT (&str)[N + 1]) noexcept {
if constexpr (N != 0)
for (std::size_t i = 0; i < N; ++i) _data[i] = str[i];
}
[[nodiscard]] constexpr std::size_t size() const noexcept { return N; }
[[nodiscard]] constexpr bool empty() const noexcept { return N == 0; }
[[nodiscard]] constexpr explicit operator std::string_view() const noexcept {
return {_data, N};
}
[[nodiscard]] explicit operator std::string() const noexcept {
return {_data, N};
}
[[nodiscard]] operator const char*() const noexcept { return _data; }
[[nodiscard]] constexpr bool operator==(
const fixed_string& other) const noexcept {
return std::string_view{_data, N} == std::string_view(other);
}
template <std::size_t N2>
[[nodiscard]] friend constexpr bool operator==(
const fixed_string&, const fixed_string<CharT, N2>&) {
return false;
}
};
template <typename CharT, std::size_t N>
fixed_string(const CharT (&str)[N]) -> fixed_string<CharT, N - 1>;
struct none {};
namespace events {
struct run_begin {
int argc{};
const char** argv{};
};
struct test_begin {
std::string_view type{};
std::string_view name{};
reflection::source_location location{};
};
struct suite_begin {
std::string_view type{};
std::string_view name{};
reflection::source_location location{};
};
struct suite_end {
std::string_view type{};
std::string_view name{};
reflection::source_location location{};
};
template <class Test, class TArg = none>
struct test {
std::string_view type{};
std::string name{}; /// might be dynamic
std::vector<std::string_view> tag{};
reflection::source_location location{};
TArg arg{};
Test run{};
constexpr auto operator()() { run_impl(static_cast<Test&&>(run), arg); }
constexpr auto operator()() const { run_impl(static_cast<Test&&>(run), arg); }
private:
static constexpr auto run_impl(Test test, const none&) { test(); }
template <class T>
static constexpr auto run_impl(T test, const TArg& arg) -> decltype(test(arg),
void()) {
test(arg);
}
template <class T>
static constexpr auto run_impl(T test, const TArg&)
-> decltype(test.template operator()<TArg>(), void()) {
test.template operator()<TArg>();
}
};
template <class Test, class TArg>
test(std::string_view, std::string_view, std::string_view,
reflection::source_location, TArg, Test) -> test<Test, TArg>;
template <class TSuite>
struct suite {
TSuite run{};
std::string_view name{};
constexpr auto operator()() { run(); }
constexpr auto operator()() const { run(); }
};
template <class TSuite>
suite(TSuite) -> suite<TSuite>;
struct test_run {
std::string_view type{};
std::string_view name{};
};
struct test_finish {
std::string_view type{};
std::string_view name{};
};
template <class TArg = none>
struct skip {
std::string_view type{};
std::string_view name{};
TArg arg{};
};
template <class TArg>
skip(std::string_view, std::string_view, TArg) -> skip<TArg>;
struct test_skip {
std::string_view type{};
std::string_view name{};
};
template <class TExpr>
struct assertion {
TExpr expr{};
reflection::source_location location{};
};
template <class TExpr>
assertion(TExpr, reflection::source_location) -> assertion<TExpr>;
template <class TExpr>
struct assertion_pass {
TExpr expr{};
reflection::source_location location{};
};
template <class TExpr>
assertion_pass(TExpr) -> assertion_pass<TExpr>;
template <class TExpr>
struct assertion_fail {
TExpr expr{};
reflection::source_location location{};
};
template <class TExpr>
assertion_fail(TExpr) -> assertion_fail<TExpr>;
struct test_end {
std::string_view type{};
std::string_view name{};
};
template <class TMsg>
struct log {
TMsg msg{};
};
template <class TMsg = std::string_view>
log(TMsg) -> log<TMsg>;
struct fatal_assertion {};
struct exception {
const char* msg{};
[[nodiscard]] auto what() const -> const char* { return msg; }
};
struct summary {};
} // namespace events
namespace detail {
struct op {};
template <class>
struct fatal_;
struct fatal {
template <class T>
[[nodiscard]] inline auto operator()(const T& t) const {
return detail::fatal_{t};
}
};
struct cfg {
using value_ref = std::variant<std::monostate, std::reference_wrapper<bool>,
std::reference_wrapper<std::size_t>,
std::reference_wrapper<std::string>>;
using option = std::tuple<std::string, std::string, value_ref, std::string>;
static inline reflection::source_location location{};
static inline bool wip{};
#if defined(_MSC_VER)
static inline int largc = __argc;
static inline const char** largv = const_cast<const char**>(__argv);
#else
static inline int largc = 0;
static inline const char** largv = nullptr;
#endif
static inline std::string executable_name = "unknown executable";
static inline std::string query_pattern = ""; // <- done
static inline bool invert_query_pattern = false; // <- done
static inline std::string query_regex_pattern = ""; // <- done
static inline bool show_help = false; // <- done
static inline bool show_tests = false; // <- done
static inline bool list_tags = false; // <- done
static inline bool show_successful_tests = false; // <- done
static inline std::string output_filename = "";
static inline std::string use_reporter = "console"; // <- done
static inline std::string suite_name = "";
static inline bool abort_early = false; // <- done
static inline std::size_t abort_after_n_failures =
std::numeric_limits<std::size_t>::max(); // <- done
static inline bool show_duration = false; // <- done
static inline std::size_t show_min_duration = 0;
static inline std::string input_filename = "";
static inline bool show_test_names = false; // <- done
static inline bool show_reporters = false; // <- done
static inline std::string sort_order = "decl";
static inline std::size_t rnd_seed = 0; // 0: use time
static inline std::string use_colour = "yes"; // <- done
static inline bool show_lib_identity = false; // <- done
static inline std::string wait_for_keypress = "never";
static inline const std::vector<option> options = {
// clang-format off
// <short long option name>, <option arg>, <ref to cfg>, <description>
{"-? -h --help", "", std::ref(show_help), "display usage information"},
{"-l --list-tests", "", std::ref(show_tests), "list all/matching test cases"},
{"-t, --list-tags", "", std::ref(list_tags), "list all/matching tags"},
{"-s, --success", "", std::ref(show_successful_tests), "include successful tests in output"},
{"-o, --out", "<filename>", std::ref(output_filename), "output filename"},
{"-r, --reporter", "<name>", std::ref(use_reporter), "reporter to use (defaults to console)"},
{"-n, --name", "<name>", std::ref(suite_name), "suite name"},
{"-a, --abort", "", std::ref(abort_early), "abort at first failure"},
{"-x, --abortx", "<no. failures>", std::ref(abort_after_n_failures), "abort after x failures"},
{"-d, --durations", "", std::ref(show_duration), "show test durations"},
{"-D, --min-duration", "<seconds>", std::ref(show_min_duration), "show test durations for [...]"},
{"-f, --input-file", "<filename>", std::ref(input_filename), "load test names to run from a file"},
{"--list-test-names-only", "", std::ref(show_test_names), "list all/matching test cases names only"},
{"--list-reporters", "", std::ref(show_reporters), "list all reporters"},
{"--order <decl|lex|rand>", "", std::ref(sort_order), "test case order (defaults to decl)"},
{"--rng-seed", "<'time'|number>", std::ref(rnd_seed), "set a specific seed for random numbers"},
{"--use-colour", "<yes|no>", std::ref(use_colour), "should output be colourised"},
{"--libidentify", "", std::ref(show_lib_identity), "report name and version according to libidentify standard"},
{"--wait-for-keypress", "<never|start|exit|both>", std::ref(wait_for_keypress), "waits for a keypress before exiting"}
// clang-format on
};
static std::optional<cfg::option> find_arg(std::string_view arg) {
for (const auto& option : cfg::options) {
if (std::get<0>(option).find(arg) != std::string::npos) {
return option;
}
}
return std::nullopt;
}
static void print_usage() {
std::size_t opt_width = 30;
std::cout << cfg::executable_name
<< " [<test name|pattern|tags> ... ] options\n\nwith options:\n";
for (const auto& [cmd, arg, val, description] : cfg::options) {
std::string s = cmd;
s.append(" ");
s.append(arg);
// pad fixed column width
const auto pad_by = (s.size() <= opt_width) ? opt_width - s.size() : 0;
s.insert(s.end(), pad_by, ' ');
std::cout << " " << s << description << std::endl;
}
}
static void print_identity() {
// according to: https://github.com/janwilmans/LibIdentify
std::cout << "description: A UT / μt test executable\n";
std::cout << "category: testframework\n";
std::cout << "framework: UT: C++20 μ(micro)/Unit Testing Framework\n";
std::cout << "version: " << BOOST_UT_VERSION << std::endl;
}
static inline void parse_arg_with_fallback(int argc, const char* argv[]) {
if (argc > 0 && argv != nullptr) {
cfg::largc = argc;
cfg::largv = argv;
}
parse(cfg::largc, cfg::largv);
}
static inline void parse(int argc, const char* argv[]) {
const std::size_t n_args = argc > 0 ? static_cast<std::size_t>(argc) : 0U;
if (n_args > 0 && argv != nullptr) {
executable_name = argv[0];
}
query_pattern = "";
bool found_first_option = false;
for (auto i = 1U; i < n_args; i++) {
std::string cmd(argv[i]);
auto cmd_option = find_arg(cmd);
if (!cmd_option.has_value()) {
if (found_first_option) {
std::cerr << "unknown option: '" << cmd << "' run:" << std::endl;
std::cerr << "'" << executable_name << " --help'" << std::endl;
std::cerr << "for additional help" << std::endl;
std::exit(-1);
} else {
if (i > 1U) {
query_pattern.append(" ");
}
query_pattern.append(cmd);
}
continue;
}
found_first_option = true;
auto var = std::get<value_ref>(*cmd_option);
const bool has_option_arg = !std::get<1>(*cmd_option).empty();
if (!has_option_arg &&
std::holds_alternative<std::reference_wrapper<bool>>(var)) {
std::get<std::reference_wrapper<bool>>(var).get() = true;
continue;
}
if ((i + 1) >= n_args) {
std::cerr << "missing argument for option " << argv[i] << std::endl;
std::exit(-1);
}
i += 1; // skip to next argv for parsing
if (std::holds_alternative<std::reference_wrapper<std::size_t>>(var)) {
// parse size argument
std::size_t last;
std::string argument(argv[i]);
std::size_t val = std::stoull(argument, &last);
if (last != argument.length()) {
std::cerr << "cannot parse option of " << argv[i - 1] << " "
<< argv[i] << std::endl;
std::exit(-1);
}
std::get<std::reference_wrapper<std::size_t>>(var).get() = val;
}
if (std::holds_alternative<std::reference_wrapper<std::string>>(var)) {
// parse string argument
std::get<std::reference_wrapper<std::string>>(var).get() = argv[i];
continue;
}
}
if (show_help) {
print_usage();
std::exit(0);
}
if (show_lib_identity) {
print_identity();
std::exit(0);
}
if (!query_pattern.empty()) { // simple glob-like search
query_regex_pattern = "";
for (const char c : query_pattern) {
if (c == '!') {
invert_query_pattern = true;
} else if (c == '*') {
query_regex_pattern += ".*";
} else if (c == '?') {
query_regex_pattern += '.';
} else if (c == '.') {
query_regex_pattern += "\\.";
} else if (c == '\\') {
query_regex_pattern += "\\\\";
} else {
query_regex_pattern += c;
}
}
}
}
};
template <class T>
[[nodiscard]] constexpr auto get_impl(const T& t, int) -> decltype(t.get()) {
return t.get();
}
template <class T>
[[nodiscard]] constexpr auto get_impl(const T& t, ...) -> decltype(auto) {
return t;
}
template <class T>
[[nodiscard]] constexpr auto get(const T& t) {
return get_impl(t, 0);
}
template <class T>
struct type_ : op {
template <class TOther>
[[nodiscard]] constexpr auto operator()(const TOther&) const
-> const type_<TOther> {
return {};
}
[[nodiscard]] constexpr auto operator==(type_<T>) -> bool { return true; }
template <class TOther>
[[nodiscard]] constexpr auto operator==(type_<TOther>) -> bool {
return false;
}
template <class TOther>
[[nodiscard]] constexpr auto operator==(const TOther&) -> bool {
return std::is_same_v<TOther, T>;
}
[[nodiscard]] constexpr auto operator!=(type_<T>) -> bool { return false; }
template <class TOther>
[[nodiscard]] constexpr auto operator!=(type_<TOther>) -> bool {
return true;
}
template <class TOther>
[[nodiscard]] constexpr auto operator!=(const TOther&) -> bool {
return not std::is_same_v<TOther, T>;
}
};
template <class T, class = int>
struct value : op {
using value_type = T;
constexpr /*explicit(false)*/ value(const T& _value) : value_{_value} {}
[[nodiscard]] constexpr explicit operator T() const { return value_; }
[[nodiscard]] constexpr decltype(auto) get() const { return value_; }
T value_{};
};
template <class T>
struct value<T, type_traits::requires_t<type_traits::is_floating_point_v<T>>>
: op {
using value_type = T;
static inline auto epsilon = T{};
constexpr value(const T& _value, const T precision) : value_{_value} {
epsilon = precision;
}
constexpr /*explicit(false)*/ value(const T& val)
: value{val, T(1) / math::pow(T(10),
math::den_size<unsigned long long>(val))} {}
[[nodiscard]] constexpr explicit operator T() const { return value_; }
[[nodiscard]] constexpr decltype(auto) get() const { return value_; }
T value_{};
};
template <class T>
class value_location : public detail::value<T> {
public:
constexpr /*explicit(false)*/ value_location(
const T& t, const reflection::source_location& sl =
reflection::source_location::current())
: detail::value<T>{t} {
cfg::location = sl;
}
constexpr value_location(const T& t, const T precision,
const reflection::source_location& sl =
reflection::source_location::current())
: detail::value<T>{t, precision} {
cfg::location = sl;
}
};
template <auto N>
struct integral_constant : op {
using value_type = decltype(N);
static constexpr auto value = N;
[[nodiscard]] constexpr auto operator-() const {
return integral_constant<-N>{};
}