saved work

This commit is contained in:
Ruixiang Du
2021-07-12 19:02:41 +08:00
parent 10d9fc734e
commit 6cbdebc38a
14 changed files with 337 additions and 93 deletions

23
test/CMakeLists.txt Normal file
View File

@@ -0,0 +1,23 @@
# Google tests
message(STATUS "Build unit tests with Google Test.")
add_subdirectory(googletest)
# reference: https://cliutils.gitlab.io/modern-cmake/chapters/testing/googletest.html
mark_as_advanced(
BUILD_GMOCK BUILD_GTEST BUILD_SHARED_LIBS
gmock_build_tests gtest_build_samples gtest_build_tests
gtest_disable_pthreads gtest_force_shared_crt gtest_hide_internal_symbols
)
# add unit tests
add_executable(utests
unit_tests/utest_scout_v1_command.cpp
unit_tests/utest_hunter_v1_command.cpp)
target_link_libraries(utests PRIVATE gtest gmock gtest_main ${PROJECT_NAME})
get_target_property(PRIVATE_HEADERS ${PROJECT_NAME} INCLUDE_DIRECTORIES)
target_include_directories(utests PRIVATE ${PRIVATE_HEADERS})
# gtest_discover_tests(utests)
# add_test(NAME gtest_all COMMAND utests)
# add_subdirectory(devel)

1
test/googletest Submodule

Submodule test/googletest added at 8d51ffdfab

View File

@@ -0,0 +1,106 @@
/*
* utest_hunter_v1_command.cpp
*
* Created on: Jul 12, 2021 17:28
* Description:
*
* Copyright (c) 2021 Weston Robot Pte. Ltd.
*/
#include <stdio.h>
#include <vector>
#include <iostream>
#include "gtest/gtest.h"
#include "ugv_sdk/details/protocol_v1/protocol_v1_parser.hpp"
#include "protocol_v1/agilex_protocol_v1.h"
using namespace westonrobot;
struct HunterV1CommandTest : testing::Test {
struct can_frame frame;
HunterProtocolV1Parser parser;
};
TEST_F(HunterV1CommandTest, MotionLinearCommandTest) {
AgxMessage msg;
msg.type = AgxMsgMotionCommandV1;
msg.body.v1_motion_command_msg.linear = 0.15;
msg.body.v1_motion_command_msg.angular = 0;
parser.EncodeMessage(&msg, &frame);
uint8_t expected_data[8] = {0x01, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x44};
ASSERT_EQ(frame.can_id, CAN_MSG_MOTION_COMMAND_ID);
ASSERT_EQ(frame.can_dlc, 8);
for (int i = 0; i < 6; ++i) {
// printf("expecting: %x, getting: %x\n", expected_data[i], frame.data[i]);
ASSERT_EQ(frame.data[i], expected_data[i]);
}
}
TEST_F(HunterV1CommandTest, MotionAngularCommandTest) {
AgxMessage msg;
msg.type = AgxMsgMotionCommandV1;
msg.body.v1_motion_command_msg.linear = 0.0;
msg.body.v1_motion_command_msg.angular = 2.55;
parser.EncodeMessage(&msg, &frame);
uint8_t expected_data[8] = {0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x01, 0x45};
ASSERT_EQ(frame.can_id, CAN_MSG_MOTION_COMMAND_ID);
ASSERT_EQ(frame.can_dlc, 8);
for (int i = 0; i < 6; ++i) {
// printf("expecting: %x, getting: %x\n", expected_data[i], frame.data[i]);
ASSERT_EQ(frame.data[i], expected_data[i]);
}
}
TEST_F(HunterV1CommandTest, ValueSetCommandTest) {
AgxMessage msg;
msg.type = AgxMsgValueSetCommandV1;
msg.body.v1_value_set_command_msg.set_neutral = true;
parser.EncodeMessage(&msg, &frame);
uint8_t expected_data[6] = {0xaa, 0x00, 0x00, 0x00, 0x00, 0x00};
ASSERT_EQ(frame.can_id, CAN_MSG_VALUE_SET_COMMAND_ID);
ASSERT_EQ(frame.can_dlc, 8);
for (int i = 0; i < 6; ++i) {
// printf("expecting: %x, getting: %x\n", expected_data[i], frame.data[i]);
ASSERT_EQ(frame.data[i], expected_data[i]);
}
}
TEST_F(HunterV1CommandTest, LightCommandTest) {
AgxMessage msg;
msg.type = AgxMsgLightCommand;
msg.body.light_command_msg.enable_cmd_ctrl = true;
msg.body.light_command_msg.front_light.mode = CONST_ON;
msg.body.light_command_msg.front_light.custom_value = 100;
msg.body.light_command_msg.rear_light.mode = CUSTOM;
msg.body.light_command_msg.rear_light.custom_value = 50;
parser.EncodeMessage(&msg, &frame);
uint8_t expected_data[8] = {0x01, 0x01, 0x64, 0x03, 0x32, 0x00, 0x00, 0xe4};
ASSERT_EQ(frame.can_id, CAN_MSG_LIGHT_COMMAND_ID);
ASSERT_EQ(frame.can_dlc, 8);
for (int i = 0; i < 6; ++i) {
// printf("expecting: %x, getting: %x\n", expected_data[i], frame.data[i]);
ASSERT_EQ(frame.data[i], expected_data[i]);
}
}

View File

@@ -0,0 +1,85 @@
/*
* utest_scout_v1_command.cpp
*
* Created on: Jul 12, 2021 16:37
* Description:
*
* Copyright (c) 2021 Weston Robot Pte. Ltd.
*/
#include <stdio.h>
#include <vector>
#include <iostream>
#include "gtest/gtest.h"
#include "ugv_sdk/details/protocol_v1/protocol_v1_parser.hpp"
#include "protocol_v1/agilex_protocol_v1.h"
using namespace westonrobot;
struct ScoutV1CommandTest : testing::Test {
struct can_frame frame;
ScoutProtocolV1Parser parser;
};
TEST_F(ScoutV1CommandTest, MotionLinearCommandTest) {
AgxMessage msg;
msg.type = AgxMsgMotionCommandV1;
msg.body.v1_motion_command_msg.linear = 0.15;
msg.body.v1_motion_command_msg.angular = 0;
parser.EncodeMessage(&msg, &frame);
uint8_t expected_data[8] = {0x01, 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x44};
ASSERT_EQ(frame.can_id, CAN_MSG_MOTION_COMMAND_ID);
ASSERT_EQ(frame.can_dlc, 8);
for (int i = 0; i < 8; ++i) {
ASSERT_EQ(frame.data[i], expected_data[i]);
}
}
TEST_F(ScoutV1CommandTest, MotionAngularCommandTest) {
AgxMessage msg;
msg.type = AgxMsgMotionCommandV1;
msg.body.v1_motion_command_msg.linear = 0.0;
msg.body.v1_motion_command_msg.angular = 0.05235;
parser.EncodeMessage(&msg, &frame);
uint8_t expected_data[8] = {0x01, 0x00, 0x00, 0x0a, 0x00, 0x00, 0x01, 0x45};
ASSERT_EQ(frame.can_id, CAN_MSG_MOTION_COMMAND_ID);
ASSERT_EQ(frame.can_dlc, 8);
for (int i = 0; i < 8; ++i) {
ASSERT_EQ(frame.data[i], expected_data[i]);
}
}
TEST_F(ScoutV1CommandTest, LightCommandTest) {
AgxMessage msg;
msg.type = AgxMsgLightCommand;
msg.body.light_command_msg.enable_cmd_ctrl = true;
msg.body.light_command_msg.front_light.mode = CONST_ON;
msg.body.light_command_msg.front_light.custom_value = 100;
msg.body.light_command_msg.rear_light.mode = CUSTOM;
msg.body.light_command_msg.rear_light.custom_value = 50;
parser.EncodeMessage(&msg, &frame);
uint8_t expected_data[8] = {0x01, 0x01, 0x64, 0x03, 0x32, 0x00, 0x00, 0xe4};
ASSERT_EQ(frame.can_id, CAN_MSG_LIGHT_COMMAND_ID);
ASSERT_EQ(frame.can_dlc, 8);
for (int i = 0; i < 8; ++i) {
// printf("expecting: %x, getting: %x\n", expected_data[i], frame.data[i]);
ASSERT_EQ(frame.data[i], expected_data[i]);
}
}