gtest_switch.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 #include "action_test_node.h"
3 #include "condition_test_node.h"
7 
8 using BT::NodeStatus;
9 using std::chrono::milliseconds;
10 
11 static const char* xml_text = R"(
12 
13 <root BTCPP_format="4" >
14 
15  <BehaviorTree ID="MainTree">
16  <Switch3 name="simple_switch" variable="{my_var}" case_1="1" case_2="42 case_3="666" >
17  <AsyncActionTest name="action_1"/>
18  <AsyncActionTest name="action_42"/>
19  <AsyncActionTest name="action_666"/>
20  <AsyncActionTest name="action_default"/>
21  </Switch3>
22  </BehaviorTree>
23 </root>
24  )";
25 
27 {
28  auto status = node.executeTick();
29  while(status == BT::NodeStatus::RUNNING)
30  {
31  status = node.executeTick();
32  }
33  return status;
34 }
35 
36 struct SwitchTest : testing::Test
37 {
39  std::unique_ptr<Switch2> root;
45 
47  : action_1("action_1", milliseconds(200))
48  , action_42("action_42", milliseconds(200))
49  , action_def("action_default", milliseconds(200))
50  {
51  BT::PortsRemapping input;
52  input.insert(std::make_pair("variable", "{my_var}"));
53  input.insert(std::make_pair("case_1", "1"));
54  input.insert(std::make_pair("case_2", "42"));
55 
59 
60  root = std::make_unique<Switch2>("simple_switch", simple_switch_config_);
61 
62  root->addChild(&action_1);
63  root->addChild(&action_42);
64  root->addChild(&action_def);
65  }
67  {
68  root->halt();
69  }
70 };
71 
72 TEST_F(SwitchTest, DefaultCase)
73 {
74  BT::NodeStatus state = root->executeTick();
75 
76  ASSERT_EQ(NodeStatus::IDLE, action_1.status());
77  ASSERT_EQ(NodeStatus::IDLE, action_42.status());
78  ASSERT_EQ(NodeStatus::RUNNING, action_def.status());
79  ASSERT_EQ(NodeStatus::RUNNING, state);
80 
81  std::this_thread::sleep_for(milliseconds(300));
82  state = root->executeTick();
83 
84  ASSERT_EQ(NodeStatus::IDLE, action_1.status());
85  ASSERT_EQ(NodeStatus::IDLE, action_42.status());
86  ASSERT_EQ(NodeStatus::IDLE, action_def.status());
87  ASSERT_EQ(NodeStatus::SUCCESS, state);
88 }
89 
91 {
92  bb->set("my_var", "1");
93  BT::NodeStatus state = root->executeTick();
94 
95  ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
96  ASSERT_EQ(NodeStatus::IDLE, action_42.status());
97  ASSERT_EQ(NodeStatus::IDLE, action_def.status());
98  ASSERT_EQ(NodeStatus::RUNNING, state);
99 
100  std::this_thread::sleep_for(milliseconds(300));
101  state = root->executeTick();
102 
103  ASSERT_EQ(NodeStatus::IDLE, action_1.status());
104  ASSERT_EQ(NodeStatus::IDLE, action_42.status());
105  ASSERT_EQ(NodeStatus::IDLE, action_def.status());
106  ASSERT_EQ(NodeStatus::SUCCESS, state);
107 }
108 
110 {
111  bb->set("my_var", "42");
112  BT::NodeStatus state = root->executeTick();
113 
114  ASSERT_EQ(NodeStatus::IDLE, action_1.status());
115  ASSERT_EQ(NodeStatus::RUNNING, action_42.status());
116  ASSERT_EQ(NodeStatus::IDLE, action_def.status());
117  ASSERT_EQ(NodeStatus::RUNNING, state);
118 
119  std::this_thread::sleep_for(milliseconds(300));
120  state = root->executeTick();
121 
122  ASSERT_EQ(NodeStatus::IDLE, action_1.status());
123  ASSERT_EQ(NodeStatus::IDLE, action_42.status());
124  ASSERT_EQ(NodeStatus::IDLE, action_def.status());
125  ASSERT_EQ(NodeStatus::SUCCESS, state);
126 }
127 
128 TEST_F(SwitchTest, CaseNone)
129 {
130  bb->set("my_var", "none");
131  BT::NodeStatus state = root->executeTick();
132 
133  ASSERT_EQ(NodeStatus::IDLE, action_1.status());
134  ASSERT_EQ(NodeStatus::IDLE, action_42.status());
135  ASSERT_EQ(NodeStatus::RUNNING, action_def.status());
136  ASSERT_EQ(NodeStatus::RUNNING, state);
137 
138  std::this_thread::sleep_for(milliseconds(300));
139  state = root->executeTick();
140 
141  ASSERT_EQ(NodeStatus::IDLE, action_1.status());
142  ASSERT_EQ(NodeStatus::IDLE, action_42.status());
143  ASSERT_EQ(NodeStatus::IDLE, action_def.status());
144  ASSERT_EQ(NodeStatus::SUCCESS, state);
145 }
146 
147 TEST_F(SwitchTest, CaseSwitchToDefault)
148 {
149  bb->set("my_var", "1");
150  BT::NodeStatus state = root->executeTick();
151 
152  ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
153  ASSERT_EQ(NodeStatus::IDLE, action_42.status());
154  ASSERT_EQ(NodeStatus::IDLE, action_def.status());
155  ASSERT_EQ(NodeStatus::RUNNING, state);
156 
157  std::this_thread::sleep_for(milliseconds(20));
158  state = root->executeTick();
159  ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
160  ASSERT_EQ(NodeStatus::IDLE, action_42.status());
161  ASSERT_EQ(NodeStatus::IDLE, action_def.status());
162  ASSERT_EQ(NodeStatus::RUNNING, state);
163 
164  // Switch Node does not feels changes. Only when tick.
165  // (not reactive)
166  std::this_thread::sleep_for(milliseconds(20));
167  bb->set("my_var", "");
168  std::this_thread::sleep_for(milliseconds(20));
169  ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
170  ASSERT_EQ(NodeStatus::IDLE, action_42.status());
171  ASSERT_EQ(NodeStatus::IDLE, action_def.status());
172  ASSERT_EQ(NodeStatus::RUNNING, root->status());
173 
174  std::this_thread::sleep_for(milliseconds(20));
175  state = root->executeTick();
176  ASSERT_EQ(NodeStatus::IDLE, action_1.status());
177  ASSERT_EQ(NodeStatus::IDLE, action_42.status());
178  ASSERT_EQ(NodeStatus::RUNNING, action_def.status());
179  ASSERT_EQ(NodeStatus::RUNNING, state);
180 
181  std::this_thread::sleep_for(milliseconds(300));
182  state = root->executeTick();
183 
184  ASSERT_EQ(NodeStatus::IDLE, action_1.status());
185  ASSERT_EQ(NodeStatus::IDLE, action_42.status());
186  ASSERT_EQ(NodeStatus::IDLE, action_def.status());
187  ASSERT_EQ(NodeStatus::SUCCESS, root->status());
188 }
189 
190 TEST_F(SwitchTest, CaseSwitchToAction2)
191 {
192  bb->set("my_var", "1");
193  BT::NodeStatus state = root->executeTick();
194 
195  ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
196  ASSERT_EQ(NodeStatus::IDLE, action_42.status());
197  ASSERT_EQ(NodeStatus::IDLE, action_def.status());
198  ASSERT_EQ(NodeStatus::RUNNING, state);
199 
200  bb->set("my_var", "42");
201  std::this_thread::sleep_for(milliseconds(20));
202  state = root->executeTick();
203  ASSERT_EQ(NodeStatus::IDLE, action_1.status());
204  ASSERT_EQ(NodeStatus::RUNNING, action_42.status());
205  ASSERT_EQ(NodeStatus::IDLE, action_def.status());
206  ASSERT_EQ(NodeStatus::RUNNING, state);
207 
208  std::this_thread::sleep_for(milliseconds(300));
209  state = root->executeTick();
210 
211  ASSERT_EQ(NodeStatus::IDLE, action_1.status());
212  ASSERT_EQ(NodeStatus::IDLE, action_42.status());
213  ASSERT_EQ(NodeStatus::IDLE, action_def.status());
214  ASSERT_EQ(NodeStatus::SUCCESS, root->status());
215 }
216 
217 TEST_F(SwitchTest, ActionFailure)
218 {
219  bb->set("my_var", "1");
220  BT::NodeStatus state = root->executeTick();
221 
222  action_1.setExpectedResult(NodeStatus::FAILURE);
223 
224  ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
225  ASSERT_EQ(NodeStatus::IDLE, action_42.status());
226  ASSERT_EQ(NodeStatus::IDLE, action_def.status());
227  ASSERT_EQ(NodeStatus::RUNNING, state);
228 
229  std::this_thread::sleep_for(milliseconds(300));
230  state = root->executeTick();
231 
232  ASSERT_EQ(NodeStatus::FAILURE, state);
233  ASSERT_EQ(NodeStatus::IDLE, action_1.status());
234  ASSERT_EQ(NodeStatus::IDLE, action_42.status());
235  ASSERT_EQ(NodeStatus::IDLE, action_def.status());
236 }
xml_text
static const char * xml_text
Definition: gtest_switch.cpp:11
BT::TreeNode::executeTick
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
Definition: tree_node.cpp:71
TickWhileRunning
BT::NodeStatus TickWhileRunning(BT::TreeNode &node)
Definition: gtest_switch.cpp:26
BT::NodeConfig::input_ports
PortsRemapping input_ports
Definition: tree_node.h:83
BT::TreeNode
Abstract base class for Behavior Tree Nodes.
Definition: tree_node.h:118
SwitchTest::bb
BT::Blackboard::Ptr bb
Definition: gtest_switch.cpp:43
bt_factory.h
BT::AsyncActionTest
Definition: action_test_node.h:32
SwitchTest::action_def
BT::AsyncActionTest action_def
Definition: gtest_switch.cpp:42
SwitchTest::action_1
BT::AsyncActionTest action_1
Definition: gtest_switch.cpp:40
BT::Blackboard::Ptr
std::shared_ptr< Blackboard > Ptr
Definition: blackboard.h:35
BT::NodeConfig::blackboard
Blackboard::Ptr blackboard
Definition: tree_node.h:79
condition_test_node.h
SwitchTest::root
std::unique_ptr< Switch2 > root
Definition: gtest_switch.cpp:39
behavior_tree.h
SwitchTest::action_42
BT::AsyncActionTest action_42
Definition: gtest_switch.cpp:41
BT::SwitchNode
Definition: switch_node.h:50
BT::Blackboard::create
static Blackboard::Ptr create(Blackboard::Ptr parent={})
Definition: blackboard.h:63
BT::PortsRemapping
std::unordered_map< std::string, std::string > PortsRemapping
Definition: tree_node.h:43
BT::NodeStatus::RUNNING
@ RUNNING
action_test_node.h
SwitchTest::simple_switch_config_
BT::NodeConfig simple_switch_config_
Definition: gtest_switch.cpp:44
TEST_F
TEST_F(SwitchTest, DefaultCase)
Definition: gtest_switch.cpp:72
BT::NodeConfig
Definition: tree_node.h:73
SwitchTest::SwitchTest
SwitchTest()
Definition: gtest_switch.cpp:46
tree_node.h
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33
SwitchTest::~SwitchTest
~SwitchTest()
Definition: gtest_switch.cpp:66
SwitchTest
Definition: gtest_switch.cpp:36


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Dec 13 2024 03:19:16