Skip to content

Commit

Permalink
Fixed several warnings in WINDOWS compilation and enabled testing in …
Browse files Browse the repository at this point in the history
…windows
  • Loading branch information
Bardo91 authored and Bardo91 committed Nov 4, 2019
1 parent 77f4687 commit f844c6f
Show file tree
Hide file tree
Showing 8 changed files with 133 additions and 22 deletions.
25 changes: 20 additions & 5 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ PROJECT(fastcom VERSION 1.3.0)
#########################################

option(BUILD_EXAMPLES "Compile examples" ON)
option(BUILD_TESTS "Prepare tests" OFF)
option(BUILD_TESTS "Prepare tests" ON)

if(UNIX)
add_definitions(-std=c++11 -pthread -lpthread)
Expand Down Expand Up @@ -82,6 +82,10 @@ if(OpenCV_FOUND)
endif(OpenCV_FOUND)


if(WIN32)
target_compile_definitions(${PROJECT_NAME} PUBLIC "_WIN32_WINNT=0x0601")
endif()

# Find python latest version
find_package (Python3)
if(${Python3_FOUND})
Expand Down Expand Up @@ -116,11 +120,22 @@ endif()
###### Test ######
#########################################

find_package(GTest)
if(${GTest_FOUND})
enable_testing()
add_subdirectory(tests)
if(${BUILD_TESTS})
if(WIN32)
enable_testing()
add_definitions("-D_SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING")
include(cmake/AddGoogleTest.cmake)
add_subdirectory(tests)
endif()
if(UNIX)
find_package(GTest)
if(${GTest_FOUND})
enable_testing()
add_subdirectory(tests)
endif()
endif()
endif()


#########################################
###### INSTALL ######
Expand Down
98 changes: 98 additions & 0 deletions cmake/AddGoogleTest.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@

#
#
# Downloads GTest and provides a helper macro to add tests. Add make check, as well, which
# gives output on failed tests without having to set an environment variable.
#
#
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)

if(CMAKE_VERSION VERSION_LESS 3.11)
set(UPDATE_DISCONNECTED_IF_AVAILABLE "UPDATE_DISCONNECTED 1")

include(DownloadProject)
download_project(PROJ googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.8.0
UPDATE_DISCONNECTED 1
QUIET
)

# CMake warning suppression will not be needed in version 1.9
set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS 1 CACHE BOOL "")
add_subdirectory(${googletest_SOURCE_DIR} ${googletest_SOURCE_DIR} EXCLUDE_FROM_ALL)
unset(CMAKE_SUPPRESS_DEVELOPER_WARNINGS)
else()
include(FetchContent)
FetchContent_Declare(googletest
GIT_REPOSITORY https://github.com/google/googletest.git
GIT_TAG release-1.8.0)
FetchContent_GetProperties(googletest)
if(NOT googletest_POPULATED)
FetchContent_Populate(googletest)
set(CMAKE_SUPPRESS_DEVELOPER_WARNINGS 1 CACHE BOOL "")
add_subdirectory(${googletest_SOURCE_DIR} ${googletest_BINARY_DIR} EXCLUDE_FROM_ALL)
unset(CMAKE_SUPPRESS_DEVELOPER_WARNINGS)
endif()
endif()


if(CMAKE_CONFIGURATION_TYPES)
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}
--force-new-ctest-process --output-on-failure
--build-config "$<CONFIGURATION>")
else()
add_custom_target(check COMMAND ${CMAKE_CTEST_COMMAND}
--force-new-ctest-process --output-on-failure)
endif()
set_target_properties(check PROPERTIES FOLDER "Scripts")

#include_directories(${gtest_SOURCE_DIR}/include)

# More modern way to do the last line, less messy but needs newish CMake:
# target_include_directories(gtest INTERFACE ${gtest_SOURCE_DIR}/include)


if(GOOGLE_TEST_INDIVIDUAL)
if(NOT CMAKE_VERSION VERSION_LESS 3.9)
include(GoogleTest)
else()
set(GOOGLE_TEST_INDIVIDUAL OFF)
endif()
endif()

# Target must already exist
macro(add_gtest TESTNAME)
target_link_libraries(${TESTNAME} PUBLIC gtest gmock gtest_main)

if(GOOGLE_TEST_INDIVIDUAL)
if(CMAKE_VERSION VERSION_LESS 3.10)
gtest_add_tests(TARGET ${TESTNAME}
TEST_PREFIX "${TESTNAME}."
TEST_LIST TmpTestList)
set_tests_properties(${TmpTestList} PROPERTIES FOLDER "Tests")
else()
gtest_discover_tests(${TESTNAME}
TEST_PREFIX "${TESTNAME}."
PROPERTIES FOLDER "Tests")
endif()
else()
add_test(${TESTNAME} ${TESTNAME})
set_target_properties(${TESTNAME} PROPERTIES FOLDER "Tests")
endif()

endmacro()

mark_as_advanced(
gmock_build_tests
gtest_build_samples
gtest_build_tests
gtest_disable_pthreads
gtest_force_shared_crt
gtest_hide_internal_symbols
BUILD_GMOCK
BUILD_GTEST
)

set_target_properties(gtest gtest_main gmock gmock_main
PROPERTIES FOLDER "Extern")
6 changes: 3 additions & 3 deletions include/fastcom/impl/StringPublisher.inl
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ namespace fastcom{
boost::system::error_code error;
boost::system::error_code ignored_error;

int nPackets = _data.size();
size_t nPackets = _data.size();
// std::vectors have a more complex structure. It is dangeorous because packets may get lost! 666
// Send Number of packets
{
boost::array<char, sizeof(int)> send_buffer;
memcpy(&send_buffer[0], &nPackets, sizeof(int));
boost::array<char, sizeof(size_t)> send_buffer;
memcpy(&send_buffer[0], &nPackets, sizeof(size_t));
try {
mServerSocket->send_to(boost::asio::buffer(send_buffer), *con, 0, ignored_error);
}
Expand Down
8 changes: 4 additions & 4 deletions include/fastcom/impl/StringSubscriber.inl
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ namespace fastcom{
bool Subscriber<std::string>::listenCallback_impl(std::string &_packet){
boost::asio::ip::udp::endpoint sender_endpoint;

int nPackets = -1;
size_t nPackets = 0;

boost::array<char, sizeof(int)> recv_buf;
boost::array<char, sizeof(size_t)> recv_buf;
size_t len = mSocket->receive_from(boost::asio::buffer(recv_buf), sender_endpoint);
if(len != sizeof(int)){
if(len != sizeof(size_t)){
return false;
}
memcpy(&nPackets, &recv_buf[0], sizeof(int));
memcpy(&nPackets, &recv_buf[0], sizeof(size_t));

std::vector<char> stdBuffer(nPackets);
len = mSocket->receive_from(boost::asio::buffer(stdBuffer), sender_endpoint);
Expand Down
6 changes: 3 additions & 3 deletions include/fastcom/impl/VectorPublisher.inl
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ namespace fastcom{
boost::system::error_code error;
boost::system::error_code ignored_error;

int nPackets = _data.size();
size_t nPackets = _data.size();
// std::vectors have a more complex structure. It is dangeorous because packets may get lost! 666
// Send Number of packets
{
boost::array<char, sizeof(int)> send_buffer;
memcpy(&send_buffer[0], &nPackets, sizeof(int));
boost::array<char, sizeof(size_t)> send_buffer;
memcpy(&send_buffer[0], &nPackets, sizeof(size_t));
try {
mServerSocket->send_to(boost::asio::buffer(send_buffer), *con, 0, ignored_error);
}
Expand Down
8 changes: 4 additions & 4 deletions include/fastcom/impl/VectorSubscriber.inl
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,14 @@ namespace fastcom{
bool Subscriber<DataType_>::listenCallback_impl(T_ &_packet){
boost::asio::ip::udp::endpoint sender_endpoint;

int nPackets = -1;
size_t nPackets = 0;

boost::array<char, sizeof(int)> recv_buf;
boost::array<char, sizeof(size_t)> recv_buf;
size_t len = mSocket->receive_from(boost::asio::buffer(recv_buf), sender_endpoint);
if(len != sizeof(int)){
if(len != sizeof(size_t)){
return false;
}
memcpy(&nPackets, &recv_buf[0], sizeof(int));
memcpy(&nPackets, &recv_buf[0], sizeof(size_t));

T_ vectorPackets(nPackets);
len = mSocket->receive_from(boost::asio::buffer(vectorPackets), sender_endpoint);
Expand Down
2 changes: 0 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@
## CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
##---------------------------------------------------------------------------------------------------------------------

cmake_minimum_required(VERSION 3.12)

### Add test with input file
add_executable(publisher_subscriber publisher_subscriber.cpp)
target_link_libraries(publisher_subscriber LINK_PRIVATE fastcom)
Expand Down
2 changes: 1 addition & 1 deletion tests/publisher_subscriber.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ TEST(FloatTest, FloatTest) {
expectedValue += 1.0f;
});

for(int msg = 1; msg < 10; msg = msg+1.0f){
for(float msg = 1; msg < 10.0f; msg = msg+1.0f){
publisher.publish(msg);
}

Expand Down

0 comments on commit f844c6f

Please sign in to comment.