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 #include "environment.h"
18 
19 #include <sstream>
20 #include <string>
21 
22 using BT::NodeStatus;
23 using std::chrono::milliseconds;
24 
25 struct BehaviorTreeTest : testing::Test
26 {
31 
33 
35  root("root_sequence"),
36  action_1("action_1", milliseconds(100)),
37  condition_1("condition_1"),
38  condition_2("condition_2"),
39  fal_conditions("fallback_conditions")
40  {
42  {
45  }
47  }
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 
59  BT::NodeStatus state = root.executeTick();
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 
73  BT::NodeStatus state = root.executeTick();
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;
89  BT::printTreeRecursively(&root, 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 // define extern variable from environment.h
123 
124 int main(int argc, char** argv)
125 {
126  testing::InitGoogleTest(&argc, argv);
127 
128  // gtest will take ownership of this pointer and free it for us
129  environment = new Environment(argc, argv);
130  testing::AddGlobalTestEnvironment(environment);
131 
132  return RUN_ALL_TESTS();
133 }
BehaviorTreeTest::root
BT::SequenceNode root
Definition: gtest_tree.cpp:27
BehaviorTreeTest::condition_1
BT::ConditionTestNode condition_1
Definition: gtest_tree.cpp:29
BehaviorTreeTest
Definition: gtest_tree.cpp:25
BehaviorTreeTest::fal_conditions
BT::FallbackNode fal_conditions
Definition: gtest_tree.cpp:32
BehaviorTreeTest::condition_2
BT::ConditionTestNode condition_2
Definition: gtest_tree.cpp:30
BT::AsyncActionTest
Definition: action_test_node.h:32
main
int main(int argc, char **argv)
Definition: gtest_tree.cpp:124
BehaviorTreeTest::BehaviorTreeTest
BehaviorTreeTest()
Definition: gtest_tree.cpp:34
TEST_F
TEST_F(BehaviorTreeTest, Condition1ToFalseCondition2True)
Definition: gtest_tree.cpp:54
condition_test_node.h
BehaviorTreeTest::~BehaviorTreeTest
~BehaviorTreeTest()
Definition: gtest_tree.cpp:48
environment
Environment * environment
Definition: gtest_tree.cpp:122
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:68
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:33
environment.h
BT::ConditionTestNode
Definition: condition_test_node.h:8
BehaviorTreeTest::action_1
BT::AsyncActionTest action_1
Definition: gtest_tree.cpp:28
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:35
Environment
Definition: environment.h:8


behaviortree_cpp_v3
Author(s): Michele Colledanchise, Davide Faconti
autogenerated on Wed Jun 26 2024 02:51:19