[docs] Add LICENSE.txt to the root of the mono-repo
[llvm-project.git] / third-party / benchmark / test / filter_test.cc
bloba567de2dd58e42b9634db41f4778b9f05ba98f88
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>
11 #include "benchmark/benchmark.h"
13 namespace {
15 class TestReporter : public benchmark::ConsoleReporter {
16 public:
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 {
22 ++count_;
23 max_family_index_ =
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_; }
36 private:
37 mutable size_t count_;
38 mutable size_t max_family_index_;
41 } // end namespace
43 static void NoPrefix(benchmark::State& state) {
44 for (auto _ : state) {
47 BENCHMARK(NoPrefix);
49 static void BM_Foo(benchmark::State& state) {
50 for (auto _ : state) {
53 BENCHMARK(BM_Foo);
55 static void BM_Bar(benchmark::State& state) {
56 for (auto _ : state) {
59 BENCHMARK(BM_Bar);
61 static void BM_FooBar(benchmark::State& state) {
62 for (auto _ : state) {
65 BENCHMARK(BM_FooBar);
67 static void BM_FooBa(benchmark::State& state) {
68 for (auto _ : state) {
71 BENCHMARK(BM_FooBa);
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") !=
77 std::string::npos;
79 benchmark::Initialize(&argc, argv);
81 TestReporter test_reporter;
82 const size_t returned_count =
83 benchmark::RunSpecifiedBenchmarks(&test_reporter);
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;
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;
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;
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;
117 return 0;