Skip to content

Commit 9d0baca

Browse files
authored
Merge 37d83a9 into 27e0b43
2 parents 27e0b43 + 37d83a9 commit 9d0baca

File tree

5 files changed

+144
-2
lines changed

5 files changed

+144
-2
lines changed

CMakeLists.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ project (benchmark)
55
foreach(p
66
CMP0054 # CMake 3.1
77
CMP0056 # export EXE_LINKER_FLAGS to try_run
8+
CMP0057 # Support no if() IN_LIST operator
89
)
910
if(POLICY ${p})
1011
cmake_policy(SET ${p} NEW)
@@ -18,6 +19,14 @@ option(BENCHMARK_USE_LIBCXX "Build and test using libc++ as the standard library
1819
option(BENCHMARK_BUILD_32_BITS "Build a 32 bit version of the library." OFF)
1920
option(BENCHMARK_ENABLE_INSTALL "Enable installation of benchmark. (Projects embedding benchmark may want to turn this OFF.)" ON)
2021

22+
# This option is useful when building Benchmark in odd configurations, such as 32bit mode
23+
# or against libc++.
24+
option(BENCHMARK_BUILD_EXTERNAL_GTEST "Download and build a custom copy of GTest to use" OFF)
25+
26+
# This option can be used to disable building and running unit tests which depend on gtest
27+
# in cases where it is not possible to build or find a valid version of gtest.
28+
option(BENCHMARK_ENABLE_GTEST_TESTS "Enable building the unit tests which depend on gtest" ON)
29+
2130
# Make sure we can import out CMake functions
2231
list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
2332

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ build_script:
4343
- md _build -Force
4444
- cd _build
4545
- echo %configuration%
46-
- cmake -G "%generator%" "-DCMAKE_BUILD_TYPE=%configuration%" ..
46+
- cmake -G "%generator%" "-DCMAKE_BUILD_TYPE=%configuration%" -DBENCHMARK_BUILD_EXTERNAL_GTEST=ON ..
4747
- cmake --build . --config %configuration%
4848

4949
test_script:

cmake/HandleGTest.cmake

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
include(ExternalProject)
2+
3+
macro(split_list listname)
4+
string(REPLACE ";" " " ${listname} "${${listname}}")
5+
endmacro()
6+
7+
if (NOT BENCHMARK_BUILD_EXTERNAL_GTEST)
8+
find_package(GTest REQUIRED)
9+
else()
10+
set(GTEST_FLAGS "")
11+
if (BENCHMARK_USE_LIBCXX)
12+
if ("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang")
13+
list(APPEND GTEST_FLAGS -stdlib=libc++)
14+
else()
15+
message(WARNING "Unsupported compiler (${CMAKE_CXX_COMPILER}) when using libc++")
16+
endif()
17+
endif()
18+
if (BENCHMARK_BUILD_32_BITS)
19+
list(APPEND GTEST_FLAGS -m32)
20+
endif()
21+
22+
split_list(GTEST_FLAGS)
23+
ExternalProject_Add(googletest
24+
EXCLUDE_FROM_ALL ON
25+
GIT_REPOSITORY https://github.com/google/googletest.git
26+
GIT_TAG master
27+
PREFIX "${CMAKE_BINARY_DIR}/googletest"
28+
INSTALL_DIR "${CMAKE_BINARY_DIR}/googletest"
29+
CMAKE_CACHE_ARGS
30+
-DCMAKE_INSTALL_PREFIX:PATH=<INSTALL_DIR>
31+
-DCMAKE_CXX_FLAGS:STRING=${GTEST_FLAGS}
32+
)
33+
34+
ExternalProject_Get_Property(googletest install_dir)
35+
add_library(gtest UNKNOWN IMPORTED)
36+
add_library(gtest_main UNKNOWN IMPORTED)
37+
set_target_properties(gtest PROPERTIES
38+
IMPORTED_LOCATION ${install_dir}/lib/libgtest.a
39+
)
40+
set_target_properties(gtest_main PROPERTIES
41+
IMPORTED_LOCATION ${install_dir}/lib/libgtest_main.a
42+
)
43+
add_dependencies(gtest googletest)
44+
add_dependencies(gtest_main googletest)
45+
set(GTEST_INCLUDE_DIRS ${CMAKE_BINARY_DIR}/googletest/include)
46+
set(GTEST_BOTH_LIBRARIES gtest gtest_main)
47+
endif()

test/CMakeLists.txt

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ macro(compile_output_test name)
4242
${BENCHMARK_CXX_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
4343
endmacro(compile_output_test)
4444

45-
4645
# Demonstration executable
4746
compile_benchmark_test(benchmark_test)
4847
add_test(benchmark benchmark_test --benchmark_min_time=0.01)
@@ -135,6 +134,32 @@ endif()
135134
compile_output_test(complexity_test)
136135
add_test(complexity_benchmark complexity_test --benchmark_min_time=${COMPLEXITY_MIN_TIME})
137136

137+
###############################################################################
138+
# GoogleTest Unit Tests
139+
###############################################################################
140+
141+
if (BENCHMARK_ENABLE_GTEST_TESTS)
142+
include(HandleGTest)
143+
include_directories(SYSTEM ${GTEST_INCLUDE_DIRS})
144+
145+
macro(compile_gtest name)
146+
add_executable(${name} "${name}.cc")
147+
if (TARGET googletest)
148+
add_dependencies(${name} googletest)
149+
endif()
150+
target_link_libraries(${name} benchmark
151+
${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
152+
endmacro(compile_gtest)
153+
154+
macro(add_gtest name)
155+
compile_gtest(${name})
156+
add_test(${name} ${name})
157+
endmacro()
158+
159+
add_gtest(statistics_test)
160+
endif(BENCHMARK_ENABLE_GTEST_TESTS)
161+
162+
138163
# Add the coverage command(s)
139164
if(CMAKE_BUILD_TYPE)
140165
string(TOLOWER ${CMAKE_BUILD_TYPE} CMAKE_BUILD_TYPE_LOWER)

test/statistics_test.cc

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//===---------------------------------------------------------------------===//
2+
// statistics_test - Unit tests for src/statistics.cc
3+
//===---------------------------------------------------------------------===//
4+
5+
#include "../src/statistics.h"
6+
#include "gtest/gtest.h"
7+
8+
namespace {
9+
TEST(StatisticsTest, Mean) {
10+
std::vector<double> Inputs;
11+
{
12+
Inputs = {42, 42, 42, 42};
13+
double Res = benchmark::StatisticsMean(Inputs);
14+
EXPECT_DOUBLE_EQ(Res, 42.0);
15+
}
16+
{
17+
Inputs = {1, 2, 3, 4};
18+
double Res = benchmark::StatisticsMean(Inputs);
19+
EXPECT_DOUBLE_EQ(Res, 2.5);
20+
}
21+
{
22+
Inputs = {1, 2, 5, 10, 10, 14};
23+
double Res = benchmark::StatisticsMean(Inputs);
24+
EXPECT_DOUBLE_EQ(Res, 7.0);
25+
}
26+
}
27+
28+
TEST(StatisticsTest, Median) {
29+
std::vector<double> Inputs;
30+
{
31+
Inputs = {42, 42, 42, 42};
32+
double Res = benchmark::StatisticsMedian(Inputs);
33+
EXPECT_DOUBLE_EQ(Res, 42.0);
34+
}
35+
{
36+
Inputs = {1, 2, 3, 4};
37+
double Res = benchmark::StatisticsMedian(Inputs);
38+
EXPECT_DOUBLE_EQ(Res, 2.5);
39+
}
40+
{
41+
Inputs = {1, 2, 5, 10, 10};
42+
double Res = benchmark::StatisticsMedian(Inputs);
43+
EXPECT_DOUBLE_EQ(Res, 5.0);
44+
}
45+
}
46+
47+
TEST(StatisticsTest, StdDev) {
48+
std::vector<double> Inputs;
49+
{
50+
Inputs = {101, 101, 101, 101};
51+
double Res = benchmark::StatisticsStdDev(Inputs);
52+
EXPECT_DOUBLE_EQ(Res, 0.0);
53+
}
54+
{
55+
Inputs = {1, 2, 3};
56+
double Res = benchmark::StatisticsStdDev(Inputs);
57+
EXPECT_DOUBLE_EQ(Res, 1.0);
58+
}
59+
}
60+
61+
} // end namespace

0 commit comments

Comments
 (0)