2016-08-29 03:24:16 +08:00
|
|
|
#ifndef TEST_OUTPUT_TEST_H
|
|
|
|
#define TEST_OUTPUT_TEST_H
|
|
|
|
|
|
|
|
#undef NDEBUG
|
2018-06-01 18:14:19 +08:00
|
|
|
#include <functional>
|
2016-08-29 03:24:16 +08:00
|
|
|
#include <initializer_list>
|
|
|
|
#include <memory>
|
2018-06-01 18:14:19 +08:00
|
|
|
#include <sstream>
|
2016-10-08 02:04:50 +08:00
|
|
|
#include <string>
|
2016-08-29 03:24:16 +08:00
|
|
|
#include <utility>
|
2016-10-08 02:04:50 +08:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "../src/re.h"
|
|
|
|
#include "benchmark/benchmark.h"
|
2016-08-29 03:24:16 +08:00
|
|
|
|
|
|
|
#define CONCAT2(x, y) x##y
|
|
|
|
#define CONCAT(x, y) CONCAT2(x, y)
|
|
|
|
|
2016-10-08 02:04:50 +08:00
|
|
|
#define ADD_CASES(...) int CONCAT(dummy, __LINE__) = ::AddCases(__VA_ARGS__)
|
2016-08-29 03:24:16 +08:00
|
|
|
|
|
|
|
#define SET_SUBSTITUTIONS(...) \
|
2016-10-08 02:04:50 +08:00
|
|
|
int CONCAT(dummy, __LINE__) = ::SetSubstitutions(__VA_ARGS__)
|
2016-08-29 03:24:16 +08:00
|
|
|
|
|
|
|
enum MatchRules {
|
2016-10-08 02:04:50 +08:00
|
|
|
MR_Default, // Skip non-matching lines until a match is found.
|
|
|
|
MR_Next, // Match must occur on the next line.
|
|
|
|
MR_Not // No line between the current position and the next match matches
|
|
|
|
// the regex
|
2016-08-29 03:24:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct TestCase {
|
|
|
|
TestCase(std::string re, int rule = MR_Default);
|
|
|
|
|
|
|
|
std::string regex_str;
|
|
|
|
int match_rule;
|
|
|
|
std::string substituted_regex;
|
|
|
|
std::shared_ptr<benchmark::Regex> regex;
|
|
|
|
};
|
|
|
|
|
|
|
|
enum TestCaseID {
|
|
|
|
TC_ConsoleOut,
|
|
|
|
TC_ConsoleErr,
|
|
|
|
TC_JSONOut,
|
|
|
|
TC_JSONErr,
|
|
|
|
TC_CSVOut,
|
|
|
|
TC_CSVErr,
|
|
|
|
|
2016-10-08 02:04:50 +08:00
|
|
|
TC_NumID // PRIVATE
|
2016-08-29 03:24:16 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
// Add a list of test cases to be run against the output specified by
|
|
|
|
// 'ID'
|
|
|
|
int AddCases(TestCaseID ID, std::initializer_list<TestCase> il);
|
|
|
|
|
|
|
|
// Add or set a list of substitutions to be performed on constructed regex's
|
|
|
|
// See 'output_test_helper.cc' for a list of default substitutions.
|
|
|
|
int SetSubstitutions(
|
|
|
|
std::initializer_list<std::pair<std::string, std::string>> il);
|
|
|
|
|
|
|
|
// Run all output tests.
|
|
|
|
void RunOutputTests(int argc, char* argv[]);
|
|
|
|
|
2018-09-12 21:26:17 +08:00
|
|
|
// Count the number of 'pat' substrings in the 'haystack' string.
|
|
|
|
int SubstrCnt(const std::string& haystack, const std::string& pat);
|
|
|
|
|
|
|
|
// Run registered benchmarks with file reporter enabled, and return the content
|
|
|
|
// outputted by the file reporter.
|
|
|
|
std::string GetFileReporterOutput(int argc, char* argv[]);
|
|
|
|
|
2017-04-28 22:02:27 +08:00
|
|
|
// ========================================================================= //
|
|
|
|
// ------------------------- Results checking ------------------------------ //
|
|
|
|
// ========================================================================= //
|
|
|
|
|
2017-05-02 05:29:26 +08:00
|
|
|
// Call this macro to register a benchmark for checking its results. This
|
|
|
|
// should be all that's needed. It subscribes a function to check the (CSV)
|
|
|
|
// results of a benchmark. This is done only after verifying that the output
|
|
|
|
// strings are really as expected.
|
|
|
|
// bm_name_pattern: a name or a regex pattern which will be matched against
|
|
|
|
// all the benchmark names. Matching benchmarks
|
|
|
|
// will be the subject of a call to checker_function
|
|
|
|
// checker_function: should be of type ResultsCheckFn (see below)
|
|
|
|
#define CHECK_BENCHMARK_RESULTS(bm_name_pattern, checker_function) \
|
2018-06-01 18:14:19 +08:00
|
|
|
size_t CONCAT(dummy, __LINE__) = AddChecker(bm_name_pattern, checker_function)
|
2017-05-02 05:29:26 +08:00
|
|
|
|
2017-04-30 03:01:18 +08:00
|
|
|
struct Results;
|
2018-06-01 18:14:19 +08:00
|
|
|
typedef std::function<void(Results const&)> ResultsCheckFn;
|
2017-04-28 22:02:27 +08:00
|
|
|
|
2021-12-06 19:18:04 +08:00
|
|
|
size_t AddChecker(const char* bm_name_pattern, const ResultsCheckFn& fn);
|
2017-04-30 03:01:18 +08:00
|
|
|
|
2017-05-02 05:33:19 +08:00
|
|
|
// Class holding the results of a benchmark.
|
2017-04-30 03:01:18 +08:00
|
|
|
// It is passed in calls to checker functions.
|
|
|
|
struct Results {
|
2017-05-02 05:33:19 +08:00
|
|
|
// the benchmark name
|
|
|
|
std::string name;
|
|
|
|
// the benchmark fields
|
2018-06-01 18:14:19 +08:00
|
|
|
std::map<std::string, std::string> values;
|
2017-04-30 02:02:07 +08:00
|
|
|
|
2017-04-30 03:01:18 +08:00
|
|
|
Results(const std::string& n) : name(n) {}
|
2017-04-28 22:02:27 +08:00
|
|
|
|
2017-04-30 02:40:39 +08:00
|
|
|
int NumThreads() const;
|
2017-04-29 03:43:44 +08:00
|
|
|
|
2018-06-27 22:45:30 +08:00
|
|
|
double NumIterations() const;
|
|
|
|
|
2017-05-02 05:32:40 +08:00
|
|
|
typedef enum { kCpuTime, kRealTime } BenchmarkTime;
|
|
|
|
|
|
|
|
// get cpu_time or real_time in seconds
|
|
|
|
double GetTime(BenchmarkTime which) const;
|
|
|
|
|
|
|
|
// get the real_time duration of the benchmark in seconds.
|
|
|
|
// it is better to use fuzzy float checks for this, as the float
|
|
|
|
// ASCII formatting is lossy.
|
2017-04-29 03:43:44 +08:00
|
|
|
double DurationRealTime() const {
|
2018-06-27 22:45:30 +08:00
|
|
|
return NumIterations() * GetTime(kRealTime);
|
2017-04-29 03:43:44 +08:00
|
|
|
}
|
|
|
|
// get the cpu_time duration of the benchmark in seconds
|
2021-11-11 00:22:31 +08:00
|
|
|
double DurationCPUTime() const { return NumIterations() * GetTime(kCpuTime); }
|
2017-04-28 22:02:27 +08:00
|
|
|
|
|
|
|
// get the string for a result by name, or nullptr if the name
|
|
|
|
// is not found
|
2017-04-29 03:42:28 +08:00
|
|
|
const std::string* Get(const char* entry_name) const {
|
2017-04-28 22:02:27 +08:00
|
|
|
auto it = values.find(entry_name);
|
2018-06-01 18:14:19 +08:00
|
|
|
if (it == values.end()) return nullptr;
|
2017-04-28 22:02:27 +08:00
|
|
|
return &it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a result by name, parsed as a specific type.
|
2017-04-30 03:01:18 +08:00
|
|
|
// NOTE: for counters, use GetCounterAs instead.
|
|
|
|
template <class T>
|
|
|
|
T GetAs(const char* entry_name) const;
|
2017-04-28 22:02:27 +08:00
|
|
|
|
|
|
|
// counters are written as doubles, so they have to be read first
|
|
|
|
// as a double, and only then converted to the asked type.
|
2017-04-30 03:01:18 +08:00
|
|
|
template <class T>
|
|
|
|
T GetCounterAs(const char* entry_name) const {
|
2018-06-01 18:14:19 +08:00
|
|
|
double dval = GetAs<double>(entry_name);
|
|
|
|
T tval = static_cast<T>(dval);
|
2017-04-28 22:02:27 +08:00
|
|
|
return tval;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-04-30 03:01:18 +08:00
|
|
|
template <class T>
|
|
|
|
T Results::GetAs(const char* entry_name) const {
|
2018-06-01 18:14:19 +08:00
|
|
|
auto* sv = Get(entry_name);
|
2021-06-25 01:21:59 +08:00
|
|
|
BM_CHECK(sv != nullptr && !sv->empty());
|
2017-04-30 03:01:18 +08:00
|
|
|
std::stringstream ss;
|
|
|
|
ss << *sv;
|
|
|
|
T out;
|
|
|
|
ss >> out;
|
2021-06-25 01:21:59 +08:00
|
|
|
BM_CHECK(!ss.fail());
|
2017-04-30 03:01:18 +08:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
2017-04-30 01:26:30 +08:00
|
|
|
//----------------------------------
|
|
|
|
// Macros to help in result checking. Do not use them with arguments causing
|
|
|
|
// side-effects.
|
|
|
|
|
2018-06-01 18:14:19 +08:00
|
|
|
// clang-format off
|
|
|
|
|
2021-05-07 03:31:14 +08:00
|
|
|
#define CHECK_RESULT_VALUE_IMPL(entry, getfn, var_type, var_name, relationship, value) \
|
2021-06-25 01:21:59 +08:00
|
|
|
CONCAT(BM_CHECK_, relationship) \
|
2017-04-30 01:26:30 +08:00
|
|
|
(entry.getfn< var_type >(var_name), (value)) << "\n" \
|
|
|
|
<< __FILE__ << ":" << __LINE__ << ": " << (entry).name << ":\n" \
|
|
|
|
<< __FILE__ << ":" << __LINE__ << ": " \
|
|
|
|
<< "expected (" << #var_type << ")" << (var_name) \
|
|
|
|
<< "=" << (entry).getfn< var_type >(var_name) \
|
|
|
|
<< " to be " #relationship " to " << (value) << "\n"
|
|
|
|
|
2017-05-02 04:48:13 +08:00
|
|
|
// check with tolerance. eps_factor is the tolerance window, which is
|
|
|
|
// interpreted relative to value (eg, 0.1 means 10% of value).
|
2021-05-07 03:31:14 +08:00
|
|
|
#define CHECK_FLOAT_RESULT_VALUE_IMPL(entry, getfn, var_type, var_name, relationship, value, eps_factor) \
|
2021-06-25 01:21:59 +08:00
|
|
|
CONCAT(BM_CHECK_FLOAT_, relationship) \
|
2017-04-30 01:26:30 +08:00
|
|
|
(entry.getfn< var_type >(var_name), (value), (eps_factor) * (value)) << "\n" \
|
|
|
|
<< __FILE__ << ":" << __LINE__ << ": " << (entry).name << ":\n" \
|
|
|
|
<< __FILE__ << ":" << __LINE__ << ": " \
|
|
|
|
<< "expected (" << #var_type << ")" << (var_name) \
|
|
|
|
<< "=" << (entry).getfn< var_type >(var_name) \
|
|
|
|
<< " to be " #relationship " to " << (value) << "\n" \
|
|
|
|
<< __FILE__ << ":" << __LINE__ << ": " \
|
|
|
|
<< "with tolerance of " << (eps_factor) * (value) \
|
|
|
|
<< " (" << (eps_factor)*100. << "%), " \
|
|
|
|
<< "but delta was " << ((entry).getfn< var_type >(var_name) - (value)) \
|
|
|
|
<< " (" << (((entry).getfn< var_type >(var_name) - (value)) \
|
|
|
|
/ \
|
|
|
|
((value) > 1.e-5 || value < -1.e-5 ? value : 1.e-5)*100.) \
|
|
|
|
<< "%)"
|
|
|
|
|
|
|
|
#define CHECK_RESULT_VALUE(entry, var_type, var_name, relationship, value) \
|
2021-05-07 03:31:14 +08:00
|
|
|
CHECK_RESULT_VALUE_IMPL(entry, GetAs, var_type, var_name, relationship, value)
|
2017-04-30 01:26:30 +08:00
|
|
|
|
|
|
|
#define CHECK_COUNTER_VALUE(entry, var_type, var_name, relationship, value) \
|
2021-05-07 03:31:14 +08:00
|
|
|
CHECK_RESULT_VALUE_IMPL(entry, GetCounterAs, var_type, var_name, relationship, value)
|
2017-04-30 01:26:30 +08:00
|
|
|
|
2017-05-02 04:48:13 +08:00
|
|
|
#define CHECK_FLOAT_RESULT_VALUE(entry, var_name, relationship, value, eps_factor) \
|
2021-05-07 03:31:14 +08:00
|
|
|
CHECK_FLOAT_RESULT_VALUE_IMPL(entry, GetAs, double, var_name, relationship, value, eps_factor)
|
2017-04-30 01:26:30 +08:00
|
|
|
|
2017-05-02 04:48:13 +08:00
|
|
|
#define CHECK_FLOAT_COUNTER_VALUE(entry, var_name, relationship, value, eps_factor) \
|
2021-05-07 03:31:14 +08:00
|
|
|
CHECK_FLOAT_RESULT_VALUE_IMPL(entry, GetCounterAs, double, var_name, relationship, value, eps_factor)
|
2017-04-30 01:26:30 +08:00
|
|
|
|
2018-06-01 18:14:19 +08:00
|
|
|
// clang-format on
|
|
|
|
|
2016-08-29 03:24:16 +08:00
|
|
|
// ========================================================================= //
|
|
|
|
// --------------------------- Misc Utilities ------------------------------ //
|
|
|
|
// ========================================================================= //
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
const char* const dec_re = "[0-9]*[.]?[0-9]+([eE][-+][0-9]+)?";
|
|
|
|
|
2016-10-08 02:04:50 +08:00
|
|
|
} // end namespace
|
2016-08-29 03:24:16 +08:00
|
|
|
|
2016-10-08 02:04:50 +08:00
|
|
|
#endif // TEST_OUTPUT_TEST_H
|