00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <gtest/gtest.h>
00014 #include "action_test_node.h"
00015 #include "condition_test_node.h"
00016 #include "behaviortree_cpp/behavior_tree.h"
00017
00018 using BT::NodeStatus;
00019
00020 struct SimpleSequenceTest : testing::Test
00021 {
00022 BT::SequenceNode root;
00023 BT::AsyncActionTest action;
00024 BT::ConditionTestNode condition;
00025
00026 SimpleSequenceTest() : root("root_sequence"), action("action"), condition("condition")
00027 {
00028 root.addChild(&condition);
00029 root.addChild(&action);
00030 }
00031 ~SimpleSequenceTest()
00032 {
00033 haltAllActions(&root);
00034 }
00035 };
00036
00037 struct ComplexSequenceTest : testing::Test
00038 {
00039 BT::SequenceNode root;
00040 BT::AsyncActionTest action_1;
00041 BT::ConditionTestNode condition_1;
00042 BT::ConditionTestNode condition_2;
00043
00044 BT::SequenceNode seq_conditions;
00045
00046 ComplexSequenceTest()
00047 : root("root_sequence")
00048 , action_1("action_1")
00049 , condition_1("condition_1")
00050 , condition_2("condition_2")
00051 , seq_conditions("sequence_conditions")
00052 {
00053 root.addChild(&seq_conditions);
00054 {
00055 seq_conditions.addChild(&condition_1);
00056 seq_conditions.addChild(&condition_2);
00057 }
00058 root.addChild(&action_1);
00059 }
00060 ~ComplexSequenceTest()
00061 {
00062 haltAllActions(&root);
00063 }
00064 };
00065
00066 struct SequenceTripleActionTest : testing::Test
00067 {
00068 BT::SequenceNode root;
00069 BT::ConditionTestNode condition;
00070 BT::AsyncActionTest action_1;
00071 BT::SyncActionTest action_2;
00072 BT::AsyncActionTest action_3;
00073
00074 SequenceTripleActionTest()
00075 : root("root_sequence")
00076 , condition("condition")
00077 , action_1("action_1")
00078 , action_2("action_2")
00079 , action_3("action_3")
00080 {
00081 root.addChild(&condition);
00082 root.addChild(&action_1);
00083 root.addChild(&action_2);
00084 root.addChild(&action_3);
00085 }
00086 ~SequenceTripleActionTest()
00087 {
00088 haltAllActions(&root);
00089 }
00090 };
00091
00092 struct ComplexSequence2ActionsTest : testing::Test
00093 {
00094 BT::SequenceNode root;
00095 BT::AsyncActionTest action_1;
00096 BT::AsyncActionTest action_2;
00097 BT::SequenceNode seq_1;
00098 BT::SequenceNode seq_2;
00099
00100 BT::ConditionTestNode condition_1;
00101 BT::ConditionTestNode condition_2;
00102
00103 ComplexSequence2ActionsTest()
00104 : root("root_sequence")
00105 , action_1("action_1")
00106 , action_2("action_2")
00107 , seq_1("sequence_1")
00108 , seq_2("sequence_2")
00109 , condition_1("condition_1")
00110 , condition_2("condition_2")
00111 {
00112 root.addChild(&seq_1);
00113 {
00114 seq_1.addChild(&condition_1);
00115 seq_1.addChild(&action_1);
00116 }
00117 root.addChild(&seq_2);
00118 {
00119 seq_2.addChild(&condition_2);
00120 seq_2.addChild(&action_2);
00121 }
00122 }
00123 ~ComplexSequence2ActionsTest()
00124 {
00125 haltAllActions(&root);
00126 }
00127 };
00128
00129 struct SimpleSequenceWithMemoryTest : testing::Test
00130 {
00131 BT::SequenceStarNode root;
00132 BT::AsyncActionTest action;
00133 BT::ConditionTestNode condition;
00134
00135 SimpleSequenceWithMemoryTest() : root("root_sequence"), action("action"), condition("condition")
00136 {
00137 root.addChild(&condition);
00138 root.addChild(&action);
00139 }
00140 ~SimpleSequenceWithMemoryTest()
00141 {
00142 haltAllActions(&root);
00143 }
00144 };
00145
00146 struct ComplexSequenceWithMemoryTest : testing::Test
00147 {
00148 BT::SequenceStarNode root;
00149
00150 BT::AsyncActionTest action_1;
00151 BT::AsyncActionTest action_2;
00152
00153 BT::ConditionTestNode condition_1;
00154 BT::ConditionTestNode condition_2;
00155
00156 BT::SequenceStarNode seq_conditions;
00157 BT::SequenceStarNode seq_actions;
00158
00159 ComplexSequenceWithMemoryTest()
00160 : root("root_sequence")
00161 , action_1("action_1")
00162 , action_2("action_2")
00163 , condition_1("condition_1")
00164 , condition_2("condition_2")
00165 , seq_conditions("sequence_conditions")
00166 , seq_actions("sequence_actions")
00167 {
00168 root.addChild(&seq_conditions);
00169 {
00170 seq_conditions.addChild(&condition_1);
00171 seq_conditions.addChild(&condition_2);
00172 }
00173 root.addChild(&seq_actions);
00174 {
00175 seq_actions.addChild(&action_1);
00176 seq_actions.addChild(&action_2);
00177 }
00178 }
00179 ~ComplexSequenceWithMemoryTest()
00180 {
00181 haltAllActions(&root);
00182 }
00183 };
00184
00185 struct SimpleParallelTest : testing::Test
00186 {
00187 BT::ParallelNode root;
00188 BT::AsyncActionTest action_1;
00189 BT::ConditionTestNode condition_1;
00190
00191 BT::AsyncActionTest action_2;
00192 BT::ConditionTestNode condition_2;
00193
00194 SimpleParallelTest()
00195 : root("root_parallel", 4)
00196 , action_1("action_1")
00197 , condition_1("condition_1")
00198 , action_2("action_2")
00199 , condition_2("condition_2")
00200 {
00201 root.addChild(&condition_1);
00202 root.addChild(&action_1);
00203 root.addChild(&condition_2);
00204 root.addChild(&action_2);
00205 }
00206 ~SimpleParallelTest()
00207 {
00208 haltAllActions(&root);
00209 }
00210 };
00211
00212
00213
00214 TEST_F(SimpleSequenceTest, ConditionTrue)
00215 {
00216 std::cout << "Ticking the root node !" << std::endl << std::endl;
00217
00218 BT::NodeStatus state = root.executeTick();
00219
00220 ASSERT_EQ(NodeStatus::RUNNING, action.status());
00221 ASSERT_EQ(NodeStatus::RUNNING, state);
00222 }
00223
00224 TEST_F(SimpleSequenceTest, ConditionTurnToFalse)
00225 {
00226 BT::NodeStatus state = root.executeTick();
00227 condition.setBoolean(false);
00228
00229 state = root.executeTick();
00230 ASSERT_EQ(NodeStatus::FAILURE, state);
00231 ASSERT_EQ(NodeStatus::IDLE, condition.status());
00232 ASSERT_EQ(NodeStatus::IDLE, action.status());
00233 }
00234
00235 TEST_F(ComplexSequenceTest, ComplexSequenceConditionsTrue)
00236 {
00237 BT::NodeStatus state = root.executeTick();
00238
00239 ASSERT_EQ(NodeStatus::RUNNING, state);
00240 ASSERT_EQ(NodeStatus::SUCCESS, seq_conditions.status());
00241 ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
00242 ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
00243 ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
00244 }
00245
00246 TEST_F(SequenceTripleActionTest, TripleAction)
00247 {
00248 using namespace BT;
00249 using namespace std::chrono;
00250 const auto timeout = system_clock::now() + milliseconds(650);
00251
00252 action_1.setTime(3);
00253 action_3.setTime(3);
00254
00255
00256
00257 NodeStatus state = root.executeTick();
00258
00259 ASSERT_EQ(NodeStatus::RUNNING, state);
00260 ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
00261 ASSERT_EQ(NodeStatus::IDLE, action_2.status());
00262 ASSERT_EQ(NodeStatus::IDLE, action_3.status());
00263
00264
00265 while (state != NodeStatus::SUCCESS && system_clock::now() < timeout)
00266 {
00267 std::this_thread::sleep_for(milliseconds(20));
00268 state = root.executeTick();
00269 }
00270
00271 ASSERT_EQ(NodeStatus::SUCCESS, state);
00272
00273
00274 ASSERT_NE(condition.tickCount(), 30);
00275
00276 ASSERT_EQ(action_1.tickCount(), 1);
00277 ASSERT_EQ(action_2.tickCount(), 1);
00278 ASSERT_EQ(action_3.tickCount(), 1);
00279
00280 ASSERT_EQ(NodeStatus::IDLE, action_1.status());
00281 ASSERT_EQ(NodeStatus::IDLE, action_2.status());
00282 ASSERT_EQ(NodeStatus::IDLE, action_3.status());
00283 ASSERT_TRUE(system_clock::now() < timeout);
00284 }
00285
00286 TEST_F(ComplexSequence2ActionsTest, ConditionsTrue)
00287 {
00288 BT::NodeStatus state = root.executeTick();
00289
00290 state = root.executeTick();
00291
00292 ASSERT_EQ(NodeStatus::RUNNING, state);
00293 ASSERT_EQ(NodeStatus::RUNNING, seq_1.status());
00294 ASSERT_EQ(NodeStatus::SUCCESS, condition_1.status());
00295 ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
00296 ASSERT_EQ(NodeStatus::IDLE, seq_2.status());
00297 ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
00298 ASSERT_EQ(NodeStatus::IDLE, action_2.status());
00299
00300 std::this_thread::sleep_for(std::chrono::milliseconds(500));
00301 state = root.executeTick();
00302
00303 ASSERT_EQ(NodeStatus::RUNNING, state);
00304 ASSERT_EQ(NodeStatus::SUCCESS, seq_1.status());
00305 ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
00306 ASSERT_EQ(NodeStatus::IDLE, action_1.status());
00307 ASSERT_EQ(NodeStatus::RUNNING, seq_2.status());
00308 ASSERT_EQ(NodeStatus::SUCCESS, condition_2.status());
00309 ASSERT_EQ(NodeStatus::RUNNING, action_2.status());
00310
00311 state = root.executeTick();
00312 }
00313
00314 TEST_F(ComplexSequenceTest, ComplexSequenceConditions1ToFalse)
00315 {
00316 BT::NodeStatus state = root.executeTick();
00317
00318 condition_1.setBoolean(false);
00319
00320 state = root.executeTick();
00321
00322 ASSERT_EQ(NodeStatus::FAILURE, state);
00323 ASSERT_EQ(NodeStatus::IDLE, seq_conditions.status());
00324 ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
00325 ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
00326 ASSERT_EQ(NodeStatus::IDLE, action_1.status());
00327 }
00328
00329 TEST_F(ComplexSequenceTest, ComplexSequenceConditions2ToFalse)
00330 {
00331 BT::NodeStatus state = root.executeTick();
00332
00333 condition_2.setBoolean(false);
00334
00335 state = root.executeTick();
00336
00337 ASSERT_EQ(NodeStatus::FAILURE, state);
00338 ASSERT_EQ(NodeStatus::IDLE, seq_conditions.status());
00339 ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
00340 ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
00341 ASSERT_EQ(NodeStatus::IDLE, action_1.status());
00342 }
00343
00344 TEST_F(SimpleSequenceWithMemoryTest, ConditionTrue)
00345 {
00346 BT::NodeStatus state = root.executeTick();
00347 std::this_thread::sleep_for(std::chrono::milliseconds(100));
00348
00349 ASSERT_EQ(NodeStatus::RUNNING, state);
00350 ASSERT_EQ(NodeStatus::SUCCESS, condition.status());
00351 ASSERT_EQ(NodeStatus::RUNNING, action.status());
00352 }
00353
00354 TEST_F(SimpleSequenceWithMemoryTest, ConditionTurnToFalse)
00355 {
00356 BT::NodeStatus state = root.executeTick();
00357
00358 ASSERT_EQ(NodeStatus::RUNNING, state);
00359 ASSERT_EQ(NodeStatus::SUCCESS, condition.status());
00360 ASSERT_EQ(NodeStatus::RUNNING, action.status());
00361
00362 condition.setBoolean(false);
00363 state = root.executeTick();
00364
00365 ASSERT_EQ(NodeStatus::RUNNING, state);
00366 ASSERT_EQ(NodeStatus::SUCCESS, condition.status());
00367 ASSERT_EQ(NodeStatus::RUNNING, action.status());
00368 }
00369
00370 TEST_F(ComplexSequenceWithMemoryTest, ConditionsTrue)
00371 {
00372 BT::NodeStatus state = root.executeTick();
00373
00374 ASSERT_EQ(NodeStatus::RUNNING, state);
00375 ASSERT_EQ(NodeStatus::SUCCESS, seq_conditions.status());
00376 ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
00377 ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
00378 ASSERT_EQ(NodeStatus::RUNNING, seq_actions.status());
00379 ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
00380 ASSERT_EQ(NodeStatus::IDLE, action_2.status());
00381 }
00382
00383 TEST_F(ComplexSequenceWithMemoryTest, Conditions1ToFase)
00384 {
00385 BT::NodeStatus state = root.executeTick();
00386
00387 condition_1.setBoolean(false);
00388 state = root.executeTick();
00389
00390
00391 ASSERT_EQ(NodeStatus::RUNNING, state);
00392 ASSERT_EQ(NodeStatus::SUCCESS, seq_conditions.status());
00393 ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
00394 ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
00395 ASSERT_EQ(NodeStatus::RUNNING, seq_actions.status());
00396 ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
00397 ASSERT_EQ(NodeStatus::IDLE, action_2.status());
00398 }
00399
00400 TEST_F(ComplexSequenceWithMemoryTest, Conditions2ToFalse)
00401 {
00402 BT::NodeStatus state = root.executeTick();
00403
00404 condition_2.setBoolean(false);
00405 state = root.executeTick();
00406
00407
00408 ASSERT_EQ(NodeStatus::RUNNING, state);
00409 ASSERT_EQ(NodeStatus::SUCCESS, seq_conditions.status());
00410 ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
00411 ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
00412 ASSERT_EQ(NodeStatus::RUNNING, seq_actions.status());
00413 ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
00414 ASSERT_EQ(NodeStatus::IDLE, action_2.status());
00415 }
00416
00417 TEST_F(ComplexSequenceWithMemoryTest, Action1DoneSeq)
00418 {
00419 root.executeTick();
00420
00421 condition_2.setBoolean(false);
00422 root.executeTick();
00423
00424
00425
00426 ASSERT_EQ(NodeStatus::SUCCESS, seq_conditions.status());
00427 ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
00428 ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
00429 ASSERT_EQ(NodeStatus::RUNNING, seq_actions.status());
00430 ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
00431 ASSERT_EQ(NodeStatus::IDLE, action_2.status());
00432
00433 std::this_thread::sleep_for(std::chrono::milliseconds(400));
00434 root.executeTick();
00435
00436 ASSERT_EQ(NodeStatus::SUCCESS, seq_conditions.status());
00437 ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
00438 ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
00439 ASSERT_EQ(NodeStatus::RUNNING, seq_actions.status());
00440 ASSERT_EQ(NodeStatus::SUCCESS, action_1.status());
00441 ASSERT_EQ(NodeStatus::RUNNING, action_2.status());
00442
00443 std::this_thread::sleep_for(std::chrono::milliseconds(400));
00444 root.executeTick();
00445
00446 ASSERT_EQ(NodeStatus::SUCCESS, root.status());
00447 ASSERT_EQ(NodeStatus::IDLE, seq_conditions.status());
00448 ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
00449 ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
00450 ASSERT_EQ(NodeStatus::IDLE, seq_actions.status());
00451 ASSERT_EQ(NodeStatus::IDLE, action_1.status());
00452 ASSERT_EQ(NodeStatus::IDLE, action_2.status());
00453 }