• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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# Enable errors for deprecated deprecations (DoNotOptimize(Tp const& value)).
126check_cxx_compiler_flag(-Werror=deprecated-declarations BENCHMARK_HAS_DEPRECATED_DECLARATIONS_FLAG)
127if (BENCHMARK_HAS_DEPRECATED_DECLARATIONS_FLAG)
128  target_compile_options (donotoptimize_test PRIVATE "-Werror=deprecated-declarations")
129endif()
130# Some of the issues with DoNotOptimize only occur when optimization is enabled
131check_cxx_compiler_flag(-O3 BENCHMARK_HAS_O3_FLAG)
132if (BENCHMARK_HAS_O3_FLAG)
133  set_target_properties(donotoptimize_test PROPERTIES COMPILE_FLAGS "-O3")
134endif()
135add_test(NAME donotoptimize_test COMMAND donotoptimize_test --benchmark_min_time=0.01s)
136
137compile_benchmark_test(fixture_test)
138add_test(NAME fixture_test COMMAND fixture_test --benchmark_min_time=0.01s)
139
140compile_benchmark_test(register_benchmark_test)
141add_test(NAME register_benchmark_test COMMAND register_benchmark_test --benchmark_min_time=0.01s)
142
143compile_benchmark_test(map_test)
144add_test(NAME map_test COMMAND map_test --benchmark_min_time=0.01s)
145
146compile_benchmark_test(multiple_ranges_test)
147add_test(NAME multiple_ranges_test COMMAND multiple_ranges_test --benchmark_min_time=0.01s)
148
149compile_benchmark_test(args_product_test)
150add_test(NAME args_product_test COMMAND args_product_test --benchmark_min_time=0.01s)
151
152compile_benchmark_test_with_main(link_main_test)
153add_test(NAME link_main_test COMMAND link_main_test --benchmark_min_time=0.01s)
154
155compile_output_test(reporter_output_test)
156add_test(NAME reporter_output_test COMMAND reporter_output_test --benchmark_min_time=0.01s)
157
158compile_output_test(templated_fixture_test)
159add_test(NAME templated_fixture_test COMMAND templated_fixture_test --benchmark_min_time=0.01s)
160
161compile_output_test(user_counters_test)
162add_test(NAME user_counters_test COMMAND user_counters_test --benchmark_min_time=0.01s)
163
164compile_output_test(perf_counters_test)
165add_test(NAME perf_counters_test COMMAND perf_counters_test --benchmark_min_time=0.01s --benchmark_perf_counters=CYCLES,BRANCHES)
166
167compile_output_test(internal_threading_test)
168add_test(NAME internal_threading_test COMMAND internal_threading_test --benchmark_min_time=0.01s)
169
170compile_output_test(report_aggregates_only_test)
171add_test(NAME report_aggregates_only_test COMMAND report_aggregates_only_test --benchmark_min_time=0.01s)
172
173compile_output_test(display_aggregates_only_test)
174add_test(NAME display_aggregates_only_test COMMAND display_aggregates_only_test --benchmark_min_time=0.01s)
175
176compile_output_test(user_counters_tabular_test)
177add_test(NAME user_counters_tabular_test COMMAND user_counters_tabular_test --benchmark_counters_tabular=true --benchmark_min_time=0.01s)
178
179compile_output_test(user_counters_thousands_test)
180add_test(NAME user_counters_thousands_test COMMAND user_counters_thousands_test --benchmark_min_time=0.01s)
181
182compile_output_test(memory_manager_test)
183add_test(NAME memory_manager_test COMMAND memory_manager_test --benchmark_min_time=0.01s)
184
185# MSVC does not allow to set the language standard to C++98/03.
186if(NOT CMAKE_CXX_COMPILER_ID STREQUAL "MSVC")
187  compile_benchmark_test(cxx03_test)
188  set_target_properties(cxx03_test
189      PROPERTIES
190      CXX_STANDARD 98
191      CXX_STANDARD_REQUIRED YES)
192  # libstdc++ provides different definitions within <map> between dialects. When
193  # LTO is enabled and -Werror is specified GCC diagnoses this ODR violation
194  # causing the test to fail to compile. To prevent this we explicitly disable
195  # the warning.
196  check_cxx_compiler_flag(-Wno-odr BENCHMARK_HAS_WNO_ODR)
197  check_cxx_compiler_flag(-Wno-lto-type-mismatch BENCHMARK_HAS_WNO_LTO_TYPE_MISMATCH)
198  # Cannot set_target_properties multiple times here because the warnings will
199  # be overwritten on each call
200  set (DISABLE_LTO_WARNINGS "")
201  if (BENCHMARK_HAS_WNO_ODR)
202    set(DISABLE_LTO_WARNINGS "${DISABLE_LTO_WARNINGS} -Wno-odr")
203  endif()
204  if (BENCHMARK_HAS_WNO_LTO_TYPE_MISMATCH)
205    set(DISABLE_LTO_WARNINGS "${DISABLE_LTO_WARNINGS} -Wno-lto-type-mismatch")
206  endif()
207  set_target_properties(cxx03_test PROPERTIES LINK_FLAGS "${DISABLE_LTO_WARNINGS}")
208  add_test(NAME cxx03 COMMAND cxx03_test --benchmark_min_time=0.01s)
209endif()
210
211# Attempt to work around flaky test failures when running on Appveyor servers.
212if (DEFINED ENV{APPVEYOR})
213  set(COMPLEXITY_MIN_TIME "0.5s")
214else()
215  set(COMPLEXITY_MIN_TIME "0.01s")
216endif()
217compile_output_test(complexity_test)
218add_test(NAME complexity_benchmark COMMAND complexity_test --benchmark_min_time=${COMPLEXITY_MIN_TIME})
219
220###############################################################################
221# GoogleTest Unit Tests
222###############################################################################
223
224if (BENCHMARK_ENABLE_GTEST_TESTS)
225  macro(compile_gtest name)
226    add_executable(${name} "${name}.cc")
227    target_link_libraries(${name} benchmark::benchmark
228        gmock_main ${CMAKE_THREAD_LIBS_INIT})
229  endmacro(compile_gtest)
230
231  macro(add_gtest name)
232    compile_gtest(${name})
233    add_test(NAME ${name} COMMAND ${name})
234  endmacro()
235
236  add_gtest(benchmark_gtest)
237  add_gtest(benchmark_name_gtest)
238  add_gtest(benchmark_random_interleaving_gtest)
239  add_gtest(commandlineflags_gtest)
240  add_gtest(statistics_gtest)
241  add_gtest(string_util_gtest)
242  add_gtest(perf_counters_gtest)
243  add_gtest(time_unit_gtest)
244  add_gtest(min_time_parse_gtest)
245endif(BENCHMARK_ENABLE_GTEST_TESTS)
246
247###############################################################################
248# Assembly Unit Tests
249###############################################################################
250
251if (BENCHMARK_ENABLE_ASSEMBLY_TESTS)
252  if (NOT LLVM_FILECHECK_EXE)
253    message(FATAL_ERROR "LLVM FileCheck is required when including this file")
254  endif()
255  include(AssemblyTests.cmake)
256  add_filecheck_test(donotoptimize_assembly_test)
257  add_filecheck_test(state_assembly_test)
258  add_filecheck_test(clobber_memory_assembly_test)
259endif()
260
261
262
263###############################################################################
264# Code Coverage Configuration
265###############################################################################
266
267# Add the coverage command(s)
268if(CMAKE_BUILD_TYPE)
269  string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER)
270endif()
271if (${CMAKE_BUILD_TYPE_LOWER} MATCHES "coverage")
272  find_program(GCOV gcov)
273  find_program(LCOV lcov)
274  find_program(GENHTML genhtml)
275  find_program(CTEST ctest)
276  if (GCOV AND LCOV AND GENHTML AND CTEST AND HAVE_CXX_FLAG_COVERAGE)
277    add_custom_command(
278      OUTPUT ${CMAKE_BINARY_DIR}/lcov/index.html
279      COMMAND ${LCOV} -q -z -d .
280      COMMAND ${LCOV} -q --no-external -c -b "${CMAKE_SOURCE_DIR}" -d . -o before.lcov -i
281      COMMAND ${CTEST} --force-new-ctest-process
282      COMMAND ${LCOV} -q --no-external -c -b "${CMAKE_SOURCE_DIR}" -d . -o after.lcov
283      COMMAND ${LCOV} -q -a before.lcov -a after.lcov --output-file final.lcov
284      COMMAND ${LCOV} -q -r final.lcov "'${CMAKE_SOURCE_DIR}/test/*'" -o final.lcov
285      COMMAND ${GENHTML} final.lcov -o lcov --demangle-cpp --sort -p "${CMAKE_BINARY_DIR}" -t benchmark
286      DEPENDS filter_test benchmark_test options_test basic_test fixture_test cxx03_test complexity_test
287      WORKING_DIRECTORY ${CMAKE_BINARY_DIR}
288      COMMENT "Running LCOV"
289    )
290    add_custom_target(coverage
291      DEPENDS ${CMAKE_BINARY_DIR}/lcov/index.html
292      COMMENT "LCOV report at lcov/index.html"
293    )
294    message(STATUS "Coverage command added")
295  else()
296    if (HAVE_CXX_FLAG_COVERAGE)
297      set(CXX_FLAG_COVERAGE_MESSAGE supported)
298    else()
299      set(CXX_FLAG_COVERAGE_MESSAGE unavailable)
300    endif()
301    message(WARNING
302      "Coverage not available:\n"
303      "  gcov: ${GCOV}\n"
304      "  lcov: ${LCOV}\n"
305      "  genhtml: ${GENHTML}\n"
306      "  ctest: ${CTEST}\n"
307      "  --coverage flag: ${CXX_FLAG_COVERAGE_MESSAGE}")
308  endif()
309endif()
310