forked from martinmoene/optional-lite
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lest_cpp03.hpp
1321 lines (1080 loc) · 36.5 KB
/
lest_cpp03.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 <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <sstream>
#include <stdexcept>
#include <set>
#include <string>
#include <utility>
#include <vector>
#include <cctype>
#include <cmath>
#include <cstddef>
#include <cstdlib>
#include <ctime>
#define lest_VERSION "1.33.0"
#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
# define lest_FEATURE_TIME 1
#endif
#ifndef lest_FEATURE_TIME_PRECISION
#define lest_FEATURE_TIME_PRECISION 0
#endif
#ifdef _WIN32
# define lest_PLATFORM_IS_WINDOWS 1
#else
# define lest_PLATFORM_IS_WINDOWS 0
#endif
#if lest_FEATURE_REGEX_SEARCH
# include <regex>
#endif
#if lest_FEATURE_TIME
# if lest_PLATFORM_IS_WINDOWS
# include <iomanip>
# include <Windows.h>
# else
# include <iomanip>
# include <sys/time.h>
# endif
#endif
// Compiler warning suppression:
#ifdef __clang__
# pragma clang diagnostic ignored "-Woverloaded-shift-op-parentheses"
# pragma clang diagnostic ignored "-Wshadow"
# pragma clang diagnostic ignored "-Wunused-parameter"
# pragma clang diagnostic ignored "-Wunused-value"
# pragma clang diagnostic push
# pragma clang diagnostic ignored "-Wdate-time"
# pragma clang diagnostic ignored "-Wundef"
#elif defined __GNUC__
# pragma GCC diagnostic ignored "-Wunused-parameter"
# pragma GCC diagnostic ignored "-Wunused-value"
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Wundef"
#endif
// Compiler versions:
#if defined(_MSC_VER)
# define lest_COMPILER_MSVC_VERSION (_MSC_VER / 100 - 5 - (_MSC_VER < 1900))
#else
# define lest_COMPILER_MSVC_VERSION 0
#endif
#define lest_COMPILER_IS_MSVC6 ( lest_COMPILER_MSVC_VERSION == 6 )
// C++ language support detection (C++20 is speculative):
// Note: MSVC supports C++14 since it supports C++17.
#ifdef _MSVC_LANG
# define lest_MSVC_LANG _MSVC_LANG
#else
# define lest_MSVC_LANG 0
#endif
#define lest_CPP11 (__cplusplus == 201103L )
#define lest_CPP11_OR_GREATER (__cplusplus >= 201103L || lest_MSVC_LANG >= 201103L || lest_COMPILER_MSVC_VERSION >= 12 )
#define lest_CPP14_OR_GREATER (__cplusplus >= 201402L || lest_MSVC_LANG >= 201703L )
#define lest_CPP17_OR_GREATER (__cplusplus >= 201703L || lest_MSVC_LANG >= 201703L )
#define lest_CPP20_OR_GREATER (__cplusplus >= 202000L || lest_MSVC_LANG >= 202000L )
// Presence of language and library features:
#define lest_HAVE(FEATURE) ( lest_HAVE_##FEATURE )
// Presence of C++11 language features:
#if lest_CPP11_OR_GREATER || lest_COMPILER_MSVC_VERSION >= 10
# define lest_HAVE_NULLPTR 1
#endif
// C++ feature usage:
#if lest_HAVE( NULLPTR )
# define lest_nullptr nullptr
#else
# define lest_nullptr NULL
#endif
// Additional includes and tie:
#if lest_CPP11_OR_GREATER || lest_COMPILER_MSVC_VERSION >= 10
# include <cstdint>
# include <random>
# include <tuple>
namespace lest
{
using std::tie;
}
#else
# if !defined(__clang__) && defined(__GNUC__)
# pragma GCC diagnostic push
# pragma GCC diagnostic ignored "-Weffc++"
# endif
namespace lest
{
template< typename T1, typename T2>
struct Tie
{
Tie( T1 & first, T2 & second )
: first( first ), second( second ) {}
std::pair<T1, T2> const &
operator=( std::pair<T1, T2> const & rhs )
{
first = rhs.first;
second = rhs.second;
return rhs;
}
private:
void operator=( Tie const & );
T1 & first;
T2 & second;
};
template<typename T1, typename T2>
inline Tie<T1,T2> tie( T1 & first, T2 & second )
{
return Tie<T1, T2>( first, second );
}
# if !defined(__clang__) && defined(__GNUC__)
# pragma GCC diagnostic pop
# endif
}
#endif // lest_CPP11_OR_GREATER
namespace lest
{
#if lest_COMPILER_IS_MSVC6
using ::strtol;
using ::rand;
using ::srand;
inline double abs( double x ) { return ::fabs( x ); }
template< typename T >
T const & (min)(T const & a, T const & b) { return a <= b ? a : b; }
#else
using std::abs;
using std::min;
using std::strtol;
using std::rand;
using std::srand;
#endif
}
#if ! defined( lest_NO_SHORT_MACRO_NAMES ) && ! defined( lest_NO_SHORT_ASSERTION_NAMES )
# 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 SCENARIO lest_SCENARIO
# 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
#define lest_SCENARIO( specification, sketch ) \
lest_CASE( specification, lest::text("Scenario: ") + sketch )
#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: ") + story )
#define lest_AND_THEN( story ) lest_SECTION( lest::text( " And: ") + story )
#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 )
#define lest_ADD_TEST( specification, test ) \
specification.push_back( test )
#define lest_SETUP( context ) \
for ( int lest__section = 0, lest__count = 1; lest__section < lest__count; lest__count -= 0==lest__section++ )
#define lest_SECTION( proposition ) \
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++ )
#define lest_EXPECT( expr ) \
do { \
try \
{ \
if ( lest::result score = lest_DECOMPOSE( expr ) ) \
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.testing ); \
} \
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.testing ); \
} \
else \
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 { expr; } \
catch (...) { lest::inform( lest_LOCATION, #expr ); } \
if ( lest_env.pass ) \
lest::report( lest_env.os, lest::got_none( lest_LOCATION, #expr ), lest_env.testing ); \
} while ( lest::is_false() )
#define lest_EXPECT_THROWS( expr ) \
do \
{ \
try \
{ \
expr; \
} \
catch (...) \
{ \
if ( lest_env.pass ) \
lest::report( lest_env.os, lest::got( lest_LOCATION, #expr ), lest_env.testing ); \
break; \
} \
throw lest::expected( lest_LOCATION, #expr ); \
} \
while ( lest::is_false() )
#define lest_EXPECT_THROWS_AS( expr, excpt ) \
do \
{ \
try \
{ \
expr; \
} \
catch ( excpt & ) \
{ \
if ( lest_env.pass ) \
lest::report( lest_env.os, lest::got( lest_LOCATION, #expr, lest::of_type( #excpt ) ), lest_env.testing ); \
break; \
} \
catch (...) {} \
throw lest::expected( lest_LOCATION, #expr, lest::of_type( #excpt ) ); \
} \
while ( lest::is_false() )
#define lest_DECOMPOSE( expr ) ( lest::expression_decomposer() << expr )
#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_LOCATION lest::location(__FILE__, __LINE__)
#define lest_FUNCTION lest_UNIQUE(__lest_function__ )
#define lest_REGISTRAR lest_UNIQUE(__lest_registrar__ )
#define lest_DIMENSION_OF( a ) ( sizeof(a) / sizeof(0[a]) )
namespace lest {
typedef std::string text;
typedef std::vector<text> texts;
struct env;
struct test
{
text name;
void (* behaviour)( env & );
test( text name, void (* behaviour)( env & ) )
: name( name ), behaviour( behaviour ) {}
};
typedef std::vector<test> tests;
typedef tests test_specification;
struct add_test
{
add_test( tests & specification, test const & test_case )
{
specification.push_back( test_case );
}
};
struct result
{
const bool passed;
const text decomposition;
template< typename T >
result( T const & passed, text decomposition )
: passed( !!passed ), decomposition( decomposition ) {}
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 ) {}
operator bool() { return ! info.empty(); }
};
struct message : std::runtime_error
{
const text kind;
const location where;
const comment note;
#if ! lest_CPP11_OR_GREATER
~message() throw() {}
#endif
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
{
success( text kind, location where, text expr, text note = "" )
: message( kind, where, expr, note ) {}
};
struct passing : success
{
passing( location where, text expr, text decomposition )
: success( "passed", where, expr + " 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_ ( 100.0 * static_cast<double>( std::numeric_limits<float>::epsilon() ) )
, scale_ ( 1.0 )
, magnitude_( magnitude ) {}
static approx custom() { return approx( 0 ); }
approx operator()( double magnitude )
{
approx approx ( magnitude );
approx.epsilon( epsilon_ );
approx.scale ( scale_ );
return approx;
}
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 lest::abs( lhs - rhs.magnitude_ ) < rhs.epsilon_ * ( rhs.scale_ + (lest::min)( lest::abs( lhs ), lest::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( failure 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:
#if lest_CPP11_OR_GREATER || lest_COMPILER_MSVC_VERSION >= 10
inline std::string to_string( std::nullptr_t const & ) { return "nullptr"; }
#endif
inline std::string to_string( std::string const & text ) { return "\"" + text + "\"" ; }
inline std::string to_string( char const * const & text ) { return "\"" + std::string( text ) + "\"" ; }
inline std::string to_string( char const & text ) { return "\'" + std::string( 1, text ) + "\'" ; }
inline std::ostream & operator<<( std::ostream & os, approx const & appr )
{
return os << appr.magnitude();
}
template <typename T>
std::string to_string( T const & value, int=0 /* VC6 */ )
{
std::ostringstream os; os << std::boolalpha << value; return os.str();
}
template<typename T1, typename T2>
std::string to_string( std::pair<T1,T2> const & pair )
{
std::ostringstream oss;
oss << "{ " << to_string( pair.first ) << ", " << to_string( pair.second ) << " }";
return oss.str();
}
#if lest_CPP11_OR_GREATER
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 to_string( std::tuple<TS...> const & tuple ) -> std::string
{
return "{ " + make_tuple_string<std::tuple<TS...>, sizeof...(TS)>::make( tuple ) + "}";
}
#endif // lest_CPP11_OR_GREATER
template <typename L, typename R>
std::string to_string( L const & lhs, std::string op, R const & rhs )
{
std::ostringstream os; os << to_string( lhs ) << " " << op << " " << to_string( rhs ); return os.str();
}
template <typename L>
struct expression_lhs
{
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 case_insensitive_equal( char a, char b )
{
return tolower( a ) == tolower( b );
}
inline bool search( text part, text line )
{
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 ( texts::iterator what = whats.begin(); what != whats.end() ; ++what )
{
if ( search( *what, line ) )
return true;
}
return false;
}
inline bool hidden( text name )
{
#if lest_FEATURE_REGEX_SEARCH
texts skipped; skipped.push_back( "\\[\\.\\]" ); skipped.push_back( "\\[hide\\]" );
#else
texts skipped; skipped.push_back( "[." ); skipped.push_back( "[hide]" );
#endif
return match( skipped, name );
}
inline bool none( texts args )
{
return args.size() == 0;
}
inline bool select( text name, texts include )
{
if ( none( include ) )
{
return ! hidden( name );
}
bool any = false;
for ( texts::reverse_iterator pos = include.rbegin(); pos != include.rend(); ++pos )
{
text & 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;
}
}
return any && ! hidden( name );
}
inline int indefinite( int repeat ) { return repeat == -1; }
typedef unsigned long seed_t;
struct options
{
options()
: help(false), abort(false), count(false), list(false), tags(false), time(false)
, pass(false), lexical(false), random(false), version(false), repeat(1), seed(0) {}
bool help;
bool abort;
bool count;
bool list;
bool tags;
bool time;
bool pass;
bool lexical;
bool random;
bool version;
int repeat;
seed_t seed;
};
struct env
{
std::ostream & os;
bool pass;
text testing;
env( std::ostream & os, bool pass )
: os( os ), pass( pass ), testing() {}
env & operator()( text test )
{
testing = test; return *this;
}
};
struct action
{
std::ostream & os;
action( std::ostream & os ) : os( os ) {}
operator int() { return 0; }
bool abort() { return false; }
action & operator()( test ) { return *this; }
private:
action( action const & );
void operator=(action const & );
};
struct print : action
{
print( std::ostream & os ) : action( os ) {}
print & operator()( test testing )
{
os << testing.name << "\n"; return *this;
}
};
inline texts tags( text name, texts result = texts() )
{
size_t none = std::string::npos;
size_t lb = name.find_first_of( "[" );
size_t rb = name.find_first_of( "]" );
if ( lb == none || rb == none )
return result;
result.push_back( name.substr( lb, rb - lb + 1 ) );
return tags( name.substr( rb + 1 ), result );
}
struct ptags : action
{
std::set<text> result;
ptags( std::ostream & os ) : action( os ), result() {}
ptags & operator()( test testing )
{
texts tags_( tags( testing.name ) );
for ( texts::iterator pos = tags_.begin(); pos != tags_.end() ; ++pos )
result.insert( *pos );
return *this;
}
~ptags()
{
std::copy( result.begin(), result.end(), std::ostream_iterator<text>( os, "\n" ) );
}
};
struct count : action
{
int n;
count( std::ostream & os ) : action( os ), n( 0 ) {}
count & operator()( test ) { ++n; return *this; }
~count()
{
os << n << " selected " << pluralise("test", n) << "\n";
}
};
#if lest_FEATURE_TIME
#if lest_PLATFORM_IS_WINDOWS
# if ! lest_CPP11_OR_GREATER && ! lest_COMPILER_MSVC_VERSION
typedef unsigned long uint64_t;
# elif lest_COMPILER_MSVC_VERSION >= 6 && lest_COMPILER_MSVC_VERSION < 10
typedef /*un*/signed __int64 uint64_t;
# else
using ::uint64_t;
# endif
#else
# if ! lest_CPP11_OR_GREATER
typedef unsigned long long uint64_t;
# endif
#endif
#if lest_PLATFORM_IS_WINDOWS
inline uint64_t current_ticks()
{
static LARGE_INTEGER hz = {{ 0,0 }}, hzo = {{ 0,0 }};
if ( ! hz.QuadPart )
{
QueryPerformanceFrequency( &hz );
QueryPerformanceCounter ( &hzo );
}
LARGE_INTEGER t = {{ 0,0 }}; QueryPerformanceCounter( &t );
return uint64_t( ( ( t.QuadPart - hzo.QuadPart ) * 1000000 ) / hz.QuadPart );
}
#else
inline uint64_t current_ticks()
{
timeval t; gettimeofday( &t, lest_nullptr );
return static_cast<uint64_t>( t.tv_sec ) * 1000000ull + static_cast<uint64_t>( t.tv_usec );
}
#endif
struct timer
{
const uint64_t start_ticks;
timer() : start_ticks( current_ticks() ) {}
double elapsed_seconds() const
{
return static_cast<double>( current_ticks() - start_ticks ) / 1e6;
}
};
struct times : action
{
env output;
options option;
int selected;
int failures;
timer total;
times( std::ostream & os, options option )
: action( os ), output( os, option.pass ), option( option ), selected( 0 ), failures( 0 ), total()
{
os << std::setfill(' ') << std::fixed << std::setprecision( lest_FEATURE_TIME_PRECISION );
}
operator int() { return failures; }
bool abort() { return option.abort && failures > 0; }
times & operator()( test testing )
{
timer t;
try
{
testing.behaviour( output( testing.name ) );
}
catch( message const & )
{
++failures;
}
os << std::setw(5) << ( 1000 * t.elapsed_seconds() ) << " ms: " << testing.name << "\n";
return *this;
}
~times()
{
os << "Elapsed time: " << std::setprecision(1) << total.elapsed_seconds() << " s\n";
}
};
#else
struct times : action { times( std::ostream &, options ) : action( os ) {} };
#endif
struct confirm : action
{
env output;
options option;