Home | History | Annotate | Download | only in test
      1 # Enable the tests
      2 
      3 find_package(Threads REQUIRED)
      4 include(CheckCXXCompilerFlag)
      5 
      6 # NOTE: Some tests use `<cassert>` to perform the test. Therefore we must
      7 # strip -DNDEBUG from the default CMake flags in DEBUG mode.
      8 string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
      9 if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
     10   add_definitions( -UNDEBUG )
     11   add_definitions(-DTEST_BENCHMARK_LIBRARY_HAS_NO_ASSERTIONS)
     12   # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines.
     13   foreach (flags_var_to_scrub
     14       CMAKE_CXX_FLAGS_RELEASE
     15       CMAKE_CXX_FLAGS_RELWITHDEBINFO
     16       CMAKE_CXX_FLAGS_MINSIZEREL
     17       CMAKE_C_FLAGS_RELEASE
     18       CMAKE_C_FLAGS_RELWITHDEBINFO
     19       CMAKE_C_FLAGS_MINSIZEREL)
     20     string (REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " "
     21       "${flags_var_to_scrub}" "${${flags_var_to_scrub}}")
     22   endforeach()
     23 endif()
     24 
     25 # NOTE: These flags must be added after find_package(Threads REQUIRED) otherwise
     26 # they will break the configuration check.
     27 if (DEFINED BENCHMARK_CXX_LINKER_FLAGS)
     28   list(APPEND CMAKE_EXE_LINKER_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS})
     29 endif()
     30 
     31 add_library(output_test_helper STATIC output_test_helper.cc output_test.h)
     32 
     33 macro(compile_benchmark_test name)
     34   add_executable(${name} "${name}.cc")
     35   target_link_libraries(${name} benchmark ${CMAKE_THREAD_LIBS_INIT})
     36 endmacro(compile_benchmark_test)
     37 
     38 
     39 macro(compile_output_test name)
     40   add_executable(${name} "${name}.cc" output_test.h)
     41   target_link_libraries(${name} output_test_helper benchmark
     42           ${BENCHMARK_CXX_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
     43 endmacro(compile_output_test)
     44 
     45 # Demonstration executable
     46 compile_benchmark_test(benchmark_test)
     47 add_test(benchmark benchmark_test --benchmark_min_time=0.01)
     48 
     49 compile_benchmark_test(filter_test)
     50 macro(add_filter_test name filter expect)
     51   add_test(${name} filter_test --benchmark_min_time=0.01 --benchmark_filter=${filter} ${expect})
     52   add_test(${name}_list_only filter_test --benchmark_list_tests --benchmark_filter=${filter} ${expect})
     53 endmacro(add_filter_test)
     54 
     55 add_filter_test(filter_simple "Foo" 3)
     56 add_filter_test(filter_suffix "BM_.*" 4)
     57 add_filter_test(filter_regex_all ".*" 5)
     58 add_filter_test(filter_regex_blank "" 5)
     59 add_filter_test(filter_regex_none "monkey" 0)
     60 add_filter_test(filter_regex_wildcard ".*Foo.*" 3)
     61 add_filter_test(filter_regex_begin "^BM_.*" 4)
     62 add_filter_test(filter_regex_begin2 "^N" 1)
     63 add_filter_test(filter_regex_end ".*Ba$" 1)
     64 
     65 compile_benchmark_test(options_test)
     66 add_test(options_benchmarks options_test --benchmark_min_time=0.01)
     67 
     68 compile_benchmark_test(basic_test)
     69 add_test(basic_benchmark basic_test --benchmark_min_time=0.01)
     70 
     71 compile_benchmark_test(diagnostics_test)
     72 add_test(diagnostics_test diagnostics_test --benchmark_min_time=0.01)
     73 
     74 compile_benchmark_test(skip_with_error_test)
     75 add_test(skip_with_error_test skip_with_error_test --benchmark_min_time=0.01)
     76 
     77 compile_benchmark_test(donotoptimize_test)
     78 # Some of the issues with DoNotOptimize only occur when optimization is enabled
     79 check_cxx_compiler_flag(-O3 BENCHMARK_HAS_O3_FLAG)
     80 if (BENCHMARK_HAS_O3_FLAG)
     81   set_target_properties(donotoptimize_test PROPERTIES COMPILE_FLAGS "-O3")
     82 endif()
     83 add_test(donotoptimize_test donotoptimize_test --benchmark_min_time=0.01)
     84 
     85 compile_benchmark_test(fixture_test)
     86 add_test(fixture_test fixture_test --benchmark_min_time=0.01)
     87 
     88 compile_benchmark_test(register_benchmark_test)
     89 add_test(register_benchmark_test register_benchmark_test --benchmark_min_time=0.01)
     90 
     91 compile_benchmark_test(map_test)
     92 add_test(map_test map_test --benchmark_min_time=0.01)
     93 
     94 compile_benchmark_test(multiple_ranges_test)
     95 add_test(multiple_ranges_test multiple_ranges_test --benchmark_min_time=0.01)
     96 
     97 compile_output_test(reporter_output_test)
     98 add_test(reporter_output_test reporter_output_test --benchmark_min_time=0.01)
     99 
    100 compile_output_test(templated_fixture_test)
    101 add_test(templated_fixture_test templated_fixture_test --benchmark_min_time=0.01)
    102 
    103 compile_output_test(user_counters_test)
    104 add_test(user_counters_test user_counters_test --benchmark_min_time=0.01)
    105 
    106 compile_output_test(user_counters_tabular_test)
    107 add_test(user_counters_tabular_test user_counters_tabular_test --benchmark_counters_tabular=true --benchmark_min_time=0.01)
    108 
    109 check_cxx_compiler_flag(-std=c++03 BENCHMARK_HAS_CXX03_FLAG)
    110 if (BENCHMARK_HAS_CXX03_FLAG)
    111   compile_benchmark_test(cxx03_test)
    112   set_target_properties(cxx03_test
    113       PROPERTIES
    114       COMPILE_FLAGS "-std=c++03")
    115   # libstdc++ provides different definitions within <map> between dialects. When
    116   # LTO is enabled and -Werror is specified GCC diagnoses this ODR violation
    117   # causing the test to fail to compile. To prevent this we explicitly disable
    118   # the warning.
    119   check_cxx_compiler_flag(-Wno-odr BENCHMARK_HAS_WNO_ODR)
    120   if (BENCHMARK_ENABLE_LTO AND BENCHMARK_HAS_WNO_ODR)
    121     set_target_properties(cxx03_test
    122         PROPERTIES
    123         LINK_FLAGS "-Wno-odr")
    124   endif()
    125   add_test(cxx03 cxx03_test --benchmark_min_time=0.01)
    126 endif()
    127 
    128 # Attempt to work around flaky test failures when running on Appveyor servers.
    129 if (DEFINED ENV{APPVEYOR})
    130   set(COMPLEXITY_MIN_TIME "0.5")
    131 else()
    132   set(COMPLEXITY_MIN_TIME "0.01")
    133 endif()
    134 compile_output_test(complexity_test)
    135 add_test(complexity_benchmark complexity_test --benchmark_min_time=${COMPLEXITY_MIN_TIME})
    136 
    137 ###############################################################################
    138 # GoogleTest Unit Tests
    139 ###############################################################################
    140 
    141 if (BENCHMARK_ENABLE_GTEST_TESTS)
    142   macro(compile_gtest name)
    143     add_executable(${name} "${name}.cc")
    144     if (TARGET googletest)
    145       add_dependencies(${name} googletest)
    146     endif()
    147     target_link_libraries(${name} benchmark
    148         "${GTEST_BOTH_LIBRARIES}" ${CMAKE_THREAD_LIBS_INIT})
    149   endmacro(compile_gtest)
    150 
    151   macro(add_gtest name)
    152     compile_gtest(${name})
    153     add_test(${name} ${name})
    154   endmacro()
    155 
    156   add_gtest(statistics_test)
    157 endif(BENCHMARK_ENABLE_GTEST_TESTS)
    158 
    159 
    160 # Add the coverage command(s)
    161 if(CMAKE_BUILD_TYPE)
    162   string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER)
    163 endif()
    164 if (${CMAKE_BUILD_TYPE_LOWER} MATCHES "coverage")
    165   find_program(GCOV gcov)
    166   find_program(LCOV lcov)
    167   find_program(GENHTML genhtml)
    168   find_program(CTEST ctest)
    169   if (GCOV AND LCOV AND GENHTML AND CTEST AND HAVE_CXX_FLAG_COVERAGE)
    170     add_custom_command(
    171       OUTPUT ${CMAKE_BINARY_DIR}/lcov/index.html
    172       COMMAND ${LCOV} -q -z -d .
    173       COMMAND ${LCOV} -q --no-external -c -b "${CMAKE_SOURCE_DIR}" -d . -o before.lcov -i
    174       COMMAND ${CTEST} --force-new-ctest-process
    175       COMMAND ${LCOV} -q --no-external -c -b "${CMAKE_SOURCE_DIR}" -d . -o after.lcov
    176       COMMAND ${LCOV} -q -a before.lcov -a after.lcov --output-file final.lcov
    177       COMMAND ${LCOV} -q -r final.lcov "'${CMAKE_SOURCE_DIR}/test/*'" -o final.lcov
    178       COMMAND ${GENHTML} final.lcov -o lcov --demangle-cpp --sort -p "${CMAKE_BINARY_DIR}" -t benchmark
    179       DEPENDS filter_test benchmark_test options_test basic_test fixture_test cxx03_test complexity_test
    180       WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
    181       COMMENT "Running LCOV"
    182     )
    183     add_custom_target(coverage
    184       DEPENDS ${CMAKE_BINARY_DIR}/lcov/index.html
    185       COMMENT "LCOV report at lcov/index.html"
    186     )
    187     message(STATUS "Coverage command added")
    188   else()
    189     if (HAVE_CXX_FLAG_COVERAGE)
    190       set(CXX_FLAG_COVERAGE_MESSAGE supported)
    191     else()
    192       set(CXX_FLAG_COVERAGE_MESSAGE unavailable)
    193     endif()
    194     message(WARNING
    195       "Coverage not available:\n"
    196       "  gcov: ${GCOV}\n"
    197       "  lcov: ${LCOV}\n"
    198       "  genhtml: ${GENHTML}\n"
    199       "  ctest: ${CTEST}\n"
    200       "  --coverage flag: ${CXX_FLAG_COVERAGE_MESSAGE}")
    201   endif()
    202 endif()
    203