gtest_tree.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2015-2017 Michele Colledanchise - All Rights Reserved
2 *
3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"),
4 * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
5 * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6 * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
9 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
10 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11 */
12 
13 #include <gtest/gtest.h>
14 #include "action_test_node.h"
15 #include "condition_test_node.h"
17 
18 #include <sstream>
19 #include <string>
20 
21 using BT::NodeStatus;
22 using std::chrono::milliseconds;
23 
24 struct BehaviorTreeTest : testing::Test
25 {
30 
32 
34  : root("root_sequence")
35  , action_1("action_1", milliseconds(100))
36  , condition_1("condition_1")
37  , condition_2("condition_2")
38  , fal_conditions("fallback_conditions")
39  {
41  {
44  }
46  }
48  {}
49 };
50 
51 /****************TESTS START HERE***************************/
52 
53 TEST_F(BehaviorTreeTest, Condition1ToFalseCondition2True)
54 {
55  condition_1.setExpectedResult(NodeStatus::FAILURE);
56  condition_2.setExpectedResult(NodeStatus::SUCCESS);
57 
58  BT::NodeStatus state = root.executeTick();
59 
60  ASSERT_EQ(NodeStatus::RUNNING, state);
61  ASSERT_EQ(NodeStatus::SUCCESS, fal_conditions.status());
62  ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
63  ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
64  ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
65 }
66 
67 TEST_F(BehaviorTreeTest, Condition2ToFalseCondition1True)
68 {
69  condition_2.setExpectedResult(NodeStatus::FAILURE);
70  condition_1.setExpectedResult(NodeStatus::SUCCESS);
71 
72  BT::NodeStatus state = root.executeTick();
73 
74  ASSERT_EQ(NodeStatus::RUNNING, state);
75  ASSERT_EQ(NodeStatus::SUCCESS, fal_conditions.status());
76  ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
77  ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
78  ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
79 }
80 
81 TEST_F(BehaviorTreeTest, PrintWithStream)
82 {
83  // no stream parameter should go to default stream (std::cout)
85 
86  // verify value for with custom stream parameter
87  std::stringstream stream;
88  BT::printTreeRecursively(&root, stream);
89  const auto string = stream.str();
90  std::string line;
91 
92  // first line is all dashes
93  ASSERT_FALSE(std::getline(stream, line, '\n').fail());
94  ASSERT_STREQ("----------------", line.c_str());
95 
96  // each line is the name of the node, indented by depth * 3 spaces
97  ASSERT_FALSE(std::getline(stream, line, '\n').fail());
98  ASSERT_STREQ(root.name().c_str(), line.c_str());
99 
100  ASSERT_FALSE(std::getline(stream, line, '\n').fail());
101  ASSERT_STREQ((" " + fal_conditions.name()).c_str(), line.c_str());
102 
103  ASSERT_FALSE(std::getline(stream, line, '\n').fail());
104  ASSERT_STREQ((" " + condition_1.name()).c_str(), line.c_str());
105 
106  ASSERT_FALSE(std::getline(stream, line, '\n').fail());
107  ASSERT_STREQ((" " + condition_2.name()).c_str(), line.c_str());
108 
109  ASSERT_FALSE(std::getline(stream, line, '\n').fail());
110  ASSERT_STREQ((" " + action_1.name()).c_str(), line.c_str());
111 
112  // last line is all dashes
113  ASSERT_FALSE(std::getline(stream, line, '\n').fail());
114  ASSERT_STREQ("----------------", line.c_str());
115 
116  // no more lines
117  ASSERT_TRUE(std::getline(stream, line, '\n').fail());
118 }
119 
120 int main(int argc, char** argv)
121 {
122  testing::InitGoogleTest(&argc, argv);
123  return RUN_ALL_TESTS();
124 }
BehaviorTreeTest::root
BT::SequenceNode root
Definition: gtest_tree.cpp:26
BehaviorTreeTest::condition_1
BT::ConditionTestNode condition_1
Definition: gtest_tree.cpp:28
BehaviorTreeTest
Definition: gtest_tree.cpp:24
BehaviorTreeTest::fal_conditions
BT::FallbackNode fal_conditions
Definition: gtest_tree.cpp:31
BehaviorTreeTest::condition_2
BT::ConditionTestNode condition_2
Definition: gtest_tree.cpp:29
BT::AsyncActionTest
Definition: action_test_node.h:32
main
int main(int argc, char **argv)
Definition: gtest_tree.cpp:120
BehaviorTreeTest::BehaviorTreeTest
BehaviorTreeTest()
Definition: gtest_tree.cpp:33
TEST_F
TEST_F(BehaviorTreeTest, Condition1ToFalseCondition2True)
Definition: gtest_tree.cpp:53
condition_test_node.h
BehaviorTreeTest::~BehaviorTreeTest
~BehaviorTreeTest()
Definition: gtest_tree.cpp:47
behavior_tree.h
BT::FallbackNode
The FallbackNode is used to try different strategies, until one succeeds. If any child returns RUNNIN...
Definition: fallback_node.h:32
BT::printTreeRecursively
void printTreeRecursively(const TreeNode *root_node, std::ostream &stream=std::cout)
Definition: behavior_tree.cpp:66
action_test_node.h
BT::SequenceNode
The SequenceNode is used to tick children in an ordered sequence. If any child returns RUNNING,...
Definition: sequence_node.h:34
BT::ConditionTestNode
Definition: condition_test_node.h:8
BehaviorTreeTest::action_1
BT::AsyncActionTest action_1
Definition: gtest_tree.cpp:27
BT::ControlNode::addChild
void addChild(TreeNode *child)
The method used to add nodes to the children vector.
Definition: control_node.cpp:22
BT::NodeStatus
NodeStatus
Definition: basic_types.h:33


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