From 3b40f0a7a320b5551259be8ffe556996efb006d6 Mon Sep 17 00:00:00 2001 From: Dominic Hamon Date: Mon, 9 Mar 2015 20:30:14 -0700 Subject: [PATCH] Add filter test, remove re test, and googletest deps --- test/CMakeLists.txt | 38 +++++-------------- test/benchmark_test.cc | 39 +------------------ test/filter_test.cc | 86 ++++++++++++++++++++++++++++++++++++++++++ test/re_test.cc | 79 -------------------------------------- 4 files changed, 98 insertions(+), 144 deletions(-) create mode 100644 test/filter_test.cc delete mode 100644 test/re_test.cc diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 392dfb59..70ecbf99 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,40 +1,22 @@ # Enable the tests -# Import and build Google Test -include(ExternalProject) -set_directory_properties(properties EP_PREFIX "${CMAKE_BINARY_DIR}/third_party") -ExternalProject_Add(googletest - URL "https://googletest.googlecode.com/files/gtest-1.7.0.zip" - URL_MD5 2d6ec8ccdf5c46b05ba54a9fd1d130d7 - SOURCE_DIR "${CMAKE_BINARY_DIR}/third_party/gtest" - CMAKE_ARGS "-DCMAKE_CXX_COMPILER=${CMAKE_CXX_COMPILER}" - INSTALL_COMMAND "") - -ExternalProject_Get_Property(googletest source_dir) -ExternalProject_Get_Property(googletest binary_dir) -include_directories(${source_dir}/include) -link_directories(${binary_dir}) - find_package(Threads REQUIRED) macro(compile_benchmark_test name) add_executable(${name} "${name}.cc") - target_link_libraries(${name} benchmark gtest ${CMAKE_THREAD_LIBS_INIT}) - add_dependencies(${name} googletest) + target_link_libraries(${name} benchmark ${CMAKE_THREAD_LIBS_INIT}) endmacro(compile_benchmark_test) -macro(add_gtest_test name) - add_executable(${name} "${name}.cc") - target_link_libraries(${name} benchmark gtest gtest_main ${CMAKE_THREAD_LIBS_INIT}) - add_dependencies(${name} googletest) - add_test(${name} ${name}) -endmacro(add_gtest_test) - # Demonstration executable compile_benchmark_test(benchmark_test) add_test(benchmark benchmark_test --benchmark_min_time=0.1 51) -add_test(benchmark_filter_simple benchmark_test --benchmark_filter=Calculate 16) -add_test(benchmark_filter_suffix benchmark_test --benchmark_filter=Calculate* 16) -add_test(benchmark_filter_regex_wildcard benchmark_test --benchmark_filter=.*Calculate.* 16) -add_gtest_test(re_test) +compile_benchmark_test(filter_test) +add_test(filter_simple filter_test --benchmark_filter=Calculate 16) +add_test(filter_suffix filter_test --benchmark_filter=Calculate* 16) +add_test(filter_regex_all filter_test --benchmark_filter=.* 16) +add_test(filter_regex_blank filter_test --benchmark_filter= 16) +add_test(filter_regex_none filter_test --benchmark_filter=monkey 0) +add_test(filter_regex_wildcard filter_test --benchmark_filter=.*Calculate.* 16) +add_test(filter_regex_begin filter_test --benchmark_filter=^BM_Calculate.* 16) +add_test(filter_regex_end filter_test --benchmark_filter=.*Pi$ 8) diff --git a/test/benchmark_test.cc b/test/benchmark_test.cc index 0a3e48e6..ec49fcde 100644 --- a/test/benchmark_test.cc +++ b/test/benchmark_test.cc @@ -14,8 +14,6 @@ #include #include -#include - #if defined(__GNUC__) # define BENCHMARK_NOINLINE __attribute__((noinline)) #else @@ -55,7 +53,7 @@ static void BM_Factorial(benchmark::State& state) { while (state.KeepRunning()) fac_42 = Factorial(8); // Prevent compiler optimizations - EXPECT_NE(fac_42, std::numeric_limits::max()); + std::cout << fac_42; } BENCHMARK(BM_Factorial); @@ -149,45 +147,12 @@ 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& report) const { - ++count_; - ConsoleReporter::ReportRuns(report); - }; - - TestReporter() : count_(0) {} - - virtual ~TestReporter() {} - - size_t GetCount() const { - return count_; - } - - private: - mutable size_t count_; -}; - int main(int argc, const char* argv[]) { benchmark::Initialize(&argc, argv); assert(Factorial(8) == 40320); assert(CalculatePi(1) == 0.0); - 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; - } + benchmark::RunSpecifiedBenchmarks(); } diff --git a/test/filter_test.cc b/test/filter_test.cc new file mode 100644 index 00000000..00c29553 --- /dev/null +++ b/test/filter_test.cc @@ -0,0 +1,86 @@ +#include "benchmark/benchmark.h" + +#include +#include +#include + +#include +#include +#include + +namespace { + +double CalculatePi(int depth) { + double pi = 0.0; + for (int i = 0; i < depth; ++i) { + double numerator = static_cast(((i % 2) * 2) - 1); + double denominator = static_cast((2 * i) - 1); + pi += numerator / denominator; + } + return (pi - 1.0) * 4; +} + +class TestReporter : public benchmark::internal::ConsoleReporter { + public: + virtual bool ReportContext(const Context& context) const { + return ConsoleReporter::ReportContext(context); + }; + + virtual void ReportRuns(const std::vector& report) const { + ++count_; + ConsoleReporter::ReportRuns(report); + }; + + TestReporter() : count_(0) {} + + virtual ~TestReporter() {} + + size_t GetCount() const { + return count_; + } + + private: + mutable size_t count_; +}; + +} // end namespace + +static void BM_CalculatePiRange(benchmark::State& state) { + double pi = 0.0; + while (state.KeepRunning()) + pi = CalculatePi(state.range_x()); + std::stringstream ss; + ss << pi; + state.SetLabel(ss.str()); +} +BENCHMARK_RANGE(BM_CalculatePiRange, 1, 1024 * 1024); + +static void BM_CalculatePi(benchmark::State& state) { + static const int depth = 1024; + double pi BENCHMARK_UNUSED = 0.0; + while (state.KeepRunning()) { + pi = CalculatePi(depth); + } +} +BENCHMARK(BM_CalculatePi)->Threads(8); +BENCHMARK(BM_CalculatePi)->ThreadRange(1, 32); +BENCHMARK(BM_CalculatePi)->ThreadPerCpu(); + +int main(int argc, const char* argv[]) { + benchmark::Initialize(&argc, argv); + + assert(CalculatePi(1) == 0.0); + + 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; + } +} + diff --git a/test/re_test.cc b/test/re_test.cc deleted file mode 100644 index 1bba661e..00000000 --- a/test/re_test.cc +++ /dev/null @@ -1,79 +0,0 @@ -// Copyright 2015 Google Inc. All rights reserved. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include -#include "../src/re.h" - -TEST(Regex, RegexSimple) { - benchmark::Regex re; - EXPECT_TRUE(re.Init("a+", NULL)); - - EXPECT_FALSE(re.Match("")); - EXPECT_TRUE(re.Match("a")); - EXPECT_TRUE(re.Match("aa")); - EXPECT_TRUE(re.Match("baa")); - EXPECT_FALSE(re.Match("b")); -} - -TEST(Regex, RegexWildcard) { - benchmark::Regex re; - EXPECT_TRUE(re.Init("^a*$", NULL)); - - EXPECT_TRUE(re.Match("")); - EXPECT_TRUE(re.Match("a")); - EXPECT_TRUE(re.Match("aa")); - EXPECT_FALSE(re.Match("baa")); - EXPECT_FALSE(re.Match("b")); -} - -TEST(Regex, RegexAny) { - benchmark::Regex re; - EXPECT_TRUE(re.Init(".", NULL)); - - EXPECT_FALSE(re.Match("")); - EXPECT_TRUE(re.Match("a")); - EXPECT_TRUE(re.Match("aa")); -} - -TEST(Regex, RegexExact) { - benchmark::Regex re; - EXPECT_TRUE(re.Init("^.$", NULL)); - - EXPECT_FALSE(re.Match("")); - EXPECT_TRUE(re.Match("a")); - EXPECT_FALSE(re.Match("aa")); -} - -TEST(Regex, RegexComplicated) { - benchmark::Regex re; - EXPECT_TRUE(re.Init("([0-9]+ )?(mon|low)key(s)?", NULL)); - - EXPECT_TRUE(re.Match("something monkey hands")); - EXPECT_TRUE(re.Match("1 lowkey")); - EXPECT_TRUE(re.Match("19 monkeys")); - EXPECT_FALSE(re.Match("09 a")); -} - -TEST(Regex, InvalidNoErrorMessage) { - benchmark::Regex re; - EXPECT_FALSE(re.Init("[", NULL)); -} - -TEST(Regex, Invalid) { - std::string error; - benchmark::Regex re; - EXPECT_FALSE(re.Init("[", &error)); - - EXPECT_NE("", error); -}