Skip to content

Commit 11a85d2

Browse files
committed
Tests for StateAndMode struct
Signed-off-by: Arne Nordmann <arne.nordmann@de.bosch.com>
1 parent 3cd0849 commit 11a85d2

6 files changed

Lines changed: 122 additions & 5 deletions

File tree

system_modes/CMakeLists.txt

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,14 @@ if(BUILD_TESTING)
118118
configure_file(test/modefiles.h.in ${CMAKE_CURRENT_BINARY_DIR}/system_modes/modefiles.h)
119119
include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
120120

121+
ament_add_gtest(test_state_and_mode_struct test/test_state_and_mode_struct.cpp)
122+
if(TARGET test_state_and_mode_struct)
123+
target_include_directories(test_state_and_mode_struct PUBLIC
124+
${rclcpp_INCLUDE_DIRS}
125+
)
126+
target_link_libraries(test_state_and_mode_struct mode)
127+
endif()
128+
121129
ament_add_gtest(test_default_mode test/test_default_mode.cpp)
122130
if(TARGET test_default_mode)
123131
target_include_directories(test_default_mode PUBLIC
@@ -134,7 +142,6 @@ if(BUILD_TESTING)
134142
target_link_libraries(test_mode mode)
135143
endif()
136144

137-
message(STATUS "CMAKE_CURRENT_BINARY_DIR: ${CMAKE_CURRENT_BINARY_DIR}/system_modes/")
138145
ament_add_gtest(test_mode_inference test/test_mode_inference.cpp)
139146
if(TARGET test_mode_inference)
140147
target_include_directories(test_mode_inference PUBLIC

system_modes/include/system_modes/mode_impl.hpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,17 @@ struct StateAndMode
6969
mode = sam.substr(dot + 1);
7070
} else {
7171
state = state_id_(sam);
72+
mode = "";
7273
}
7374
}
75+
76+
std::string as_string() const
77+
{
78+
if (mode.empty()) {
79+
return state_label_(state);
80+
}
81+
return state_label_(state) + "." + mode;
82+
}
7483
};
7584

7685
class ModeImpl

system_modes/include/system_modes/mode_manager.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ class ModeManager : public rclcpp::Node
122122
mode_request_pub_;
123123

124124
// Remember states and modes of the systems
125-
std::map<std::string, std::pair<unsigned int, std::string>> current_modes_;
125+
std::map<std::string, StateAndMode> current_modes_;
126126
};
127127

128128
} // namespace system_modes

system_modes/src/system_modes/mode_inference.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -494,6 +494,7 @@ ModeInference::read_modes_from_model(const string & model_path)
494494
if (param.get_name().compare("type") != 0) {
495495
string mode_name;
496496

497+
// Parse mode definitions
497498
std::size_t foundm = param.get_name().find("modes.");
498499
if (foundm != string::npos) {
499500
std::size_t foundmr = param.get_name().find(".", 6);

system_modes/src/system_modes/mode_manager.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ ModeManager::change_state(
343343
// Publish info about this request
344344
auto info = std::make_shared<TransitionEvent>();
345345
if (this->current_modes_.find(node_name) != this->current_modes_.end()) {
346-
info->start_state.label = this->current_modes_.at(node_name).first;
346+
info->start_state.label = this->current_modes_.at(node_name).state;
347347
}
348348
info->transition.label = transition_label_(transition_id);
349349
info->transition.id = transition_id;
@@ -407,7 +407,7 @@ ModeManager::change_mode(
407407

408408
this->current_modes_.emplace(
409409
node_name,
410-
std::make_pair(State::PRIMARY_STATE_ACTIVE, mode_name.c_str()));
410+
StateAndMode(State::PRIMARY_STATE_ACTIVE, mode_name.c_str()));
411411

412412
auto nodes = this->mode_inference_->get_nodes();
413413
if (std::find(nodes.begin(), nodes.end(), node_name) != nodes.end()) {
@@ -461,7 +461,7 @@ ModeManager::change_part_state(const string & node, unsigned int transition)
461461
info->transition.label = transition_label_(transition);
462462
info->transition.id = transition;
463463
if (this->current_modes_.find(node) != this->current_modes_.end()) {
464-
info->start_state.label = this->current_modes_.at(node).first;
464+
info->start_state.label = this->current_modes_.at(node).state;
465465
info->start_state.id = state_id_(info->start_state.label);
466466
}
467467
info->goal_state.id = goal_state_(info->transition.id);
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
// Copyright (c) 2018 - for information on the respective copyright owner
2+
// see the NOTICE file and/or the repository https://github.com/microros/system_modes
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
16+
#include <gtest/gtest.h>
17+
#include <lifecycle_msgs/msg/state.hpp>
18+
19+
#include <string>
20+
#include <iostream>
21+
22+
#include "system_modes/mode.hpp"
23+
24+
using std::string;
25+
using namespace std::string_literals;
26+
27+
using system_modes::StateAndMode;
28+
using lifecycle_msgs::msg::State;
29+
30+
class TestStateAndMode : public ::testing::Test
31+
{
32+
protected:
33+
void SetUp()
34+
{
35+
inactive.state = State::PRIMARY_STATE_INACTIVE;
36+
37+
active.state = State::PRIMARY_STATE_ACTIVE;
38+
39+
active_default.state = State::PRIMARY_STATE_ACTIVE;
40+
active_default.mode = "__DEFAULT__";
41+
42+
active_foo.state = State::PRIMARY_STATE_ACTIVE;
43+
active_foo.mode = "FOO";
44+
}
45+
46+
void TearDown()
47+
{
48+
}
49+
50+
StateAndMode inactive, active, active_default, active_foo;
51+
};
52+
53+
TEST_F(TestStateAndMode, comparison) {
54+
{
55+
EXPECT_EQ(inactive, inactive);
56+
EXPECT_EQ(active, active);
57+
EXPECT_EQ(active, active_default);
58+
59+
EXPECT_NE(active, inactive);
60+
EXPECT_NE(active, active_foo);
61+
EXPECT_NE(active_default, active_foo);
62+
EXPECT_NE(active_default, inactive);
63+
}
64+
}
65+
66+
TEST_F(TestStateAndMode, string_getter) {
67+
{
68+
EXPECT_EQ("active"s, active.as_string());
69+
EXPECT_EQ("active.__DEFAULT__"s, active_default.as_string());
70+
EXPECT_EQ("active.FOO"s, active_foo.as_string());
71+
EXPECT_EQ("inactive"s, inactive.as_string());
72+
}
73+
}
74+
75+
TEST_F(TestStateAndMode, string_setter) {
76+
{
77+
StateAndMode copy;
78+
79+
copy.from_string("active");
80+
EXPECT_EQ(active, copy);
81+
82+
copy.from_string("active.__DEFAULT__");
83+
EXPECT_EQ(active_default, copy);
84+
85+
copy.from_string("active.FOO");
86+
EXPECT_EQ(active_foo, copy);
87+
88+
copy.from_string("inactive");
89+
EXPECT_EQ(inactive, copy);
90+
91+
copy.from_string(active.as_string());
92+
EXPECT_EQ(active, copy);
93+
94+
copy.from_string(active_foo.as_string());
95+
EXPECT_EQ(active_foo, copy);
96+
97+
copy.from_string(inactive.as_string());
98+
EXPECT_EQ(inactive, copy);
99+
}
100+
}

0 commit comments

Comments
 (0)