11 #include "benchmark/benchmark.h"
13 // Tests that we can override benchmark-spec value from FLAGS_benchmark_filter
14 // with argument to RunSpecifiedBenchmarks(...).
18 class TestReporter
: public benchmark::ConsoleReporter
{
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
);
32 virtual ~TestReporter() {}
34 const std::vector
<std::string
>& GetMatchedFunctions() const {
35 return matched_functions
;
39 std::vector
<std::string
> matched_functions
;
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
) {
57 int main(int argc
, char** argv
) {
58 const std::string flag
= "BM_NotChosen";
60 // Verify that argv specify --benchmark_filter=BM_NotChosen.
62 for (int i
= 0; i
< argc
; ++i
) {
63 if (strcmp("--benchmark_filter=BM_NotChosen", argv
[i
]) == 0) {
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()) {
76 << "Seeing different value for flags. GetBenchmarkFilter() returns ["
77 << benchmark::GetBenchmarkFilter() << "] expected flag=[" << flag
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";