11 #include "benchmark/benchmark.h"
15 class TestReporter
: public benchmark::ConsoleReporter
{
17 virtual bool ReportContext(const Context
& context
) BENCHMARK_OVERRIDE
{
18 return ConsoleReporter::ReportContext(context
);
21 virtual void ReportRuns(const std::vector
<Run
>& report
) BENCHMARK_OVERRIDE
{
24 std::max
<size_t>(max_family_index_
, report
[0].family_index
);
25 ConsoleReporter::ReportRuns(report
);
28 TestReporter() : count_(0), max_family_index_(0) {}
30 virtual ~TestReporter() {}
32 size_t GetCount() const { return count_
; }
34 size_t GetMaxFamilyIndex() const { return max_family_index_
; }
37 mutable size_t count_
;
38 mutable size_t max_family_index_
;
43 static void NoPrefix(benchmark::State
& state
) {
44 for (auto _
: state
) {
49 static void BM_Foo(benchmark::State
& state
) {
50 for (auto _
: state
) {
55 static void BM_Bar(benchmark::State
& state
) {
56 for (auto _
: state
) {
61 static void BM_FooBar(benchmark::State
& state
) {
62 for (auto _
: state
) {
67 static void BM_FooBa(benchmark::State
& state
) {
68 for (auto _
: state
) {
73 int main(int argc
, char** argv
) {
74 bool list_only
= false;
75 for (int i
= 0; i
< argc
; ++i
)
76 list_only
|= std::string(argv
[i
]).find("--benchmark_list_tests") !=
79 benchmark::Initialize(&argc
, argv
);
81 TestReporter test_reporter
;
82 const size_t returned_count
=
83 benchmark::RunSpecifiedBenchmarks(&test_reporter
);
86 // Make sure we ran all of the tests
87 std::stringstream
ss(argv
[1]);
88 size_t expected_return
;
89 ss
>> expected_return
;
91 if (returned_count
!= expected_return
) {
92 std::cerr
<< "ERROR: Expected " << expected_return
93 << " tests to match the filter but returned_count = "
94 << returned_count
<< std::endl
;
98 const size_t expected_reports
= list_only
? 0 : expected_return
;
99 const size_t reports_count
= test_reporter
.GetCount();
100 if (reports_count
!= expected_reports
) {
101 std::cerr
<< "ERROR: Expected " << expected_reports
102 << " tests to be run but reported_count = " << reports_count
107 const size_t max_family_index
= test_reporter
.GetMaxFamilyIndex();
108 const size_t num_families
= reports_count
== 0 ? 0 : 1 + max_family_index
;
109 if (num_families
!= expected_reports
) {
110 std::cerr
<< "ERROR: Expected " << expected_reports
111 << " test families to be run but num_families = "
112 << num_families
<< std::endl
;