1 #include "benchmark/benchmark.h" 2 3 #include <cassert> 4 #include <cmath> 5 #include <cstdint> 6 #include <cstdlib> 7 8 #include <iostream> 9 #include <limits> 10 #include <sstream> 11 #include <string> 12 13 namespace { 14 15 class TestReporter : public benchmark::ConsoleReporter { 16 public: 17 virtual bool ReportContext(const Context& context) { 18 return ConsoleReporter::ReportContext(context); 19 }; 20 21 virtual void ReportRuns(const std::vector<Run>& report) { 22 ++count_; 23 ConsoleReporter::ReportRuns(report); 24 }; 25 26 TestReporter() : count_(0) {} 27 28 virtual ~TestReporter() {} 29 30 size_t GetCount() const { 31 return count_; 32 } 33 34 private: 35 mutable size_t count_; 36 }; 37 38 } // end namespace 39 40 41 static void NoPrefix(benchmark::State& state) { 42 while (state.KeepRunning()) {} 43 } 44 BENCHMARK(NoPrefix); 45 46 static void BM_Foo(benchmark::State& state) { 47 while (state.KeepRunning()) {} 48 } 49 BENCHMARK(BM_Foo); 50 51 52 static void BM_Bar(benchmark::State& state) { 53 while (state.KeepRunning()) {} 54 } 55 BENCHMARK(BM_Bar); 56 57 58 static void BM_FooBar(benchmark::State& state) { 59 while (state.KeepRunning()) {} 60 } 61 BENCHMARK(BM_FooBar); 62 63 64 static void BM_FooBa(benchmark::State& state) { 65 while (state.KeepRunning()) {} 66 } 67 BENCHMARK(BM_FooBa); 68 69 70 71 int main(int argc, char* argv[]) { 72 benchmark::Initialize(&argc, argv); 73 74 TestReporter test_reporter; 75 benchmark::RunSpecifiedBenchmarks(&test_reporter); 76 77 if (argc == 2) { 78 // Make sure we ran all of the tests 79 std::stringstream ss(argv[1]); 80 size_t expected; 81 ss >> expected; 82 83 const size_t count = test_reporter.GetCount(); 84 if (count != expected) { 85 std::cerr << "ERROR: Expected " << expected << " tests to be ran but only " 86 << count << " completed" << std::endl; 87 return -1; 88 } 89 } 90 return 0; 91 } 92