CMSDK 2.0.1
Cross-platform C++ base library and SDK for the Psyclone AIOS platform
Loading...
Searching...
No Matches
UnitTestFramework.h
Go to the documentation of this file.
1
79#if !defined(_UNITTESTFRAMEWORK_H_)
80#define _UNITTESTFRAMEWORK_H_
81
82#include "Standard.h"
83#include "Types.h"
84#include "Utils.h"
85
86#include <string>
87#include <vector>
88#include <map>
89
90namespace cmlabs {
91
95typedef bool (*UnitTestFunc)();
96
102namespace unittest {
103
109 void progress(int percent, const char* action);
111 void progressf(int percent, const char* fmt, ...);
112
123 void metric(const char* name, double value, const char* unit = "", bool higherIsBetter = true);
124
126 void detail(const char* fmt, ...);
127
130 void fail(const char* fmt, ...);
131
133 bool verbose();
134
135} // namespace unittest
136
140 std::string name;
141 double value;
142 std::string unit;
144};
145
149 std::string name;
150 std::string description;
151 std::string category;
154
155 // Filled in at run time
156 bool ran;
157 bool passed;
158 uint64 durationUs;
159 std::string failReason;
160 std::string endNote;
161 std::vector<UnitTestMetric> metrics;
162
164 UnitTestRecord() : func(NULL), inDefaultRun(true), ran(false), passed(false), durationUs(0) {}
165};
166
175class UnitTestRunner {
176public:
178 static UnitTestRunner& instance();
179
186 void registerTest(const char* name, UnitTestFunc func,
187 const char* description = "", const char* category = "",
188 bool inDefaultRun = true);
189
191 void setVerbose(bool v);
193 void setCompareFile(const char* path);
195 void setOutputFile(const char* path);
197 void setOutputDir(const char* dir);
199 void setWriteJSON(bool v);
201 void setForkPerTest(bool v);
202
204 bool hasTest(const char* name) const;
206 std::vector<std::string> testNames() const;
208 void listTests() const;
209
212 bool runAll();
215 bool runOne(const char* name);
216
218 void hookProgress(int percent, const char* action);
220 void hookMetric(const char* name, double value, const char* unit, bool higherIsBetter);
222 void hookDetail(const char* text);
224 void hookFail(const char* text);
226 bool hookVerbose() const;
227
228private:
229 UnitTestRunner();
230
232 bool runRecords(std::vector<UnitTestRecord*>& records);
234 bool executeRecord(UnitTestRecord* rec);
236 void beginTest(UnitTestRecord* rec);
238 void finishTest(UnitTestRecord* rec);
240 void clearProgressLine();
242 void printStatusLine(const UnitTestRecord* rec);
244 void printMetricLines(const UnitTestRecord* rec);
246 void writeResults(const std::vector<UnitTestRecord*>& records,
247 uint64 totalUs, int passed, int failed);
249 void loadComparison();
251 bool comparisonValue(const std::string& key, double& out) const;
253 static std::string timestampForFilename(uint64 t);
254
255 std::vector<UnitTestRecord> tests;
256 UnitTestRecord* current;
257
258 bool verboseFlag;
259 bool writeJSONFlag;
260 bool forkPerTest;
261 bool isTTY;
262 int lastProgressLen;
263 std::string progressPath;
265
266 std::string compareFile;
267 std::string outputFile;
268 std::string outputDir;
269
270 bool compareLoaded;
271 std::map<std::string, double> compareMetrics;
272
273 utils::Mutex lock;
274};
275
276} // namespace cmlabs
277
278#endif // _UNITTESTFRAMEWORK_H_
Platform/compiler detection and base portability macros for all of CMSDK.
Core fixed-width scalar typedefs and the PsyType / PsyContext hierarchical identifiers.
Cross-platform utility toolbox for CMSDK: threading, synchronization, shared memory,...
std::vector< std::string > testNames() const
void hookDetail(const char *text)
Hook behind unittest::detail() for the current test.
void setForkPerTest(bool v)
Run each test in its own process (default: on where supported) so crashes/hangs are isolated.
void setWriteJSON(bool v)
Enable/disable writing the perf JSON file at all.
void listTests() const
Print the registered tests (name, category, description) to stdout.
void hookProgress(int percent, const char *action)
Hook behind unittest::progress()/progressf() for the current test.
void setOutputFile(const char *path)
Set an explicit perf JSON output path (overrides the timestamped default).
bool hasTest(const char *name) const
void hookMetric(const char *name, double value, const char *unit, bool higherIsBetter)
Hook behind unittest::metric() for the current test.
static UnitTestRunner & instance()
Access the singleton (created on first use).
void setVerbose(bool v)
Enable/disable verbose diagnostics (unittest::detail output).
bool hookVerbose() const
Hook behind unittest::verbose().
void setCompareFile(const char *path)
Set a previous perf JSON file to compare metrics against.
bool runAll()
Run every test marked inDefaultRun.
void registerTest(const char *name, UnitTestFunc func, const char *description="", const char *category="", bool inDefaultRun=true)
Register a test with the runner.
bool runOne(const char *name)
Run a single named test in isolation.
void setOutputDir(const char *dir)
Set the directory for the default (timestamped + latest) output files.
void hookFail(const char *text)
Hook behind unittest::fail() for the current test.
Recursive mutual-exclusion lock, optionally named for cross-process use.
Definition Utils.h:463
bool(* UnitTestFunc)()
Signature every unit test uses.
API used by the body of a unit test.
void fail(const char *fmt,...)
Set an explanatory reason shown on the FAIL line.
void progressf(int percent, const char *fmt,...)
printf-style variant of progress().
void metric(const char *name, double value, const char *unit="", bool higherIsBetter=true)
Record a performance metric.
void detail(const char *fmt,...)
Verbose-only indented diagnostic line (shown only when verbose=1).
void progress(int percent, const char *action)
Report progress with a short description of the current action.
One recorded performance metric of a test run.
bool higherIsBetter
Delta-direction hint for compare mode.
std::string unit
Unit label (e.g.
double value
Measured value.
std::string name
Metric name (unique within the test).
Registration and result record for a single unit test.
UnitTestFunc func
The test function itself.
UnitTestRecord()
Default: unregistered, not run, in the default suite.
bool passed
Outcome (only meaningful when ran).
std::vector< UnitTestMetric > metrics
Metrics recorded via unittest::metric().
std::string failReason
Reason set by the test via unittest::fail() (printed immediately).
bool inDefaultRun
Included when running the whole suite (test=cmsdk).
std::string description
One-line human description.
std::string name
Canonical name, used by test=<name>.
std::string endNote
Framework outcome note (timed out / crashed / no test function).
std::string category
Grouping label (e.g.
uint64 durationUs
Wall-clock duration in microseconds.
bool ran
True once the runner attempted this test.