Skip to content

Commit 7c8bbc5

Browse files
authored
Merge 913358b into 7b03df7
2 parents 7b03df7 + 913358b commit 7c8bbc5

1 file changed

Lines changed: 96 additions & 107 deletions

File tree

test/CMakeLists.txt

Lines changed: 96 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,6 @@ if( NOT uppercase_CMAKE_BUILD_TYPE STREQUAL "DEBUG" )
2222
endforeach()
2323
endif()
2424

25-
check_cxx_compiler_flag(-O3 BENCHMARK_HAS_O3_FLAG)
26-
set(BENCHMARK_O3_FLAG "")
27-
if (BENCHMARK_HAS_O3_FLAG)
28-
set(BENCHMARK_O3_FLAG "-O3")
29-
endif()
3025

3126
# NOTE: These flags must be added after find_package(Threads REQUIRED) otherwise
3227
# they will break the configuration check.
@@ -35,29 +30,66 @@ if (DEFINED BENCHMARK_CXX_LINKER_FLAGS)
3530
endif()
3631

3732
add_library(output_test_helper STATIC output_test_helper.cc output_test.h)
33+
target_link_libraries(output_test_helper benchmark)
34+
35+
function(add_benchmark_test target)
36+
cmake_parse_arguments(ARG
37+
"EXCLUDE_FROM_TEST;OUTPUT_TEST;GTEST_TEST"
38+
"SOURCE;REQUIRES"
39+
"COMPILE_FLAGS;LINK_FLAGS;LIBRARIES;TEST_ARGS"
40+
${ARGN})
41+
if (NOT BENCHMARK_ENABLE_GTEST_TESTS AND ARG_GTEST_TEST)
42+
return()
43+
endif()
44+
if (ARG_REQUIRES)
45+
if (NOT ${ARG_REQUIRES})
46+
return()
47+
endif()
48+
endif()
3849

39-
macro(compile_benchmark_test name)
40-
add_executable(${name} "${name}.cc")
41-
target_link_libraries(${name} benchmark ${CMAKE_THREAD_LIBS_INIT})
42-
endmacro(compile_benchmark_test)
50+
# Create the test executable
51+
if (NOT ARG_SOURCE)
52+
set(ARG_SOURCE "${target}.cc")
53+
endif()
54+
add_executable(${target} ${ARG_SOURCE})
55+
set_target_properties(${target}
56+
PROPERTIES
57+
COMPILE_FLAGS "${ARG_COMPILE_FLAGS}"
58+
LINK_FLAGS "${ARG_LINK_FLAGS}")
59+
target_link_libraries(${target} benchmark${ARG_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
60+
61+
# Handle specifics for each different type of test. ex. output tests, gtests, ect.
62+
if (ARG_OUTPUT_TEST)
63+
target_link_libraries(${target} output_test_helper ${BENCHMARK_CXX_LIBRARIES})
64+
elseif (ARG_GTEST_TEST)
65+
if (TARGET googletest)
66+
add_dependencies(${target} googletest)
67+
endif()
68+
if (GTEST_INCLUDE_DIRS)
69+
target_include_directories(${target} PRIVATE ${GTEST_INCLUDE_DIRS})
70+
endif()
71+
target_link_libraries(${target} ${GTEST_BOTH_LIBRARIES})
72+
endif()
4373

74+
# Unless otherwise specified, supply default arguments to each benchmark test.
75+
if (NOT ARG_TEST_ARGS AND NOT ARG_GTEST_TEST)
76+
set(ARG_TEST_ARGS --benchmark_min_time=0.01)
77+
endif()
4478

45-
macro(compile_output_test name)
46-
add_executable(${name} "${name}.cc" output_test.h)
47-
target_link_libraries(${name} output_test_helper benchmark
48-
${BENCHMARK_CXX_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
49-
endmacro(compile_output_test)
79+
if (NOT ARG_EXCLUDE_FROM_TEST)
80+
add_test(${target} ${target} ${ARG_TEST_ARGS})
81+
endif()
82+
endfunction(add_benchmark_test)
5083

51-
# Demonstration executable
52-
compile_benchmark_test(benchmark_test)
53-
add_test(benchmark benchmark_test --benchmark_min_time=0.01)
5484

55-
compile_benchmark_test(filter_test)
85+
###############################################################################
86+
# Filter Tests
87+
###############################################################################
88+
add_benchmark_test(filter_test EXCLUDE_FROM_TEST)
5689
macro(add_filter_test name filter expect)
5790
add_test(${name} filter_test --benchmark_min_time=0.01 --benchmark_filter=${filter} ${expect})
5891
add_test(${name}_list_only filter_test --benchmark_list_tests --benchmark_filter=${filter} ${expect})
5992
endmacro(add_filter_test)
60-
6193
add_filter_test(filter_simple "Foo" 3)
6294
add_filter_test(filter_suffix "BM_.*" 4)
6395
add_filter_test(filter_regex_all ".*" 5)
@@ -68,107 +100,66 @@ add_filter_test(filter_regex_begin "^BM_.*" 4)
68100
add_filter_test(filter_regex_begin2 "^N" 1)
69101
add_filter_test(filter_regex_end ".*Ba$" 1)
70102

71-
compile_benchmark_test(options_test)
72-
add_test(options_benchmarks options_test --benchmark_min_time=0.01)
73-
74-
compile_benchmark_test(basic_test)
75-
add_test(basic_benchmark basic_test --benchmark_min_time=0.01)
76-
77-
compile_benchmark_test(diagnostics_test)
78-
add_test(diagnostics_test diagnostics_test --benchmark_min_time=0.01)
79-
80-
compile_benchmark_test(skip_with_error_test)
81-
add_test(skip_with_error_test skip_with_error_test --benchmark_min_time=0.01)
82-
83-
compile_benchmark_test(donotoptimize_test)
84-
# Some of the issues with DoNotOptimize only occur when optimization is enabled
103+
###############################################################################
104+
# Basic Benchmark Tests
105+
###############################################################################
106+
add_benchmark_test(benchmark_test)
107+
add_benchmark_test(options_test)
108+
add_benchmark_test(basic_benchmark SOURCE basic_test.cc)
109+
add_benchmark_test(diagnostics_test)
110+
add_benchmark_test(skip_with_error_test)
111+
add_benchmark_test(donotoptimize_test)
112+
# Some of the issues with DoNotOptimize only occur when optimization is enabled.
113+
# Others only occur when they are disabled. Test both.
85114
check_cxx_compiler_flag(-O3 BENCHMARK_HAS_O3_FLAG)
86-
if (BENCHMARK_HAS_O3_FLAG)
87-
set_target_properties(donotoptimize_test PROPERTIES COMPILE_FLAGS "-O3")
115+
add_benchmark_test(donotoptimize_test_O3
116+
REQUIRES BENCHMARK_HAS_O0_FLAG
117+
SOURCE donotoptimize_test.cc
118+
COMPILE_FLAGS "-O3")
119+
check_cxx_compiler_flag(-O0 BENCHMARK_HAS_O0_FLAG)
120+
add_benchmark_test(donotoptimize_test_O0
121+
REQUIRES BENCHMARK_HAS_O0_FLAG
122+
SOURCE donotoptimize_test.cc
123+
COMPILE_FLAGS "-O0")
124+
add_benchmark_test(fixture_test)
125+
add_benchmark_test(register_benchmark_test)
126+
add_benchmark_test(map_test)
127+
add_benchmark_test(multiple_ranges_test)
128+
add_benchmark_test(reporter_output_test OUTPUT_TEST)
129+
add_benchmark_test(templated_fixture_test OUTPUT_TEST)
130+
add_benchmark_test(user_counters_test OUTPUT_TEST)
131+
add_benchmark_test(user_counters_tabular_test OUTPUT_TEST
132+
TEST_ARGS --benchmark_counters_tabular=true --benchmark_min_time=0.01)
133+
134+
# libstdc++ provides different definitions within <map> between dialects. When
135+
# LTO is enabled and -Werror is specified GCC diagnoses this ODR violation
136+
# causing the test to fail to compile. To prevent this we explicitly disable
137+
# the warning.
138+
check_cxx_compiler_flag(-Wno-odr BENCHMARK_HAS_WNO_ODR)
139+
if (BENCHMARK_ENABLE_LTO AND BENCHMARK_HAS_WNO_ODR)
140+
set(LINK_FLAGS "-Wno-odr")
88141
endif()
89-
add_test(donotoptimize_test donotoptimize_test --benchmark_min_time=0.01)
90-
91-
compile_benchmark_test(fixture_test)
92-
add_test(fixture_test fixture_test --benchmark_min_time=0.01)
93-
94-
compile_benchmark_test(register_benchmark_test)
95-
add_test(register_benchmark_test register_benchmark_test --benchmark_min_time=0.01)
96-
97-
compile_benchmark_test(map_test)
98-
add_test(map_test map_test --benchmark_min_time=0.01)
99-
100-
compile_benchmark_test(multiple_ranges_test)
101-
add_test(multiple_ranges_test multiple_ranges_test --benchmark_min_time=0.01)
102-
103-
compile_output_test(reporter_output_test)
104-
add_test(reporter_output_test reporter_output_test --benchmark_min_time=0.01)
105-
106-
compile_output_test(templated_fixture_test)
107-
add_test(templated_fixture_test templated_fixture_test --benchmark_min_time=0.01)
108-
109-
compile_output_test(user_counters_test)
110-
add_test(user_counters_test user_counters_test --benchmark_min_time=0.01)
111-
112-
compile_output_test(user_counters_tabular_test)
113-
add_test(user_counters_tabular_test user_counters_tabular_test --benchmark_counters_tabular=true --benchmark_min_time=0.01)
114-
115142
check_cxx_compiler_flag(-std=c++03 BENCHMARK_HAS_CXX03_FLAG)
116-
if (BENCHMARK_HAS_CXX03_FLAG)
117-
compile_benchmark_test(cxx03_test)
118-
set_target_properties(cxx03_test
119-
PROPERTIES
120-
COMPILE_FLAGS "-std=c++03")
121-
# libstdc++ provides different definitions within <map> between dialects. When
122-
# LTO is enabled and -Werror is specified GCC diagnoses this ODR violation
123-
# causing the test to fail to compile. To prevent this we explicitly disable
124-
# the warning.
125-
check_cxx_compiler_flag(-Wno-odr BENCHMARK_HAS_WNO_ODR)
126-
if (BENCHMARK_ENABLE_LTO AND BENCHMARK_HAS_WNO_ODR)
127-
set_target_properties(cxx03_test
128-
PROPERTIES
129-
LINK_FLAGS "-Wno-odr")
130-
endif()
131-
add_test(cxx03 cxx03_test --benchmark_min_time=0.01)
132-
endif()
143+
add_benchmark_test(cxx03_test
144+
REQUIRES BENCHMARK_HAS_CXX03_FLAG
145+
COMPILE_FLAGS "-std=c++03"
146+
LINK_FLAGS "${LINK_FLAGS}")
147+
133148

134149
# Attempt to work around flaky test failures when running on Appveyor servers.
135150
if (DEFINED ENV{APPVEYOR})
136151
set(COMPLEXITY_MIN_TIME "0.5")
137152
else()
138153
set(COMPLEXITY_MIN_TIME "0.01")
139154
endif()
140-
compile_output_test(complexity_test)
141-
add_test(complexity_benchmark complexity_test --benchmark_min_time=${COMPLEXITY_MIN_TIME})
155+
add_benchmark_test(complexity_test OUTPUT_TEST
156+
TEST_ARGS --benchmark_min_time=${COMPLEXITY_MIN_TIME})
157+
add_benchmark_test(statistics_test GTEST_TEST)
142158

143-
###############################################################################
144-
# GoogleTest Unit Tests
145-
###############################################################################
146-
147-
if (BENCHMARK_ENABLE_GTEST_TESTS)
148-
macro(compile_gtest name)
149-
add_executable(${name} "${name}.cc")
150-
if (TARGET googletest)
151-
add_dependencies(${name} googletest)
152-
endif()
153-
if (GTEST_INCLUDE_DIRS)
154-
target_include_directories(${name} PRIVATE ${GTEST_INCLUDE_DIRS})
155-
endif()
156-
target_link_libraries(${name} benchmark
157-
${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
158-
endmacro(compile_gtest)
159-
160-
macro(add_gtest name)
161-
compile_gtest(${name})
162-
add_test(${name} ${name})
163-
endmacro()
164-
165-
add_gtest(statistics_test)
166-
endif(BENCHMARK_ENABLE_GTEST_TESTS)
167159

168160
###############################################################################
169161
# Assembly Unit Tests
170162
###############################################################################
171-
172163
if (BENCHMARK_ENABLE_ASSEMBLY_TESTS)
173164
if (NOT LLVM_FILECHECK_EXE)
174165
message(FATAL_ERROR "LLVM FileCheck is required when including this file")
@@ -179,8 +170,6 @@ if (BENCHMARK_ENABLE_ASSEMBLY_TESTS)
179170
add_filecheck_test(clobber_memory_assembly_test)
180171
endif()
181172

182-
183-
184173
###############################################################################
185174
# Code Coverage Configuration
186175
###############################################################################

0 commit comments

Comments
 (0)