1 #include <algorithm> 2 #include <cassert> 3 #include <cmath> 4 #include <cstdint> 5 #include <cstdlib> 6 #include <iostream> 7 #include <limits> 8 #include <sstream> 9 #include <string> 10 11 #include "benchmark/benchmark.h" 12 13 namespace { 14 15 class TestReporter : public benchmark::ConsoleReporter { 16 public: ReportContext(const Context & context)17 virtual bool ReportContext(const Context& context) BENCHMARK_OVERRIDE { 18 return ConsoleReporter::ReportContext(context); 19 }; 20 ReportRuns(const std::vector<Run> & report)21 virtual void ReportRuns(const std::vector<Run>& report) BENCHMARK_OVERRIDE { 22 ++count_; 23 max_family_index_ = 24 std::max<size_t>(max_family_index_, report[0].family_index); 25 ConsoleReporter::ReportRuns(report); 26 }; 27 TestReporter()28 TestReporter() : count_(0), max_family_index_(0) {} 29 ~TestReporter()30 virtual ~TestReporter() {} 31 GetCount() const32 size_t GetCount() const { return count_; } 33 GetMaxFamilyIndex() const34 size_t GetMaxFamilyIndex() const { return max_family_index_; } 35 36 private: 37 mutable size_t count_; 38 mutable size_t max_family_index_; 39 }; 40 41 } // end namespace 42 NoPrefix(benchmark::State & state)43static void NoPrefix(benchmark::State& state) { 44 for (auto _ : state) { 45 } 46 } 47 BENCHMARK(NoPrefix); 48 BM_Foo(benchmark::State & state)49static void BM_Foo(benchmark::State& state) { 50 for (auto _ : state) { 51 } 52 } 53 BENCHMARK(BM_Foo); 54 BM_Bar(benchmark::State & state)55static void BM_Bar(benchmark::State& state) { 56 for (auto _ : state) { 57 } 58 } 59 BENCHMARK(BM_Bar); 60 BM_FooBar(benchmark::State & state)61static void BM_FooBar(benchmark::State& state) { 62 for (auto _ : state) { 63 } 64 } 65 BENCHMARK(BM_FooBar); 66 BM_FooBa(benchmark::State & state)67static void BM_FooBa(benchmark::State& state) { 68 for (auto _ : state) { 69 } 70 } 71 BENCHMARK(BM_FooBa); 72 main(int argc,char ** argv)73int 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") != 77 std::string::npos; 78 79 benchmark::Initialize(&argc, argv); 80 81 TestReporter test_reporter; 82 const size_t returned_count = 83 benchmark::RunSpecifiedBenchmarks(&test_reporter); 84 85 if (argc == 2) { 86 // Make sure we ran all of the tests 87 std::stringstream ss(argv[1]); 88 size_t expected_return; 89 ss >> expected_return; 90 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; 95 return -1; 96 } 97 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 103 << std::endl; 104 return -1; 105 } 106 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; 113 return -1; 114 } 115 } 116 117 return 0; 118 } 119