1# Enable the tests 2 3set(THREADS_PREFER_PTHREAD_FLAG ON) 4 5find_package(Threads REQUIRED) 6include(CheckCXXCompilerFlag) 7 8# NOTE: Some tests use `<cassert>` to perform the test. Therefore we must 9# strip -DNDEBUG from the default CMake flags in DEBUG mode. 10string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE) 11if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" ) 12 add_definitions( -UNDEBUG ) 13 add_definitions(-DTEST_BENCHMARK_LIBRARY_HAS_NO_ASSERTIONS) 14 # Also remove /D NDEBUG to avoid MSVC warnings about conflicting defines. 15 foreach (flags_var_to_scrub 16 CMAKE_CXX_FLAGS_RELEASE 17 CMAKE_CXX_FLAGS_RELWITHDEBINFO 18 CMAKE_CXX_FLAGS_MINSIZEREL 19 CMAKE_C_FLAGS_RELEASE 20 CMAKE_C_FLAGS_RELWITHDEBINFO 21 CMAKE_C_FLAGS_MINSIZEREL) 22 string (REGEX REPLACE "(^| )[/-]D *NDEBUG($| )" " " 23 "${flags_var_to_scrub}" "${${flags_var_to_scrub}}") 24 endforeach() 25endif() 26 27if (NOT BUILD_SHARED_LIBS) 28 add_definitions(-DBENCHMARK_STATIC_DEFINE) 29endif() 30 31check_cxx_compiler_flag(-O3 BENCHMARK_HAS_O3_FLAG) 32set(BENCHMARK_O3_FLAG "") 33if (BENCHMARK_HAS_O3_FLAG) 34 set(BENCHMARK_O3_FLAG "-O3") 35endif() 36 37# NOTE: These flags must be added after find_package(Threads REQUIRED) otherwise 38# they will break the configuration check. 39if (DEFINED BENCHMARK_CXX_LINKER_FLAGS) 40 list(APPEND CMAKE_EXE_LINKER_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS}) 41endif() 42 43add_library(output_test_helper STATIC output_test_helper.cc output_test.h) 44target_link_libraries(output_test_helper PRIVATE benchmark::benchmark) 45 46macro(compile_benchmark_test name) 47 add_executable(${name} "${name}.cc") 48 target_link_libraries(${name} benchmark::benchmark ${CMAKE_THREAD_LIBS_INIT}) 49 if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "NVHPC") 50 target_compile_options( ${name} PRIVATE --diag_suppress partial_override ) 51 endif() 52endmacro(compile_benchmark_test) 53 54macro(compile_benchmark_test_with_main name) 55 add_executable(${name} "${name}.cc") 56 target_link_libraries(${name} benchmark::benchmark_main) 57endmacro(compile_benchmark_test_with_main) 58 59macro(compile_output_test name) 60 add_executable(${name} "${name}.cc" output_test.h) 61 target_link_libraries(${name} output_test_helper benchmark::benchmark_main 62 ${BENCHMARK_CXX_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT}) 63endmacro(compile_output_test) 64 65# Demonstration executable 66compile_benchmark_test(benchmark_test) 67add_test(NAME benchmark COMMAND benchmark_test --benchmark_min_time=0.01s) 68 69compile_benchmark_test(spec_arg_test) 70add_test(NAME spec_arg COMMAND spec_arg_test --benchmark_filter=BM_NotChosen) 71 72compile_benchmark_test(spec_arg_verbosity_test) 73add_test(NAME spec_arg_verbosity COMMAND spec_arg_verbosity_test --v=42) 74 75compile_benchmark_test(benchmark_setup_teardown_test) 76add_test(NAME benchmark_setup_teardown COMMAND benchmark_setup_teardown_test) 77 78compile_benchmark_test(filter_test) 79macro(add_filter_test name filter expect) 80 add_test(NAME ${name} COMMAND filter_test --benchmark_min_time=0.01s --benchmark_filter=${filter} ${expect}) 81 add_test(NAME ${name}_list_only COMMAND filter_test --benchmark_list_tests --benchmark_filter=${filter} ${expect}) 82endmacro(add_filter_test) 83 84compile_benchmark_test(benchmark_min_time_flag_time_test) 85add_test(NAME min_time_flag_time COMMAND benchmark_min_time_flag_time_test) 86 87compile_benchmark_test(benchmark_min_time_flag_iters_test) 88add_test(NAME min_time_flag_iters COMMAND benchmark_min_time_flag_iters_test) 89 90add_filter_test(filter_simple "Foo" 3) 91add_filter_test(filter_simple_negative "-Foo" 2) 92add_filter_test(filter_suffix "BM_.*" 4) 93add_filter_test(filter_suffix_negative "-BM_.*" 1) 94add_filter_test(filter_regex_all ".*" 5) 95add_filter_test(filter_regex_all_negative "-.*" 0) 96add_filter_test(filter_regex_blank "" 5) 97add_filter_test(filter_regex_blank_negative "-" 0) 98add_filter_test(filter_regex_none "monkey" 0) 99add_filter_test(filter_regex_none_negative "-monkey" 5) 100add_filter_test(filter_regex_wildcard ".*Foo.*" 3) 101add_filter_test(filter_regex_wildcard_negative "-.*Foo.*" 2) 102add_filter_test(filter_regex_begin "^BM_.*" 4) 103add_filter_test(filter_regex_begin_negative "-^BM_.*" 1) 104add_filter_test(filter_regex_begin2 "^N" 1) 105add_filter_test(filter_regex_begin2_negative "-^N" 4) 106add_filter_test(filter_regex_end ".*Ba$" 1) 107add_filter_test(filter_regex_end_negative "-.*Ba$" 4) 108 109compile_benchmark_test(options_test) 110add_test(NAME options_benchmarks COMMAND options_test --benchmark_min_time=0.01s) 111 112compile_benchmark_test(basic_test) 113add_test(NAME basic_benchmark COMMAND basic_test --benchmark_min_time=0.01s) 114 115compile_output_test(repetitions_test) 116add_test(NAME repetitions_benchmark COMMAND repetitions_test --benchmark_min_time=0.01s --benchmark_repetitions=3) 117 118compile_benchmark_test(diagnostics_test) 119add_test(NAME diagnostics_test COMMAND diagnostics_test --benchmark_min_time=0.01s) 120 121compile_benchmark_test(skip_with_error_test) 122add_test(NAME skip_with_error_test COMMAND skip_with_error_test --benchmark_min_time=0.01s) 123 124compile_benchmark_test(donotoptimize_test) 125# Some of the issues with DoNotOptimize only occur when optimization is enabled 126check_cxx_compiler_flag(-O3 BENCHMARK_HAS_O3_FLAG) 127if (BENCHMARK_HAS_O3_FLAG) 128 set_target_properties(donotoptimize_test PROPERTIES COMPILE_FLAGS "-O3") 129endif() 130add_test(NAME donotoptimize_test COMMAND donotoptimize_test --benchmark_min_time=0.01s) 131 132compile_benchmark_test(fixture_test) 133add_test(NAME fixture_test COMMAND fixture_test --benchmark_min_time=0.01s) 134 135compile_benchmark_test(register_benchmark_test) 136add_test(NAME register_benchmark_test COMMAND register_benchmark_test --benchmark_min_time=0.01s) 137 138compile_benchmark_test(map_test) 139add_test(NAME map_test COMMAND map_test --benchmark_min_time=0.01s) 140 141compile_benchmark_test(multiple_ranges_test) 142add_test(NAME multiple_ranges_test COMMAND multiple_ranges_test --benchmark_min_time=0.01s) 143 144compile_benchmark_test(args_product_test) 145add_test(NAME args_product_test COMMAND args_product_test --benchmark_min_time=0.01s) 146 147compile_benchmark_test_with_main(link_main_test) 148add_test(NAME link_main_test COMMAND link_main_test --benchmark_min_time=0.01s) 149 150compile_output_test(reporter_output_test) 151add_test(NAME reporter_output_test COMMAND reporter_output_test --benchmark_min_time=0.01s) 152 153compile_output_test(templated_fixture_test) 154add_test(NAME templated_fixture_test COMMAND templated_fixture_test --benchmark_min_time=0.01s) 155 156compile_output_test(user_counters_test) 157add_test(NAME user_counters_test COMMAND user_counters_test --benchmark_min_time=0.01s) 158 159compile_output_test(perf_counters_test) 160add_test(NAME perf_counters_test COMMAND perf_counters_test --benchmark_min_time=0.01s --benchmark_perf_counters=CYCLES,BRANCHES) 161 162compile_output_test(internal_threading_test) 163add_test(NAME internal_threading_test COMMAND internal_threading_test --benchmark_min_time=0.01s) 164 165compile_output_test(report_aggregates_only_test) 166add_test(NAME report_aggregates_only_test COMMAND report_aggregates_only_test --benchmark_min_time=0.01s) 167 168compile_output_test(display_aggregates_only_test) 169add_test(NAME display_aggregates_only_test COMMAND display_aggregates_only_test --benchmark_min_time=0.01s) 170 171compile_output_test(user_counters_tabular_test) 172add_test(NAME user_counters_tabular_test COMMAND user_counters_tabular_test --benchmark_counters_tabular=true --benchmark_min_time=0.01s) 173 174compile_output_test(user_counters_thousands_test) 175add_test(NAME user_counters_thousands_test COMMAND user_counters_thousands_test --benchmark_min_time=0.01s) 176 177compile_output_test(memory_manager_test) 178add_test(NAME memory_manager_test COMMAND memory_manager_test --benchmark_min_time=0.01s) 179 180# MSVC does not allow to set the language standard to C++98/03. 181if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC") 182 compile_benchmark_test(cxx03_test) 183 set_target_properties(cxx03_test 184 PROPERTIES 185 CXX_STANDARD 98 186 CXX_STANDARD_REQUIRED YES) 187 # libstdc++ provides different definitions within <map> between dialects. When 188 # LTO is enabled and -Werror is specified GCC diagnoses this ODR violation 189 # causing the test to fail to compile. To prevent this we explicitly disable 190 # the warning. 191 check_cxx_compiler_flag(-Wno-odr BENCHMARK_HAS_WNO_ODR) 192 check_cxx_compiler_flag(-Wno-lto-type-mismatch BENCHMARK_HAS_WNO_LTO_TYPE_MISMATCH) 193 # Cannot set_target_properties multiple times here because the warnings will 194 # be overwritten on each call 195 set (DISABLE_LTO_WARNINGS "") 196 if (BENCHMARK_HAS_WNO_ODR) 197 set(DISABLE_LTO_WARNINGS "${DISABLE_LTO_WARNINGS} -Wno-odr") 198 endif() 199 if (BENCHMARK_HAS_WNO_LTO_TYPE_MISMATCH) 200 set(DISABLE_LTO_WARNINGS "${DISABLE_LTO_WARNINGS} -Wno-lto-type-mismatch") 201 endif() 202 set_target_properties(cxx03_test PROPERTIES LINK_FLAGS "${DISABLE_LTO_WARNINGS}") 203 add_test(NAME cxx03 COMMAND cxx03_test --benchmark_min_time=0.01s) 204endif() 205 206# Attempt to work around flaky test failures when running on Appveyor servers. 207if (DEFINED ENV{APPVEYOR}) 208 set(COMPLEXITY_MIN_TIME "0.5s") 209else() 210 set(COMPLEXITY_MIN_TIME "0.01s") 211endif() 212compile_output_test(complexity_test) 213add_test(NAME complexity_benchmark COMMAND complexity_test --benchmark_min_time=${COMPLEXITY_MIN_TIME}) 214 215############################################################################### 216# GoogleTest Unit Tests 217############################################################################### 218 219if (BENCHMARK_ENABLE_GTEST_TESTS) 220 macro(compile_gtest name) 221 add_executable(${name} "${name}.cc") 222 target_link_libraries(${name} benchmark::benchmark 223 gmock_main ${CMAKE_THREAD_LIBS_INIT}) 224 endmacro(compile_gtest) 225 226 macro(add_gtest name) 227 compile_gtest(${name}) 228 add_test(NAME ${name} COMMAND ${name}) 229 endmacro() 230 231 add_gtest(benchmark_gtest) 232 add_gtest(benchmark_name_gtest) 233 add_gtest(benchmark_random_interleaving_gtest) 234 add_gtest(commandlineflags_gtest) 235 add_gtest(statistics_gtest) 236 add_gtest(string_util_gtest) 237 add_gtest(perf_counters_gtest) 238 add_gtest(time_unit_gtest) 239 add_gtest(min_time_parse_gtest) 240endif(BENCHMARK_ENABLE_GTEST_TESTS) 241 242############################################################################### 243# Assembly Unit Tests 244############################################################################### 245 246if (BENCHMARK_ENABLE_ASSEMBLY_TESTS) 247 if (NOT LLVM_FILECHECK_EXE) 248 message(FATAL_ERROR "LLVM FileCheck is required when including this file") 249 endif() 250 include(AssemblyTests.cmake) 251 add_filecheck_test(donotoptimize_assembly_test) 252 add_filecheck_test(state_assembly_test) 253 add_filecheck_test(clobber_memory_assembly_test) 254endif() 255 256 257 258############################################################################### 259# Code Coverage Configuration 260############################################################################### 261 262# Add the coverage command(s) 263if(CMAKE_BUILD_TYPE) 264 string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER) 265endif() 266if (${CMAKE_BUILD_TYPE_LOWER} MATCHES "coverage") 267 find_program(GCOV gcov) 268 find_program(LCOV lcov) 269 find_program(GENHTML genhtml) 270 find_program(CTEST ctest) 271 if (GCOV AND LCOV AND GENHTML AND CTEST AND HAVE_CXX_FLAG_COVERAGE) 272 add_custom_command( 273 OUTPUT ${CMAKE_BINARY_DIR}/lcov/index.html 274 COMMAND ${LCOV} -q -z -d . 275 COMMAND ${LCOV} -q --no-external -c -b "${CMAKE_SOURCE_DIR}" -d . -o before.lcov -i 276 COMMAND ${CTEST} --force-new-ctest-process 277 COMMAND ${LCOV} -q --no-external -c -b "${CMAKE_SOURCE_DIR}" -d . -o after.lcov 278 COMMAND ${LCOV} -q -a before.lcov -a after.lcov --output-file final.lcov 279 COMMAND ${LCOV} -q -r final.lcov "'${CMAKE_SOURCE_DIR}/test/*'" -o final.lcov 280 COMMAND ${GENHTML} final.lcov -o lcov --demangle-cpp --sort -p "${CMAKE_BINARY_DIR}" -t benchmark 281 DEPENDS filter_test benchmark_test options_test basic_test fixture_test cxx03_test complexity_test 282 WORKING_DIRECTORY ${CMAKE_BINARY_DIR} 283 COMMENT "Running LCOV" 284 ) 285 add_custom_target(coverage 286 DEPENDS ${CMAKE_BINARY_DIR}/lcov/index.html 287 COMMENT "LCOV report at lcov/index.html" 288 ) 289 message(STATUS "Coverage command added") 290 else() 291 if (HAVE_CXX_FLAG_COVERAGE) 292 set(CXX_FLAG_COVERAGE_MESSAGE supported) 293 else() 294 set(CXX_FLAG_COVERAGE_MESSAGE unavailable) 295 endif() 296 message(WARNING 297 "Coverage not available:\n" 298 " gcov: ${GCOV}\n" 299 " lcov: ${LCOV}\n" 300 " genhtml: ${GENHTML}\n" 301 " ctest: ${CTEST}\n" 302 " --coverage flag: ${CXX_FLAG_COVERAGE_MESSAGE}") 303 endif() 304endif() 305