-
Notifications
You must be signed in to change notification settings - Fork 739
Expand file tree
/
Copy pathPcppTestFrameworkRun.h
More file actions
170 lines (149 loc) · 4.97 KB
/
PcppTestFrameworkRun.h
File metadata and controls
170 lines (149 loc) · 4.97 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
#pragma once
#include <algorithm>
#include <vector>
#include <iomanip>
#include <iostream>
#include <string>
#include <sstream>
#include <functional>
#include "memplumber.h"
#include "PcppTestFrameworkCommon.h"
static bool verboseMode = false;
static bool showSkippedTests = false;
static void __ptfSplitString(const std::string& input, std::vector<std::string>& result)
{
std::istringstream ss(input);
std::string token;
while (std::getline(ss, token, ';'))
{
result.push_back(token);
}
}
static bool __ptfCheckTags(const std::string& tagSet, const std::string& tagSetToCompareWith, bool emptyTagSetMeansAll)
{
std::vector<std::string> tagSetVec, tagSetToCompareWithVec;
if (tagSetToCompareWith == "")
{
return emptyTagSetMeansAll;
}
__ptfSplitString(tagSet, tagSetVec);
__ptfSplitString(tagSetToCompareWith, tagSetToCompareWithVec);
for (const auto& tagSetToCompareWithIter : tagSetToCompareWithVec)
{
if (std::any_of(tagSetVec.begin(), tagSetVec.end(),
[tagSetToCompareWithIter](const std::string& val) { return val == tagSetToCompareWithIter; }))
{
return true;
}
}
return false;
}
namespace ptf
{
class TestRunner
{
public:
using TestFunc = std::function<void(int& result, bool verboseMode, bool showSkippedTests)>;
TestRunner(std::string userIncludeTags, std::string userExcludeTags, std::string configTags)
: m_UserIncludeTags(std::move(userIncludeTags)), m_UserExcludeTags(std::move(userExcludeTags)),
m_ConfigTagsToRun(std::move(configTags))
{}
int runTest(TestFunc testFn, std::string const& testName, std::string const& additionalTestTags)
{
std::string allTestTags = testName + ";" + additionalTestTags;
int result = PTF_RESULT_PASSED;
if (!__ptfCheckTags(allTestTags, m_UserIncludeTags, true) ||
__ptfCheckTags(allTestTags, m_UserExcludeTags, false))
{
if (showSkippedTests)
{
std::cout << std::left << std::setw(35) << testName << ": SKIPPED (tags don't match)" << std::endl;
}
result = PTF_RESULT_SKIPPED;
}
else
{
bool runMemLeakCheck = !__ptfCheckTags("skip_mem_leak_check", m_ConfigTagsToRun, false) &&
!__ptfCheckTags(allTestTags, "skip_mem_leak_check", false);
if (runMemLeakCheck)
{
bool memAllocVerbose = __ptfCheckTags("mem_leak_check_verbose", m_ConfigTagsToRun, false);
MemPlumber::start(memAllocVerbose);
}
try
{
testFn(result, verboseMode, showSkippedTests);
}
catch (std::exception const& e)
{
result = PTF_RESULT_FAILED;
std::cout << std::left << std::setw(35) << testName << ": FAILED. Unhandled exception occurred! "
<< "Exception: " << e.what() << std::endl;
}
if (runMemLeakCheck)
{
if (result != PTF_RESULT_PASSED)
{
MemPlumber::stopAndFreeAllMemory();
}
else
{
size_t memLeakCount = 0;
uint64_t memLeakSize = 0;
MemPlumber::memLeakCheck(memLeakCount, memLeakSize, true);
MemPlumber::stopAndFreeAllMemory();
if (memLeakCount > 0 || memLeakSize > 0)
{
result = PTF_RESULT_FAILED;
std::cout << std::left << std::setw(35) << testName << ": FAILED. Memory leak found! "
<< memLeakCount << " objects and " << memLeakSize << "[bytes] leaked"
<< std::endl;
}
}
}
if (result == PTF_RESULT_PASSED)
{
std::cout << std::left << std::setw(35) << testName << ": PASSED" << std::endl;
}
}
if (result == PTF_RESULT_PASSED)
testsPassed++;
if (result == PTF_RESULT_FAILED)
testsFailed++;
if (result == PTF_RESULT_SKIPPED)
testsSkipped++;
return result;
}
int finalizeResults()
{
std::string message = (!hasFailures() ? "ALL TESTS PASSED!!" : "NOT ALL TESTS PASSED!!");
std::cout << std::endl
<< message << std::endl
<< "Test cases: " << testsPassed + testsFailed + testsSkipped << ", "
<< "Passed: " << testsPassed << ", "
<< "Failed: " << testsFailed << ", "
<< "Skipped: " << testsSkipped << std::endl;
return hasFailures();
}
private:
bool hasFailures(bool ignoreSkipped = true) const
{
if (ignoreSkipped)
return testsFailed != 0;
return testsFailed + testsSkipped != 0;
}
std::string m_UserIncludeTags;
std::string m_UserExcludeTags;
std::string m_ConfigTagsToRun;
int testsPassed = 0;
int testsFailed = 0;
int testsSkipped = 0;
};
} // namespace ptf
#define PTF_START_RUNNING_TESTS(userIncludeTags, userExcludeTags, configTags) \
ptf::TestRunner testRunner(userIncludeTags, userExcludeTags, configTags); \
std::cout << "Start running tests..." << std::endl << std::endl
#define PTF_RUN_TEST(TestName, tags) testRunner.runTest(TestName, #TestName, tags)
#define PTF_END_RUNNING_TESTS return testRunner.finalizeResults();
#define PTF_SET_VERBOSE_MODE(flag) verboseMode = flag
#define PTF_SHOW_SKIPPED_TESTS(flag) showSkippedTests = flag