Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / third-party / benchmark / test / spec_arg_test.cc
blob043db1be47a2e493d3379d934d67282ee703da91
1 #include <algorithm>
2 #include <cassert>
3 #include <cstdint>
4 #include <cstdlib>
5 #include <cstring>
6 #include <iostream>
7 #include <limits>
8 #include <string>
9 #include <vector>
11 #include "benchmark/benchmark.h"
13 // Tests that we can override benchmark-spec value from FLAGS_benchmark_filter
14 // with argument to RunSpecifiedBenchmarks(...).
16 namespace {
18 class TestReporter : public benchmark::ConsoleReporter {
19 public:
20 virtual bool ReportContext(const Context& context) BENCHMARK_OVERRIDE {
21 return ConsoleReporter::ReportContext(context);
24 virtual void ReportRuns(const std::vector<Run>& report) BENCHMARK_OVERRIDE {
25 assert(report.size() == 1);
26 matched_functions.push_back(report[0].run_name.function_name);
27 ConsoleReporter::ReportRuns(report);
30 TestReporter() {}
32 virtual ~TestReporter() {}
34 const std::vector<std::string>& GetMatchedFunctions() const {
35 return matched_functions;
38 private:
39 std::vector<std::string> matched_functions;
42 } // end namespace
44 static void BM_NotChosen(benchmark::State& state) {
45 assert(false && "SHOULD NOT BE CALLED");
46 for (auto _ : state) {
49 BENCHMARK(BM_NotChosen);
51 static void BM_Chosen(benchmark::State& state) {
52 for (auto _ : state) {
55 BENCHMARK(BM_Chosen);
57 int main(int argc, char** argv) {
58 const std::string flag = "BM_NotChosen";
60 // Verify that argv specify --benchmark_filter=BM_NotChosen.
61 bool found = false;
62 for (int i = 0; i < argc; ++i) {
63 if (strcmp("--benchmark_filter=BM_NotChosen", argv[i]) == 0) {
64 found = true;
65 break;
68 assert(found);
70 benchmark::Initialize(&argc, argv);
72 // Check that the current flag value is reported accurately via the
73 // GetBenchmarkFilter() function.
74 if (flag != benchmark::GetBenchmarkFilter()) {
75 std::cerr
76 << "Seeing different value for flags. GetBenchmarkFilter() returns ["
77 << benchmark::GetBenchmarkFilter() << "] expected flag=[" << flag
78 << "]\n";
79 return 1;
81 TestReporter test_reporter;
82 const char* const spec = "BM_Chosen";
83 const size_t returned_count =
84 benchmark::RunSpecifiedBenchmarks(&test_reporter, spec);
85 assert(returned_count == 1);
86 const std::vector<std::string> matched_functions =
87 test_reporter.GetMatchedFunctions();
88 assert(matched_functions.size() == 1);
89 if (strcmp(spec, matched_functions.front().c_str()) != 0) {
90 std::cerr << "Expected benchmark [" << spec << "] to run, but got ["
91 << matched_functions.front() << "]\n";
92 return 2;
94 return 0;