2016-08-29 03:24:16 +08:00
|
|
|
#ifndef TEST_OUTPUT_TEST_H
|
|
|
|
#define TEST_OUTPUT_TEST_H
|
|
|
|
|
|
|
|
#undef NDEBUG
|
|
|
|
#include <initializer_list>
|
|
|
|
#include <memory>
|
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[]);
|
|
|
|
|
2017-04-28 22:02:27 +08:00
|
|
|
// ========================================================================= //
|
|
|
|
// ------------------------- Results checking ------------------------------ //
|
|
|
|
// ========================================================================= //
|
|
|
|
|
|
|
|
struct ResultsCheckerEntry;
|
|
|
|
typedef std::function< void(ResultsCheckerEntry const&) > ResultsCheckFn;
|
|
|
|
|
|
|
|
// Class to test the results of a benchmark.
|
|
|
|
// It inspects the results by looking at the CSV output of a subscribed
|
|
|
|
// benchmark.
|
|
|
|
struct ResultsCheckerEntry {
|
|
|
|
std::string name;
|
|
|
|
std::map< std::string, std::string > values;
|
|
|
|
ResultsCheckFn check_fn;
|
|
|
|
|
|
|
|
// int NumThreads() const; // TODO
|
|
|
|
// double duration_real_time() const {} // TODO
|
|
|
|
// double duration_cpu_time() const {} // TODO
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
if(it == values.end()) return nullptr;
|
|
|
|
return &it->second;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a result by name, parsed as a specific type.
|
|
|
|
// For counters, use GetCounterAs instead.
|
2017-04-29 03:42:28 +08:00
|
|
|
template< class T > T GetAs(const char* entry_name) const {
|
2017-04-28 22:02:27 +08:00
|
|
|
auto *sv = Get(entry_name);
|
|
|
|
CHECK(sv != nullptr && !sv->empty());
|
|
|
|
std::stringstream ss;
|
|
|
|
ss << *sv;
|
|
|
|
T out;
|
|
|
|
ss >> out;
|
|
|
|
CHECK(!ss.fail());
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-29 03:42:28 +08:00
|
|
|
template< class T > T GetCounterAs(const char* entry_name) const {
|
2017-04-28 22:02:27 +08:00
|
|
|
double dval = GetAs< double >(entry_name);
|
|
|
|
T tval = static_cast< T >(dval);
|
|
|
|
return tval;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#define _CHECK_RESULT_VALUE(entry, getfn, var_type, var_name, relationship, value) \
|
|
|
|
CONCAT(CHECK_, relationship)(entry.getfn< var_type >(var_name), (value)) \
|
|
|
|
<< "\n" << __FILE__ << ":" << __LINE__ << ": " \
|
|
|
|
<< entry.name << ": expected (" << #var_type << ")" \
|
|
|
|
<< var_name << "=" << entry.GetAs< var_type >(var_name) \
|
|
|
|
<< " to be " #relationship " to " << (value);
|
|
|
|
|
|
|
|
#define CHECK_RESULT_VALUE(entry, var_type, var_name, relationship, value) \
|
|
|
|
_CHECK_RESULT_VALUE(entry, GetAs, var_type, var_name, relationship, value)
|
|
|
|
|
|
|
|
#define CHECK_COUNTER_VALUE(entry, var_type, var_name, relationship, value) \
|
|
|
|
_CHECK_RESULT_VALUE(entry, GetCounterAs, var_type, var_name, relationship, value)
|
|
|
|
|
|
|
|
#define CHECK_BENCHMARK_RESULTS(bm_name, checker_function) \
|
|
|
|
size_t CONCAT(dummy, __LINE__) = AddChecker(bm_name, checker_function)
|
|
|
|
|
|
|
|
// Add a function to check the (CSV) results of a benchmark. These
|
|
|
|
// functions will be called only after the output was successfully
|
|
|
|
// checked.
|
|
|
|
size_t AddChecker(const char* bm_name, ResultsCheckFn fn);
|
|
|
|
|
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
|