Skip to content

Commit 883cba9

Browse files
Merge pull request #133 from thirtytwobits/master
#132 proper googletest dependency and fixes for unittests on OSX
2 parents 1e60063 + 784ec11 commit 883cba9

File tree

6 files changed

+75
-9
lines changed

6 files changed

+75
-9
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ __pycache__
1717
.pydevproject
1818
.gdbinit
1919

20+
# vsstudio code
21+
.vscode
22+
2023
# libuavcan DSDL compiler default output directory
2124
dsdlc_generated
2225

.travis.yml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,7 @@ before_install:
2525
- sudo apt-get update -qq
2626
- if [ "$CXX" = "g++" ]; then sudo apt-get install --force-yes -qq g++-4.8; fi
2727
- if [ "$CXX" = "g++" ]; then export CXX="g++-4.8" CC="gcc-4.8"; fi
28-
- sudo apt-get install --force-yes libgtest-dev gcc-arm-none-eabi
29-
- "cd /usr/src/gtest && sudo cmake . && sudo cmake --build . && sudo mv libg* /usr/local/lib/ ; cd -"
28+
- sudo apt-get install --force-yes gcc-arm-none-eabi
3029
before_script: "mkdir build && cd build && cmake .. -DCMAKE_BUILD_TYPE=Debug -DCONTINUOUS_INTEGRATION_BUILD=1"
3130
script:
3231
- if [ "${COVERITY_SCAN_BRANCH}" != 1 ] && [ "${TARGET}" == "native" ]; then make ; fi

CMakeLists.txt

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Copyright (C) 2014 Pavel Kirienko <pavel.kirienko@gmail.com>
33
#
44

5-
cmake_minimum_required(VERSION 2.8)
5+
cmake_minimum_required(VERSION 2.8.11)
66

77
project(uavcan C CXX)
88

@@ -67,6 +67,42 @@ include_directories(
6767
# DSDL definitions
6868
install(DIRECTORY dsdl DESTINATION share/uavcan)
6969

70+
#
71+
# Googletest
72+
#
73+
if( CMAKE_BUILD_TYPE STREQUAL "Debug" )
74+
# (Taken from googletest/README.md documentation)
75+
# GTest executables
76+
# Download and unpack googletest at configure time
77+
configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt)
78+
execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" .
79+
RESULT_VARIABLE result
80+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
81+
if(result)
82+
message(WARNING "CMake step for googletest failed: ${result}")
83+
else()
84+
execute_process(COMMAND ${CMAKE_COMMAND} --build .
85+
RESULT_VARIABLE result
86+
WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download )
87+
if(result)
88+
message(WARNING "Build step for googletest failed: ${result}")
89+
else()
90+
91+
# Prevent overriding the parent project's compiler/linker
92+
# settings on Windows
93+
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
94+
95+
# Add googletest directly to our build. This defines
96+
# the gtest and gtest_main targets.
97+
add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src
98+
${CMAKE_BINARY_DIR}/googletest-build
99+
EXCLUDE_FROM_ALL)
100+
101+
set(GTEST_FOUND ON)
102+
endif()
103+
endif()
104+
endif()
105+
70106
#
71107
# Subdirectories
72108
#

CMakeLists.txt.in

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
cmake_minimum_required(VERSION 2.8.2)
2+
3+
project(googletest-download NONE)
4+
5+
include(ExternalProject)
6+
ExternalProject_Add(googletest
7+
GIT_REPOSITORY https://github.com/google/googletest.git
8+
GIT_TAG 98a0d007d7092b72eea0e501bb9ad17908a1a036
9+
SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src"
10+
BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build"
11+
CONFIGURE_COMMAND ""
12+
BUILD_COMMAND ""
13+
INSTALL_COMMAND ""
14+
TEST_COMMAND ""
15+
)

libuavcan/CMakeLists.txt

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,6 @@ install(CODE "execute_process(COMMAND ${PYTHON} setup.py install --record instal
8989
#
9090
function(add_libuavcan_test name library flags) # Adds GTest executable and creates target to execute it every build
9191
find_package(Threads REQUIRED)
92-
include_directories(${GTEST_INCLUDE_DIRS})
9392

9493
file(GLOB_RECURSE TEST_CXX_FILES RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "test/*.cpp")
9594
add_executable(${name} ${TEST_CXX_FILES})
@@ -99,9 +98,11 @@ function(add_libuavcan_test name library flags) # Adds GTest executable and crea
9998
set_target_properties(${name} PROPERTIES COMPILE_FLAGS ${flags})
10099
endif ()
101100

102-
target_link_libraries(${name} ${GTEST_BOTH_LIBRARIES} ${CMAKE_THREAD_LIBS_INIT})
101+
target_link_libraries(${name} gmock_main)
103102
target_link_libraries(${name} ${library})
104-
target_link_libraries(${name} rt)
103+
if (${UAVCAN_PLATFORM} STREQUAL "linux")
104+
target_link_libraries(${name} rt)
105+
endif()
105106

106107
# Tests run automatically upon successful build
107108
# If failing tests need to be investigated with debugger, use 'make --ignore-errors'
@@ -145,10 +146,8 @@ if (DEBUG_BUILD)
145146
set_target_properties(uavcan_optim PROPERTIES COMPILE_FLAGS ${optim_flags})
146147
add_dependencies(uavcan_optim libuavcan_dsdlc)
147148

148-
# GTest executables
149-
find_package(GTest)
150149
if (GTEST_FOUND)
151-
message(STATUS "GTest found, tests will be built and run [${GTEST_INCLUDE_DIRS}] [${GTEST_BOTH_LIBRARIES}]")
150+
message(STATUS "GTest found, tests will be built and run.")
152151
add_libuavcan_test(libuavcan_test uavcan "") # Default
153152
add_libuavcan_test(libuavcan_test_cpp03 uavcan_cpp03 "${cpp03_flags}") # C++03
154153
add_libuavcan_test(libuavcan_test_optim uavcan_optim "${optim_flags}") # Max optimization

libuavcan/test/helpers/heap_based_pool_allocator.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44

55
#include <gtest/gtest.h>
66
#include <uavcan/helpers/heap_based_pool_allocator.hpp>
7+
#ifdef __linux__
78
#include <malloc.h>
89

10+
#else
11+
#include <stdlib.h>
12+
#endif
913

1014
TEST(HeapBasedPoolAllocator, Basic)
1115
{
16+
#ifdef __linux__
1217
std::cout << ">>> HEAP BEFORE:" << std::endl;
1318
malloc_stats();
19+
#endif
1420

1521
uavcan::HeapBasedPoolAllocator<uavcan::MemPoolBlockSize> al(0xEEEE);
1622

@@ -61,8 +67,10 @@ TEST(HeapBasedPoolAllocator, Basic)
6167
ASSERT_EQ(0, al.getNumReservedBlocks());
6268
ASSERT_EQ(0, al.getNumAllocatedBlocks());
6369

70+
#ifdef __linux__
6471
std::cout << ">>> HEAP AFTER:" << std::endl;
6572
malloc_stats();
73+
#endif
6674
}
6775

6876

@@ -115,9 +123,11 @@ std::mutex RaiiSynchronizer::mutex;
115123

116124
TEST(HeapBasedPoolAllocator, Concurrency)
117125
{
126+
#ifdef __linux__
118127
std::cout << ">>> HEAP BEFORE:" << std::endl;
119128
malloc_stats();
120129

130+
#endif
121131
uavcan::HeapBasedPoolAllocator<uavcan::MemPoolBlockSize, RaiiSynchronizer> al(1000);
122132

123133
ASSERT_EQ(1000, al.getBlockCapacity());
@@ -167,13 +177,17 @@ TEST(HeapBasedPoolAllocator, Concurrency)
167177
std::cout << "Allocated: " << al.getNumAllocatedBlocks() << std::endl;
168178
std::cout << "Reserved: " << al.getNumReservedBlocks() << std::endl;
169179

180+
#ifdef __linux__
170181
std::cout << ">>> HEAP BEFORE SHRINK:" << std::endl;
171182
malloc_stats();
172183

184+
#endif
173185
al.shrink();
174186

187+
#ifdef __linux__
175188
std::cout << ">>> HEAP AFTER SHRINK:" << std::endl;
176189
malloc_stats();
190+
#endif
177191
}
178192

179193
#endif

0 commit comments

Comments
 (0)