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 46 # Demonstration executable 47 compile_benchmark_test(benchmark_test) 48 add_test(benchmark benchmark_test --benchmark_min_time=0.01) 49 50 compile_benchmark_test(filter_test) 51 macro(add_filter_test name filter expect) 52 add_test(${name} filter_test --benchmark_min_time=0.01 --benchmark_filter=${filter} ${expect}) 53 add_test(${name}_list_only filter_test --benchmark_list_tests --benchmark_filter=${filter} ${expect}) 54 endmacro(add_filter_test) 55 56 add_filter_test(filter_simple "Foo" 3) 57 add_filter_test(filter_suffix "BM_.*" 4) 58 add_filter_test(filter_regex_all ".*" 5) 59 add_filter_test(filter_regex_blank "" 5) 60 add_filter_test(filter_regex_none "monkey" 0) 61 add_filter_test(filter_regex_wildcard ".*Foo.*" 3) 62 add_filter_test(filter_regex_begin "^BM_.*" 4) 63 add_filter_test(filter_regex_begin2 "^N" 1) 64 add_filter_test(filter_regex_end ".*Ba$" 1) 65 66 compile_benchmark_test(options_test) 67 add_test(options_benchmarks options_test --benchmark_min_time=0.01) 68 69 compile_benchmark_test(basic_test) 70 add_test(basic_benchmark basic_test --benchmark_min_time=0.01) 71 72 compile_benchmark_test(diagnostics_test) 73 add_test(diagnostics_test diagnostics_test --benchmark_min_time=0.01) 74 75 compile_benchmark_test(skip_with_error_test) 76 add_test(skip_with_error_test skip_with_error_test --benchmark_min_time=0.01) 77 78 compile_benchmark_test(donotoptimize_test) 79 # Some of the issues with DoNotOptimize only occur when optimization is enabled 80 check_cxx_compiler_flag(-O3 BENCHMARK_HAS_O3_FLAG) 81 if (BENCHMARK_HAS_O3_FLAG) 82 set_target_properties(donotoptimize_test PROPERTIES COMPILE_FLAGS "-O3") 83 endif() 84 add_test(donotoptimize_test donotoptimize_test --benchmark_min_time=0.01) 85 86 compile_benchmark_test(fixture_test) 87 add_test(fixture_test fixture_test --benchmark_min_time=0.01) 88 89 compile_benchmark_test(register_benchmark_test) 90 add_test(register_benchmark_test register_benchmark_test --benchmark_min_time=0.01) 91 92 compile_benchmark_test(map_test) 93 add_test(map_test map_test --benchmark_min_time=0.01) 94 95 compile_benchmark_test(multiple_ranges_test) 96 add_test(multiple_ranges_test multiple_ranges_test --benchmark_min_time=0.01) 97 98 compile_output_test(reporter_output_test) 99 add_test(reporter_output_test reporter_output_test --benchmark_min_time=0.01) 100 101 compile_output_test(user_counters_test) 102 add_test(user_counters_test user_counters_test --benchmark_min_time=0.01) 103 104 compile_output_test(user_counters_tabular_test) 105 add_test(user_counters_tabular_test user_counters_tabular_test --benchmark_counters_tabular=true --benchmark_min_time=0.01) 106 107 check_cxx_compiler_flag(-std=c++03 BENCHMARK_HAS_CXX03_FLAG) 108 if (BENCHMARK_HAS_CXX03_FLAG) 109 set(CXX03_FLAGS "${CMAKE_CXX_FLAGS}") 110 string(REPLACE "-std=c++11" "-std=c++03" CXX03_FLAGS "${CXX03_FLAGS}") 111 string(REPLACE "-std=c++0x" "-std=c++03" CXX03_FLAGS "${CXX03_FLAGS}") 112 113 compile_benchmark_test(cxx03_test) 114 set_target_properties(cxx03_test 115 PROPERTIES COMPILE_FLAGS "${CXX03_FLAGS}") 116 add_test(cxx03 cxx03_test --benchmark_min_time=0.01) 117 endif() 118 119 # Attempt to work around flaky test failures when running on Appveyor servers. 120 if (DEFINED ENV{APPVEYOR}) 121 set(COMPLEXITY_MIN_TIME "0.5") 122 else() 123 set(COMPLEXITY_MIN_TIME "0.01") 124 endif() 125 compile_output_test(complexity_test) 126 add_test(complexity_benchmark complexity_test --benchmark_min_time=${COMPLEXITY_MIN_TIME}) 127 128 # Add the coverage command(s) 129 if(CMAKE_BUILD_TYPE) 130 string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER) 131 endif() 132 if (${CMAKE_BUILD_TYPE_LOWER} MATCHES "coverage") 133 find_program(GCOV gcov) 134 find_program(LCOV lcov) 135 find_program(GENHTML genhtml) 136 find_program(CTEST ctest) 137 if (GCOV AND LCOV AND GENHTML AND CTEST AND HAVE_CXX_FLAG_COVERAGE) 138 add_custom_command( 139 OUTPUT ${CMAKE_BINARY_DIR}/lcov/index.html 140 COMMAND ${LCOV} -q -z -d . 141 COMMAND ${LCOV} -q --no-external -c -b "${CMAKE_SOURCE_DIR}" -d . -o before.lcov -i 142 COMMAND ${CTEST} --force-new-ctest-process 143 COMMAND ${LCOV} -q --no-external -c -b "${CMAKE_SOURCE_DIR}" -d . -o after.lcov 144 COMMAND ${LCOV} -q -a before.lcov -a after.lcov --output-file final.lcov 145 COMMAND ${LCOV} -q -r final.lcov "'${CMAKE_SOURCE_DIR}/test/*'" -o final.lcov 146 COMMAND ${GENHTML} final.lcov -o lcov --demangle-cpp --sort -p "${CMAKE_BINARY_DIR}" -t benchmark 147 DEPENDS filter_test benchmark_test options_test basic_test fixture_test cxx03_test complexity_test 148 WORKING_DIRECTORY ${CMAKE_BINARY_DIR} 149 COMMENT "Running LCOV" 150 ) 151 add_custom_target(coverage 152 DEPENDS ${CMAKE_BINARY_DIR}/lcov/index.html 153 COMMENT "LCOV report at lcov/index.html" 154 ) 155 message(STATUS "Coverage command added") 156 else() 157 if (HAVE_CXX_FLAG_COVERAGE) 158 set(CXX_FLAG_COVERAGE_MESSAGE supported) 159 else() 160 set(CXX_FLAG_COVERAGE_MESSAGE unavailable) 161 endif() 162 message(WARNING 163 "Coverage not available:\n" 164 " gcov: ${GCOV}\n" 165 " lcov: ${LCOV}\n" 166 " genhtml: ${GENHTML}\n" 167 " ctest: ${CTEST}\n" 168 " --coverage flag: ${CXX_FLAG_COVERAGE_MESSAGE}") 169 endif() 170 endif() 171