1 #include "output_test.h"
2 #include "../src/check.h" // NOTE: check.h is for internal use only!
3 #include "../src/re.h" // NOTE: re.h is for internal use only
4 #include <memory>
5 #include <map>
6 #include <iostream>
7 #include <sstream>
8
9
10 // ========================================================================= //
11 // ------------------------------ Internals -------------------------------- //
12 // ========================================================================= //
13 namespace internal { namespace {
14
15 using TestCaseList = std::vector<TestCase>;
16
17 // Use a vector because the order elements are added matters during iteration.
18 // std::map/unordered_map don't guarantee that.
19 // For example:
20 // SetSubstitutions({{"%HelloWorld", "Hello"}, {"%Hello", "Hi"}});
21 // Substitute("%HelloWorld") // Always expands to Hello.
22 using SubMap = std::vector<std::pair<std::string, std::string>>;
23
GetTestCaseList(TestCaseID ID)24 TestCaseList& GetTestCaseList(TestCaseID ID) {
25 // Uses function-local statics to ensure initialization occurs
26 // before first use.
27 static TestCaseList lists[TC_NumID];
28 return lists[ID];
29 }
30
GetSubstitutions()31 SubMap& GetSubstitutions() {
32 // Don't use 'dec_re' from header because it may not yet be initialized.
33 static std::string dec_re = "[0-9]*[.]?[0-9]+([eE][-+][0-9]+)?";
34 static SubMap map = {
35 {"%float", "[0-9]*[.]?[0-9]+([eE][-+][0-9]+)?"},
36 {"%int", "[ ]*[0-9]+"},
37 {" %s ", "[ ]+"},
38 {"%time", "[ ]*[0-9]{1,5} ns"},
39 {"%console_report", "[ ]*[0-9]{1,5} ns [ ]*[0-9]{1,5} ns [ ]*[0-9]+"},
40 {"%csv_report", "[0-9]+," + dec_re + "," + dec_re + ",ns,,,,,"}
41 };
42 return map;
43 }
44
PerformSubstitutions(std::string source)45 std::string PerformSubstitutions(std::string source) {
46 SubMap const& subs = GetSubstitutions();
47 using SizeT = std::string::size_type;
48 for (auto const& KV : subs) {
49 SizeT pos;
50 SizeT next_start = 0;
51 while ((pos = source.find(KV.first, next_start)) != std::string::npos) {
52 next_start = pos + KV.second.size();
53 source.replace(pos, KV.first.size(), KV.second);
54 }
55 }
56 return source;
57 }
58
CheckCase(std::stringstream & remaining_output,TestCase const & TC,TestCaseList const & not_checks)59 void CheckCase(std::stringstream& remaining_output, TestCase const& TC,
60 TestCaseList const& not_checks)
61 {
62 std::string first_line;
63 bool on_first = true;
64 std::string line;
65 while (remaining_output.eof() == false) {
66 CHECK(remaining_output.good());
67 std::getline(remaining_output, line);
68 if (on_first) {
69 first_line = line;
70 on_first = false;
71 }
72 for (auto& NC : not_checks) {
73 CHECK(!NC.regex->Match(line))
74 << "Unexpected match for line \"" << line
75 << "\" for MR_Not regex \"" << NC.regex_str << "\""
76 << "\n actual regex string \"" << TC.substituted_regex << "\""
77 << "\n started matching near: " << first_line;
78 }
79 if (TC.regex->Match(line)) return;
80 CHECK(TC.match_rule != MR_Next)
81 << "Expected line \"" << line << "\" to match regex \"" << TC.regex_str << "\""
82 << "\n actual regex string \"" << TC.substituted_regex << "\""
83 << "\n started matching near: " << first_line;
84 }
85 CHECK(remaining_output.eof() == false)
86 << "End of output reached before match for regex \"" << TC.regex_str
87 << "\" was found"
88 << "\n actual regex string \"" << TC.substituted_regex << "\""
89 << "\n started matching near: " << first_line;
90 }
91
92
CheckCases(TestCaseList const & checks,std::stringstream & output)93 void CheckCases(TestCaseList const& checks, std::stringstream& output) {
94 std::vector<TestCase> not_checks;
95 for (size_t i=0; i < checks.size(); ++i) {
96 const auto& TC = checks[i];
97 if (TC.match_rule == MR_Not) {
98 not_checks.push_back(TC);
99 continue;
100 }
101 CheckCase(output, TC, not_checks);
102 not_checks.clear();
103 }
104 }
105
106 class TestReporter : public benchmark::BenchmarkReporter {
107 public:
TestReporter(std::vector<benchmark::BenchmarkReporter * > reps)108 TestReporter(std::vector<benchmark::BenchmarkReporter*> reps)
109 : reporters_(reps) {}
110
ReportContext(const Context & context)111 virtual bool ReportContext(const Context& context) {
112 bool last_ret = false;
113 bool first = true;
114 for (auto rep : reporters_) {
115 bool new_ret = rep->ReportContext(context);
116 CHECK(first || new_ret == last_ret)
117 << "Reports return different values for ReportContext";
118 first = false;
119 last_ret = new_ret;
120 }
121 return last_ret;
122 }
123
ReportRuns(const std::vector<Run> & report)124 void ReportRuns(const std::vector<Run>& report)
125 { for (auto rep : reporters_) rep->ReportRuns(report); }
Finalize()126 void Finalize() { for (auto rep : reporters_) rep->Finalize(); }
127
128 private:
129 std::vector<benchmark::BenchmarkReporter*> reporters_;
130 };
131
132 }} // end namespace internal
133
134 // ========================================================================= //
135 // -------------------------- Public API Definitions------------------------ //
136 // ========================================================================= //
137
TestCase(std::string re,int rule)138 TestCase::TestCase(std::string re, int rule)
139 : regex_str(std::move(re)), match_rule(rule),
140 substituted_regex(internal::PerformSubstitutions(regex_str)),
141 regex(std::make_shared<benchmark::Regex>())
142 {
143 std::string err_str;
144 regex->Init(substituted_regex, &err_str);
145 CHECK(err_str.empty())
146 << "Could not construct regex \"" << substituted_regex << "\""
147 << "\n originally \"" << regex_str << "\""
148 << "\n got error: " << err_str;
149 }
150
AddCases(TestCaseID ID,std::initializer_list<TestCase> il)151 int AddCases(TestCaseID ID, std::initializer_list<TestCase> il) {
152 auto& L = internal::GetTestCaseList(ID);
153 L.insert(L.end(), il);
154 return 0;
155 }
156
SetSubstitutions(std::initializer_list<std::pair<std::string,std::string>> il)157 int SetSubstitutions(std::initializer_list<std::pair<std::string, std::string>> il) {
158 auto& subs = internal::GetSubstitutions();
159 for (auto const& KV : il) {
160 bool exists = false;
161 for (auto& EKV : subs) {
162 if (EKV.first == KV.first) {
163 EKV.second = KV.second;
164 exists = true;
165 break;
166 }
167 }
168 if (!exists) subs.push_back(KV);
169 }
170 return 0;
171 }
172
RunOutputTests(int argc,char * argv[])173 void RunOutputTests(int argc, char* argv[]) {
174 using internal::GetTestCaseList;
175 benchmark::Initialize(&argc, argv);
176 benchmark::ConsoleReporter CR(benchmark::ConsoleReporter::OO_None);
177 benchmark::JSONReporter JR;
178 benchmark::CSVReporter CSVR;
179 struct ReporterTest {
180 const char* name;
181 std::vector<TestCase>& output_cases;
182 std::vector<TestCase>& error_cases;
183 benchmark::BenchmarkReporter& reporter;
184 std::stringstream out_stream;
185 std::stringstream err_stream;
186
187 ReporterTest(const char* n,
188 std::vector<TestCase>& out_tc,
189 std::vector<TestCase>& err_tc,
190 benchmark::BenchmarkReporter& br)
191 : name(n), output_cases(out_tc), error_cases(err_tc), reporter(br) {
192 reporter.SetOutputStream(&out_stream);
193 reporter.SetErrorStream(&err_stream);
194 }
195 } TestCases[] = {
196 {"ConsoleReporter", GetTestCaseList(TC_ConsoleOut),
197 GetTestCaseList(TC_ConsoleErr), CR},
198 {"JSONReporter", GetTestCaseList(TC_JSONOut),
199 GetTestCaseList(TC_JSONErr), JR},
200 {"CSVReporter", GetTestCaseList(TC_CSVOut),
201 GetTestCaseList(TC_CSVErr), CSVR},
202 };
203
204 // Create the test reporter and run the benchmarks.
205 std::cout << "Running benchmarks...\n";
206 internal::TestReporter test_rep({&CR, &JR, &CSVR});
207 benchmark::RunSpecifiedBenchmarks(&test_rep);
208
209 for (auto& rep_test : TestCases) {
210 std::string msg = std::string("\nTesting ") + rep_test.name + " Output\n";
211 std::string banner(msg.size() - 1, '-');
212 std::cout << banner << msg << banner << "\n";
213
214 std::cerr << rep_test.err_stream.str();
215 std::cout << rep_test.out_stream.str();
216
217 internal::CheckCases(rep_test.error_cases,rep_test.err_stream);
218 internal::CheckCases(rep_test.output_cases, rep_test.out_stream);
219
220 std::cout << "\n";
221 }
222 }
223
224
225