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
48#define UTPP_VERSION "2.2.0"
59#define DEFAULT_SUITE "DefaultSuite"
63#error Macro TEST is already defined
67#error Macro TEST_FIXTURE is already defined
71#error Macro SUITE is already defined
78#define _MSVC_LANG __cplusplus
97#if _MSVC_LANG < 201703L
98#define TEST_MAIN(ARGC, ARGV) \
99UnitTest::Test *UnitTest::CurrentTest; \
100UnitTest::Reporter *UnitTest::CurrentReporter; \
101double UnitTest::default_tolerance; \
102std::string UnitTest::CurrentSuite; \
105#define TEST_MAIN(ARGC, ARGV) int main (ARGC, ARGV)
125 namespace Suite##Name \
127 inline char const* GetSuiteName () { return #Name ; } \
129 namespace Suite##Name
138 class Test##Name : public UnitTest::Test \
141 Test##Name() : Test(#Name) {} \
143 void RunImpl() override; \
145 UnitTest::Test* Name##_maker() {return new Test##Name; } \
146 UnitTest::TestSuite::Inserter Name##_inserter (GetSuiteName(), #Name, __FILE__, __LINE__,\
148 void Test##Name::RunImpl()
162#define TEST_FIXTURE(Fixture, Name) \
163 class Fixture##Name##Helper : public Fixture, public UnitTest::Test \
166 Fixture##Name##Helper() : Fixture (), Test(#Name) {} \
168 void RunImpl() override; \
170 UnitTest::Test* Name##_maker() {return new Fixture##Name##Helper;} \
171 UnitTest::TestSuite::Inserter Name##_inserter (GetSuiteName(), #Name, \
172 __FILE__, __LINE__, Name##_maker); \
173 void Fixture##Name##Helper::RunImpl()
176#error Macro ABORT is already defined
185#define ABORT(value) \
188 if (UnitTest::Check(value)) \
189 throw UnitTest::test_abort (__FILE__, __LINE__, #value); \
193#error Macro ABORT_EX is already defined
202#define ABORT_EX(value, ...) \
205 if (UnitTest::Check(value)) { \
206 char message[UnitTest::MAX_MESSAGE_SIZE]; \
207 snprintf (message, UnitTest::MAX_MESSAGE_SIZE, __VA_ARGS__); \
208 throw UnitTest::test_abort (__FILE__, __LINE__, message); \
223#define UTPP_TIME_CONSTRAINT(ms) \
224 UnitTest::TimeConstraint unitTest__timeConstraint__(ms, __FILE__, __LINE__)
231#define UTPP_TIME_CONSTRAINT_EXEMPT() no_time_constraint ()
241void ReportFailure(
const std::string& filename,
int line,
const std::string& message);
247 Test (
const std::string& testName);
270 Test& operator =(
Test const&) =
delete;
271 friend class TestSuite;
288 virtual ~Reporter () {};
312 virtual void Clear ();
331class ReporterDeferred :
public Reporter
334 ReporterDeferred () {};
339 void Clear ()
override;
346 TestResult (
const std::string& suite,
const std::string& test);
374 const std::string& test,
375 const std::string& file,
380 const std::string test_name,
385 friend class TestSuite;
388 explicit TestSuite (
const std::string&
name);
391 void Enable (
bool on_off);
397 std::deque <const Inserter*> test_list;
401 bool SetupCurrentTest (
const Inserter* inf);
402 void RunCurrentTest (
const Inserter* inf);
403 void TearDownCurrentTest (
const Inserter* inf);
416 std::chrono::steady_clock::time_point startTime;
431 std::string filename;
440 int Run (
const std::string& suite,
Reporter& reporter,
int max_time_ms);
443 void Enable (
const std::string& suite,
bool enable =
true);
447 std::deque <TestSuite> suites;
451struct test_abort :
public std::runtime_error
453 test_abort (
const char* file_,
int line_,
const char* msg)
454 : std::runtime_error (msg)
462struct tolerance_not_set :
public std::runtime_error
465 : std::runtime_error (
"UnitTest::default_tolerance not set")
494void ReportFailure (
const std::string& filename,
int line,
const std::string& message);
569 : suite_test_count (0)
570 , suite_failed_count (0)
571 , suite_failures_count (0)
572 , suite_time_msec (0)
573 , total_test_count (0)
574 , total_failed_count (0)
575 , total_failures_count (0)
576 , total_time_msec (0)
695 results.back ().failures.push_back (failure);
719TestSuite::TestSuite (
const std::string& name_)
737 test_list.push_back (inf);
758 std::deque <const Inserter*>::iterator listp = test_list.begin ();
759 max_runtime = maxtime;
760 while (listp != test_list.end ())
763 if (SetupCurrentTest (*listp))
765 RunCurrentTest (*listp);
766 TearDownCurrentTest (*listp);
785bool TestSuite::SetupCurrentTest (
const Inserter* inf)
794 std::stringstream stream;
795 stream <<
" Aborted setup of " << inf->test_name <<
" - " << x.what ();
796 CurrentTest =
new Test (inf->test_name);
797 ReportFailure (x.file, x.line, stream.str ());
801 catch (
const std::exception& e)
803 std::stringstream stream;
804 stream <<
"Unhandled exception: " << e.what ()
805 <<
" while setting up test " << inf->test_name;
813 std::stringstream stream;
814 stream <<
"Setup unhandled exception while setting up test " << inf->test_name;
825void TestSuite::RunCurrentTest (
const Inserter* inf)
827 assert (CurrentTest);
834 catch (UnitTest::test_abort& x)
836 ReportFailure (x.file, x.line, std::string (
"Test aborted: ") + x.what ());
838 catch (
const std::exception& e)
840 std::stringstream stream;
841 stream <<
"Unhandled exception: " << e.what ()
842 <<
" while running test " << inf->test_name;
847 std::stringstream stream;
848 stream <<
"Unhandled exception while running test " << inf->test_name;
855 std::stringstream stream;
856 stream <<
"Global time constraint failed while running test " << inf->test_name
857 <<
" Expected under " << max_runtime
858 <<
"ms but took " << actual_time <<
"ms.";
867void TestSuite::TearDownCurrentTest (
const Inserter* inf)
873 catch (
const std::exception& e)
875 std::stringstream stream;
876 stream <<
"Unhandled exception: " << e.what ()
877 <<
" while tearing down test " << inf->test_name;
882 std::stringstream stream;
883 stream <<
"Unhandled exception tearing down test " << inf->test_name;
915 const std::string& file,
int ln,
Testmaker func)
927 : startTime (std::chrono::steady_clock::now())
935 startTime = std::chrono::steady_clock::now ();
942 auto elapsedTime = std::chrono::steady_clock::now () - startTime;
943 return (
int)std::chrono::duration_cast<std::chrono::milliseconds>
944 (elapsedTime).count ();
951 auto elapsedTime = std::chrono::steady_clock::now () - startTime;
952 return std::chrono::duration_cast<std::chrono::microseconds>(elapsedTime).count ();
962 usleep (
static_cast<useconds_t
>(ms * 1000));
993 int t = timer.GetTimeInMs ();
996 std::stringstream stream;
997 stream <<
"Time constraint failed. Expected to run test under " << max_ms <<
998 "ms but took " << t <<
"ms.";
1018 auto p = suites.begin ();
1019 while (p != suites.end () && p->name != suite_name)
1022 if (p == suites.end ())
1024 suites.emplace_back (suite_name);
1025 suites.back ().Add (inf);
1043 for (
auto& s : suites)
1045 if (s.name == suite_name)
1047 s.RunTests (reporter, max_time_ms);
1064 for (
auto& s : suites)
1067 s.RunTests (reporter, max_time_ms);
1094 for (
auto& s : suites)
1096 if (s.name == suite)
1180void ReportFailure(
const std::string& filename,
int line,
const std::string& message)
1184 Failure f = { filename, message, line };
1213 return the_default_reporter;
1216#if (defined(_MSVC_LANG) && _MSVC_LANG >= 201703L) \
1217 || (!defined(_MSVC_LANG) && (__cplusplus >= 201703L))
Definition of Check... template functions and CHECK_... macro-definitions.
void Clear() override
Reset all statistics.
Definition utpp.h:709
void TestStart(const Test &test) override
Definition utpp.h:679
std::deque< TestResult > results
Results of all tests.
Definition utpp.h:354
void ReportFailure(const Failure &failure) override
Definition utpp.h:690
void SuiteStart(const TestSuite &suite) override
Definition utpp.h:667
void TestFinish(const Test &test) override
Definition utpp.h:703
Abstract base for all reporters.
Definition utpp.h:285
virtual void Clear()
Reset all statistics.
Definition utpp.h:627
int suites_count
number of suites ran
Definition utpp.h:325
int suite_failures_count
number of failures in suite
Definition utpp.h:317
int total_failures_count
total number of failures
Definition utpp.h:322
int suite_test_count
number of tests in suite
Definition utpp.h:315
virtual void SuiteStart(const TestSuite &suite)
Invoked at the beginning of a test suite.
Definition utpp.h:584
virtual void TestStart(const Test &test)
Invoked at the beginning of a test.
Definition utpp.h:591
bool trace
true if tracing is enabled
Definition utpp.h:326
int suite_failed_count
number of failed tests in suite
Definition utpp.h:316
virtual void TestFinish(const Test &test)
Invoked at the end of a test.
Definition utpp.h:603
int suite_time_msec
total suite running time in milliseconds
Definition utpp.h:318
int total_test_count
total number of tests
Definition utpp.h:320
virtual void ReportFailure(const Failure &failure)
Called when a test has failed.
Definition utpp.h:598
virtual int Summary()
Generate results report.
Definition utpp.h:309
int total_failed_count
total number of failed tests
Definition utpp.h:321
virtual int SuiteFinish(const TestSuite &suite)
Invoked at the end of a test suite.
Definition utpp.h:620
int total_time_msec
total running time in milliseconds
Definition utpp.h:323
void SetTrace(bool on_off)
Controls test tracing feature.
Definition utpp.h:291
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:437
void Add(const std::string &suite, const TestSuite::Inserter *inf)
Definition utpp.h:1016
void Enable(const std::string &suite, bool enable=true)
Definition utpp.h:1092
int RunAll(Reporter &reporter, int max_time_ms)
Definition utpp.h:1062
static SuitesList & GetSuitesList()
Definition utpp.h:1078
int Run(const std::string &suite, Reporter &reporter, int max_time_ms)
Definition utpp.h:1041
Representation of a test case.
Definition utpp.h:245
int failures
Number of failures in this test.
Definition utpp.h:264
void run()
Definition utpp.h:513
void failure()
Definition utpp.h:526
const std::string & test_name() const
Return test name.
Definition utpp.h:547
int failure_count() const
Return the number of failures in this test.
Definition utpp.h:533
int test_time_ms() const
Return test running time in milliseconds.
Definition utpp.h:540
virtual void RunImpl()
Actual body of test.
Definition utpp.h:260
Test(const std::string &testName)
Constructor.
Definition utpp.h:499
int time
Run time.
Definition utpp.h:265
bool is_time_constraint() const
Return true if test must be run under global time constraints.
Definition utpp.h:561
std::string name
Name of this test.
Definition utpp.h:263
bool time_exempt
true if exempt from time constraints
Definition utpp.h:266
void no_time_constraint()
Flags the test as exempt from global time constraint.
Definition utpp.h:554
Constructor of this objects inserts the test in suite.
Definition utpp.h:371
Inserter(const std::string &suite, const std::string &test, const std::string &file, int line, Testmaker func)
Definition utpp.h:914
std::string name
Suite name.
Definition utpp.h:394
bool IsEnabled() const
Returns true if suite is enabled.
Definition utpp.h:890
void Add(const Inserter *inf)
Definition utpp.h:735
int RunTests(Reporter &reporter, int max_runtime_ms)
Definition utpp.h:750
void Enable(bool on_off)
Enables or disables this suite.
Definition utpp.h:897
~TimeConstraint()
Definition utpp.h:991
TimeConstraint(int ms, const char *file, int line)
Definition utpp.h:978
An object that can be interrogated to get elapsed time.
Definition utpp.h:408
void Start()
Record starting time.
Definition utpp.h:933
int GetTimeInMs() const
Return elapsed time in milliseconds since the starting time.
Definition utpp.h:940
long long GetTimeInUs() const
Return elapsed time in microseconds since the starting time.
Definition utpp.h:949
int RunAllTests(Reporter &rpt=GetDefaultReporter(), int max_time_ms=0)
Run all tests from all test suites.
Definition utpp.h:1120
void EnableSuite(const std::string &suite_name)
Enable a test suite.
Definition utpp.h:1165
int RunSuite(const char *suite_name, Reporter &rpt=GetDefaultReporter(), int max_time_ms=0)
Run all tests from one suite.
Definition utpp.h:1138
void DisableSuite(const std::string &suite_name)
Disable a test suite.
Definition utpp.h:1152
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:276
int line_number
Line number where the failure has occurred.
Definition utpp.h:279
std::string message
Description of failure.
Definition utpp.h:278
std::string filename
Name of file where a failure has occurred.
Definition utpp.h:277
Test results including all failure messages
Definition utpp.h:344
std::deque< Failure > failures
All failures of a test.
Definition utpp.h:351
std::string suite_name
suite name
Definition utpp.h:348
std::string test_name
test name
Definition utpp.h:349
TestResult()
Default constructor needed container inclusion.
Definition utpp.h:645
int test_time_ms
test running time in milliseconds
Definition utpp.h:350
Exception thrown by ABORT macro.
Definition utpp.h:452
UnitTest::Test *(* Testmaker)()
Function pointer to a function that creates a test object.
Definition utpp.h:358
#define DEFAULT_SUITE
Name of default suite.
Definition utpp.h:59
void ReportFailure(const std::string &filename, int line, const std::string &message)
Main error reporting function.
Definition utpp.h:1180
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:957
Reporter & GetDefaultReporter()
Return the default reporter object.
Definition utpp.h:1210
const size_t MAX_MESSAGE_SIZE
Maximum size of message buffer for ..._EX macro definitions.
Definition utpp.h:55
const char * GetSuiteName()
Definition utpp.h:1196