gtest_postconditions.cpp
Go to the documentation of this file.
1 #include <gtest/gtest.h>
2 #include <string>
5 
6 using namespace BT;
7 
8 TEST(PostConditions, BasicTest)
9 {
10  BehaviorTreeFactory factory;
11 
12  const std::string xml_text = R"(
13 
14  <root BTCPP_format="4" >
15  <BehaviorTree ID="MainTree">
16  <Sequence>
17  <Script code = "A:=1; B:=1; C:=1; D:=1" />
18 
19  <AlwaysSuccess _onSuccess="B=42"/>
20 
21  <ForceSuccess>
22  <AlwaysSuccess _failureIf="A!=0" _onFailure="C=42"/>
23  </ForceSuccess>
24 
25  <ForceSuccess>
26  <AlwaysFailure _onFailure="D=42"/>
27  </ForceSuccess>
28  </Sequence>
29  </BehaviorTree>
30  </root>)";
31 
32  auto tree = factory.createTreeFromText(xml_text);
33  const auto status = tree.tickWhileRunning();
34  ASSERT_EQ(status, NodeStatus::SUCCESS);
35 
36  ASSERT_EQ(tree.rootBlackboard()->get<int>("B"), 42);
37  ASSERT_EQ(tree.rootBlackboard()->get<int>("C"), 42);
38  ASSERT_EQ(tree.rootBlackboard()->get<int>("D"), 42);
39 }
40 
41 TEST(PostConditions, Issue539)
42 {
43  const std::string xml_text = R"(
44  <root BTCPP_format="4" >
45  <BehaviorTree ID="MainTree">
46  <Sequence>
47  <Script code = "x:=0; y:=0" />
48  <RetryUntilSuccessful num_attempts="5">
49  <AlwaysFailure _onFailure="x += 1" _post="y += 1" />
50  </RetryUntilSuccessful>
51  </Sequence>
52  </BehaviorTree>
53  </root>)";
54 
55  BehaviorTreeFactory factory;
56  auto tree = factory.createTreeFromText(xml_text);
57  const auto status = tree.tickWhileRunning();
58  ASSERT_EQ(status, NodeStatus::FAILURE);
59 
60  ASSERT_EQ(tree.rootBlackboard()->get<int>("x"), 5);
61  ASSERT_EQ(tree.rootBlackboard()->get<int>("y"), 5);
62 }
63 
64 TEST(PostConditions, Issue601)
65 {
66  const std::string xml_text = R"(
67  <root BTCPP_format="4" >
68  <BehaviorTree ID="test_tree">
69  <Sequence>
70  <Script code="test := 'start'"/>
71  <Parallel failure_count="1"
72  success_count="-1">
73  <Sleep msec="1000"
74  _onHalted="test = 'halted'"
75  _post="test = 'post'"/>
76  <AlwaysFailure/>
77  </Parallel>
78  </Sequence>
79  </BehaviorTree>
80  </root>)";
81 
82  BehaviorTreeFactory factory;
83  auto tree = factory.createTreeFromText(xml_text);
84  const auto status = tree.tickWhileRunning();
85 
86  ASSERT_EQ(tree.rootBlackboard()->get<std::string>("test"), "halted");
87 }
BT
Definition: ex01_wrap_legacy.cpp:29
basic_types.h
bt_factory.h
BT::Tree::tickWhileRunning
NodeStatus tickWhileRunning(std::chrono::milliseconds sleep_time=std::chrono::milliseconds(10))
Definition: bt_factory.cpp:609
BT::NodeStatus::FAILURE
@ FAILURE
BT::BehaviorTreeFactory::createTreeFromText
Tree createTreeFromText(const std::string &text, Blackboard::Ptr blackboard=Blackboard::create())
createTreeFromText will parse the XML directly from string. The XML needs to contain either a single ...
Definition: bt_factory.cpp:395
BT::BehaviorTreeFactory
The BehaviorTreeFactory is used to create instances of a TreeNode at run-time.
Definition: bt_factory.h:205
BT::NodeStatus::SUCCESS
@ SUCCESS
xml_text
static const char * xml_text
Definition: ex01_wrap_legacy.cpp:52
TEST
TEST(PostConditions, BasicTest)
Definition: gtest_postconditions.cpp:8


behaviortree_cpp_v4
Author(s): Davide Faconti
autogenerated on Fri Jun 28 2024 02:20:07