Skip to content

Commit

Permalink
more test added
Browse files Browse the repository at this point in the history
  • Loading branch information
facontidavide committed Jul 20, 2023
1 parent 0622f5d commit 6a5152a
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 2 deletions.
74 changes: 72 additions & 2 deletions tests/gtest_blackboard.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ TEST(ParserTest, Issue605_whitespaces)
<Script code=" sub_value:=false " />
</BehaviorTree>
<BehaviorTree ID="MyMainTree">
<BehaviorTree ID="MainTree">
<Sequence>
<Script code=" my_value:=true " />
<SubTree ID="MySubtree" sub_value="{my_value} "/>
Expand All @@ -380,7 +380,7 @@ TEST(ParserTest, Issue605_whitespaces)
</root> )";

factory.registerBehaviorTreeFromText(xml_text);
auto tree = factory.createTree("MyMainTree");
auto tree = factory.createTree("MainTree");
const auto status = tree.tickWhileRunning();

for(auto const& subtree: tree.subtrees)
Expand All @@ -391,3 +391,73 @@ TEST(ParserTest, Issue605_whitespaces)
ASSERT_EQ(status, BT::NodeStatus::SUCCESS);
ASSERT_EQ(false, tree.rootBlackboard()->get<bool>("my_value"));
}


class ComparisonNode : public BT::ConditionNode
{
public:

ComparisonNode(const std::string& name, const BT::NodeConfiguration& config):
BT::ConditionNode(name, config) {}

static BT::PortsList providedPorts()
{
return {BT::InputPort<int32_t>("first"),
BT::InputPort<int32_t>("second"),
BT::InputPort<std::string>("operator")};
}

BT::NodeStatus tick() override
{
int32_t firstValue = 0;
int32_t secondValue = 0;
std::string inputOperator;
if (!getInput("first", firstValue) ||
!getInput("second", secondValue) ||
!getInput("operator", inputOperator))
{
throw RuntimeError("can't access input");
}
if( (inputOperator == "==" && firstValue == secondValue) ||
(inputOperator == "!=" && firstValue != secondValue) ||
(inputOperator == "<=" && firstValue <= secondValue) ||
(inputOperator == ">=" && firstValue <= secondValue) ||
(inputOperator == "<" && firstValue < secondValue) ||
(inputOperator == ">" && firstValue < secondValue) )
{
return BT::NodeStatus::SUCCESS;
}
// skipping the rest of the implementation
return BT::NodeStatus::FAILURE;
}
};

TEST(BlackboardTest, IssueSetBlackboard)
{
BT::BehaviorTreeFactory factory;

const std::string xml_text = R"(
<root BTCPP_format="4" >
<BehaviorTree ID="MySubtree">
<ComparisonNode first="{value}" second="42" operator="==" />
</BehaviorTree>
<BehaviorTree ID="MainTree">
<Sequence>
<SetBlackboard value="42" output_key="value" />
<SubTree ID="MySubtree" value="{value} "/>
</Sequence>
</BehaviorTree>
</root> )";

factory.registerNodeType<ComparisonNode>("ComparisonNode");
factory.registerBehaviorTreeFromText(xml_text);
auto tree = factory.createTree("MainTree");
const auto status = tree.tickWhileRunning();

ASSERT_EQ(status, BT::NodeStatus::SUCCESS);
ASSERT_EQ(42, tree.rootBlackboard()->get<int>("value"));
}



36 changes: 36 additions & 0 deletions tests/gtest_reactive.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,40 @@ TEST(Reactive, Issue587)
ASSERT_EQ(counters[0], 1);
}

TEST(Reactive, PreTickHooks)
{
using namespace BT;

static const char* reactive_xml_text = R"(
<root BTCPP_format="4" >
<BehaviorTree ID="Main">
<ReactiveSequence>
<AlwaysFailure name="failureA"/>
<AlwaysFailure name="failureB"/>
<Sleep msec="100"/>
</ReactiveSequence>
</BehaviorTree>
</root>
)";

BehaviorTreeFactory factory;

auto tree = factory.createTreeFromText(reactive_xml_text);

TreeNode::PreTickCallback callback =
[](TreeNode& node) -> NodeStatus {
std::cout << node.name() << " callback" << std::endl;
return NodeStatus::SUCCESS;
};

tree.applyVisitor([&](TreeNode* node) -> void {
if(auto dd = dynamic_cast<BT::AlwaysFailureNode*>(node)) {
dd->setPreTickFunction(callback);
}
});

auto ret = tree.tickWhileRunning();
ASSERT_EQ(ret, NodeStatus::SUCCESS);
}


0 comments on commit 6a5152a

Please sign in to comment.