• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# Enable the tests
2
3find_package(Threads REQUIRED)
4include(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.
8string(TOUPPER "${CMAKE_BUILD_TYPE}" uppercase_CMAKE_BUILD_TYPE)
9if( 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()
23endif()
24
25check_cxx_compiler_flag(-O3 BENCHMARK_HAS_O3_FLAG)
26set(BENCHMARK_O3_FLAG "")
27if (BENCHMARK_HAS_O3_FLAG)
28  set(BENCHMARK_O3_FLAG "-O3")
29endif()
30
31# NOTE: These flags must be added after find_package(Threads REQUIRED) otherwise
32# they will break the configuration check.
33if (DEFINED BENCHMARK_CXX_LINKER_FLAGS)
34  list(APPEND CMAKE_EXE_LINKER_FLAGS ${BENCHMARK_CXX_LINKER_FLAGS})
35endif()
36
37add_library(output_test_helper STATIC output_test_helper.cc output_test.h)
38
39macro(compile_benchmark_test name)
40  add_executable(${name} "${name}.cc")
41  target_link_libraries(${name} benchmark ${CMAKE_THREAD_LIBS_INIT})
42endmacro(compile_benchmark_test)
43
44macro(compile_benchmark_test_with_main name)
45  add_executable(${name} "${name}.cc")
46  target_link_libraries(${name} benchmark_main)
47endmacro(compile_benchmark_test_with_main)
48
49macro(compile_output_test name)
50  add_executable(${name} "${name}.cc" output_test.h)
51  target_link_libraries(${name} output_test_helper benchmark
52          ${BENCHMARK_CXX_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
53endmacro(compile_output_test)
54
55# Demonstration executable
56compile_benchmark_test(benchmark_test)
57add_test(benchmark benchmark_test --benchmark_min_time=0.01)
58
59compile_benchmark_test(filter_test)
60macro(add_filter_test name filter expect)
61  add_test(${name} filter_test --benchmark_min_time=0.01 --benchmark_filter=${filter} ${expect})
62  add_test(${name}_list_only filter_test --benchmark_list_tests --benchmark_filter=${filter} ${expect})
63endmacro(add_filter_test)
64
65add_filter_test(filter_simple "Foo" 3)
66add_filter_test(filter_simple_negative "-Foo" 2)
67add_filter_test(filter_suffix "BM_.*" 4)
68add_filter_test(filter_suffix_negative "-BM_.*" 1)
69add_filter_test(filter_regex_all ".*" 5)
70add_filter_test(filter_regex_all_negative "-.*" 0)
71add_filter_test(filter_regex_blank "" 5)
72add_filter_test(filter_regex_blank_negative "-" 0)
73add_filter_test(filter_regex_none "monkey" 0)
74add_filter_test(filter_regex_none_negative "-monkey" 5)
75add_filter_test(filter_regex_wildcard ".*Foo.*" 3)
76add_filter_test(filter_regex_wildcard_negative "-.*Foo.*" 2)
77add_filter_test(filter_regex_begin "^BM_.*" 4)
78add_filter_test(filter_regex_begin_negative "-^BM_.*" 1)
79add_filter_test(filter_regex_begin2 "^N" 1)
80add_filter_test(filter_regex_begin2_negative "-^N" 4)
81add_filter_test(filter_regex_end ".*Ba$" 1)
82add_filter_test(filter_regex_end_negative "-.*Ba$" 4)
83
84compile_benchmark_test(options_test)
85add_test(options_benchmarks options_test --benchmark_min_time=0.01)
86
87compile_benchmark_test(basic_test)
88add_test(basic_benchmark basic_test --benchmark_min_time=0.01)
89
90compile_benchmark_test(diagnostics_test)
91add_test(diagnostics_test diagnostics_test --benchmark_min_time=0.01)
92
93compile_benchmark_test(skip_with_error_test)
94add_test(skip_with_error_test skip_with_error_test --benchmark_min_time=0.01)
95
96compile_benchmark_test(donotoptimize_test)
97# Some of the issues with DoNotOptimize only occur when optimization is enabled
98check_cxx_compiler_flag(-O3 BENCHMARK_HAS_O3_FLAG)
99if (BENCHMARK_HAS_O3_FLAG)
100  set_target_properties(donotoptimize_test PROPERTIES COMPILE_FLAGS "-O3")
101endif()
102add_test(donotoptimize_test donotoptimize_test --benchmark_min_time=0.01)
103
104compile_benchmark_test(fixture_test)
105add_test(fixture_test fixture_test --benchmark_min_time=0.01)
106
107compile_benchmark_test(register_benchmark_test)
108add_test(register_benchmark_test register_benchmark_test --benchmark_min_time=0.01)
109
110compile_benchmark_test(map_test)
111add_test(map_test map_test --benchmark_min_time=0.01)
112
113compile_benchmark_test(multiple_ranges_test)
114add_test(multiple_ranges_test multiple_ranges_test --benchmark_min_time=0.01)
115
116compile_benchmark_test_with_main(link_main_test)
117add_test(link_main_test link_main_test --benchmark_min_time=0.01)
118
119compile_output_test(reporter_output_test)
120add_test(reporter_output_test reporter_output_test --benchmark_min_time=0.01)
121
122compile_output_test(templated_fixture_test)
123add_test(templated_fixture_test templated_fixture_test --benchmark_min_time=0.01)
124
125compile_output_test(user_counters_test)
126add_test(user_counters_test user_counters_test --benchmark_min_time=0.01)
127
128compile_output_test(report_aggregates_only_test)
129add_test(report_aggregates_only_test report_aggregates_only_test --benchmark_min_time=0.01)
130
131compile_output_test(display_aggregates_only_test)
132add_test(display_aggregates_only_test display_aggregates_only_test --benchmark_min_time=0.01)
133
134compile_output_test(user_counters_tabular_test)
135add_test(user_counters_tabular_test user_counters_tabular_test --benchmark_counters_tabular=true --benchmark_min_time=0.01)
136
137compile_output_test(user_counters_thousands_test)
138add_test(user_counters_thousands_test user_counters_thousands_test --benchmark_min_time=0.01)
139
140compile_output_test(memory_manager_test)
141add_test(memory_manager_test memory_manager_test --benchmark_min_time=0.01)
142
143check_cxx_compiler_flag(-std=c++03 BENCHMARK_HAS_CXX03_FLAG)
144if (BENCHMARK_HAS_CXX03_FLAG)
145  compile_benchmark_test(cxx03_test)
146  set_target_properties(cxx03_test
147      PROPERTIES
148      COMPILE_FLAGS "-std=c++03")
149  # libstdc++ provides different definitions within <map> between dialects. When
150  # LTO is enabled and -Werror is specified GCC diagnoses this ODR violation
151  # causing the test to fail to compile. To prevent this we explicitly disable
152  # the warning.
153  check_cxx_compiler_flag(-Wno-odr BENCHMARK_HAS_WNO_ODR)
154  if (BENCHMARK_ENABLE_LTO AND BENCHMARK_HAS_WNO_ODR)
155    set_target_properties(cxx03_test
156        PROPERTIES
157        LINK_FLAGS "-Wno-odr")
158  endif()
159  add_test(cxx03 cxx03_test --benchmark_min_time=0.01)
160endif()
161
162# Attempt to work around flaky test failures when running on Appveyor servers.
163if (DEFINED ENV{APPVEYOR})
164  set(COMPLEXITY_MIN_TIME "0.5")
165else()
166  set(COMPLEXITY_MIN_TIME "0.01")
167endif()
168compile_output_test(complexity_test)
169add_test(complexity_benchmark complexity_test --benchmark_min_time=${COMPLEXITY_MIN_TIME})
170
171###############################################################################
172# GoogleTest Unit Tests
173###############################################################################
174
175if (BENCHMARK_ENABLE_GTEST_TESTS)
176  macro(compile_gtest name)
177    add_executable(${name} "${name}.cc")
178    if (TARGET googletest)
179      add_dependencies(${name} googletest)
180    endif()
181    if (GTEST_INCLUDE_DIRS)
182      target_include_directories(${name} PRIVATE ${GTEST_INCLUDE_DIRS})
183    endif()
184    target_link_libraries(${name} benchmark
185        ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
186  endmacro(compile_gtest)
187
188  macro(add_gtest name)
189    compile_gtest(${name})
190    add_test(${name} ${name})
191  endmacro()
192
193  add_gtest(benchmark_gtest)
194  add_gtest(statistics_gtest)
195  add_gtest(string_util_gtest)
196endif(BENCHMARK_ENABLE_GTEST_TESTS)
197
198###############################################################################
199# Assembly Unit Tests
200###############################################################################
201
202if (BENCHMARK_ENABLE_ASSEMBLY_TESTS)
203  if (NOT LLVM_FILECHECK_EXE)
204    message(FATAL_ERROR "LLVM FileCheck is required when including this file")
205  endif()
206  include(AssemblyTests.cmake)
207  add_filecheck_test(donotoptimize_assembly_test)
208  add_filecheck_test(state_assembly_test)
209  add_filecheck_test(clobber_memory_assembly_test)
210endif()
211
212
213
214###############################################################################
215# Code Coverage Configuration
216###############################################################################
217
218# Add the coverage command(s)
219if(CMAKE_BUILD_TYPE)
220  string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER)
221endif()
222if (${CMAKE_BUILD_TYPE_LOWER} MATCHES "coverage")
223  find_program(GCOV gcov)
224  find_program(LCOV lcov)
225  find_program(GENHTML genhtml)
226  find_program(CTEST ctest)
227  if (GCOV AND LCOV AND GENHTML AND CTEST AND HAVE_CXX_FLAG_COVERAGE)
228    add_custom_command(
229      OUTPUT ${CMAKE_BINARY_DIR}/lcov/index.html
230      COMMAND ${LCOV} -q -z -d .
231      COMMAND ${LCOV} -q --no-external -c -b "${CMAKE_SOURCE_DIR}" -d . -o before.lcov -i
232      COMMAND ${CTEST} --force-new-ctest-process
233      COMMAND ${LCOV} -q --no-external -c -b "${CMAKE_SOURCE_DIR}" -d . -o after.lcov
234      COMMAND ${LCOV} -q -a before.lcov -a after.lcov --output-file final.lcov
235      COMMAND ${LCOV} -q -r final.lcov "'${CMAKE_SOURCE_DIR}/test/*'" -o final.lcov
236      COMMAND ${GENHTML} final.lcov -o lcov --demangle-cpp --sort -p "${CMAKE_BINARY_DIR}" -t benchmark
237      DEPENDS filter_test benchmark_test options_test basic_test fixture_test cxx03_test complexity_test
238      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
239      COMMENT "Running LCOV"
240    )
241    add_custom_target(coverage
242      DEPENDS ${CMAKE_BINARY_DIR}/lcov/index.html
243      COMMENT "LCOV report at lcov/index.html"
244    )
245    message(STATUS "Coverage command added")
246  else()
247    if (HAVE_CXX_FLAG_COVERAGE)
248      set(CXX_FLAG_COVERAGE_MESSAGE supported)
249    else()
250      set(CXX_FLAG_COVERAGE_MESSAGE unavailable)
251    endif()
252    message(WARNING
253      "Coverage not available:\n"
254      "  gcov: ${GCOV}\n"
255      "  lcov: ${LCOV}\n"
256      "  genhtml: ${GENHTML}\n"
257      "  ctest: ${CTEST}\n"
258      "  --coverage flag: ${CXX_FLAG_COVERAGE_MESSAGE}")
259  endif()
260endif()
261