Skip to content

Commit 6a5152a

Browse files
committed
more test added
1 parent 0622f5d commit 6a5152a

File tree

2 files changed

+108
-2
lines changed

2 files changed

+108
-2
lines changed

tests/gtest_blackboard.cpp

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ TEST(ParserTest, Issue605_whitespaces)
371371
<Script code=" sub_value:=false " />
372372
</BehaviorTree>
373373
374-
<BehaviorTree ID="MyMainTree">
374+
<BehaviorTree ID="MainTree">
375375
<Sequence>
376376
<Script code=" my_value:=true " />
377377
<SubTree ID="MySubtree" sub_value="{my_value} "/>
@@ -380,7 +380,7 @@ TEST(ParserTest, Issue605_whitespaces)
380380
</root> )";
381381

382382
factory.registerBehaviorTreeFromText(xml_text);
383-
auto tree = factory.createTree("MyMainTree");
383+
auto tree = factory.createTree("MainTree");
384384
const auto status = tree.tickWhileRunning();
385385

386386
for(auto const& subtree: tree.subtrees)
@@ -391,3 +391,73 @@ TEST(ParserTest, Issue605_whitespaces)
391391
ASSERT_EQ(status, BT::NodeStatus::SUCCESS);
392392
ASSERT_EQ(false, tree.rootBlackboard()->get<bool>("my_value"));
393393
}
394+
395+
396+
class ComparisonNode : public BT::ConditionNode
397+
{
398+
public:
399+
400+
ComparisonNode(const std::string& name, const BT::NodeConfiguration& config):
401+
BT::ConditionNode(name, config) {}
402+
403+
static BT::PortsList providedPorts()
404+
{
405+
return {BT::InputPort<int32_t>("first"),
406+
BT::InputPort<int32_t>("second"),
407+
BT::InputPort<std::string>("operator")};
408+
}
409+
410+
BT::NodeStatus tick() override
411+
{
412+
int32_t firstValue = 0;
413+
int32_t secondValue = 0;
414+
std::string inputOperator;
415+
if (!getInput("first", firstValue) ||
416+
!getInput("second", secondValue) ||
417+
!getInput("operator", inputOperator))
418+
{
419+
throw RuntimeError("can't access input");
420+
}
421+
if( (inputOperator == "==" && firstValue == secondValue) ||
422+
(inputOperator == "!=" && firstValue != secondValue) ||
423+
(inputOperator == "<=" && firstValue <= secondValue) ||
424+
(inputOperator == ">=" && firstValue <= secondValue) ||
425+
(inputOperator == "<" && firstValue < secondValue) ||
426+
(inputOperator == ">" && firstValue < secondValue) )
427+
{
428+
return BT::NodeStatus::SUCCESS;
429+
}
430+
// skipping the rest of the implementation
431+
return BT::NodeStatus::FAILURE;
432+
}
433+
};
434+
435+
TEST(BlackboardTest, IssueSetBlackboard)
436+
{
437+
BT::BehaviorTreeFactory factory;
438+
439+
const std::string xml_text = R"(
440+
<root BTCPP_format="4" >
441+
<BehaviorTree ID="MySubtree">
442+
<ComparisonNode first="{value}" second="42" operator="==" />
443+
</BehaviorTree>
444+
445+
<BehaviorTree ID="MainTree">
446+
<Sequence>
447+
<SetBlackboard value="42" output_key="value" />
448+
<SubTree ID="MySubtree" value="{value} "/>
449+
</Sequence>
450+
</BehaviorTree>
451+
</root> )";
452+
453+
factory.registerNodeType<ComparisonNode>("ComparisonNode");
454+
factory.registerBehaviorTreeFromText(xml_text);
455+
auto tree = factory.createTree("MainTree");
456+
const auto status = tree.tickWhileRunning();
457+
458+
ASSERT_EQ(status, BT::NodeStatus::SUCCESS);
459+
ASSERT_EQ(42, tree.rootBlackboard()->get<int>("value"));
460+
}
461+
462+
463+

tests/gtest_reactive.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,40 @@ TEST(Reactive, Issue587)
8787
ASSERT_EQ(counters[0], 1);
8888
}
8989

90+
TEST(Reactive, PreTickHooks)
91+
{
92+
using namespace BT;
93+
94+
static const char* reactive_xml_text = R"(
95+
<root BTCPP_format="4" >
96+
<BehaviorTree ID="Main">
97+
<ReactiveSequence>
98+
<AlwaysFailure name="failureA"/>
99+
<AlwaysFailure name="failureB"/>
100+
<Sleep msec="100"/>
101+
</ReactiveSequence>
102+
</BehaviorTree>
103+
</root>
104+
)";
105+
106+
BehaviorTreeFactory factory;
107+
108+
auto tree = factory.createTreeFromText(reactive_xml_text);
109+
110+
TreeNode::PreTickCallback callback =
111+
[](TreeNode& node) -> NodeStatus {
112+
std::cout << node.name() << " callback" << std::endl;
113+
return NodeStatus::SUCCESS;
114+
};
115+
116+
tree.applyVisitor([&](TreeNode* node) -> void {
117+
if(auto dd = dynamic_cast<BT::AlwaysFailureNode*>(node)) {
118+
dd->setPreTickFunction(callback);
119+
}
120+
});
121+
122+
auto ret = tree.tickWhileRunning();
123+
ASSERT_EQ(ret, NodeStatus::SUCCESS);
124+
}
125+
90126

0 commit comments

Comments
 (0)