1 #include <gtest/gtest.h>
9 TEST(PreconditionsDecorator, Integers)
12 std::array<int, 3> counters;
17 <root BTCPP_format="4" >
18 <BehaviorTree ID="MainTree">
20 <Script code = "A:=1; B:=1; C:=3" />
21 <Precondition if="A==B" else="FAILURE">
24 <Precondition if="A==C" else="SUCCESS">
27 <Precondition if="A!=C" else="FAILURE">
37 ASSERT_EQ(counters[0], 1);
38 ASSERT_EQ(counters[1], 0);
39 ASSERT_EQ(counters[2], 1);
42 TEST(PreconditionsDecorator, DoubleEquals)
45 std::array<int, 3> counters;
50 <root BTCPP_format="4" >
51 <BehaviorTree ID="MainTree">
53 <Script code = " A:=1.1; B:=(1.0+0.1); C:= 2.0 " />
55 <Precondition if="A==B" else="FAILURE">
59 <Precondition if="A==C" else="SUCCESS">
63 <Precondition if="A!=C" else="FAILURE">
73 ASSERT_EQ(counters[0], 1);
74 ASSERT_EQ(counters[1], 0);
75 ASSERT_EQ(counters[2], 1);
78 TEST(PreconditionsDecorator, StringEquals)
81 std::array<int, 2> counters;
86 <root BTCPP_format="4" >
87 <BehaviorTree ID="MainTree">
89 <Script code = "A:='hello'" />
90 <Script code = "B:='world'" />
91 <Script code = "C:='world'" />
93 <Precondition if=" A==B " else="SUCCESS">
96 <Precondition if=" B==C " else="FAILURE">
106 ASSERT_EQ(counters[0], 0);
107 ASSERT_EQ(counters[1], 1);
113 std::array<int, 4> counters;
118 <root BTCPP_format="4" >
119 <BehaviorTree ID="MainTree">
121 <Script code = "A:=1" />
122 <TestA _successIf= "A==1"/>
123 <TestB _successIf= "A==2"/>
125 <TestC _failureIf= "A==1"/>
126 <TestD _failureIf= "A!=1"/>
135 ASSERT_EQ(counters[0], 0);
136 ASSERT_EQ(counters[1], 1);
137 ASSERT_EQ(counters[2], 0);
138 ASSERT_EQ(counters[3], 1);
144 std::array<int, 3> counters;
149 <root BTCPP_format="4" >
150 <BehaviorTree ID="MainTree">
152 <TestA _skipIf="A!=1" />
153 <TestB _skipIf="A!=2" _onSuccess="A=1"/>
154 <TestC _skipIf="A!=3" _onSuccess="A=2"/>
160 tree.
subtrees.front()->blackboard->set(
"A", 3);
163 ASSERT_EQ(counters[0], 0);
164 ASSERT_EQ(counters[1], 0);
165 ASSERT_EQ(counters[2], 1);
168 ASSERT_EQ(counters[0], 0);
169 ASSERT_EQ(counters[1], 1);
170 ASSERT_EQ(counters[2], 1);
173 ASSERT_EQ(counters[0], 1);
174 ASSERT_EQ(counters[1], 1);
175 ASSERT_EQ(counters[2], 1);
187 for(
int i = 0; i < 10; i++)
190 setStatusRunningAndYield();
200 int times_ticked = 0;
210 <root BTCPP_format="4" >
211 <BehaviorTree ID="MainTree">
213 <Script code="A:=1" />
214 <CoroTest _skipIf="A==1" />
222 auto coro =
dynamic_cast<CoroTestNode*
>(tree.subtrees.front()->nodes.back().get());
223 ASSERT_EQ(coro->times_ticked, 0);
226 TEST(Preconditions, Issue615_NoSkipWhenRunning_A)
228 static constexpr
auto xml_text = R
"(
229 <root BTCPP_format="4">
231 <KeepRunningUntilFailure _skipIf="check == true">
233 </KeepRunningUntilFailure>
245 tree.rootBlackboard()->set(
"check",
true);
273 std::cout <<
"Node halted\n";
277 TEST(Preconditions, Issue615_NoSkipWhenRunning_B)
279 static constexpr
auto xml_text = R
"(
280 <root BTCPP_format="4">
282 <KeepRunning _skipIf="check==false"/>
291 tree.rootBlackboard()->set(
"check",
false);
295 tree.rootBlackboard()->set(
"check",
true);
299 tree.rootBlackboard()->set(
"check",
false);
312 return { OutputPort<bool>(
"output") };
317 setOutput(
"output",
true);
324 static constexpr
auto xml_text = R
"(
325 <root BTCPP_format="4">
327 <BehaviorTree ID="Main">
329 <SimpleOutput output="{param}" />
330 <Script code="value:=true" />
332 <SubTree ID="Sub1" param="{param}"/>
333 <SubTree ID="Sub1" param="{value}"/>
334 <SubTree ID="Sub1" param="true"/>
339 <BehaviorTree ID="Sub1">
341 <SubTree ID="Sub2" _skipIf="param != true" />
345 <BehaviorTree ID="Sub2">
353 std::array<int, 2> counters;
363 ASSERT_EQ(counters[0], 1);
364 ASSERT_EQ(counters[1], 3);
367 TEST(Preconditions, WhileCallsOnHalt)
369 static constexpr
auto xml_text = R
"(
370 <root BTCPP_format="4">
372 <BehaviorTree ID="Main">
374 <KeepRunning _while="A==1" _onHalted="B=69" />
387 tree.rootBlackboard()->set(
"B", 0);
388 auto status = tree.tickOnce();
391 ASSERT_EQ(tree.rootBlackboard()->get<
int>(
"B"), 0);
394 tree.rootBlackboard()->set(
"A", 0);
395 status = tree.tickOnce();
398 ASSERT_EQ(tree.rootBlackboard()->get<
int>(
"B"), 69);