Check the number of benchmark tests ran

Previously the benchmark_test program executed the benchmark tests to make sure
the API was working but was not checking the number of tests that were
completed. If the regular expression matching breaks, zero tests could be ran.
Similarly, if one of the APIs breaks and doesn't run the correct amount of tests
then `make test` will catch this.
This commit is contained in:
Matt Clarkson 2014-08-08 10:22:31 +01:00
parent 92ddf09f1d
commit 1176936966
2 changed files with 41 additions and 2 deletions

View File

@ -1,7 +1,12 @@
# Demonstration executable
add_executable(benchmark_test benchmark_test.cc)
target_link_libraries(benchmark_test benchmark ${CMAKE_THREAD_LIBS_INIT})
add_test(benchmark benchmark_test)
add_test(benchmark benchmark_test 50)
add_test(benchmark_filter_simple benchmark_test --benchmark_filter=Calculate 16)
add_test(benchmark_filter_prefix benchmark_test --benchmark_filter=*Calculate 0)
add_test(benchmark_filter_suffix benchmark_test --benchmark_filter=Calculate* 16)
add_test(benchmark_filter_both benchmark_test --benchmark_filter=*Calculate* 0)
add_test(benchmark_filter_regex_wildcard benchmark_test --benchmark_filter=.*Calculate.* 16)
# Test harness for regex wrapper
add_executable(re_test ${RE_FILES} "re_test.cc")

View File

@ -10,6 +10,7 @@
#include <mutex>
#include <set>
#include <sstream>
#include <string>
#include <vector>
namespace {
@ -141,12 +142,45 @@ static void BM_LongTest(benchmark::State& state) {
}
BENCHMARK(BM_LongTest)->Range(1<<16,1<<28);
class TestReporter : public benchmark::internal::ConsoleReporter {
public:
virtual bool ReportContext(const Context& context) const {
return ConsoleReporter::ReportContext(context);
};
virtual void ReportRuns(const std::vector<Run>& report) const {
++count_;
ConsoleReporter::ReportRuns(report);
};
TestReporter() : count_(0) {}
virtual ~TestReporter() {}
int GetCount() const {
return count_;
}
private:
mutable size_t count_;
};
int main(int argc, const char* argv[]) {
benchmark::Initialize(&argc, argv);
CHECK(Factorial(8) == 40320);
CHECK(CalculatePi(1) == 0.0);
benchmark::RunSpecifiedBenchmarks();
TestReporter test_reporter;
benchmark::RunSpecifiedBenchmarks(&test_reporter);
// Make sure we ran all of the tests
const size_t count = test_reporter.GetCount();
const size_t expected = (argc == 2) ? std::stoul(argv[1]) : count;
if (count != expected) {
std::cerr << "ERROR: Expected " << expected << " tests to be ran but only "
<< count << " completed" << std::endl;
return -1;
}
}