79#if !defined(_UNITTESTFRAMEWORK_H_)
80#define _UNITTESTFRAMEWORK_H_
108#if defined(__SANITIZE_ADDRESS__) || defined(__SANITIZE_THREAD__)
109 #define UT_SANITIZED 1
110#elif defined(__has_feature)
111 #if __has_feature(address_sanitizer) || __has_feature(thread_sanitizer)
112 #define UT_SANITIZED 1
116 #define UT_TIME_SCALE 5
118 #define UT_TIME_SCALE 1
140 void progress(
int percent,
const char* action);
142 void progressf(
int percent,
const char* fmt, ...);
154 void metric(
const char* name,
double value,
const char* unit =
"",
bool higherIsBetter =
true);
157 void detail(
const char* fmt, ...);
161 void fail(
const char* fmt, ...);
214class UnitTestRunner {
226 const char* description =
"",
const char* category =
"",
227 bool inDefaultRun =
true);
243 bool hasTest(
const char* name)
const;
245 std::vector<std::string>
testNames()
const;
254 bool runOne(
const char* name);
259 void hookMetric(
const char* name,
double value,
const char* unit,
bool higherIsBetter);
271 bool runRecords(std::vector<UnitTestRecord*>& records);
279 void clearProgressLine();
285 void writeResults(
const std::vector<UnitTestRecord*>& records,
286 uint64 totalUs,
int passed,
int failed);
288 void loadComparison();
290 bool comparisonValue(
const std::string& key,
double& out)
const;
292 static std::string timestampForFilename(uint64 t);
294 std::vector<UnitTestRecord> tests;
302 std::string progressPath;
305 std::string compareFile;
306 std::string outputFile;
307 std::string outputDir;
310 std::map<std::string, double> compareMetrics;
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.
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.
std::vector< std::string > detailLog
Ring of the most recent unittest::detail() lines, kept even when NOT verbose so they can be printed i...
bool ran
True once the runner attempted this test.