-
Notifications
You must be signed in to change notification settings - Fork 688
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backport changes in 4.x and fix #643
- Loading branch information
1 parent
2fb1175
commit 51f92c4
Showing
5 changed files
with
105 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
#include <gtest/gtest.h> | ||
#include "behaviortree_cpp_v3/bt_factory.h" | ||
#include "test_helper.hpp" | ||
#include "behaviortree_cpp_v3/loggers/bt_cout_logger.h" | ||
|
||
using BT::NodeStatus; | ||
using std::chrono::milliseconds; | ||
|
||
class SleepNode : public BT::StatefulActionNode | ||
{ | ||
public: | ||
|
||
SleepNode(const std::string& name, const BT::NodeConfiguration& config): | ||
StatefulActionNode(name, config) {} | ||
|
||
NodeStatus onStart() override { | ||
count_ = 0; | ||
return NodeStatus::RUNNING; | ||
} | ||
|
||
NodeStatus onRunning() override { | ||
return ++count_ < 10 ? NodeStatus::RUNNING : NodeStatus::SUCCESS; | ||
} | ||
|
||
void onHalted() override {} | ||
|
||
static BT::PortsList providedPorts(){ | ||
return {}; | ||
} | ||
|
||
private: | ||
int count_ = 0; | ||
}; | ||
|
||
|
||
TEST(Reactive, TestLogging) | ||
{ | ||
using namespace BT; | ||
|
||
static const char* reactive_xml_text = R"( | ||
<root> | ||
<BehaviorTree ID="Main"> | ||
<ReactiveSequence> | ||
<TestA name="testA"/> | ||
<AlwaysSuccess name="success"/> | ||
<Sleep/> | ||
</ReactiveSequence> | ||
</BehaviorTree> | ||
</root> | ||
)"; | ||
|
||
BehaviorTreeFactory factory; | ||
|
||
factory.registerNodeType<SleepNode>("Sleep"); | ||
|
||
std::array<int, 1> counters; | ||
RegisterTestTick(factory, "Test", counters); | ||
|
||
auto tree = factory.createTreeFromText(reactive_xml_text); | ||
StdCoutLogger logger(tree); | ||
|
||
auto ret = tree.tickRootWhileRunning(); | ||
ASSERT_EQ(ret, NodeStatus::SUCCESS); | ||
|
||
int num_ticks = counters[0]; | ||
ASSERT_GE(num_ticks, 10); | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#ifndef TEST_HELPER_HPP | ||
#define TEST_HELPER_HPP | ||
|
||
#include "behaviortree_cpp_v3/bt_factory.h" | ||
|
||
inline BT::NodeStatus TestTick(int* tick_counter) | ||
{ | ||
(*tick_counter)++; | ||
return BT::NodeStatus::SUCCESS; | ||
} | ||
|
||
template <size_t N> inline | ||
void RegisterTestTick(BT::BehaviorTreeFactory& factory, const std::string& name_prefix, | ||
std::array<int, N>& tick_counters) | ||
{ | ||
for(size_t i=0; i<tick_counters.size(); i++) | ||
{ | ||
tick_counters[i] = false; | ||
char str[100]; | ||
sprintf(str, "%s%c", name_prefix.c_str(), char('A'+i ) ); | ||
int* counter_ptr = &(tick_counters[i]); | ||
factory.registerSimpleAction(str, std::bind(&TestTick, counter_ptr)); | ||
} | ||
} | ||
|
||
|
||
#endif // TEST_HELPER_HPP |