Skip to content

Commit 43280de

Browse files
committed
Tests for mode handling
Signed-off-by: Nordmann Arne (CR/ADT3) <arne.nordmann@de.bosch.com>
1 parent 69826ea commit 43280de

5 files changed

Lines changed: 190 additions & 2 deletions

File tree

system_modes/CMakeLists.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ if(BUILD_TESTING)
109109
ament_lint_auto_find_test_dependencies()
110110

111111
set(MODE_FILE_CORRECT ${CMAKE_CURRENT_SOURCE_DIR}/test/test_modes.yaml)
112+
set(MODE_FILE_RULES ${CMAKE_CURRENT_SOURCE_DIR}/test/test_modes_rules.yaml)
112113
set(MODE_FILE_WRONG ${CMAKE_CURRENT_SOURCE_DIR}/test/test_modes_wrong.yaml)
113114
configure_file(test/modefiles.h.in ${CMAKE_CURRENT_BINARY_DIR}/system_modes/modefiles.h)
114115
include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR})
@@ -147,6 +148,15 @@ if(BUILD_TESTING)
147148
target_link_libraries(test_mode_inference mode)
148149
endif()
149150

151+
ament_add_gtest(test_mode_handling test/test_mode_handling.cpp)
152+
if(TARGET test_mode_handling)
153+
target_include_directories(test_mode_handling PUBLIC
154+
${rclcpp_INCLUDE_DIRS}
155+
${CMAKE_CURRENT_BINARY_DIR}/system_modes/
156+
)
157+
target_link_libraries(test_mode_handling mode)
158+
endif()
159+
150160
ament_add_gtest(test_mode_manager
151161
test/test_mode_manager.cpp
152162
src/system_modes/mode_manager.cpp)

system_modes/src/system_modes/mode_handling.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,7 @@ ModeHandling::get_rules_for(const std::string & system, const StateAndMode & tar
135135
{
136136
std::vector<ModeRule> rules;
137137
try {
138-
auto rulesmap = this->rules_[system];
139-
for (auto rule : rulesmap) {
138+
for (auto rule : this->rules_[system]) {
140139
if (target == rule.second.system_target) {
141140
rules.push_back(rule.second);
142141
}

system_modes/test/modefiles.h.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@
33
#include <string>
44

55
const static std::string MODE_FILE_CORRECT = "@MODE_FILE_CORRECT@";
6+
const static std::string MODE_FILE_RULES = "@MODE_FILE_RULES@";
67
const static std::string MODE_FILE_WRONG = "@MODE_FILE_WRONG@";
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
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+
#include <rclcpp/rclcpp.hpp>
16+
#include <lifecycle_msgs/msg/state.hpp>
17+
18+
#include <gtest/gtest.h>
19+
20+
#include <stdexcept>
21+
#include <string>
22+
#include <memory>
23+
#include <vector>
24+
25+
#include "system_modes/modefiles.h"
26+
#include "system_modes/mode_handling.hpp"
27+
28+
using std::string;
29+
using std::vector;
30+
using rclcpp::Parameter;
31+
32+
using system_modes::ModeHandling;
33+
using system_modes::StateAndMode;
34+
35+
using lifecycle_msgs::msg::State;
36+
37+
/*
38+
Testing parsing of mode files
39+
*/
40+
TEST(TestModeFilesParse, constructor) {
41+
ModeHandling * handling;
42+
43+
EXPECT_NO_THROW(handling = new ModeHandling(MODE_FILE_CORRECT));
44+
EXPECT_NO_THROW(handling = new ModeHandling(MODE_FILE_RULES));
45+
46+
EXPECT_THROW(
47+
handling = new ModeHandling("incorrect path"),
48+
std::runtime_error);
49+
50+
(void) handling;
51+
}
52+
53+
/*
54+
Testing parsing of mode files
55+
*/
56+
TEST(TestModeFilesParse, parse_rules) {
57+
ModeHandling * handling = new ModeHandling(MODE_FILE_RULES);
58+
59+
EXPECT_EQ(
60+
0u,
61+
handling->get_rules_for("system", StateAndMode(State::PRIMARY_STATE_ACTIVE, "")).size());
62+
EXPECT_EQ(
63+
2u,
64+
handling->get_rules_for("system", StateAndMode(State::PRIMARY_STATE_ACTIVE, "AA")).size());
65+
EXPECT_EQ(
66+
2u,
67+
handling->get_rules_for("system", StateAndMode(State::PRIMARY_STATE_ACTIVE, "BB")).size());
68+
}
69+
70+
/*
71+
Testing parsing of mode files
72+
*/
73+
TEST(TestModeFilesParse, test_rules) {
74+
ModeHandling * handling = new ModeHandling(MODE_FILE_RULES);
75+
76+
auto rules = handling->get_rules_for("system", StateAndMode(State::PRIMARY_STATE_ACTIVE, "AA"));
77+
78+
EXPECT_EQ("degrade_from_AA", rules[0].name);
79+
EXPECT_EQ("system", rules[0].system);
80+
EXPECT_EQ(StateAndMode(State::PRIMARY_STATE_ACTIVE, "AA"), rules[0].system_target);
81+
EXPECT_EQ("part0", rules[0].part);
82+
EXPECT_EQ(StateAndMode(State::PRIMARY_STATE_INACTIVE), rules[0].part_actual);
83+
EXPECT_EQ(StateAndMode(State::PRIMARY_STATE_ACTIVE), rules[0].new_system_target);
84+
85+
EXPECT_EQ("inactive_from_AA", rules[1].name);
86+
EXPECT_EQ("system", rules[1].system);
87+
EXPECT_EQ(StateAndMode(State::PRIMARY_STATE_ACTIVE, "AA"), rules[1].system_target);
88+
EXPECT_EQ("part1", rules[1].part);
89+
EXPECT_EQ(StateAndMode(State::PRIMARY_STATE_INACTIVE), rules[1].part_actual);
90+
EXPECT_EQ(StateAndMode(State::PRIMARY_STATE_INACTIVE), rules[1].new_system_target);
91+
92+
rules = handling->get_rules_for("system", StateAndMode(State::PRIMARY_STATE_ACTIVE, "BB"));
93+
94+
EXPECT_EQ("degrade_from_BB", rules[0].name);
95+
EXPECT_EQ("system", rules[0].system);
96+
EXPECT_EQ(StateAndMode(State::PRIMARY_STATE_ACTIVE, "BB"), rules[0].system_target);
97+
EXPECT_EQ("part0", rules[0].part);
98+
EXPECT_EQ(StateAndMode(State::PRIMARY_STATE_INACTIVE), rules[0].part_actual);
99+
EXPECT_EQ(StateAndMode(State::PRIMARY_STATE_ACTIVE), rules[0].new_system_target);
100+
101+
EXPECT_EQ("inactive_from_BB", rules[1].name);
102+
EXPECT_EQ("system", rules[1].system);
103+
EXPECT_EQ(StateAndMode(State::PRIMARY_STATE_ACTIVE, "BB"), rules[1].system_target);
104+
EXPECT_EQ("part1", rules[1].part);
105+
EXPECT_EQ(StateAndMode(State::PRIMARY_STATE_INACTIVE), rules[1].part_actual);
106+
EXPECT_EQ(StateAndMode(State::PRIMARY_STATE_INACTIVE), rules[1].new_system_target);
107+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# config/test
2+
---
3+
4+
system:
5+
ros__parameters:
6+
type: system
7+
parts:
8+
part0
9+
part1
10+
modes:
11+
__DEFAULT__:
12+
part0: inactive
13+
part1: active
14+
AA:
15+
part0: active.FOO
16+
part1: active.AAA
17+
BB:
18+
part0: active.BAR
19+
part1: active.BBB
20+
rules:
21+
degrade_from_AA:
22+
if_target: active.AA
23+
if_part: [part0, inactive]
24+
new_target: active.__DEFAULT__
25+
degrade_from_BB:
26+
if_target: active.BB
27+
if_part: [part0, inactive]
28+
new_target: active.__DEFAULT__
29+
inactive_from_AA:
30+
if_target: active.AA
31+
if_part: [part1, inactive]
32+
new_target: inactive
33+
inactive_from_BB:
34+
if_target: active.BB
35+
if_part: [part1, inactive]
36+
new_target: inactive
37+
38+
part0:
39+
ros__parameters:
40+
type: node
41+
modes:
42+
__DEFAULT__:
43+
ros__parameters:
44+
foo: 0.1
45+
bar: WARN
46+
FOO:
47+
ros__parameters:
48+
foo: 0.1
49+
bar: DBG
50+
BAR:
51+
ros__parameters:
52+
foo: 0.2
53+
bar: ERR
54+
55+
part1:
56+
ros__parameters:
57+
type: node
58+
modes:
59+
__DEFAULT__:
60+
ros__parameters:
61+
foo: 0.1
62+
bar: WARN
63+
AAA:
64+
ros__parameters:
65+
foo: 0.2
66+
bar: DBG
67+
BBB:
68+
ros__parameters:
69+
foo: 0.9
70+
bar: ERR
71+

0 commit comments

Comments
 (0)