diff --git a/CMakeLists.txt b/CMakeLists.txt index 487e846..325f554 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,4 +1,4 @@ -# After installation this project can be found by 'find_package' command: +# After installation this project can be found by 'find_package' command: # # find_package(libcreate REQUIRED) # include_directores(${libcreate_INCLUDE_DIRS}) @@ -10,6 +10,8 @@ project(libcreate) set(PACKAGE_VERSION 1.5.0) +option(LIBCREATE_BUILD_TESTS "Enable the build of tests." ON) + find_package(Boost REQUIRED COMPONENTS system thread) find_package(Threads REQUIRED) @@ -149,3 +151,68 @@ install( FILES package.xml DESTINATION ${SHARE_INSTALL_DIR} ) + +########### +# Testing # +########### + +if(LIBCREATE_BUILD_TESTS) + enable_testing() + + # Download and unpack googletest at configure time + configure_file(CMakeLists.txt.in googletest-download/CMakeLists.txt) + execute_process(COMMAND ${CMAKE_COMMAND} -G "${CMAKE_GENERATOR}" . + RESULT_VARIABLE GTEST_DOWNLOAD_RESULT + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download + ) + + if(GTEST_DOWNLOAD_RESULT) + message(FATAL_ERROR "CMake step for googletest failed: ${GTEST_DOWNLOAD_RESULT}") + endif() + + # Build googletest + execute_process(COMMAND ${CMAKE_COMMAND} --build . + RESULT_VARIABLE GTEST_BUILD_RESULT + WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/googletest-download + ) + + if(GTEST_BUILD_RESULT) + message(FATAL_ERROR "Build step for googletest failed: ${GTEST_BUILD_RESULT}") + endif() + + # Add googletest directly to our build. This defines the gtest and gtest_main targets. + add_subdirectory(${CMAKE_BINARY_DIR}/googletest-src + ${CMAKE_BINARY_DIR}/googletest-build) + + # The gtest/gtest_main targets carry header search path + # dependencies automatically when using CMake 2.8.11 or + # later. Otherwise we have to add them here ourselves. + if (CMAKE_VERSION VERSION_LESS 2.8.11) + include_directories("${gtest_SOURCE_DIR}/include") + endif() + + # Add tests + set(LIBCREATE_TESTS + test_create + test_data + test_packet + test_robot_model + test_serial_stream + test_serial_query + ) + + foreach(LIBCREATE_TEST ${LIBCREATE_TESTS}) + add_executable(${LIBCREATE_TEST} tests/${LIBCREATE_TEST}.cpp) + + target_link_libraries(${LIBCREATE_TEST} + # ${Boost_LIBRARIES} + ${LIBRARY_NAME} + gtest_main + ) + + add_test( + NAME ${LIBCREATE_TEST} + COMMAND ${LIBCREATE_TEST} + ) + endforeach() +endif() diff --git a/CMakeLists.txt.in b/CMakeLists.txt.in new file mode 100644 index 0000000..4c67ef5 --- /dev/null +++ b/CMakeLists.txt.in @@ -0,0 +1,15 @@ +cmake_minimum_required(VERSION 2.8.2) + +project(googletest-download NONE) + +include(ExternalProject) +ExternalProject_Add(googletest + GIT_REPOSITORY https://github.com/google/googletest.git + GIT_TAG master + SOURCE_DIR "${CMAKE_BINARY_DIR}/googletest-src" + BINARY_DIR "${CMAKE_BINARY_DIR}/googletest-build" + CONFIGURE_COMMAND "" + BUILD_COMMAND "" + INSTALL_COMMAND "" + TEST_COMMAND "" +) diff --git a/tests/test_create.cpp b/tests/test_create.cpp new file mode 100644 index 0000000..57e76e8 --- /dev/null +++ b/tests/test_create.cpp @@ -0,0 +1,67 @@ +/** +Software License Agreement (BSD) + +\file test_create.cpp +\authors Jacob Perron +\copyright Copyright (c) 2018, Autonomy Lab (Simon Fraser University), All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Autonomy Lab nor the names of its contributors may + be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +*/ +#include "create/create.h" +#include "create/types.h" + +#include "gtest/gtest.h" + +#include + +TEST(CreateTest, ConstructorSingleParam) +{ + create::Create create_default; + + create::Create create_1(create::RobotModel::CREATE_1); + + create::Create create_2(create::RobotModel::CREATE_2); + + create::Create create_roomba_400(create::RobotModel::ROOMBA_400); +} + +// TEST(CreateTest, ConstructorMultiParam) +// { +// TODO(jacobperron): Document exception thrown and consider defining custom exception +// create::Create create(std::string("/dev/ttyUSB0"), 11520); +// } + +TEST(CreateTest, Connected) +{ + create::Create create; + // Nothing to be connected to + EXPECT_FALSE(create.connected()); +} + +TEST(CreateTest, Disconnect) +{ + create::Create create; + // Even though not connected, this should not crash + create.disconnect(); +} diff --git a/tests/test_data.cpp b/tests/test_data.cpp new file mode 100644 index 0000000..8f893fe --- /dev/null +++ b/tests/test_data.cpp @@ -0,0 +1,169 @@ +/** +Software License Agreement (BSD) + +\file test_data.cpp +\authors Jacob Perron +\copyright Copyright (c) 2018, Autonomy Lab (Simon Fraser University), All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Autonomy Lab nor the names of its contributors may + be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +*/ +#include "create/data.h" +#include "create/packet.h" +#include "create/types.h" + +#include "gtest/gtest.h" + +#include + +TEST(DataTest, Constructor) +{ + create::Data data_default; + + create::Data data_v_1(create::V_1); + + create::Data data_v_2(create::V_2); + + create::Data data_v_3(create::V_3); + + create::Data data_v_all(create::V_ALL); +} + +// Number of packets for a given protocol are determined in the Data() constructor +TEST(DataTest, GetNumPackets) +{ + // Number of packets shared by all protocols is 16 + create::Data data_v_1(create::V_1); + // Number exclusive to V_1 = 4 + // 16 + 4 = 20 + EXPECT_EQ(static_cast(data_v_1.getNumPackets()), 20); + + create::Data data_v_2(create::V_2); + // Number exclusive to V_2 = 3 + // 16 + 3 = 19 + EXPECT_EQ(static_cast(data_v_2.getNumPackets()), 19); + + create::Data data_v_3(create::V_3); + // Number exclusive to V_3 = 13 + // 16 + 13 = 29 + EXPECT_EQ(static_cast(data_v_3.getNumPackets()), 29); + + create::Data data_v_all(create::V_ALL); + EXPECT_EQ(static_cast(data_v_all.getNumPackets()), 33); +} + +TEST(DataTest, GetPacket) +{ + // Get a packet exclusive to V_1 + create::Data data_v_1(create::V_1); + boost::shared_ptr v_1_packet_ptr = data_v_1.getPacket(create::ID_OVERCURRENTS); + EXPECT_NE(v_1_packet_ptr, boost::shared_ptr()) + << "ID_OVERCURRENTS packet not found for protocol V_1"; + EXPECT_EQ(static_cast(v_1_packet_ptr->nbytes), 1); + EXPECT_EQ(v_1_packet_ptr->info, std::string("overcurrents")); + + // Get a packet for V_2 + create::Data data_v_2(create::V_2); + boost::shared_ptr v_2_packet_ptr = data_v_2.getPacket(create::ID_DISTANCE); + EXPECT_NE(v_2_packet_ptr, boost::shared_ptr()) + << "ID_DISTANCE packet not found for protocol V_2"; + EXPECT_EQ(static_cast(v_2_packet_ptr->nbytes), 2); + EXPECT_EQ(v_2_packet_ptr->info, std::string("distance")); + + // Get a packet exclusive to V_3 + create::Data data_v_3(create::V_3); + boost::shared_ptr v_3_packet_ptr = data_v_3.getPacket(create::ID_LIGHT_FRONT_RIGHT); + EXPECT_NE(v_3_packet_ptr, boost::shared_ptr()) + << "ID_LIGHT_FRONT_RIGHT packet not found for protocol V_3"; + EXPECT_EQ(static_cast(v_3_packet_ptr->nbytes), 2); + EXPECT_EQ(v_3_packet_ptr->info, std::string("light_bumper_front_right")); + + // Get a non-existent packet + boost::shared_ptr not_a_packet_ptr = data_v_3.getPacket(60); + EXPECT_EQ(not_a_packet_ptr, boost::shared_ptr()); +} + +TEST(DataTest, GetPacketIDs) +{ + create::Data data_v_3(create::V_3); + const std::vector packet_ids = data_v_3.getPacketIDs(); + // Vector should have same length as reported by getNumPackets() + ASSERT_EQ(static_cast(packet_ids.size()), 29); + + // Vector should contain ID_LEFT_ENC + bool found = false; + for (std::size_t i = 0; (i < packet_ids.size()) && !found; i++) + { + if (packet_ids[i] == create::ID_LEFT_ENC) + { + found = true; + } + } + EXPECT_TRUE(found) << "ID_LEFT_ENC packet ID not returned for protocol V_3"; +} + +TEST(DataTest, GetTotalDataBytes) +{ + // All protocols have 20 mutual data bytes + // V_1 has an additional 6 bytes + create::Data data_v_1(create::V_1); + EXPECT_EQ(static_cast(data_v_1.getTotalDataBytes()), 26); + + // V_2 has an additional 5 bytes + create::Data data_v_2(create::V_2); + EXPECT_EQ(static_cast(data_v_2.getTotalDataBytes()), 25); + + // V_3 has an additional 21 bytes + create::Data data_v_3(create::V_3); + EXPECT_EQ(static_cast(data_v_3.getTotalDataBytes()), 41); +} + +TEST(DataTest, IsValidPacketID) +{ + create::Data data_v_1(create::V_1); + EXPECT_TRUE(data_v_1.isValidPacketID(create::ID_DIRT_DETECT_RIGHT)) + << "ID_DIRT_DETECT_RIGHT packet not found for protocol V_1"; + EXPECT_FALSE(data_v_1.isValidPacketID(create::ID_OI_MODE)) + << "ID_OI_MODE packet should not exist for protocol V_1"; + + // V_2 has an additional 5 bytes + create::Data data_v_2(create::V_2); + EXPECT_TRUE(data_v_2.isValidPacketID(create::ID_ANGLE)) + << "ID_ANGLE packet not found for protocol V_2"; + EXPECT_FALSE(data_v_2.isValidPacketID(create::ID_LIGHT)) + << "ID_LIGHT packet should not exist for protocol V_2"; + + // V_3 has an additional 21 bytes + create::Data data_v_3(create::V_3); + EXPECT_TRUE(data_v_3.isValidPacketID(create::ID_STASIS)) + << "ID_STATIS packet not found for protocol V_3"; + EXPECT_FALSE(data_v_3.isValidPacketID(create::ID_DISTANCE)) + << "ID_DISTANCE packet should not exist for protocol V_3"; +} + +TEST(DataTest, ValidateAll) +{ + create::Data data_v_3(create::V_3); + // Don't crash + data_v_3.validateAll(); +} diff --git a/tests/test_packet.cpp b/tests/test_packet.cpp new file mode 100644 index 0000000..f1482ce --- /dev/null +++ b/tests/test_packet.cpp @@ -0,0 +1,73 @@ +/** +Software License Agreement (BSD) + +\file test_packet.cpp +\authors Jacob Perron +\copyright Copyright (c) 2018, Autonomy Lab (Simon Fraser University), All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Autonomy Lab nor the names of its contributors may + be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +*/ +#include "create/packet.h" +#include "create/types.h" + +#include "gtest/gtest.h" + +TEST(PacketTest, Constructor) +{ + create::Packet empty_packet(0, std::string("")); + EXPECT_EQ(static_cast(empty_packet.nbytes), 0); + EXPECT_EQ(empty_packet.info, std::string("")); + + create::Packet some_packet(2, std::string("test_packet")); + EXPECT_EQ(static_cast(some_packet.nbytes), 2); + EXPECT_EQ(some_packet.info, std::string("test_packet")); +} + +TEST(PacketTest, SetValidateAndGetData) +{ + create::Packet packet(2, std::string("test_packet")); + + // Set some data and validate it + const uint16_t some_data = 123; + packet.setDataToValidate(some_data); + packet.validate(); + // Confirm data was validated + const uint16_t some_data_result = packet.getData(); + EXPECT_EQ(some_data_result, some_data); + + // Set zero data and validate it + const uint16_t zero_data = 0; + packet.setDataToValidate(zero_data); + packet.validate(); + // Confirm data was validated + const uint16_t zero_data_result = packet.getData(); + EXPECT_EQ(zero_data_result, zero_data); + + // Set some data but do not validate it + const uint16_t do_not_validate_data = 321; + packet.setDataToValidate(do_not_validate_data); + // Confirm data was not validated + const uint16_t unvalidated_data_result = packet.getData(); + EXPECT_NE(unvalidated_data_result, do_not_validate_data); +} diff --git a/tests/test_robot_model.cpp b/tests/test_robot_model.cpp new file mode 100644 index 0000000..7ddcc86 --- /dev/null +++ b/tests/test_robot_model.cpp @@ -0,0 +1,43 @@ +/** +Software License Agreement (BSD) + +\file test_robot_model.cpp +\authors Jacob Perron +\copyright Copyright (c) 2018, Autonomy Lab (Simon Fraser University), All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Autonomy Lab nor the names of its contributors may + be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +*/ +#include "create/types.h" + +#include "gtest/gtest.h" + +TEST(RobotModelTest, Equality) +{ + EXPECT_EQ(create::RobotModel::CREATE_1, create::RobotModel::CREATE_1); + EXPECT_EQ(create::RobotModel::CREATE_2, create::RobotModel::CREATE_2); + EXPECT_EQ(create::RobotModel::ROOMBA_400, create::RobotModel::ROOMBA_400); + EXPECT_NE(create::RobotModel::CREATE_1, create::RobotModel::CREATE_2); + EXPECT_NE(create::RobotModel::CREATE_1, create::RobotModel::ROOMBA_400); + EXPECT_NE(create::RobotModel::CREATE_2, create::RobotModel::ROOMBA_400); +} diff --git a/tests/test_serial_query.cpp b/tests/test_serial_query.cpp new file mode 100644 index 0000000..3527bb9 --- /dev/null +++ b/tests/test_serial_query.cpp @@ -0,0 +1,90 @@ +/** +Software License Agreement (BSD) + +\file test_serial_query.cpp +\authors Jacob Perron +\copyright Copyright (c) 2018, Autonomy Lab (Simon Fraser University), All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Autonomy Lab nor the names of its contributors may + be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +*/ +#include "create/data.h" +#include "create/serial_query.h" + +#include "gtest/gtest.h" + +#include + +TEST(SerialQueryTest, Constructor) +{ + boost::shared_ptr data_ptr = boost::make_shared(); + create::SerialQuery serial_query(data_ptr); +} + +TEST(SerialQueryTest, Connected) +{ + boost::shared_ptr data_ptr = boost::make_shared(); + create::SerialQuery serial_query(data_ptr); + + // Did not call connect and nothing to connect to, so expect false + EXPECT_FALSE(serial_query.connected()); +} + +TEST(SerialQueryTest, Disconnect) +{ + boost::shared_ptr data_ptr = boost::make_shared(); + create::SerialQuery serial_query(data_ptr); + + // Not connected, but should not fail + serial_query.disconnect(); +} + +TEST(SerialQueryTest, NumPackets) +{ + boost::shared_ptr data_ptr = boost::make_shared(); + create::SerialQuery serial_query(data_ptr); + + // Not connected, so zero packets should have been received + EXPECT_EQ(serial_query.getNumCorruptPackets(), 0); + EXPECT_EQ(serial_query.getTotalPackets(), 0); +} + +TEST(SerialQueryTest, Send) +{ + boost::shared_ptr data_ptr = boost::make_shared(); + create::SerialQuery serial_query(data_ptr); + + // Some bytes to send (to set date) + uint8_t bytes[4] = { create::OC_DATE, 0, 1, 2 }; + // Not connected, so failure expected + EXPECT_FALSE(serial_query.send(bytes, 4)); +} + +TEST(SerialQueryTest, SendOpcode) +{ + boost::shared_ptr data_ptr = boost::make_shared(); + create::SerialQuery serial_query(data_ptr); + + // Not connected, so failure expected + EXPECT_FALSE(serial_query.sendOpcode(create::OC_POWER)); +} diff --git a/tests/test_serial_stream.cpp b/tests/test_serial_stream.cpp new file mode 100644 index 0000000..7445939 --- /dev/null +++ b/tests/test_serial_stream.cpp @@ -0,0 +1,90 @@ +/** +Software License Agreement (BSD) + +\file test_serial_stream.cpp +\authors Jacob Perron +\copyright Copyright (c) 2018, Autonomy Lab (Simon Fraser University), All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + * Neither the name of Autonomy Lab nor the names of its contributors may + be used to endorse or promote products derived from this software without + specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. +*/ +#include "create/data.h" +#include "create/serial_stream.h" + +#include "gtest/gtest.h" + +#include + +TEST(SerialStreamTest, Constructor) +{ + boost::shared_ptr data_ptr = boost::make_shared(); + create::SerialStream serial_stream(data_ptr); +} + +TEST(SerialStreamTest, Connected) +{ + boost::shared_ptr data_ptr = boost::make_shared(); + create::SerialStream serial_stream(data_ptr); + + // Did not call connect and nothing to connect to, so expect false + EXPECT_FALSE(serial_stream.connected()); +} + +TEST(SerialStreamTest, Disconnect) +{ + boost::shared_ptr data_ptr = boost::make_shared(); + create::SerialStream serial_stream(data_ptr); + + // Not connected, but should not fail + serial_stream.disconnect(); +} + +TEST(SerialStreamTest, NumPackets) +{ + boost::shared_ptr data_ptr = boost::make_shared(); + create::SerialStream serial_stream(data_ptr); + + // Not connected, so zero packets should have been received + EXPECT_EQ(serial_stream.getNumCorruptPackets(), 0); + EXPECT_EQ(serial_stream.getTotalPackets(), 0); +} + +TEST(SerialStreamTest, Send) +{ + boost::shared_ptr data_ptr = boost::make_shared(); + create::SerialStream serial_stream(data_ptr); + + // Some bytes to send (to set date) + uint8_t bytes[4] = { create::OC_DATE, 0, 1, 2 }; + // Not connected, so failure expected + EXPECT_FALSE(serial_stream.send(bytes, 4)); +} + +TEST(SerialStreamTest, SendOpcode) +{ + boost::shared_ptr data_ptr = boost::make_shared(); + create::SerialStream serial_stream(data_ptr); + + // Not connected, so failure expected + EXPECT_FALSE(serial_stream.sendOpcode(create::OC_POWER)); +}