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  {
40  root.addChild(&fal_conditions);
41  {
42  fal_conditions.addChild(&condition_1);
43  fal_conditions.addChild(&condition_2);
44  }
45  root.addChild(&action_1);
46  }
48  {
49  }
50 };
51 
52 /****************TESTS START HERE***************************/
53 
54 TEST_F(BehaviorTreeTest, Condition1ToFalseCondition2True)
55 {
56  condition_1.setExpectedResult(NodeStatus::FAILURE);
57  condition_2.setExpectedResult(NodeStatus::SUCCESS);
58 
60 
61  ASSERT_EQ(NodeStatus::RUNNING, state);
62  ASSERT_EQ(NodeStatus::SUCCESS, fal_conditions.status());
63  ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
64  ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
65  ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
66 }
67 
68 TEST_F(BehaviorTreeTest, Condition2ToFalseCondition1True)
69 {
70  condition_2.setExpectedResult(NodeStatus::FAILURE);
71  condition_1.setExpectedResult(NodeStatus::SUCCESS);
72 
74 
75  ASSERT_EQ(NodeStatus::RUNNING, state);
76  ASSERT_EQ(NodeStatus::SUCCESS, fal_conditions.status());
77  ASSERT_EQ(NodeStatus::IDLE, condition_1.status());
78  ASSERT_EQ(NodeStatus::IDLE, condition_2.status());
79  ASSERT_EQ(NodeStatus::RUNNING, action_1.status());
80 }
81 
82 TEST_F(BehaviorTreeTest, PrintWithStream)
83 {
84  // no stream parameter should go to default stream (std::cout)
86 
87  // verify value for with custom stream parameter
88  std::stringstream stream;
90  const auto string = stream.str();
91  std::string line;
92 
93  // first line is all dashes
94  ASSERT_FALSE(std::getline(stream, line, '\n').fail());
95  ASSERT_STREQ("----------------", line.c_str());
96 
97  // each line is the name of the node, indented by depth * 3 spaces
98  ASSERT_FALSE(std::getline(stream, line, '\n').fail());
99  ASSERT_STREQ(root.name().c_str(), line.c_str());
100 
101  ASSERT_FALSE(std::getline(stream, line, '\n').fail());
102  ASSERT_STREQ((" " + fal_conditions.name()).c_str(), line.c_str());
103 
104  ASSERT_FALSE(std::getline(stream, line, '\n').fail());
105  ASSERT_STREQ((" " + condition_1.name()).c_str(), line.c_str());
106 
107  ASSERT_FALSE(std::getline(stream, line, '\n').fail());
108  ASSERT_STREQ((" " + condition_2.name()).c_str(), line.c_str());
109 
110  ASSERT_FALSE(std::getline(stream, line, '\n').fail());
111  ASSERT_STREQ((" " + action_1.name()).c_str(), line.c_str());
112 
113  // last line is all dashes
114  ASSERT_FALSE(std::getline(stream, line, '\n').fail());
115  ASSERT_STREQ("----------------", line.c_str());
116 
117  // no more lines
118  ASSERT_TRUE(std::getline(stream, line, '\n').fail());
119 }
120 
121 int main(int argc, char** argv)
122 {
123  testing::InitGoogleTest(&argc, argv);
124  return RUN_ALL_TESTS();
125 }
const std::string & name() const
Name of the instance, not the type.
Definition: tree_node.cpp:73
BT::ConditionTestNode condition_2
Definition: gtest_tree.cpp:29
BT::ConditionTestNode condition_1
Definition: gtest_tree.cpp:28
BT::FallbackNode fal_conditions
Definition: gtest_tree.cpp:31
void setExpectedResult(NodeStatus res)
int main(int argc, char **argv)
Definition: gtest_tree.cpp:121
void printTreeRecursively(const TreeNode *root_node, std::ostream &stream=std::cout)
void addChild(TreeNode *child)
The method used to add nodes to the children vector.
NodeStatus status() const
Definition: tree_node.cpp:56
NodeStatus
Definition: basic_types.h:35
BT::SequenceNode root
Definition: gtest_tree.cpp:26
virtual BT::NodeStatus executeTick()
The method that should be used to invoke tick() and setStatus();.
Definition: tree_node.cpp:33
The FallbackNode is used to try different strategies, until one succeeds. If any child returns RUNNIN...
Definition: fallback_node.h:33
The SequenceNode is used to tick children in an ordered sequence. If any child returns RUNNING...
Definition: sequence_node.h:34
TEST_F(BehaviorTreeTest, Condition1ToFalseCondition2True)
Definition: gtest_tree.cpp:54
BT::AsyncActionTest action_1
Definition: gtest_tree.cpp:27


behaviotree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Tue May 4 2021 02:56:25