18#if !defined(_CRT_SECURE_NO_WARNINGS)
19#define _CRT_SECURE_NO_WARNINGS 1
24#define MUST_UNDEF_WINSOCK
29#ifdef MUST_UNDEF_WINSOCK
31#undef MUST_UNDEF_WINSOCK
54#define DEFAULT_SUITE "DefaultSuite"
58#error Macro TEST is already defined
62#error Macro TEST_FIXTURE is already defined
66#error Macro SUITE is already defined
73#define _MSVC_LANG __cplusplus
92#if _MSVC_LANG < 201703L
93#define TEST_MAIN(ARGC, ARGV) \
94UnitTest::Test *UnitTest::CurrentTest; \
95UnitTest::Reporter *UnitTest::CurrentReporter; \
96double UnitTest::default_tolerance; \
97std::string UnitTest::CurrentSuite; \
100#define TEST_MAIN(ARGC, ARGV) int main (ARGC, ARGV)
120 namespace Suite##Name \
122 inline char const* GetSuiteName () { return #Name ; } \
124 namespace Suite##Name
133 class Test##Name : public UnitTest::Test \
136 Test##Name() : Test(#Name) {} \
138 void RunImpl() override; \
140 UnitTest::Test* Name##_maker() {return new Test##Name; } \
141 UnitTest::TestSuite::Inserter Name##_inserter (GetSuiteName(), #Name, __FILE__, __LINE__,\
143 void Test##Name::RunImpl()
157#define TEST_FIXTURE(Fixture, Name) \
158 class Fixture##Name##Helper : public Fixture, public UnitTest::Test \
161 Fixture##Name##Helper() : Fixture (), Test(#Name) {} \
163 void RunImpl() override; \
165 UnitTest::Test* Name##_maker() {return new Fixture##Name##Helper;} \
166 UnitTest::TestSuite::Inserter Name##_inserter (GetSuiteName(), #Name, \
167 __FILE__, __LINE__, Name##_maker); \
168 void Fixture##Name##Helper::RunImpl()
171#error Macro ABORT is already defined
180#define ABORT(value) \
183 if (UnitTest::Check(value)) \
184 throw UnitTest::test_abort (__FILE__, __LINE__, #value); \
188#error Macro ABORT_EX is already defined
197#define ABORT_EX(value, ...) \
200 if (UnitTest::Check(value)) { \
201 char message[UnitTest::MAX_MESSAGE_SIZE]; \
202 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
203 throw UnitTest::test_abort (__FILE__, __LINE__, message); \
218#define UTPP_TIME_CONSTRAINT(ms) \
219 UnitTest::TimeConstraint unitTest__timeConstraint__(ms, __FILE__, __LINE__)
226#define UTPP_TIME_CONSTRAINT_EXEMPT() no_time_constraint ()
236void ReportFailure(
const std::string& filename,
int line,
const std::string& message);
242 Test (
const std::string& testName);
265 Test& operator =(
Test const&) =
delete;
266 friend class TestSuite;
283 virtual ~Reporter () {};
307 virtual void Clear ();
326class ReporterDeferred :
public Reporter
329 ReporterDeferred () {};
334 void Clear ()
override;
341 TestResult (
const std::string& suite,
const std::string& test);
369 const std::string& test,
370 const std::string& file,
375 const std::string test_name,
380 friend class TestSuite;
383 explicit TestSuite (
const std::string&
name);
386 void Enable (
bool on_off);
392 std::deque <const Inserter*> test_list;
396 bool SetupCurrentTest (
const Inserter* inf);
397 void RunCurrentTest (
const Inserter* inf);
398 void TearDownCurrentTest (
const Inserter* inf);
411 long long GetTime ()
const;
413 static double frequency();
428 std::string filename;
437 int Run (
const std::string& suite,
Reporter& reporter,
int max_time_ms);
440 void Enable (
const std::string& suite,
bool enable =
true);
444 std::deque <TestSuite> suites;
448struct test_abort :
public std::runtime_error
450 test_abort (
const char* file_,
int line_,
const char* msg)
451 : std::runtime_error (msg)
459struct tolerance_not_set :
public std::runtime_error
462 : std::runtime_error (
"UnitTest::default_tolerance not set")
491void ReportFailure (
const std::string& filename,
int line,
const std::string& message);
566 : suite_test_count (0)
567 , suite_failed_count (0)
568 , suite_failures_count (0)
569 , suite_time_msec (0)
570 , total_test_count (0)
571 , total_failed_count (0)
572 , total_failures_count (0)
573 , total_time_msec (0)
692 results.back ().failures.push_back (failure);
716TestSuite::TestSuite (
const std::string& name_)
734 test_list.push_back (inf);
755 std::deque <const Inserter*>::iterator listp = test_list.begin ();
756 max_runtime = maxtime;
757 while (listp != test_list.end ())
760 if (SetupCurrentTest (*listp))
762 RunCurrentTest (*listp);
763 TearDownCurrentTest (*listp);
782bool TestSuite::SetupCurrentTest (
const Inserter* inf)
791 std::stringstream stream;
792 stream <<
" Aborted setup of " << inf->test_name <<
" - " << x.what ();
793 CurrentTest =
new Test (inf->test_name);
794 ReportFailure (x.file, x.line, stream.str ());
798 catch (
const std::exception& e)
800 std::stringstream stream;
801 stream <<
"Unhandled exception: " << e.what ()
802 <<
" while setting up test " << inf->test_name;
810 std::stringstream stream;
811 stream <<
"Setup unhandled exception while setting up test " << inf->test_name;
822void TestSuite::RunCurrentTest (
const Inserter* inf)
824 assert (CurrentTest);
831 catch (UnitTest::test_abort& x)
833 ReportFailure (x.file, x.line, std::string (
"Test aborted: ") + x.what ());
835 catch (
const std::exception& e)
837 std::stringstream stream;
838 stream <<
"Unhandled exception: " << e.what ()
839 <<
" while running test " << inf->test_name;
844 std::stringstream stream;
845 stream <<
"Unhandled exception while running test " << inf->test_name;
852 std::stringstream stream;
853 stream <<
"Global time constraint failed while running test " << inf->test_name
854 <<
" Expected under " << max_runtime
855 <<
"ms but took " << actual_time <<
"ms.";
864void TestSuite::TearDownCurrentTest (
const Inserter* inf)
870 catch (
const std::exception& e)
872 std::stringstream stream;
873 stream <<
"Unhandled exception: " << e.what ()
874 <<
" while tearing down test " << inf->test_name;
879 std::stringstream stream;
880 stream <<
"Unhandled exception tearing down test " << inf->test_name;
912 const std::string& file,
int ln,
Testmaker func)
932 startTime = GetTime ();
939 long long elapsedTime = GetTime () - startTime;
940 double seconds = (double)elapsedTime / frequency ();
941 return int (seconds * 1000.0);
948 long long int elapsedTime = GetTime () - startTime;
949 double seconds = (double)elapsedTime / frequency ();
950 return (
long long int) (seconds * 1000000.0);
954long long Timer::GetTime ()
const
957 LARGE_INTEGER curTime;
958 ::QueryPerformanceCounter (&curTime);
959 return curTime.QuadPart;
961 struct timeval currentTime;
962 gettimeofday (¤tTime, 0);
963 return currentTime.tv_sec * (
long long)frequency () + currentTime.tv_usec;
968double Timer::frequency ()
973 ::QueryPerformanceFrequency (
reinterpret_cast<LARGE_INTEGER*
>(&f));
987 usleep (
static_cast<useconds_t
>(ms * 1000));
1005 , line_number (line)
1018 int t = timer.GetTimeInMs ();
1021 std::stringstream stream;
1022 stream <<
"Time constraint failed. Expected to run test under " << max_ms <<
1023 "ms but took " << t <<
"ms.";
1043 auto p = suites.begin ();
1044 while (p != suites.end () && p->name != suite_name)
1047 if (p == suites.end ())
1049 suites.emplace_back (suite_name);
1050 suites.back ().Add (inf);
1068 for (
auto& s : suites)
1070 if (s.name == suite_name)
1072 s.RunTests (reporter, max_time_ms);
1089 for (
auto& s : suites)
1092 s.RunTests (reporter, max_time_ms);
1119 for (
auto& s : suites)
1121 if (s.name == suite)
1205void ReportFailure(
const std::string& filename,
int line,
const std::string& message)
1209 Failure f = { filename, message, line };
1238 return the_default_reporter;
1241#if (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) \
1242 || (!defined(_MSVC_LANG) && (__cplusplus >= 201703L))
Definition of Check... template functions and CHECK_... macro-definitions.
void Clear() override
Reset all statistics.
Definition utpp.h:706
void TestStart(const Test &test) override
Definition utpp.h:676
std::deque< TestResult > results
Results of all tests.
Definition utpp.h:349
void ReportFailure(const Failure &failure) override
Definition utpp.h:687
void SuiteStart(const TestSuite &suite) override
Definition utpp.h:664
void TestFinish(const Test &test) override
Definition utpp.h:700
Abstract base for all reporters.
Definition utpp.h:280
virtual void Clear()
Reset all statistics.
Definition utpp.h:624
int suites_count
number of suites ran
Definition utpp.h:320
int suite_failures_count
number of failures in suite
Definition utpp.h:312
int total_failures_count
total number of failures
Definition utpp.h:317
int suite_test_count
number of tests in suite
Definition utpp.h:310
virtual void SuiteStart(const TestSuite &suite)
Invoked at the beginning of a test suite.
Definition utpp.h:581
virtual void TestStart(const Test &test)
Invoked at the beginning of a test.
Definition utpp.h:588
bool trace
true if tracing is enabled
Definition utpp.h:321
int suite_failed_count
number of failed tests in suite
Definition utpp.h:311
virtual void TestFinish(const Test &test)
Invoked at the end of a test.
Definition utpp.h:600
int suite_time_msec
total suite running time in milliseconds
Definition utpp.h:313
int total_test_count
total number of tests
Definition utpp.h:315
virtual void ReportFailure(const Failure &failure)
Called when a test has failed.
Definition utpp.h:595
virtual int Summary()
Generate results report.
Definition utpp.h:304
int total_failed_count
total number of failed tests
Definition utpp.h:316
virtual int SuiteFinish(const TestSuite &suite)
Invoked at the end of a test suite.
Definition utpp.h:617
int total_time_msec
total running time in milliseconds
Definition utpp.h:318
void SetTrace(bool on_off)
Controls test tracing feature.
Definition utpp.h:286
A Reporter that sends results directly to an output stream.
Definition reporter_stream.h:21
A singleton object containing all test suites.
Definition utpp.h:434
void Add(const std::string &suite, const TestSuite::Inserter *inf)
Definition utpp.h:1041
void Enable(const std::string &suite, bool enable=true)
Definition utpp.h:1117
int RunAll(Reporter &reporter, int max_time_ms)
Definition utpp.h:1087
static SuitesList & GetSuitesList()
Definition utpp.h:1103
int Run(const std::string &suite, Reporter &reporter, int max_time_ms)
Definition utpp.h:1066
Representation of a test case.
Definition utpp.h:240
int failures
Number of failures in this test.
Definition utpp.h:259
void run()
Definition utpp.h:510
void failure()
Definition utpp.h:523
const std::string & test_name() const
Return test name.
Definition utpp.h:544
int failure_count() const
Return the number of failures in this test.
Definition utpp.h:530
int test_time_ms() const
Return test running time in milliseconds.
Definition utpp.h:537
virtual void RunImpl()
Actual body of test.
Definition utpp.h:255
Test(const std::string &testName)
Constructor.
Definition utpp.h:496
int time
Run time.
Definition utpp.h:260
bool is_time_constraint() const
Return true if test must be run under global time constraints.
Definition utpp.h:558
std::string name
Name of this test.
Definition utpp.h:258
bool time_exempt
true if exempt from time constraints
Definition utpp.h:261
void no_time_constraint()
Flags the test as exempt from global time constraint.
Definition utpp.h:551
Constructor of this objects inserts the test in suite.
Definition utpp.h:366
Inserter(const std::string &suite, const std::string &test, const std::string &file, int line, Testmaker func)
Definition utpp.h:911
std::string name
Suite name.
Definition utpp.h:389
bool IsEnabled() const
Returns true if suite is enabled.
Definition utpp.h:887
void Add(const Inserter *inf)
Definition utpp.h:732
int RunTests(Reporter &reporter, int max_runtime_ms)
Definition utpp.h:747
void Enable(bool on_off)
Enables or disables this suite.
Definition utpp.h:894
~TimeConstraint()
Definition utpp.h:1016
TimeConstraint(int ms, const char *file, int line)
Definition utpp.h:1003
An object that can be interrogated to get elapsed time.
Definition utpp.h:403
void Start()
Record starting time.
Definition utpp.h:930
int GetTimeInMs() const
Return elapsed time in milliseconds since the starting time.
Definition utpp.h:937
long long GetTimeInUs() const
Return elapsed time in microseconds since the starting time.
Definition utpp.h:946
int RunAllTests(Reporter &rpt=GetDefaultReporter(), int max_time_ms=0)
Run all tests from all test suites.
Definition utpp.h:1145
void EnableSuite(const std::string &suite_name)
Enable a test suite.
Definition utpp.h:1190
int RunSuite(const char *suite_name, Reporter &rpt=GetDefaultReporter(), int max_time_ms=0)
Run all tests from one suite.
Definition utpp.h:1163
void DisableSuite(const std::string &suite_name)
Disable a test suite.
Definition utpp.h:1177
Definition of UnitTest::ReporterDbgout class.
Definition of UnitTest::ReporterStream class.
Definition of UnitTest::ReporterXml class.
The failure object records the file name, the line number and a message.
Definition utpp.h:271
int line_number
Line number where the failure has occurred.
Definition utpp.h:274
std::string message
Description of failure.
Definition utpp.h:273
std::string filename
Name of file where a failure has occurred.
Definition utpp.h:272
Test results including all failure messages
Definition utpp.h:339
std::deque< Failure > failures
All failures of a test.
Definition utpp.h:346
std::string suite_name
suite name
Definition utpp.h:343
std::string test_name
test name
Definition utpp.h:344
TestResult()
Default constructor needed container inclusion.
Definition utpp.h:642
int test_time_ms
test running time in milliseconds
Definition utpp.h:345
Exception thrown by ABORT macro.
Definition utpp.h:449
UnitTest::Test *(* Testmaker)()
Function pointer to a function that creates a test object.
Definition utpp.h:353
#define DEFAULT_SUITE
Name of default suite.
Definition utpp.h:54
void ReportFailure(const std::string &filename, int line, const std::string &message)
Main error reporting function.
Definition utpp.h:1205
Reporter * CurrentReporter
Pointer to current reporter object.
Test * CurrentTest
Currently executing test.
std::string CurrentSuite
Name of currently running suite.
void SleepMs(int const ms)
Pause current thread for the specified time.
Definition utpp.h:982
Reporter & GetDefaultReporter()
Return the default reporter object.
Definition utpp.h:1235
const size_t MAX_MESSAGE_SIZE
Maximum size of message buffer for ..._EX macro definitions.
Definition utpp.h:50
const char * GetSuiteName()
Definition utpp.h:1221