Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor clproto tests #75

Merged
merged 12 commits into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Release Versions:
- Use pytest to run Python bindings tests (#70)
- Rename State::initialize to State::reset (#73)
- Correct parameter map in impedance controller (#76)
- Refactor clproto tests (#75)

## 6.3.1

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
6.3.46
6.3.47
2 changes: 1 addition & 1 deletion demos/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic)
endif()

find_package(control_libraries 6.3.46 CONFIG REQUIRED)
find_package(control_libraries 6.3.47 CONFIG REQUIRED)

set(DEMOS_SCRIPTS
task_space_control_loop
Expand Down
2 changes: 1 addition & 1 deletion doxygen/doxygen.conf
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ PROJECT_NAME = "Control Libraries"
# could be handy for archiving the generated documentation or if some version
# control system is used.

PROJECT_NUMBER = 6.3.46
PROJECT_NUMBER = 6.3.47

# Using the PROJECT_BRIEF tag one can provide an optional one line description
# for a project that appears at the top of each page and should give viewer a
Expand Down
3 changes: 2 additions & 1 deletion protocol/clproto_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cmake_minimum_required(VERSION 3.15)

project(clproto VERSION 6.3.46)
project(clproto VERSION 6.3.47)

# Default to C99
if(NOT CMAKE_C_STANDARD)
Expand Down Expand Up @@ -89,6 +89,7 @@ install(EXPORT ${PROJECT_NAME}_targets
if (BUILD_TESTING)
file(GLOB_RECURSE TEST_SOURCES test/ test_*.cpp)
add_executable(test_${PROJECT_NAME} ${TEST_SOURCES})
include_directories(test/tests/include)
target_link_libraries(test_${PROJECT_NAME}
protobuf
${PROJECT_NAME}
Expand Down
41 changes: 41 additions & 0 deletions protocol/clproto_cpp/test/tests/include/test_encode_decode.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <gtest/gtest.h>
#include <state_representation/State.hpp>

#include "clproto.h"

namespace clproto {

template<typename T>
static void test_encode_decode(
const T& send_state, clproto::MessageType type, std::function<void(const T&, const T&)> test_func,
clproto::ParameterMessageType param_type = clproto::ParameterMessageType::UNKNOWN_PARAMETER
) {
std::string msg = clproto::encode(send_state);
EXPECT_TRUE(clproto::is_valid(msg));
EXPECT_EQ(clproto::check_message_type(msg), type);
EXPECT_EQ(clproto::check_parameter_message_type(msg), param_type);

T recv_state;
EXPECT_NO_THROW(clproto::decode<T>(msg));
EXPECT_TRUE(clproto::decode(msg, recv_state));
EXPECT_EQ(send_state.is_empty(), recv_state.is_empty());

test_func(send_state, recv_state);

auto send_state_ptr = make_shared_state(send_state);
msg = clproto::encode(send_state_ptr);
EXPECT_TRUE(clproto::is_valid(msg));
EXPECT_EQ(clproto::check_message_type(msg), type);
EXPECT_EQ(clproto::check_parameter_message_type(msg), param_type);

T recv_state_2;
auto recv_state_ptr = make_shared_state(recv_state_2);
EXPECT_NO_THROW(clproto::decode<std::shared_ptr<state_representation::State>>(msg));
EXPECT_TRUE(clproto::decode(msg, recv_state_ptr));

recv_state_2 = *std::dynamic_pointer_cast<T>(recv_state_ptr);
test_func(send_state, recv_state_2);
}
}// namespace clproto
101 changes: 23 additions & 78 deletions protocol/clproto_cpp/test/tests/test_cartesian_space_proto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <state_representation/space/cartesian/CartesianWrench.hpp>

#include "clproto.h"
#include "test_encode_decode.hpp"

using namespace state_representation;

Expand All @@ -16,91 +17,35 @@ static void test_cart_state_equal(const T& send_state, const T& recv_state) {
EXPECT_EQ(send_state.get_type(), recv_state.get_type());
EXPECT_STREQ(send_state.get_name().c_str(), recv_state.get_name().c_str());
EXPECT_STREQ(send_state.get_reference_frame().c_str(), recv_state.get_reference_frame().c_str());
EXPECT_NEAR(send_state.dist(recv_state), 0, 1e-5);
if (send_state) {
EXPECT_NEAR(send_state.dist(recv_state), 0, 1e-5);
}
}

template<typename T>
static void test_encode_decode_cartesian(const T& send_state, clproto::MessageType type) {
std::string msg = clproto::encode(send_state);
EXPECT_TRUE(clproto::is_valid(msg));
EXPECT_EQ(clproto::check_message_type(msg), type);

T recv_state;
EXPECT_NO_THROW(clproto::decode<T>(msg));
EXPECT_TRUE(clproto::decode(msg, recv_state));
EXPECT_FALSE(recv_state.is_empty());

test_cart_state_equal(send_state, recv_state);

auto send_state_ptr = make_shared_state(send_state);
msg = clproto::encode(send_state_ptr);
EXPECT_TRUE(clproto::is_valid(msg));
EXPECT_EQ(clproto::check_message_type(msg), type);

T recv_state_2;
auto recv_state_ptr = make_shared_state(recv_state_2);
EXPECT_NO_THROW(clproto::decode<std::shared_ptr<State>>(msg));
EXPECT_TRUE(clproto::decode(msg, recv_state_ptr));

recv_state_2 = *std::dynamic_pointer_cast<T>(recv_state_ptr);
test_cart_state_equal(send_state, recv_state_2);
}

template<typename T>
static void test_encode_decode_empty_cartesian(const T& state) {
EXPECT_TRUE(state.is_empty());
std::string msg;
EXPECT_NO_THROW(msg = clproto::encode(state));

T recv_state;
EXPECT_NO_THROW(recv_state = clproto::decode<T>(msg));
EXPECT_TRUE(recv_state.is_empty());
static void encode_decode_cartesian(T send_state, clproto::MessageType type) {
clproto::test_encode_decode<T>(send_state, type, test_cart_state_equal<T>);
send_state.reset();
clproto::test_encode_decode<T>(send_state, type, test_cart_state_equal<T>);
}

TEST(CartesianProtoTest, EncodeDecodeSpatialState) {
auto send_state = SpatialState("A", "B");
std::string msg = clproto::encode(send_state);
EXPECT_TRUE(clproto::is_valid(msg));
EXPECT_TRUE(clproto::check_message_type(msg) == clproto::SPATIAL_STATE_MESSAGE);

SpatialState recv_state;
EXPECT_NO_THROW(clproto::decode<SpatialState>(msg));
EXPECT_TRUE(clproto::decode(msg, recv_state));

EXPECT_EQ(send_state.get_type(), recv_state.get_type());
EXPECT_EQ(send_state.is_empty(), recv_state.is_empty());
EXPECT_STREQ(send_state.get_name().c_str(), recv_state.get_name().c_str());
EXPECT_STREQ(send_state.get_reference_frame().c_str(), recv_state.get_reference_frame().c_str());

std::shared_ptr<State> send_state_ptr = std::make_shared<SpatialState>(send_state);
msg = clproto::encode(send_state_ptr);
EXPECT_TRUE(clproto::is_valid(msg));
EXPECT_TRUE(clproto::check_message_type(msg) == clproto::SPATIAL_STATE_MESSAGE);

SpatialState recv_state_2;
auto recv_state_ptr = make_shared_state(recv_state_2);
EXPECT_NO_THROW(clproto::decode<std::shared_ptr<State>>(msg));
EXPECT_TRUE(clproto::decode(msg, recv_state_ptr));

recv_state_2 = *std::dynamic_pointer_cast<SpatialState>(recv_state_ptr);
EXPECT_EQ(send_state.get_type(), recv_state_2.get_type());
EXPECT_EQ(send_state.is_empty(), recv_state_2.is_empty());
EXPECT_STREQ(send_state.get_name().c_str(), recv_state_2.get_name().c_str());
EXPECT_STREQ(send_state.get_reference_frame().c_str(), recv_state_2.get_reference_frame().c_str());
}

TEST(CartesianProtoTest, EncodeDecodeRandomCartesian) {
test_encode_decode_cartesian(CartesianState::Random("A", "B"), clproto::CARTESIAN_STATE_MESSAGE);
test_encode_decode_cartesian(CartesianPose::Random("A", "B"), clproto::CARTESIAN_POSE_MESSAGE);
test_encode_decode_cartesian(CartesianTwist::Random("A", "B"), clproto::CARTESIAN_TWIST_MESSAGE);
test_encode_decode_cartesian(CartesianAcceleration::Random("A", "B"), clproto::CARTESIAN_ACCELERATION_MESSAGE);
test_encode_decode_cartesian(CartesianWrench::Random("A", "B"), clproto::CARTESIAN_WRENCH_MESSAGE);
clproto::test_encode_decode<SpatialState>(
send_state, clproto::SPATIAL_STATE_MESSAGE, [](
const SpatialState& send, const SpatialState& recv
) {
EXPECT_EQ(send.get_type(), recv.get_type());
EXPECT_STREQ(send.get_name().c_str(), recv.get_name().c_str());
EXPECT_STREQ(send.get_reference_frame().c_str(), recv.get_reference_frame().c_str());
}
);
}

TEST(CartesianProtoTest, EncodeDecodeEmptyCartesian) {
test_encode_decode_empty_cartesian(CartesianState());
test_encode_decode_empty_cartesian(CartesianPose());
test_encode_decode_empty_cartesian(CartesianTwist());
test_encode_decode_empty_cartesian(CartesianAcceleration());
test_encode_decode_empty_cartesian(CartesianWrench());
TEST(CartesianProtoTest, EncodeDecodeCartesian) {
encode_decode_cartesian(CartesianState::Random("A", "B"), clproto::CARTESIAN_STATE_MESSAGE);
encode_decode_cartesian(CartesianPose::Random("A", "B"), clproto::CARTESIAN_POSE_MESSAGE);
encode_decode_cartesian(CartesianTwist::Random("A", "B"), clproto::CARTESIAN_TWIST_MESSAGE);
encode_decode_cartesian(CartesianAcceleration::Random("A", "B"), clproto::CARTESIAN_ACCELERATION_MESSAGE);
encode_decode_cartesian(CartesianWrench::Random("A", "B"), clproto::CARTESIAN_WRENCH_MESSAGE);
}
105 changes: 19 additions & 86 deletions protocol/clproto_cpp/test/tests/test_joint_space_proto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <state_representation/space/joint/JointTorques.hpp>

#include "clproto.h"
#include "test_encode_decode.hpp"

using namespace state_representation;

Expand All @@ -19,7 +20,9 @@ static void test_joint_state_equal(const T& send_state, const T& recv_state) {
for (std::size_t ind = 0; ind < send_state.get_size(); ++ind) {
EXPECT_STREQ(send_state.get_names().at(ind).c_str(), recv_state.get_names().at(ind).c_str());
}
EXPECT_NEAR(send_state.dist(recv_state), 0, 1e-5);
if (send_state) {
EXPECT_NEAR(send_state.dist(recv_state), 0, 1e-5);
}
}

static void test_jacobian_equal(const Jacobian& send_state, const Jacobian& recv_state) {
Expand All @@ -33,99 +36,29 @@ static void test_jacobian_equal(const Jacobian& send_state, const Jacobian& recv
for (std::size_t ind = 0; ind < send_state.get_joint_names().size(); ++ind) {
EXPECT_STREQ(send_state.get_joint_names().at(ind).c_str(), recv_state.get_joint_names().at(ind).c_str());
}
EXPECT_NEAR(send_state.data().norm(), recv_state.data().norm(), 1e-5);
}

template<typename T>
static void test_encode_decode(const T& send_state, clproto::MessageType type) {
std::string msg = clproto::encode(send_state);
EXPECT_TRUE(clproto::is_valid(msg));
EXPECT_EQ(clproto::check_message_type(msg), type);

T recv_state;
EXPECT_NO_THROW(clproto::decode<T>(msg));
EXPECT_TRUE(clproto::decode(msg, recv_state));
EXPECT_FALSE(recv_state.is_empty());

test_joint_state_equal(send_state, recv_state);

auto send_state_ptr = make_shared_state(send_state);
msg = clproto::encode(send_state_ptr);
EXPECT_TRUE(clproto::is_valid(msg));
EXPECT_EQ(clproto::check_message_type(msg), type);

T recv_state_2;
auto recv_state_ptr = make_shared_state(recv_state_2);
EXPECT_NO_THROW(clproto::decode<std::shared_ptr<State>>(msg));
EXPECT_TRUE(clproto::decode(msg, recv_state_ptr));

recv_state_2 = *std::dynamic_pointer_cast<T>(recv_state_ptr);
test_joint_state_equal(send_state, recv_state_2);
if (send_state) {
EXPECT_NEAR(send_state.data().norm(), recv_state.data().norm(), 1e-5);
}
}

template<typename T>
static void test_encode_decode_empty_joint(const T& state) {
EXPECT_TRUE(state.is_empty());
std::string msg;
EXPECT_NO_THROW(msg = clproto::encode(state));

T recv_state;
EXPECT_NO_THROW(recv_state = clproto::decode<T>(msg));
EXPECT_TRUE(recv_state.is_empty());
static void encode_decode_joint(T send_state, clproto::MessageType type) {
clproto::test_encode_decode<T>(send_state, type, test_joint_state_equal<T>);
send_state.reset();
clproto::test_encode_decode<T>(send_state, type, test_joint_state_equal<T>);
}

TEST(JointProtoTest, EncodeDecodeRandomJoint) {
std::vector<std::string> joint_names = {"apple", "orange", "banana", "prune"};
auto send_state = JointState::Random("zeiss", joint_names);
test_encode_decode(send_state, clproto::JOINT_STATE_MESSAGE);
test_encode_decode(JointPositions::Random("robot", 3), clproto::JOINT_POSITIONS_MESSAGE);
test_encode_decode(JointVelocities::Random("robot", 3), clproto::JOINT_VELOCITIES_MESSAGE);
test_encode_decode(JointAccelerations::Random("robot", 3), clproto::JOINT_ACCELERATIONS_MESSAGE);
test_encode_decode(JointTorques::Random("robot", 3), clproto::JOINT_TORQUES_MESSAGE);
}

TEST(CartesianProtoTest, EncodeDecodeEmptyJoint) {
test_encode_decode_empty_joint(JointState());
test_encode_decode_empty_joint(JointPositions());
test_encode_decode_empty_joint(JointVelocities());
test_encode_decode_empty_joint(JointAccelerations());
test_encode_decode_empty_joint(JointTorques());
encode_decode_joint(JointState::Random("robot", {"one", "two", "three"}), clproto::JOINT_STATE_MESSAGE);
encode_decode_joint(JointPositions::Random("robot", {"one", "two", "three"}), clproto::JOINT_POSITIONS_MESSAGE);
encode_decode_joint(JointVelocities::Random("robot", {"one", "two", "three"}), clproto::JOINT_VELOCITIES_MESSAGE);
encode_decode_joint(JointAccelerations::Random("robot", {"one", "two", "three"}), clproto::JOINT_ACCELERATIONS_MESSAGE);
encode_decode_joint(JointTorques::Random("robot", {"one", "two", "three"}), clproto::JOINT_TORQUES_MESSAGE);
}

TEST(JointProtoTest, EncodeDecodeJacobian) {
auto send_state = Jacobian::Random("robot", {"one", "two", "three"}, "A", "B");
std::string msg = clproto::encode(send_state);
EXPECT_TRUE(clproto::is_valid(msg));
EXPECT_EQ(clproto::check_message_type(msg), clproto::JACOBIAN_MESSAGE);

Jacobian recv_state;
EXPECT_NO_THROW(clproto::decode<Jacobian>(msg));
EXPECT_TRUE(clproto::decode(msg, recv_state));
EXPECT_FALSE(recv_state.is_empty());

test_jacobian_equal(send_state, recv_state);

auto send_state_ptr = make_shared_state(send_state);
msg = clproto::encode(send_state_ptr);
EXPECT_TRUE(clproto::is_valid(msg));
EXPECT_EQ(clproto::check_message_type(msg), clproto::JACOBIAN_MESSAGE);

Jacobian recv_state_2;
auto recv_state_ptr = make_shared_state(recv_state_2);
EXPECT_NO_THROW(clproto::decode<std::shared_ptr<State>>(msg));
EXPECT_TRUE(clproto::decode(msg, recv_state_ptr));

recv_state_2 = *std::dynamic_pointer_cast<Jacobian>(recv_state_ptr);
test_jacobian_equal(send_state, recv_state_2);
}

TEST(JointProtoTest, EncodeDecodeEmptyJacobian) {
Jacobian empty_state;
EXPECT_TRUE(empty_state.is_empty());
std::string msg;
EXPECT_NO_THROW(msg = clproto::encode(empty_state));

Jacobian recv_state;
EXPECT_NO_THROW(recv_state = clproto::decode<Jacobian>(msg));
EXPECT_TRUE(recv_state.is_empty());
clproto::test_encode_decode<Jacobian>(send_state, clproto::JACOBIAN_MESSAGE, test_jacobian_equal);
send_state.reset();
clproto::test_encode_decode<Jacobian>(send_state, clproto::JACOBIAN_MESSAGE, test_jacobian_equal);
}
20 changes: 9 additions & 11 deletions protocol/clproto_cpp/test/tests/test_messages.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@
#include <state_representation/space/cartesian/CartesianPose.hpp>

#include "clproto.h"
#include "test_encode_decode.hpp"

using namespace state_representation;

TEST(MessageProtoTest, EncodeDecodeState) {
auto send_state = State("A");
std::string msg = clproto::encode(send_state);
EXPECT_TRUE(clproto::is_valid(msg));
EXPECT_TRUE(clproto::check_message_type(msg) == clproto::STATE_MESSAGE);

State recv_state;
EXPECT_NO_THROW(clproto::decode<State>(msg));
EXPECT_TRUE(clproto::decode(msg, recv_state));

EXPECT_EQ(send_state.is_empty(), recv_state.is_empty());
EXPECT_EQ(send_state.get_type(), recv_state.get_type());
EXPECT_STREQ(send_state.get_name().c_str(), recv_state.get_name().c_str());
clproto::test_encode_decode<State>(
send_state, clproto::STATE_MESSAGE, [](
const State& send, const State& recv
) {
EXPECT_EQ(send.get_type(), recv.get_type());
EXPECT_STREQ(send.get_name().c_str(), recv.get_name().c_str());
}
);
}

TEST(MessageProtoTest, EncodeDecodeInvalidState) {
Expand Down
Loading