diff --git a/CMakeLists.txt b/CMakeLists.txt index 6888814..91b3d81 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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) @@ -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}) @@ -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 ###### diff --git a/cmake/AddGoogleTest.cmake b/cmake/AddGoogleTest.cmake new file mode 100644 index 0000000..7c6871f --- /dev/null +++ b/cmake/AddGoogleTest.cmake @@ -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 "$") +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") diff --git a/include/fastcom/impl/StringPublisher.inl b/include/fastcom/impl/StringPublisher.inl index 63c460e..cbb27cf 100644 --- a/include/fastcom/impl/StringPublisher.inl +++ b/include/fastcom/impl/StringPublisher.inl @@ -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 send_buffer; - memcpy(&send_buffer[0], &nPackets, sizeof(int)); + boost::array send_buffer; + memcpy(&send_buffer[0], &nPackets, sizeof(size_t)); try { mServerSocket->send_to(boost::asio::buffer(send_buffer), *con, 0, ignored_error); } diff --git a/include/fastcom/impl/StringSubscriber.inl b/include/fastcom/impl/StringSubscriber.inl index 5925740..81aeea9 100644 --- a/include/fastcom/impl/StringSubscriber.inl +++ b/include/fastcom/impl/StringSubscriber.inl @@ -30,14 +30,14 @@ namespace fastcom{ bool Subscriber::listenCallback_impl(std::string &_packet){ boost::asio::ip::udp::endpoint sender_endpoint; - int nPackets = -1; + size_t nPackets = 0; - boost::array recv_buf; + boost::array 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 stdBuffer(nPackets); len = mSocket->receive_from(boost::asio::buffer(stdBuffer), sender_endpoint); diff --git a/include/fastcom/impl/VectorPublisher.inl b/include/fastcom/impl/VectorPublisher.inl index 6bf6344..d8145ad 100644 --- a/include/fastcom/impl/VectorPublisher.inl +++ b/include/fastcom/impl/VectorPublisher.inl @@ -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 send_buffer; - memcpy(&send_buffer[0], &nPackets, sizeof(int)); + boost::array send_buffer; + memcpy(&send_buffer[0], &nPackets, sizeof(size_t)); try { mServerSocket->send_to(boost::asio::buffer(send_buffer), *con, 0, ignored_error); } diff --git a/include/fastcom/impl/VectorSubscriber.inl b/include/fastcom/impl/VectorSubscriber.inl index 8d4ebd8..013b098 100644 --- a/include/fastcom/impl/VectorSubscriber.inl +++ b/include/fastcom/impl/VectorSubscriber.inl @@ -30,14 +30,14 @@ namespace fastcom{ bool Subscriber::listenCallback_impl(T_ &_packet){ boost::asio::ip::udp::endpoint sender_endpoint; - int nPackets = -1; + size_t nPackets = 0; - boost::array recv_buf; + boost::array 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); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 0cde80e..79a764c 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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) diff --git a/tests/publisher_subscriber.cpp b/tests/publisher_subscriber.cpp index 7e87bee..9238236 100644 --- a/tests/publisher_subscriber.cpp +++ b/tests/publisher_subscriber.cpp @@ -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); }