Repository navigation
Expand file tree
/
Copy pathdoctest.cpp
More file actions
4016 lines (3410 loc) · 162 KB
/
Copy pathdoctest.cpp
File metadata and controls
4016 lines (3410 loc) · 162 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
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
#if defined(DOCTEST_CONFIG_IMPLEMENT) || !defined(DOCTEST_SINGLE_HEADER)
#ifndef DOCTEST_SINGLE_HEADER
#include "doctest_fwd.h"
#endif // DOCTEST_SINGLE_HEADER
DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wunused-macros")
#ifndef DOCTEST_LIBRARY_IMPLEMENTATION
#define DOCTEST_LIBRARY_IMPLEMENTATION
DOCTEST_CLANG_SUPPRESS_WARNING_POP
DOCTEST_SUPPRESS_COMMON_WARNINGS_PUSH
DOCTEST_CLANG_SUPPRESS_WARNING_PUSH
DOCTEST_CLANG_SUPPRESS_WARNING("-Wglobal-constructors")
DOCTEST_CLANG_SUPPRESS_WARNING("-Wexit-time-destructors")
DOCTEST_CLANG_SUPPRESS_WARNING("-Wsign-conversion")
DOCTEST_CLANG_SUPPRESS_WARNING("-Wshorten-64-to-32")
DOCTEST_CLANG_SUPPRESS_WARNING("-Wmissing-variable-declarations")
DOCTEST_CLANG_SUPPRESS_WARNING("-Wswitch")
DOCTEST_CLANG_SUPPRESS_WARNING("-Wswitch-enum")
DOCTEST_CLANG_SUPPRESS_WARNING("-Wcovered-switch-default")
DOCTEST_CLANG_SUPPRESS_WARNING("-Wmissing-noreturn")
DOCTEST_CLANG_SUPPRESS_WARNING("-Wdisabled-macro-expansion")
DOCTEST_CLANG_SUPPRESS_WARNING("-Wmissing-braces")
DOCTEST_CLANG_SUPPRESS_WARNING("-Wmissing-field-initializers")
DOCTEST_CLANG_SUPPRESS_WARNING("-Wunused-member-function")
DOCTEST_CLANG_SUPPRESS_WARNING("-Wnonportable-system-include-path")
DOCTEST_GCC_SUPPRESS_WARNING_PUSH
DOCTEST_GCC_SUPPRESS_WARNING("-Wconversion")
DOCTEST_GCC_SUPPRESS_WARNING("-Wsign-conversion")
DOCTEST_GCC_SUPPRESS_WARNING("-Wmissing-field-initializers")
DOCTEST_GCC_SUPPRESS_WARNING("-Wmissing-braces")
DOCTEST_GCC_SUPPRESS_WARNING("-Wswitch")
DOCTEST_GCC_SUPPRESS_WARNING("-Wswitch-enum")
DOCTEST_GCC_SUPPRESS_WARNING("-Wswitch-default")
DOCTEST_GCC_SUPPRESS_WARNING("-Wunsafe-loop-optimizations")
DOCTEST_GCC_SUPPRESS_WARNING("-Wold-style-cast")
DOCTEST_GCC_SUPPRESS_WARNING("-Wunused-function")
DOCTEST_GCC_SUPPRESS_WARNING("-Wmultiple-inheritance")
DOCTEST_GCC_SUPPRESS_WARNING("-Wsuggest-attribute")
DOCTEST_MSVC_SUPPRESS_WARNING_PUSH
DOCTEST_MSVC_SUPPRESS_WARNING(4267) // 'var' : conversion from 'x' to 'y', possible loss of data
DOCTEST_MSVC_SUPPRESS_WARNING(4530) // C++ exception handler used, but unwind semantics not enabled
DOCTEST_MSVC_SUPPRESS_WARNING(4577) // 'noexcept' used with no exception handling mode specified
DOCTEST_MSVC_SUPPRESS_WARNING(4774) // format string expected in argument is not a string literal
DOCTEST_MSVC_SUPPRESS_WARNING(4365) // conversion from 'int' to 'unsigned', signed/unsigned mismatch
DOCTEST_MSVC_SUPPRESS_WARNING(5039) // pointer to potentially throwing function passed to extern C
DOCTEST_MSVC_SUPPRESS_WARNING(4800) // forcing value to bool 'true' or 'false' (performance warning)
DOCTEST_MSVC_SUPPRESS_WARNING(5245) // unreferenced function with internal linkage has been removed
DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_BEGIN
// required includes - will go only in one translation unit!
#include <ctime>
#include <cmath>
#include <climits>
// borland (Embarcadero) compiler requires math.h and not cmath - https://github.com/doctest/doctest/pull/37
#ifdef __BORLANDC__
#include <math.h>
#endif // __BORLANDC__
#include <new>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <limits>
#include <utility>
#include <fstream>
#include <sstream>
#ifndef DOCTEST_CONFIG_NO_INCLUDE_IOSTREAM
#include <iostream>
#endif // DOCTEST_CONFIG_NO_INCLUDE_IOSTREAM
#include <algorithm>
#include <iomanip>
#include <vector>
#ifndef DOCTEST_CONFIG_NO_MULTITHREADING
#include <atomic>
#include <mutex>
#define DOCTEST_DECLARE_MUTEX(name) std::mutex name;
#define DOCTEST_DECLARE_STATIC_MUTEX(name) static DOCTEST_DECLARE_MUTEX(name)
#define DOCTEST_LOCK_MUTEX(name) std::lock_guard<std::mutex> DOCTEST_ANONYMOUS(DOCTEST_ANON_LOCK_)(name);
#else // DOCTEST_CONFIG_NO_MULTITHREADING
#define DOCTEST_DECLARE_MUTEX(name)
#define DOCTEST_DECLARE_STATIC_MUTEX(name)
#define DOCTEST_LOCK_MUTEX(name)
#endif // DOCTEST_CONFIG_NO_MULTITHREADING
#include <set>
#include <map>
#include <unordered_set>
#include <exception>
#include <stdexcept>
#include <csignal>
#include <cfloat>
#include <cctype>
#include <cstdint>
#include <string>
#ifdef DOCTEST_PLATFORM_MAC
#include <sys/types.h>
#include <unistd.h>
#include <sys/sysctl.h>
#endif // DOCTEST_PLATFORM_MAC
#ifdef DOCTEST_PLATFORM_WINDOWS
// defines for a leaner windows.h
#ifndef WIN32_LEAN_AND_MEAN
#define WIN32_LEAN_AND_MEAN
#define DOCTEST_UNDEF_WIN32_LEAN_AND_MEAN
#endif // WIN32_LEAN_AND_MEAN
#ifndef NOMINMAX
#define NOMINMAX
#define DOCTEST_UNDEF_NOMINMAX
#endif // NOMINMAX
// not sure what AfxWin.h is for - here I do what Catch does
#ifdef __AFXDLL
#include <AfxWin.h>
#else
#include <windows.h>
#endif
#include <io.h>
#else // DOCTEST_PLATFORM_WINDOWS
#include <sys/time.h>
#include <unistd.h>
#endif // DOCTEST_PLATFORM_WINDOWS
// this is a fix for https://github.com/doctest/doctest/issues/348
// https://mail.gnome.org/archives/xml/2012-January/msg00000.html
#if !defined(HAVE_UNISTD_H) && !defined(STDOUT_FILENO)
#define STDOUT_FILENO fileno(stdout)
#endif // HAVE_UNISTD_H
DOCTEST_MAKE_STD_HEADERS_CLEAN_FROM_WARNINGS_ON_WALL_END
// counts the number of elements in a C array
#define DOCTEST_COUNTOF(x) (sizeof(x) / sizeof(x[0]))
#ifdef DOCTEST_CONFIG_DISABLE
#define DOCTEST_BRANCH_ON_DISABLED(if_disabled, if_not_disabled) if_disabled
#else // DOCTEST_CONFIG_DISABLE
#define DOCTEST_BRANCH_ON_DISABLED(if_disabled, if_not_disabled) if_not_disabled
#endif // DOCTEST_CONFIG_DISABLE
#ifndef DOCTEST_CONFIG_OPTIONS_PREFIX
#define DOCTEST_CONFIG_OPTIONS_PREFIX "dt-"
#endif
#ifndef DOCTEST_THREAD_LOCAL
#if defined(DOCTEST_CONFIG_NO_MULTITHREADING) || DOCTEST_MSVC && (DOCTEST_MSVC < DOCTEST_COMPILER(19, 0, 0))
#define DOCTEST_THREAD_LOCAL
#else // DOCTEST_MSVC
#define DOCTEST_THREAD_LOCAL thread_local
#endif // DOCTEST_MSVC
#endif // DOCTEST_THREAD_LOCAL
#ifndef DOCTEST_MULTI_LANE_ATOMICS_THREAD_LANES
#define DOCTEST_MULTI_LANE_ATOMICS_THREAD_LANES 32
#endif
#ifndef DOCTEST_MULTI_LANE_ATOMICS_CACHE_LINE_SIZE
#define DOCTEST_MULTI_LANE_ATOMICS_CACHE_LINE_SIZE 64
#endif
#ifdef DOCTEST_CONFIG_NO_UNPREFIXED_OPTIONS
#define DOCTEST_OPTIONS_PREFIX_DISPLAY DOCTEST_CONFIG_OPTIONS_PREFIX
#else
#define DOCTEST_OPTIONS_PREFIX_DISPLAY ""
#endif
#if defined(WINAPI_FAMILY) && (WINAPI_FAMILY == WINAPI_FAMILY_APP)
#define DOCTEST_CONFIG_NO_MULTI_LANE_ATOMICS
#endif
#ifndef DOCTEST_CDECL
#define DOCTEST_CDECL __cdecl
#endif
namespace doctest {
bool is_running_in_test = false;
namespace {
using namespace detail;
template <typename Ex>
DOCTEST_NORETURN void throw_exception(Ex const& e) {
#ifndef DOCTEST_CONFIG_NO_EXCEPTIONS
throw e;
#else // DOCTEST_CONFIG_NO_EXCEPTIONS
#ifdef DOCTEST_CONFIG_HANDLE_EXCEPTION
DOCTEST_CONFIG_HANDLE_EXCEPTION(e);
#else // DOCTEST_CONFIG_HANDLE_EXCEPTION
#ifndef DOCTEST_CONFIG_NO_INCLUDE_IOSTREAM
std::cerr << "doctest will terminate because it needed to throw an exception.\n"
<< "The message was: " << e.what() << '\n';
#endif // DOCTEST_CONFIG_NO_INCLUDE_IOSTREAM
#endif // DOCTEST_CONFIG_HANDLE_EXCEPTION
std::terminate();
#endif // DOCTEST_CONFIG_NO_EXCEPTIONS
}
#ifndef DOCTEST_INTERNAL_ERROR
#define DOCTEST_INTERNAL_ERROR(msg) \
throw_exception(std::logic_error( \
__FILE__ ":" DOCTEST_TOSTR(__LINE__) ": Internal doctest error: " msg))
#endif // DOCTEST_INTERNAL_ERROR
// case insensitive strcmp
int stricmp(const char* a, const char* b) {
for(;; a++, b++) {
const int d = tolower(*a) - tolower(*b);
if(d != 0 || !*a)
return d;
}
}
struct Endianness
{
enum Arch
{
Big,
Little
};
static Arch which() {
int x = 1;
// casting any data pointer to char* is allowed
auto ptr = reinterpret_cast<char*>(&x);
if(*ptr)
return Little;
return Big;
}
};
} // namespace
namespace detail {
DOCTEST_THREAD_LOCAL class
{
std::vector<std::streampos> stack;
std::stringstream ss;
public:
std::ostream* push() {
stack.push_back(ss.tellp());
return &ss;
}
String pop() {
if (stack.empty())
DOCTEST_INTERNAL_ERROR("TLSS was empty when trying to pop!");
std::streampos pos = stack.back();
stack.pop_back();
unsigned sz = static_cast<unsigned>(ss.tellp() - pos);
ss.rdbuf()->pubseekpos(pos, std::ios::in | std::ios::out);
return String(ss, sz);
}
} g_oss;
std::ostream* tlssPush() {
return g_oss.push();
}
String tlssPop() {
return g_oss.pop();
}
#ifndef DOCTEST_CONFIG_DISABLE
namespace timer_large_integer
{
#if defined(DOCTEST_PLATFORM_WINDOWS)
using type = ULONGLONG;
#else // DOCTEST_PLATFORM_WINDOWS
using type = std::uint64_t;
#endif // DOCTEST_PLATFORM_WINDOWS
}
using ticks_t = timer_large_integer::type;
#ifdef DOCTEST_CONFIG_GETCURRENTTICKS
ticks_t getCurrentTicks() { return DOCTEST_CONFIG_GETCURRENTTICKS(); }
#elif defined(DOCTEST_PLATFORM_WINDOWS)
ticks_t getCurrentTicks() {
static LARGE_INTEGER hz = { {0} }, hzo = { {0} };
if(!hz.QuadPart) {
QueryPerformanceFrequency(&hz);
QueryPerformanceCounter(&hzo);
}
LARGE_INTEGER t;
QueryPerformanceCounter(&t);
return ((t.QuadPart - hzo.QuadPart) * LONGLONG(1000000)) / hz.QuadPart;
}
#else // DOCTEST_PLATFORM_WINDOWS
ticks_t getCurrentTicks() {
timeval t;
gettimeofday(&t, nullptr);
return static_cast<ticks_t>(t.tv_sec) * 1000000 + static_cast<ticks_t>(t.tv_usec);
}
#endif // DOCTEST_PLATFORM_WINDOWS
struct Timer
{
void start() { m_ticks = getCurrentTicks(); }
unsigned int getElapsedMicroseconds() const {
return static_cast<unsigned int>(getCurrentTicks() - m_ticks);
}
//unsigned int getElapsedMilliseconds() const {
// return static_cast<unsigned int>(getElapsedMicroseconds() / 1000);
//}
double getElapsedSeconds() const { return static_cast<double>(getCurrentTicks() - m_ticks) / 1000000.0; }
private:
ticks_t m_ticks = 0;
};
#ifdef DOCTEST_CONFIG_NO_MULTITHREADING
template <typename T>
using Atomic = T;
#else // DOCTEST_CONFIG_NO_MULTITHREADING
template <typename T>
using Atomic = std::atomic<T>;
#endif // DOCTEST_CONFIG_NO_MULTITHREADING
#if defined(DOCTEST_CONFIG_NO_MULTI_LANE_ATOMICS) || defined(DOCTEST_CONFIG_NO_MULTITHREADING)
template <typename T>
using MultiLaneAtomic = Atomic<T>;
#else // DOCTEST_CONFIG_NO_MULTI_LANE_ATOMICS
// Provides a multilane implementation of an atomic variable that supports add, sub, load,
// store. Instead of using a single atomic variable, this splits up into multiple ones,
// each sitting on a separate cache line. The goal is to provide a speedup when most
// operations are modifying. It achieves this with two properties:
//
// * Multiple atomics are used, so chance of congestion from the same atomic is reduced.
// * Each atomic sits on a separate cache line, so false sharing is reduced.
//
// The disadvantage is that there is a small overhead due to the use of TLS, and load/store
// is slower because all atomics have to be accessed.
template <typename T>
class MultiLaneAtomic
{
struct CacheLineAlignedAtomic
{
Atomic<T> atomic{};
char padding[DOCTEST_MULTI_LANE_ATOMICS_CACHE_LINE_SIZE - sizeof(Atomic<T>)];
};
CacheLineAlignedAtomic m_atomics[DOCTEST_MULTI_LANE_ATOMICS_THREAD_LANES];
static_assert(sizeof(CacheLineAlignedAtomic) == DOCTEST_MULTI_LANE_ATOMICS_CACHE_LINE_SIZE,
"guarantee one atomic takes exactly one cache line");
public:
T operator++() DOCTEST_NOEXCEPT { return fetch_add(1) + 1; }
T operator++(int) DOCTEST_NOEXCEPT { return fetch_add(1); }
T fetch_add(T arg, std::memory_order order = std::memory_order_seq_cst) DOCTEST_NOEXCEPT {
return myAtomic().fetch_add(arg, order);
}
T fetch_sub(T arg, std::memory_order order = std::memory_order_seq_cst) DOCTEST_NOEXCEPT {
return myAtomic().fetch_sub(arg, order);
}
operator T() const DOCTEST_NOEXCEPT { return load(); }
T load(std::memory_order order = std::memory_order_seq_cst) const DOCTEST_NOEXCEPT {
auto result = T();
for(auto const& c : m_atomics) {
result += c.atomic.load(order);
}
return result;
}
T operator=(T desired) DOCTEST_NOEXCEPT { // lgtm [cpp/assignment-does-not-return-this]
store(desired);
return desired;
}
void store(T desired, std::memory_order order = std::memory_order_seq_cst) DOCTEST_NOEXCEPT {
// first value becomes desired", all others become 0.
for(auto& c : m_atomics) {
c.atomic.store(desired, order);
desired = {};
}
}
private:
// Each thread has a different atomic that it operates on. If more than NumLanes threads
// use this, some will use the same atomic. So performance will degrade a bit, but still
// everything will work.
//
// The logic here is a bit tricky. The call should be as fast as possible, so that there
// is minimal to no overhead in determining the correct atomic for the current thread.
//
// 1. A global static counter laneCounter counts continuously up.
// 2. Each successive thread will use modulo operation of that counter so it gets an atomic
// assigned in a round-robin fashion.
// 3. This tlsLaneIdx is stored in the thread local data, so it is directly available with
// little overhead.
Atomic<T>& myAtomic() DOCTEST_NOEXCEPT {
static Atomic<size_t> laneCounter;
DOCTEST_THREAD_LOCAL size_t tlsLaneIdx =
laneCounter++ % DOCTEST_MULTI_LANE_ATOMICS_THREAD_LANES;
return m_atomics[tlsLaneIdx].atomic;
}
};
#endif // DOCTEST_CONFIG_NO_MULTI_LANE_ATOMICS
// this holds both parameters from the command line and runtime data for tests
struct ContextState : ContextOptions, TestRunStats, CurrentTestCaseStats
{
MultiLaneAtomic<int> numAssertsCurrentTest_atomic;
MultiLaneAtomic<int> numAssertsFailedCurrentTest_atomic;
std::vector<std::vector<String>> filters = decltype(filters)(9); // 9 different filters
std::vector<IReporter*> reporters_currently_used;
assert_handler ah = nullptr;
Timer timer;
std::vector<String> stringifiedContexts; // logging from INFO() due to an exception
// stuff for subcases
bool reachedLeaf;
std::vector<SubcaseSignature> subcaseStack;
std::vector<SubcaseSignature> nextSubcaseStack;
std::unordered_set<unsigned long long> fullyTraversedSubcases;
size_t currentSubcaseDepth;
Atomic<bool> shouldLogCurrentException;
void resetRunData() {
numTestCases = 0;
numTestCasesPassingFilters = 0;
numTestSuitesPassingFilters = 0;
numTestCasesFailed = 0;
numAsserts = 0;
numAssertsFailed = 0;
numAssertsCurrentTest = 0;
numAssertsFailedCurrentTest = 0;
}
void finalizeTestCaseData() {
seconds = timer.getElapsedSeconds();
// update the non-atomic counters
numAsserts += numAssertsCurrentTest_atomic;
numAssertsFailed += numAssertsFailedCurrentTest_atomic;
numAssertsCurrentTest = numAssertsCurrentTest_atomic;
numAssertsFailedCurrentTest = numAssertsFailedCurrentTest_atomic;
if(numAssertsFailedCurrentTest)
failure_flags |= TestCaseFailureReason::AssertFailure;
if(Approx(currentTest->m_timeout).epsilon(DBL_EPSILON) != 0 &&
Approx(seconds).epsilon(DBL_EPSILON) > currentTest->m_timeout)
failure_flags |= TestCaseFailureReason::Timeout;
if(currentTest->m_should_fail) {
if(failure_flags) {
failure_flags |= TestCaseFailureReason::ShouldHaveFailedAndDid;
} else {
failure_flags |= TestCaseFailureReason::ShouldHaveFailedButDidnt;
}
} else if(failure_flags && currentTest->m_may_fail) {
failure_flags |= TestCaseFailureReason::CouldHaveFailedAndDid;
} else if(currentTest->m_expected_failures > 0) {
if(numAssertsFailedCurrentTest == currentTest->m_expected_failures) {
failure_flags |= TestCaseFailureReason::FailedExactlyNumTimes;
} else {
failure_flags |= TestCaseFailureReason::DidntFailExactlyNumTimes;
}
}
bool ok_to_fail = (TestCaseFailureReason::ShouldHaveFailedAndDid & failure_flags) ||
(TestCaseFailureReason::CouldHaveFailedAndDid & failure_flags) ||
(TestCaseFailureReason::FailedExactlyNumTimes & failure_flags);
// if any subcase has failed - the whole test case has failed
testCaseSuccess = !(failure_flags && !ok_to_fail);
if(!testCaseSuccess)
numTestCasesFailed++;
}
};
ContextState* g_cs = nullptr;
// used to avoid locks for the debug output
// TODO: figure out if this is indeed necessary/correct - seems like either there still
// could be a race or that there wouldn't be a race even if using the context directly
DOCTEST_THREAD_LOCAL bool g_no_colors;
#endif // DOCTEST_CONFIG_DISABLE
} // namespace detail
char* String::allocate(size_type sz) {
if (sz <= last) {
buf[sz] = '\0';
setLast(last - sz);
return buf;
} else {
setOnHeap();
data.size = sz;
data.capacity = data.size + 1;
data.ptr = new char[data.capacity];
data.ptr[sz] = '\0';
return data.ptr;
}
}
void String::setOnHeap() noexcept { *reinterpret_cast<unsigned char*>(&buf[last]) = 128; }
void String::setLast(size_type in) noexcept { buf[last] = char(in); }
void String::setSize(size_type sz) noexcept {
if (isOnStack()) { buf[sz] = '\0'; setLast(last - sz); }
else { data.ptr[sz] = '\0'; data.size = sz; }
}
void String::copy(const String& other) {
if(other.isOnStack()) {
memcpy(buf, other.buf, len);
} else {
memcpy(allocate(other.data.size), other.data.ptr, other.data.size);
}
}
String::String() noexcept {
buf[0] = '\0';
setLast();
}
String::~String() {
if(!isOnStack())
delete[] data.ptr;
} // NOLINT(clang-analyzer-cplusplus.NewDeleteLeaks)
String::String(const char* in)
: String(in, strlen(in)) {}
String::String(const char* in, size_type in_size) {
memcpy(allocate(in_size), in, in_size);
}
String::String(std::istream& in, size_type in_size) {
in.read(allocate(in_size), in_size);
}
String::String(const String& other) { copy(other); }
String& String::operator=(const String& other) {
if(this != &other) {
if(!isOnStack())
delete[] data.ptr;
copy(other);
}
return *this;
}
String& String::operator+=(const String& other) {
const size_type my_old_size = size();
const size_type other_size = other.size();
const size_type total_size = my_old_size + other_size;
if(isOnStack()) {
if(total_size < len) {
// append to the current stack space
memcpy(buf + my_old_size, other.c_str(), other_size + 1);
// NOLINTNEXTLINE(clang-analyzer-cplusplus.NewDeleteLeaks)
setLast(last - total_size);
} else {
// alloc new chunk
char* temp = new char[total_size + 1];
// copy current data to new location before writing in the union
memcpy(temp, buf, my_old_size); // skip the +1 ('\0') for speed
// update data in union
setOnHeap();
data.size = total_size;
data.capacity = data.size + 1;
data.ptr = temp;
// transfer the rest of the data
memcpy(data.ptr + my_old_size, other.c_str(), other_size + 1);
}
} else {
if(data.capacity > total_size) {
// append to the current heap block
data.size = total_size;
memcpy(data.ptr + my_old_size, other.c_str(), other_size + 1);
} else {
// resize
data.capacity *= 2;
if(data.capacity <= total_size)
data.capacity = total_size + 1;
// alloc new chunk
char* temp = new char[data.capacity];
// copy current data to new location before releasing it
memcpy(temp, data.ptr, my_old_size); // skip the +1 ('\0') for speed
// release old chunk
delete[] data.ptr;
// update the rest of the union members
data.size = total_size;
data.ptr = temp;
// transfer the rest of the data
memcpy(data.ptr + my_old_size, other.c_str(), other_size + 1);
}
}
return *this;
}
String::String(String&& other) noexcept {
memcpy(buf, other.buf, len);
other.buf[0] = '\0';
other.setLast();
}
String& String::operator=(String&& other) noexcept {
if(this != &other) {
if(!isOnStack())
delete[] data.ptr;
memcpy(buf, other.buf, len);
other.buf[0] = '\0';
other.setLast();
}
return *this;
}
char String::operator[](size_type i) const {
return const_cast<String*>(this)->operator[](i);
}
char& String::operator[](size_type i) {
if(isOnStack())
return reinterpret_cast<char*>(buf)[i];
return data.ptr[i];
}
DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH("-Wmaybe-uninitialized")
String::size_type String::size() const {
if(isOnStack())
return last - (size_type(buf[last]) & 31); // using "last" would work only if "len" is 32
return data.size;
}
DOCTEST_GCC_SUPPRESS_WARNING_POP
String::size_type String::capacity() const {
if(isOnStack())
return len;
return data.capacity;
}
String String::substr(size_type pos, size_type cnt) && {
cnt = std::min(cnt, size() - 1 - pos);
char* cptr = c_str();
memmove(cptr, cptr + pos, cnt);
setSize(cnt);
return std::move(*this);
}
String String::substr(size_type pos, size_type cnt) const & {
cnt = std::min(cnt, size() - 1 - pos);
return String{ c_str() + pos, cnt };
}
String::size_type String::find(char ch, size_type pos) const {
const char* begin = c_str();
const char* end = begin + size();
const char* it = begin + pos;
for (; it < end && *it != ch; it++);
if (it < end) { return static_cast<size_type>(it - begin); }
else { return npos; }
}
String::size_type String::rfind(char ch, size_type pos) const {
const char* begin = c_str();
const char* it = begin + std::min(pos, size() - 1);
for (; it >= begin && *it != ch; it--);
if (it >= begin) { return static_cast<size_type>(it - begin); }
else { return npos; }
}
int String::compare(const char* other, bool no_case) const {
if(no_case)
return doctest::stricmp(c_str(), other);
return std::strcmp(c_str(), other);
}
int String::compare(const String& other, bool no_case) const {
return compare(other.c_str(), no_case);
}
String operator+(const String& lhs, const String& rhs) { return String(lhs) += rhs; }
bool operator==(const String& lhs, const String& rhs) { return lhs.compare(rhs) == 0; }
bool operator!=(const String& lhs, const String& rhs) { return lhs.compare(rhs) != 0; }
bool operator< (const String& lhs, const String& rhs) { return lhs.compare(rhs) < 0; }
bool operator> (const String& lhs, const String& rhs) { return lhs.compare(rhs) > 0; }
bool operator<=(const String& lhs, const String& rhs) { return (lhs != rhs) ? lhs.compare(rhs) < 0 : true; }
bool operator>=(const String& lhs, const String& rhs) { return (lhs != rhs) ? lhs.compare(rhs) > 0 : true; }
std::ostream& operator<<(std::ostream& s, const String& in) { return s << in.c_str(); }
Contains::Contains(const String& str) : string(str) { }
bool Contains::checkWith(const String& other) const {
return strstr(other.c_str(), string.c_str()) != nullptr;
}
String toString(const Contains& in) {
return "Contains( " + in.string + " )";
}
bool operator==(const String& lhs, const Contains& rhs) { return rhs.checkWith(lhs); }
bool operator==(const Contains& lhs, const String& rhs) { return lhs.checkWith(rhs); }
bool operator!=(const String& lhs, const Contains& rhs) { return !rhs.checkWith(lhs); }
bool operator!=(const Contains& lhs, const String& rhs) { return !lhs.checkWith(rhs); }
namespace {
void color_to_stream(std::ostream&, Color::Enum) DOCTEST_BRANCH_ON_DISABLED({}, ;)
} // namespace
namespace Color {
std::ostream& operator<<(std::ostream& s, Color::Enum code) {
color_to_stream(s, code);
return s;
}
} // namespace Color
// clang-format off
const char* assertString(assertType::Enum at) {
DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4061) // enum 'x' in switch of enum 'y' is not explicitly handled
#define DOCTEST_GENERATE_ASSERT_TYPE_CASE(assert_type) case assertType::DT_ ## assert_type: return #assert_type
#define DOCTEST_GENERATE_ASSERT_TYPE_CASES(assert_type) \
DOCTEST_GENERATE_ASSERT_TYPE_CASE(WARN_ ## assert_type); \
DOCTEST_GENERATE_ASSERT_TYPE_CASE(CHECK_ ## assert_type); \
DOCTEST_GENERATE_ASSERT_TYPE_CASE(REQUIRE_ ## assert_type)
switch(at) {
DOCTEST_GENERATE_ASSERT_TYPE_CASE(WARN);
DOCTEST_GENERATE_ASSERT_TYPE_CASE(CHECK);
DOCTEST_GENERATE_ASSERT_TYPE_CASE(REQUIRE);
DOCTEST_GENERATE_ASSERT_TYPE_CASES(FALSE);
DOCTEST_GENERATE_ASSERT_TYPE_CASES(THROWS);
DOCTEST_GENERATE_ASSERT_TYPE_CASES(THROWS_AS);
DOCTEST_GENERATE_ASSERT_TYPE_CASES(THROWS_WITH);
DOCTEST_GENERATE_ASSERT_TYPE_CASES(THROWS_WITH_AS);
DOCTEST_GENERATE_ASSERT_TYPE_CASES(NOTHROW);
DOCTEST_GENERATE_ASSERT_TYPE_CASES(EQ);
DOCTEST_GENERATE_ASSERT_TYPE_CASES(NE);
DOCTEST_GENERATE_ASSERT_TYPE_CASES(GT);
DOCTEST_GENERATE_ASSERT_TYPE_CASES(LT);
DOCTEST_GENERATE_ASSERT_TYPE_CASES(GE);
DOCTEST_GENERATE_ASSERT_TYPE_CASES(LE);
DOCTEST_GENERATE_ASSERT_TYPE_CASES(UNARY);
DOCTEST_GENERATE_ASSERT_TYPE_CASES(UNARY_FALSE);
default: DOCTEST_INTERNAL_ERROR("Tried stringifying invalid assert type!");
}
DOCTEST_MSVC_SUPPRESS_WARNING_POP
}
// clang-format on
const char* failureString(assertType::Enum at) {
if(at & assertType::is_warn) //!OCLINT bitwise operator in conditional
return "WARNING";
if(at & assertType::is_check) //!OCLINT bitwise operator in conditional
return "ERROR";
if(at & assertType::is_require) //!OCLINT bitwise operator in conditional
return "FATAL ERROR";
return "";
}
DOCTEST_CLANG_SUPPRESS_WARNING_WITH_PUSH("-Wnull-dereference")
DOCTEST_GCC_SUPPRESS_WARNING_WITH_PUSH("-Wnull-dereference")
// depending on the current options this will remove the path of filenames
const char* skipPathFromFilename(const char* file) {
#ifndef DOCTEST_CONFIG_DISABLE
if(getContextOptions()->no_path_in_filenames) {
auto back = std::strrchr(file, '\\');
auto forward = std::strrchr(file, '/');
if(back || forward) {
if(back > forward)
forward = back;
return forward + 1;
}
}
#endif // DOCTEST_CONFIG_DISABLE
return file;
}
DOCTEST_CLANG_SUPPRESS_WARNING_POP
DOCTEST_GCC_SUPPRESS_WARNING_POP
bool SubcaseSignature::operator==(const SubcaseSignature& other) const {
return m_line == other.m_line
&& std::strcmp(m_file, other.m_file) == 0
&& m_name == other.m_name;
}
bool SubcaseSignature::operator<(const SubcaseSignature& other) const {
if(m_line != other.m_line)
return m_line < other.m_line;
if(std::strcmp(m_file, other.m_file) != 0)
return std::strcmp(m_file, other.m_file) < 0;
return m_name.compare(other.m_name) < 0;
}
DOCTEST_DEFINE_INTERFACE(IContextScope)
namespace detail {
void filldata<const void*>::fill(std::ostream* stream, const void* in) {
if (in) { *stream << in; }
else { *stream << "nullptr"; }
}
template <typename T>
String toStreamLit(T t) {
std::ostream* os = tlssPush();
os->operator<<(t);
return tlssPop();
}
}
#ifdef DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
String toString(const char* in) { return String("\"") + (in ? in : "{null string}") + "\""; }
#endif // DOCTEST_CONFIG_TREAT_CHAR_STAR_AS_STRING
#if DOCTEST_MSVC >= DOCTEST_COMPILER(19, 20, 0)
// see this issue on why this is needed: https://github.com/doctest/doctest/issues/183
String toString(const std::string& in) { return in.c_str(); }
#endif // VS 2019
String toString(String in) { return in; }
String toString(std::nullptr_t) { return "nullptr"; }
String toString(bool in) { return in ? "true" : "false"; }
String toString(float in) { return toStreamLit(in); }
String toString(double in) { return toStreamLit(in); }
String toString(double long in) { return toStreamLit(in); }
String toString(char in) { return toStreamLit(static_cast<signed>(in)); }
String toString(char signed in) { return toStreamLit(static_cast<signed>(in)); }
String toString(char unsigned in) { return toStreamLit(static_cast<unsigned>(in)); }
String toString(short in) { return toStreamLit(in); }
String toString(short unsigned in) { return toStreamLit(in); }
String toString(signed in) { return toStreamLit(in); }
String toString(unsigned in) { return toStreamLit(in); }
String toString(long in) { return toStreamLit(in); }
String toString(long unsigned in) { return toStreamLit(in); }
String toString(long long in) { return toStreamLit(in); }
String toString(long long unsigned in) { return toStreamLit(in); }
Approx::Approx(double value)
: m_epsilon(static_cast<double>(std::numeric_limits<float>::epsilon()) * 100)
, m_scale(1.0)
, m_value(value) {}
Approx Approx::operator()(double value) const {
Approx approx(value);
approx.epsilon(m_epsilon);
approx.scale(m_scale);
return approx;
}
Approx& Approx::epsilon(double newEpsilon) {
m_epsilon = newEpsilon;
return *this;
}
Approx& Approx::scale(double newScale) {
m_scale = newScale;
return *this;
}
bool operator==(double lhs, const Approx& rhs) {
// Thanks to Richard Harris for his help refining this formula
return std::fabs(lhs - rhs.m_value) <
rhs.m_epsilon * (rhs.m_scale + std::max<double>(std::fabs(lhs), std::fabs(rhs.m_value)));
}
bool operator==(const Approx& lhs, double rhs) { return operator==(rhs, lhs); }
bool operator!=(double lhs, const Approx& rhs) { return !operator==(lhs, rhs); }
bool operator!=(const Approx& lhs, double rhs) { return !operator==(rhs, lhs); }
bool operator<=(double lhs, const Approx& rhs) { return lhs < rhs.m_value || lhs == rhs; }
bool operator<=(const Approx& lhs, double rhs) { return lhs.m_value < rhs || lhs == rhs; }
bool operator>=(double lhs, const Approx& rhs) { return lhs > rhs.m_value || lhs == rhs; }
bool operator>=(const Approx& lhs, double rhs) { return lhs.m_value > rhs || lhs == rhs; }
bool operator<(double lhs, const Approx& rhs) { return lhs < rhs.m_value && lhs != rhs; }
bool operator<(const Approx& lhs, double rhs) { return lhs.m_value < rhs && lhs != rhs; }
bool operator>(double lhs, const Approx& rhs) { return lhs > rhs.m_value && lhs != rhs; }
bool operator>(const Approx& lhs, double rhs) { return lhs.m_value > rhs && lhs != rhs; }
String toString(const Approx& in) {
return "Approx( " + doctest::toString(in.m_value) + " )";
}
const ContextOptions* getContextOptions() { return DOCTEST_BRANCH_ON_DISABLED(nullptr, g_cs); }
DOCTEST_MSVC_SUPPRESS_WARNING_WITH_PUSH(4738)
template <typename F>
IsNaN<F>::operator bool() const {
return std::isnan(value) ^ flipped;
}
DOCTEST_MSVC_SUPPRESS_WARNING_POP
template struct DOCTEST_INTERFACE_DEF IsNaN<float>;
template struct DOCTEST_INTERFACE_DEF IsNaN<double>;
template struct DOCTEST_INTERFACE_DEF IsNaN<long double>;
template <typename F>
String toString(IsNaN<F> in) { return String(in.flipped ? "! " : "") + "IsNaN( " + doctest::toString(in.value) + " )"; }
String toString(IsNaN<float> in) { return toString<float>(in); }
String toString(IsNaN<double> in) { return toString<double>(in); }
String toString(IsNaN<double long> in) { return toString<double long>(in); }
} // namespace doctest
#ifdef DOCTEST_CONFIG_DISABLE
namespace doctest {
Context::Context(int, const char* const*) {}
Context::~Context() = default;
void Context::applyCommandLine(int, const char* const*) {}
void Context::addFilter(const char*, const char*) {}
void Context::clearFilters() {}
void Context::setOption(const char*, bool) {}
void Context::setOption(const char*, int) {}
void Context::setOption(const char*, const char*) {}
bool Context::shouldExit() { return false; }
void Context::setAsDefaultForAssertsOutOfTestCases() {}
void Context::setAssertHandler(detail::assert_handler) {}
void Context::setCout(std::ostream*) {}
int Context::run() { return 0; }
int IReporter::get_num_active_contexts() { return 0; }
const IContextScope* const* IReporter::get_active_contexts() { return nullptr; }
int IReporter::get_num_stringified_contexts() { return 0; }
const String* IReporter::get_stringified_contexts() { return nullptr; }
int registerReporter(const char*, int, IReporter*) { return 0; }
} // namespace doctest
#else // DOCTEST_CONFIG_DISABLE
#if !defined(DOCTEST_CONFIG_COLORS_NONE)
#if !defined(DOCTEST_CONFIG_COLORS_WINDOWS) && !defined(DOCTEST_CONFIG_COLORS_ANSI)
#ifdef DOCTEST_PLATFORM_WINDOWS
#define DOCTEST_CONFIG_COLORS_WINDOWS
#else // linux
#define DOCTEST_CONFIG_COLORS_ANSI
#endif // platform
#endif // DOCTEST_CONFIG_COLORS_WINDOWS && DOCTEST_CONFIG_COLORS_ANSI
#endif // DOCTEST_CONFIG_COLORS_NONE
namespace doctest_detail_test_suite_ns {
// holds the current test suite
doctest::detail::TestSuite& getCurrentTestSuite() {
static doctest::detail::TestSuite data{};
return data;
}
} // namespace doctest_detail_test_suite_ns
namespace doctest {
namespace {
// the int (priority) is part of the key for automatic sorting - sadly one can register a
// reporter with a duplicate name and a different priority but hopefully that won't happen often :|
using reporterMap = std::map<std::pair<int, String>, reporterCreatorFunc>;
reporterMap& getReporters() {
static reporterMap data;
return data;
}
reporterMap& getListeners() {
static reporterMap data;
return data;
}
} // namespace
namespace detail {
#define DOCTEST_ITERATE_THROUGH_REPORTERS(function, ...) \
for(auto& curr_rep : g_cs->reporters_currently_used) \
curr_rep->function(__VA_ARGS__)
bool checkIfShouldThrow(assertType::Enum at) {
if(at & assertType::is_require) //!OCLINT bitwise operator in conditional
return true;
if((at & assertType::is_check) //!OCLINT bitwise operator in conditional