-
Notifications
You must be signed in to change notification settings - Fork 0
/
lest.hpp
1489 lines (1220 loc) · 43.8 KB
/
lest.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 2013-2018 by Martin Moene
//
// lest is based on ideas by Kevlin Henney, see video at
// http://skillsmatter.com/podcast/agile-testing/kevlin-henney-rethinking-unit-testing-in-c-plus-plus
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
#ifndef LEST_LEST_HPP_INCLUDED
#define LEST_LEST_HPP_INCLUDED
#include <algorithm>
#include <chrono>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <random>
#include <sstream>
#include <stdexcept>
#include <string>
#include <set>
#include <tuple>
#include <typeinfo>
#include <type_traits>
#include <utility>
#include <vector>
#include <cctype>
#include <cmath>
#include <cstddef>
#define lest_MAJOR 1
#define lest_MINOR 35
#define lest_PATCH 1
#define lest_VERSION lest_STRINGIFY(lest_MAJOR) "." lest_STRINGIFY(lest_MINOR) "." lest_STRINGIFY(lest_PATCH)
#ifndef lest_FEATURE_AUTO_REGISTER
# define lest_FEATURE_AUTO_REGISTER 0
#endif
#ifndef lest_FEATURE_COLOURISE
# define lest_FEATURE_COLOURISE 0
#endif
#ifndef lest_FEATURE_LITERAL_SUFFIX
# define lest_FEATURE_LITERAL_SUFFIX 0
#endif
#ifndef lest_FEATURE_REGEX_SEARCH
# define lest_FEATURE_REGEX_SEARCH 0
#endif
#ifndef lest_FEATURE_TIME_PRECISION
# define lest_FEATURE_TIME_PRECISION 0
#endif
#ifndef lest_FEATURE_WSTRING
# define lest_FEATURE_WSTRING 1
#endif
#ifdef lest_FEATURE_RTTI
# define lest__cpp_rtti lest_FEATURE_RTTI
#elif defined(__cpp_rtti)
# define lest__cpp_rtti __cpp_rtti
#elif defined(__GXX_RTTI) || defined (_CPPRTTI)
# define lest__cpp_rtti 1
#else
# define lest__cpp_rtti 0
#endif
#if lest_FEATURE_REGEX_SEARCH
# include <regex>
#endif
// Stringify:
#define lest_STRINGIFY( x ) lest_STRINGIFY_( x )
#define lest_STRINGIFY_( x ) #x
// Compiler warning suppression:
#if defined (__clang__)
# pragma clang diagnostic ignored "-Waggregate-return"
# pragma clang diagnostic ignored "-Woverloaded-shift-op-parentheses"
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wunused-comparison"
#elif defined (__GNUC__)
# pragma GCC diagnostic ignored "-Waggregate-return"
# pragma GCC diagnostic push
#endif
// Suppress shadow and unused-value warning for sections:
#if defined (__clang__)
# define lest_SUPPRESS_WSHADOW _Pragma( "clang diagnostic push" ) \
_Pragma( "clang diagnostic ignored \"-Wshadow\"" )
# define lest_SUPPRESS_WUNUSED _Pragma( "clang diagnostic push" ) \
_Pragma( "clang diagnostic ignored \"-Wunused-value\"" )
# define lest_RESTORE_WARNINGS _Pragma( "clang diagnostic pop" )
#elif defined (__GNUC__)
# define lest_SUPPRESS_WSHADOW _Pragma( "GCC diagnostic push" ) \
_Pragma( "GCC diagnostic ignored \"-Wshadow\"" )
# define lest_SUPPRESS_WUNUSED _Pragma( "GCC diagnostic push" ) \
_Pragma( "GCC diagnostic ignored \"-Wunused-value\"" )
# define lest_RESTORE_WARNINGS _Pragma( "GCC diagnostic pop" )
#else
# define lest_SUPPRESS_WSHADOW /*empty*/
# define lest_SUPPRESS_WUNUSED /*empty*/
# define lest_RESTORE_WARNINGS /*empty*/
#endif
// C++ language version detection (C++20 is speculative):
// Note: VC14.0/1900 (VS2015) lacks too much from C++14.
#ifndef lest_CPLUSPLUS
# if defined(_MSVC_LANG ) && !defined(__clang__)
# define lest_CPLUSPLUS (_MSC_VER == 1900 ? 201103L : _MSVC_LANG )
# else
# define lest_CPLUSPLUS __cplusplus
# endif
#endif
#define lest_CPP98_OR_GREATER ( lest_CPLUSPLUS >= 199711L )
#define lest_CPP11_OR_GREATER ( lest_CPLUSPLUS >= 201103L )
#define lest_CPP14_OR_GREATER ( lest_CPLUSPLUS >= 201402L )
#define lest_CPP17_OR_GREATER ( lest_CPLUSPLUS >= 201703L )
#define lest_CPP20_OR_GREATER ( lest_CPLUSPLUS >= 202000L )
#if ! defined( lest_NO_SHORT_MACRO_NAMES ) && ! defined( lest_NO_SHORT_ASSERTION_NAMES )
# define MODULE lest_MODULE
# if ! lest_FEATURE_AUTO_REGISTER
# define CASE lest_CASE
# define CASE_ON lest_CASE_ON
# define SCENARIO lest_SCENARIO
# endif
# define SETUP lest_SETUP
# define SECTION lest_SECTION
# define EXPECT lest_EXPECT
# define EXPECT_NOT lest_EXPECT_NOT
# define EXPECT_NO_THROW lest_EXPECT_NO_THROW
# define EXPECT_THROWS lest_EXPECT_THROWS
# define EXPECT_THROWS_AS lest_EXPECT_THROWS_AS
# define GIVEN lest_GIVEN
# define WHEN lest_WHEN
# define THEN lest_THEN
# define AND_WHEN lest_AND_WHEN
# define AND_THEN lest_AND_THEN
#endif
#if lest_FEATURE_AUTO_REGISTER
#define lest_SCENARIO( specification, sketch ) lest_CASE( specification, lest::text("Scenario: ") + sketch )
#else
#define lest_SCENARIO( sketch ) lest_CASE( lest::text("Scenario: ") + sketch )
#endif
#define lest_GIVEN( context ) lest_SETUP( lest::text(" Given: ") + context )
#define lest_WHEN( story ) lest_SECTION( lest::text(" When: ") + story )
#define lest_THEN( story ) lest_SECTION( lest::text(" Then: ") + story )
#define lest_AND_WHEN( story ) lest_SECTION( lest::text("And then: ") + story )
#define lest_AND_THEN( story ) lest_SECTION( lest::text("And then: ") + story )
#if lest_FEATURE_AUTO_REGISTER
# define lest_CASE( specification, proposition ) \
static void lest_FUNCTION( lest::env & ); \
namespace { lest::add_test lest_REGISTRAR( specification, lest::test( proposition, lest_FUNCTION ) ); } \
static void lest_FUNCTION( lest::env & lest_env )
#else // lest_FEATURE_AUTO_REGISTER
# define lest_CASE( proposition ) \
proposition, []( lest::env & lest_env )
# define lest_CASE_ON( proposition, ... ) \
proposition, [__VA_ARGS__]( lest::env & lest_env )
# define lest_MODULE( specification, module ) \
namespace { lest::add_module _( specification, module ); }
#endif //lest_FEATURE_AUTO_REGISTER
#define lest_SETUP( context ) \
for ( int lest__section = 0, lest__count = 1; lest__section < lest__count; lest__count -= 0==lest__section++ ) \
for ( lest::ctx lest__ctx_setup( lest_env, context ); lest__ctx_setup; )
#define lest_SECTION( proposition ) \
lest_SUPPRESS_WSHADOW \
static int lest_UNIQUE( id ) = 0; \
if ( lest::guard( lest_UNIQUE( id ), lest__section, lest__count ) ) \
for ( int lest__section = 0, lest__count = 1; lest__section < lest__count; lest__count -= 0==lest__section++ ) \
for ( lest::ctx lest__ctx_section( lest_env, proposition ); lest__ctx_section; ) \
lest_RESTORE_WARNINGS
#define lest_EXPECT( expr ) \
do { \
try \
{ \
if ( lest::result score = lest_DECOMPOSE( expr ) ) \
{ \
lest::report( lest_env.os, lest::failure{ lest_LOCATION, #expr, score.decomposition }, lest_env.context() ); \
throw lest::failure{ lest_LOCATION, #expr, score.decomposition }; \
} \
else if ( lest_env.pass() ) \
lest::report( lest_env.os, lest::passing{ lest_LOCATION, #expr, score.decomposition, lest_env.zen() }, lest_env.context() ); \
} \
catch(...) \
{ \
lest::inform( lest_LOCATION, #expr ); \
} \
} while ( lest::is_false() )
#define lest_EXPECT_NOT( expr ) \
do { \
try \
{ \
if ( lest::result score = lest_DECOMPOSE( expr ) ) \
{ \
if ( lest_env.pass() ) \
lest::report( lest_env.os, lest::passing{ lest_LOCATION, lest::not_expr( #expr ), lest::not_expr( score.decomposition ), lest_env.zen() }, lest_env.context() ); \
} \
else \
{ \
lest::report( lest_env.os, lest::failure{ lest_LOCATION, lest::not_expr( #expr ), lest::not_expr( score.decomposition ) }, lest_env.context() ); \
throw lest::failure{ lest_LOCATION, lest::not_expr( #expr ), lest::not_expr( score.decomposition ) }; \
} \
} \
catch(...) \
{ \
lest::inform( lest_LOCATION, lest::not_expr( #expr ) ); \
} \
} while ( lest::is_false() )
#define lest_EXPECT_NO_THROW( expr ) \
do \
{ \
try \
{ \
lest_SUPPRESS_WUNUSED \
expr; \
lest_RESTORE_WARNINGS \
} \
catch (...) \
{ \
lest::inform( lest_LOCATION, #expr ); \
} \
if ( lest_env.pass() ) \
lest::report( lest_env.os, lest::got_none( lest_LOCATION, #expr ), lest_env.context() ); \
} while ( lest::is_false() )
#define lest_EXPECT_THROWS( expr ) \
do \
{ \
try \
{ \
lest_SUPPRESS_WUNUSED \
expr; \
lest_RESTORE_WARNINGS \
} \
catch (...) \
{ \
if ( lest_env.pass() ) \
lest::report( lest_env.os, lest::got{ lest_LOCATION, #expr }, lest_env.context() ); \
break; \
} \
throw lest::expected{ lest_LOCATION, #expr }; \
} \
while ( lest::is_false() )
#define lest_EXPECT_THROWS_AS( expr, excpt ) \
do \
{ \
try \
{ \
lest_SUPPRESS_WUNUSED \
expr; \
lest_RESTORE_WARNINGS \
} \
catch ( excpt & ) \
{ \
if ( lest_env.pass() ) \
lest::report( lest_env.os, lest::got{ lest_LOCATION, #expr, lest::of_type( #excpt ) }, lest_env.context() ); \
break; \
} \
catch (...) {} \
throw lest::expected{ lest_LOCATION, #expr, lest::of_type( #excpt ) }; \
} \
while ( lest::is_false() )
#define lest_UNIQUE( name ) lest_UNIQUE2( name, __LINE__ )
#define lest_UNIQUE2( name, line ) lest_UNIQUE3( name, line )
#define lest_UNIQUE3( name, line ) name ## line
#define lest_DECOMPOSE( expr ) ( lest::expression_decomposer() << expr )
#define lest_FUNCTION lest_UNIQUE(__lest_function__ )
#define lest_REGISTRAR lest_UNIQUE(__lest_registrar__ )
#define lest_LOCATION lest::location{__FILE__, __LINE__}
namespace lest {
const int exit_max_value = 255;
using text = std::string;
using texts = std::vector<text>;
struct env;
struct test
{
text name;
std::function<void( env & )> behaviour;
#if lest_FEATURE_AUTO_REGISTER
test( text name_, std::function<void( env & )> behaviour_ )
: name( name_), behaviour( behaviour_) {}
#endif
};
using tests = std::vector<test>;
#if lest_FEATURE_AUTO_REGISTER
struct add_test
{
add_test( tests & specification, test const & test_case )
{
specification.push_back( test_case );
}
};
#else
struct add_module
{
template< std::size_t N >
add_module( tests & specification, test const (&module)[N] )
{
specification.insert( specification.end(), std::begin( module ), std::end( module ) );
}
};
#endif
struct result
{
const bool passed;
const text decomposition;
template< typename T >
result( T const & passed_, text decomposition_)
: passed( !!passed_), decomposition( decomposition_) {}
explicit operator bool() { return ! passed; }
};
struct location
{
const text file;
const int line;
location( text file_, int line_)
: file( file_), line( line_) {}
};
struct comment
{
const text info;
comment( text info_) : info( info_) {}
explicit operator bool() { return ! info.empty(); }
};
struct message : std::runtime_error
{
const text kind;
const location where;
const comment note;
~message() throw() {} // GCC 4.6
message( text kind_, location where_, text expr_, text note_ = "" )
: std::runtime_error( expr_), kind( kind_), where( where_), note( note_) {}
};
struct failure : message
{
failure( location where_, text expr_, text decomposition_)
: message{ "failed", where_, expr_ + " for " + decomposition_ } {}
};
struct success : message
{
// using message::message; // VC is lagging here
success( text kind_, location where_, text expr_, text note_ = "" )
: message( kind_, where_, expr_, note_ ) {}
};
struct passing : success
{
passing( location where_, text expr_, text decomposition_, bool zen )
: success( "passed", where_, expr_ + (zen ? "":" for " + decomposition_) ) {}
};
struct got_none : success
{
got_none( location where_, text expr_ )
: success( "passed: got no exception", where_, expr_ ) {}
};
struct got : success
{
got( location where_, text expr_)
: success( "passed: got exception", where_, expr_) {}
got( location where_, text expr_, text excpt_)
: success( "passed: got exception " + excpt_, where_, expr_) {}
};
struct expected : message
{
expected( location where_, text expr_, text excpt_ = "" )
: message{ "failed: didn't get exception", where_, expr_, excpt_ } {}
};
struct unexpected : message
{
unexpected( location where_, text expr_, text note_ = "" )
: message{ "failed: got unexpected exception", where_, expr_, note_ } {}
};
struct guard
{
int & id;
int const & section;
guard( int & id_, int const & section_, int & count )
: id( id_), section( section_)
{
if ( section == 0 )
id = count++ - 1;
}
operator bool() { return id == section; }
};
class approx
{
public:
explicit approx ( double magnitude )
: epsilon_ { std::numeric_limits<float>::epsilon() * 100 }
, scale_ { 1.0 }
, magnitude_{ magnitude } {}
approx( approx const & other ) = default;
static approx custom() { return approx( 0 ); }
approx operator()( double new_magnitude )
{
approx appr( new_magnitude );
appr.epsilon( epsilon_ );
appr.scale ( scale_ );
return appr;
}
double magnitude() const { return magnitude_; }
approx & epsilon( double epsilon ) { epsilon_ = epsilon; return *this; }
approx & scale ( double scale ) { scale_ = scale; return *this; }
friend bool operator == ( double lhs, approx const & rhs )
{
// Thanks to Richard Harris for his help refining this formula.
return std::abs( lhs - rhs.magnitude_ ) < rhs.epsilon_ * ( rhs.scale_ + (std::min)( std::abs( lhs ), std::abs( rhs.magnitude_ ) ) );
}
friend bool operator == ( approx const & lhs, double rhs ) { return operator==( rhs, lhs ); }
friend bool operator != ( double lhs, approx const & rhs ) { return !operator==( lhs, rhs ); }
friend bool operator != ( approx const & lhs, double rhs ) { return !operator==( rhs, lhs ); }
friend bool operator <= ( double lhs, approx const & rhs ) { return lhs < rhs.magnitude_ || lhs == rhs; }
friend bool operator <= ( approx const & lhs, double rhs ) { return lhs.magnitude_ < rhs || lhs == rhs; }
friend bool operator >= ( double lhs, approx const & rhs ) { return lhs > rhs.magnitude_ || lhs == rhs; }
friend bool operator >= ( approx const & lhs, double rhs ) { return lhs.magnitude_ > rhs || lhs == rhs; }
private:
double epsilon_;
double scale_;
double magnitude_;
};
inline bool is_false( ) { return false; }
inline bool is_true ( bool flag ) { return flag; }
inline text not_expr( text message )
{
return "! ( " + message + " )";
}
inline text with_message( text message )
{
return "with message \"" + message + "\"";
}
inline text of_type( text type )
{
return "of type " + type;
}
inline void inform( location where, text expr )
{
try
{
throw;
}
catch( message const & )
{
throw;
}
catch( std::exception const & e )
{
throw unexpected{ where, expr, with_message( e.what() ) }; \
}
catch(...)
{
throw unexpected{ where, expr, "of unknown type" }; \
}
}
// Expression decomposition:
template< typename T >
auto make_value_string( T const & value ) -> std::string;
template< typename T >
auto make_memory_string( T const & item ) -> std::string;
#if lest_FEATURE_LITERAL_SUFFIX
inline char const * sfx( char const * txt ) { return txt; }
#else
inline char const * sfx( char const * ) { return ""; }
#endif
inline std::string transformed( char chr )
{
struct Tr { char chr; char const * str; } table[] =
{
{'\\', "\\\\" },
{'\r', "\\r" }, {'\f', "\\f" },
{'\n', "\\n" }, {'\t', "\\t" },
};
for ( auto tr : table )
{
if ( chr == tr.chr )
return tr.str;
}
auto unprintable = [](char c){ return 0 <= c && c < ' '; };
auto to_hex_string = [](char c)
{
std::ostringstream os;
os << "\\x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>( static_cast<unsigned char>(c) );
return os.str();
};
return unprintable( chr ) ? to_hex_string( chr ) : std::string( 1, chr );
}
inline std::string make_tran_string( std::string const & txt ) { std::ostringstream os; for(auto c:txt) os << transformed(c); return os.str(); }
inline std::string make_strg_string( std::string const & txt ) { return "\"" + make_tran_string( txt ) + "\"" ; }
inline std::string make_char_string( char chr ) { return "\'" + make_tran_string( std::string( 1, chr ) ) + "\'" ; }
inline std::string to_string( std::nullptr_t ) { return "nullptr"; }
inline std::string to_string( std::string const & txt ) { return make_strg_string( txt ); }
#if lest_FEATURE_WSTRING
inline std::string to_string( std::wstring const & txt ) ;
#endif
inline std::string to_string( char const * const txt ) { return txt ? make_strg_string( txt ) : "{null string}"; }
inline std::string to_string( char * const txt ) { return txt ? make_strg_string( txt ) : "{null string}"; }
#if lest_FEATURE_WSTRING
inline std::string to_string( wchar_t const * const txt ) { return txt ? to_string( std::wstring( txt ) ) : "{null string}"; }
inline std::string to_string( wchar_t * const txt ) { return txt ? to_string( std::wstring( txt ) ) : "{null string}"; }
#endif
inline std::string to_string( bool flag ) { return flag ? "true" : "false"; }
inline std::string to_string( signed short value ) { return make_value_string( value ) ; }
inline std::string to_string( unsigned short value ) { return make_value_string( value ) + sfx("u" ); }
inline std::string to_string( signed int value ) { return make_value_string( value ) ; }
inline std::string to_string( unsigned int value ) { return make_value_string( value ) + sfx("u" ); }
inline std::string to_string( signed long value ) { return make_value_string( value ) + sfx("l" ); }
inline std::string to_string( unsigned long value ) { return make_value_string( value ) + sfx("ul" ); }
inline std::string to_string( signed long long value ) { return make_value_string( value ) + sfx("ll" ); }
inline std::string to_string( unsigned long long value ) { return make_value_string( value ) + sfx("ull"); }
inline std::string to_string( double value ) { return make_value_string( value ) ; }
inline std::string to_string( float value ) { return make_value_string( value ) + sfx("f" ); }
inline std::string to_string( signed char chr ) { return make_char_string( static_cast<char>( chr ) ); }
inline std::string to_string( unsigned char chr ) { return make_char_string( static_cast<char>( chr ) ); }
inline std::string to_string( char chr ) { return make_char_string( chr ); }
template< typename T >
struct is_streamable
{
template< typename U >
static auto test( int ) -> decltype( std::declval<std::ostream &>() << std::declval<U>(), std::true_type() );
template< typename >
static auto test( ... ) -> std::false_type;
#ifdef _MSC_VER
enum { value = std::is_same< decltype( test<T>(0) ), std::true_type >::value };
#else
static constexpr bool value = std::is_same< decltype( test<T>(0) ), std::true_type >::value;
#endif
};
template< typename T >
struct is_container
{
template< typename U >
static auto test( int ) -> decltype( std::declval<U>().begin() == std::declval<U>().end(), std::true_type() );
template< typename >
static auto test( ... ) -> std::false_type;
#ifdef _MSC_VER
enum { value = std::is_same< decltype( test<T>(0) ), std::true_type >::value };
#else
static constexpr bool value = std::is_same< decltype( test<T>(0) ), std::true_type >::value;
#endif
};
template< typename T, typename R >
using ForEnum = typename std::enable_if< std::is_enum<T>::value, R>::type;
template< typename T, typename R >
using ForNonEnum = typename std::enable_if< ! std::is_enum<T>::value, R>::type;
template< typename T, typename R >
using ForStreamable = typename std::enable_if< is_streamable<T>::value, R>::type;
template< typename T, typename R >
using ForNonStreamable = typename std::enable_if< ! is_streamable<T>::value, R>::type;
template< typename T, typename R >
using ForContainer = typename std::enable_if< is_container<T>::value, R>::type;
template< typename T, typename R >
using ForNonContainerNonPointer = typename std::enable_if< ! (is_container<T>::value || std::is_pointer<T>::value), R>::type;
template< typename T >
auto make_enum_string( T const & item ) -> ForNonEnum<T, std::string>
{
#if lest__cpp_rtti
return text("[type: ") + typeid(T).name() + "]: " + make_memory_string( item );
#else
return text("[type: (no RTTI)]: ") + make_memory_string( item );
#endif
}
template< typename T >
auto make_enum_string( T const & item ) -> ForEnum<T, std::string>
{
return to_string( static_cast<typename std::underlying_type<T>::type>( item ) );
}
template< typename T >
auto make_string( T const & item ) -> ForNonStreamable<T, std::string>
{
return make_enum_string( item );
}
template< typename T >
auto make_string( T const & item ) -> ForStreamable<T, std::string>
{
std::ostringstream os; os << item; return os.str();
}
template<typename T1, typename T2>
auto make_string( std::pair<T1,T2> const & pair ) -> std::string
{
std::ostringstream oss;
oss << "{ " << to_string( pair.first ) << ", " << to_string( pair.second ) << " }";
return oss.str();
}
template< typename TU, std::size_t N >
struct make_tuple_string
{
static std::string make( TU const & tuple )
{
std::ostringstream os;
os << to_string( std::get<N - 1>( tuple ) ) << ( N < std::tuple_size<TU>::value ? ", ": " ");
return make_tuple_string<TU, N - 1>::make( tuple ) + os.str();
}
};
template< typename TU >
struct make_tuple_string<TU, 0>
{
static std::string make( TU const & ) { return ""; }
};
template< typename ...TS >
auto make_string( std::tuple<TS...> const & tuple ) -> std::string
{
return "{ " + make_tuple_string<std::tuple<TS...>, sizeof...(TS)>::make( tuple ) + "}";
}
template< typename T >
inline std::string make_string( T const * ptr )
{
// Note showbase affects the behavior of /integer/ output;
std::ostringstream os;
os << std::internal << std::hex << std::showbase << std::setw( 2 + 2 * sizeof(T*) ) << std::setfill('0') << reinterpret_cast<std::ptrdiff_t>( ptr );
return os.str();
}
template< typename C, typename R >
inline std::string make_string( R C::* ptr )
{
std::ostringstream os;
os << std::internal << std::hex << std::showbase << std::setw( 2 + 2 * sizeof(R C::* ) ) << std::setfill('0') << ptr;
return os.str();
}
template< typename T >
auto to_string( T const * ptr ) -> std::string
{
return ! ptr ? "nullptr" : make_string( ptr );
}
template<typename C, typename R>
auto to_string( R C::* ptr ) -> std::string
{
return ! ptr ? "nullptr" : make_string( ptr );
}
template< typename T >
auto to_string( T const & item ) -> ForNonContainerNonPointer<T, std::string>
{
return make_string( item );
}
template< typename C >
auto to_string( C const & cont ) -> ForContainer<C, std::string>
{
std::ostringstream os;
os << "{ ";
for ( auto & x : cont )
{
os << to_string( x ) << ", ";
}
os << "}";
return os.str();
}
#if lest_FEATURE_WSTRING
inline
auto to_string( std::wstring const & txt ) -> std::string
{
std::string result; result.reserve( txt.size() );
for( auto & chr : txt )
{
result += chr <= 0xff ? static_cast<char>( chr ) : '?';
}
return to_string( result );
}
#endif
template< typename T >
auto make_value_string( T const & value ) -> std::string
{
std::ostringstream os; os << value; return os.str();
}
inline
auto make_memory_string( void const * item, std::size_t size ) -> std::string
{
// reverse order for little endian architectures:
auto is_little_endian = []
{
union U { int i = 1; char c[ sizeof(int) ]; };
return 1 != U{}.c[ sizeof(int) - 1 ];
};
int i = 0, end = static_cast<int>( size ), inc = 1;
if ( is_little_endian() ) { i = end - 1; end = inc = -1; }
unsigned char const * bytes = static_cast<unsigned char const *>( item );
std::ostringstream os;
os << "0x" << std::setfill( '0' ) << std::hex;
for ( ; i != end; i += inc )
{
os << std::setw(2) << static_cast<unsigned>( bytes[i] ) << " ";
}
return os.str();
}
template< typename T >
auto make_memory_string( T const & item ) -> std::string
{
return make_memory_string( &item, sizeof item );
}
inline
auto to_string( approx const & appr ) -> std::string
{
return to_string( appr.magnitude() );
}
template< typename L, typename R >
auto to_string( L const & lhs, std::string op, R const & rhs ) -> std::string
{
std::ostringstream os; os << to_string( lhs ) << " " << op << " " << to_string( rhs ); return os.str();
}
template< typename L >
struct expression_lhs
{
const L lhs;
expression_lhs( L lhs_) : lhs( lhs_) {}
operator result() { return result{ !!lhs, to_string( lhs ) }; }
template< typename R > result operator==( R const & rhs ) { return result{ lhs == rhs, to_string( lhs, "==", rhs ) }; }
template< typename R > result operator!=( R const & rhs ) { return result{ lhs != rhs, to_string( lhs, "!=", rhs ) }; }
template< typename R > result operator< ( R const & rhs ) { return result{ lhs < rhs, to_string( lhs, "<" , rhs ) }; }
template< typename R > result operator<=( R const & rhs ) { return result{ lhs <= rhs, to_string( lhs, "<=", rhs ) }; }
template< typename R > result operator> ( R const & rhs ) { return result{ lhs > rhs, to_string( lhs, ">" , rhs ) }; }
template< typename R > result operator>=( R const & rhs ) { return result{ lhs >= rhs, to_string( lhs, ">=", rhs ) }; }
};
struct expression_decomposer
{
template <typename L>
expression_lhs<L const &> operator<< ( L const & operand )
{
return expression_lhs<L const &>( operand );
}
};
// Reporter:
#if lest_FEATURE_COLOURISE
inline text red ( text words ) { return "\033[1;31m" + words + "\033[0m"; }
inline text green( text words ) { return "\033[1;32m" + words + "\033[0m"; }
inline text gray ( text words ) { return "\033[1;30m" + words + "\033[0m"; }
inline bool starts_with( text words, text with )
{
return 0 == words.find( with );
}
inline text replace( text words, text from, text to )
{
size_t pos = words.find( from );
return pos == std::string::npos ? words : words.replace( pos, from.length(), to );
}
inline text colour( text words )
{
if ( starts_with( words, "failed" ) ) return replace( words, "failed", red ( "failed" ) );
else if ( starts_with( words, "passed" ) ) return replace( words, "passed", green( "passed" ) );
return replace( words, "for", gray( "for" ) );
}
inline bool is_cout( std::ostream & os ) { return &os == &std::cout; }
struct colourise
{
const text words;
colourise( text words )
: words( words ) {}
// only colourise for std::cout, not for a stringstream as used in tests:
std::ostream & operator()( std::ostream & os ) const
{
return is_cout( os ) ? os << colour( words ) : os << words;
}
};
inline std::ostream & operator<<( std::ostream & os, colourise words ) { return words( os ); }
#else
inline text colourise( text words ) { return words; }
#endif
inline text pluralise( text word, int n )
{
return n == 1 ? word : word + "s";
}
inline std::ostream & operator<<( std::ostream & os, comment note )
{
return os << (note ? " " + note.info : "" );
}
inline std::ostream & operator<<( std::ostream & os, location where )
{
#ifdef __GNUG__
return os << where.file << ":" << where.line;
#else
return os << where.file << "(" << where.line << ")";
#endif
}
inline void report( std::ostream & os, message const & e, text test )
{
os << e.where << ": " << colourise( e.kind ) << e.note << ": " << test << ": " << colourise( e.what() ) << std::endl;
}
// Test runner:
#if lest_FEATURE_REGEX_SEARCH
inline bool search( text re, text line )
{
return std::regex_search( line, std::regex( re ) );
}
#else
inline bool search( text part, text line )
{
auto case_insensitive_equal = []( char a, char b )
{
return tolower( a ) == tolower( b );
};
return std::search(
line.begin(), line.end(),
part.begin(), part.end(), case_insensitive_equal ) != line.end();
}
#endif
inline bool match( texts whats, text line )
{
for ( auto & what : whats )
{
if ( search( what, line ) )
return true;
}
return false;
}
inline bool select( text name, texts include )
{
auto none = []( texts args ) { return args.size() == 0; };
#if lest_FEATURE_REGEX_SEARCH
auto hidden = []( text arg ){ return match( { "\\[\\..*", "\\[hide\\]" }, arg ); };
#else
auto hidden = []( text arg ){ return match( { "[.", "[hide]" }, arg ); };
#endif
if ( none( include ) )
{
return ! hidden( name );
}
bool any = false;
for ( auto pos = include.rbegin(); pos != include.rend(); ++pos )
{
auto & part = *pos;
if ( part == "@" || part == "*" )
return true;
if ( search( part, name ) )
return true;
if ( '!' == part[0] )
{
any = true;
if ( search( part.substr(1), name ) )
return false;
}
else
{
any = false;
}