Commit ed1bac84 by Tim Bradgate Committed by Dominic Hamon

Issue 571: Allow support for negative regex filtering (#576)

* Allow support for negative regex filtering This patch allows one to apply a negation to the entire regex filter by appending it with a '-' character, much in the same style as GoogleTest uses. * Address issues in PR * Add unit tests for negative filtering
parent 105ac14b
...@@ -77,7 +77,7 @@ class BenchmarkFamilies { ...@@ -77,7 +77,7 @@ class BenchmarkFamilies {
// Extract the list of benchmark instances that match the specified // Extract the list of benchmark instances that match the specified
// regular expression. // regular expression.
bool FindBenchmarks(const std::string& re, bool FindBenchmarks(std::string re,
std::vector<Benchmark::Instance>* benchmarks, std::vector<Benchmark::Instance>* benchmarks,
std::ostream* Err); std::ostream* Err);
...@@ -107,13 +107,18 @@ void BenchmarkFamilies::ClearBenchmarks() { ...@@ -107,13 +107,18 @@ void BenchmarkFamilies::ClearBenchmarks() {
} }
bool BenchmarkFamilies::FindBenchmarks( bool BenchmarkFamilies::FindBenchmarks(
const std::string& spec, std::vector<Benchmark::Instance>* benchmarks, std::string spec, std::vector<Benchmark::Instance>* benchmarks,
std::ostream* ErrStream) { std::ostream* ErrStream) {
CHECK(ErrStream); CHECK(ErrStream);
auto& Err = *ErrStream; auto& Err = *ErrStream;
// Make regular expression out of command-line flag // Make regular expression out of command-line flag
std::string error_msg; std::string error_msg;
Regex re; Regex re;
bool isNegativeFilter = false;
if(spec[0] == '-') {
spec.replace(0, 1, "");
isNegativeFilter = true;
}
if (!re.Init(spec, &error_msg)) { if (!re.Init(spec, &error_msg)) {
Err << "Could not compile benchmark re: " << error_msg << std::endl; Err << "Could not compile benchmark re: " << error_msg << std::endl;
return false; return false;
...@@ -199,7 +204,8 @@ bool BenchmarkFamilies::FindBenchmarks( ...@@ -199,7 +204,8 @@ bool BenchmarkFamilies::FindBenchmarks(
instance.name += StrFormat("/threads:%d", instance.threads); instance.name += StrFormat("/threads:%d", instance.threads);
} }
if (re.Match(instance.name)) { if ((re.Match(instance.name) && !isNegativeFilter) ||
(!re.Match(instance.name) && isNegativeFilter)) {
instance.last_benchmark_instance = (&args == &family->args_.back()); instance.last_benchmark_instance = (&args == &family->args_.back());
benchmarks->push_back(std::move(instance)); benchmarks->push_back(std::move(instance));
} }
......
...@@ -59,14 +59,23 @@ macro(add_filter_test name filter expect) ...@@ -59,14 +59,23 @@ macro(add_filter_test name filter expect)
endmacro(add_filter_test) endmacro(add_filter_test)
add_filter_test(filter_simple "Foo" 3) add_filter_test(filter_simple "Foo" 3)
add_filter_test(filter_simple_negative "-Foo" 2)
add_filter_test(filter_suffix "BM_.*" 4) add_filter_test(filter_suffix "BM_.*" 4)
add_filter_test(filter_suffix_negative "-BM_.*" 1)
add_filter_test(filter_regex_all ".*" 5) add_filter_test(filter_regex_all ".*" 5)
add_filter_test(filter_regex_all_negative "-.*" 0)
add_filter_test(filter_regex_blank "" 5) add_filter_test(filter_regex_blank "" 5)
add_filter_test(filter_regex_blank_negative "-" 0)
add_filter_test(filter_regex_none "monkey" 0) add_filter_test(filter_regex_none "monkey" 0)
add_filter_test(filter_regex_none_negative "-monkey" 5)
add_filter_test(filter_regex_wildcard ".*Foo.*" 3) add_filter_test(filter_regex_wildcard ".*Foo.*" 3)
add_filter_test(filter_regex_wildcard_negative "-.*Foo.*" 2)
add_filter_test(filter_regex_begin "^BM_.*" 4) add_filter_test(filter_regex_begin "^BM_.*" 4)
add_filter_test(filter_regex_begin_negative "-^BM_.*" 1)
add_filter_test(filter_regex_begin2 "^N" 1) add_filter_test(filter_regex_begin2 "^N" 1)
add_filter_test(filter_regex_begin2_negative "-^N" 4)
add_filter_test(filter_regex_end ".*Ba$" 1) add_filter_test(filter_regex_end ".*Ba$" 1)
add_filter_test(filter_regex_end_negative "-.*Ba$" 4)
compile_benchmark_test(options_test) compile_benchmark_test(options_test)
add_test(options_benchmarks options_test --benchmark_min_time=0.01) add_test(options_benchmarks options_test --benchmark_min_time=0.01)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment