Skip to content

Commit

Permalink
added more tests on memory nodes (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
safoex authored Apr 28, 2024
1 parent 54d91ce commit 23def54
Show file tree
Hide file tree
Showing 2 changed files with 128 additions and 56 deletions.
92 changes: 92 additions & 0 deletions behavior_tree/test/unit/memory_nodes/fallback.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include "../include/behavior_tree/bt_base.h"
#include <gtest/gtest.h>


using namespace ::testing;
using namespace evo::behavior;
using namespace evo::behavior::bt_factory;


// Test the behavior of the FallbackMemory node when conditions are initially
// not met
TEST(BehaviorTreeTest, FallbackMemory) {
// Create a node that always succeeds
auto true_action_node = action([]() {}, "returns Success");

// Setup a condition node that initially returns Running to simulate ongoing
// operations
bool flag = false;
auto condition_node =
condition([&flag] { return flag ? Status::FAILURE : Status::RUNNING; });

// Create a fallback memory node with both nodes
auto fallback_memory_node =
fallback_memory("", condition_node, true_action_node);

// Initially, the condition node is not satisfied, so the fallback should
// return Running
ASSERT_EQ(Status::State((*fallback_memory_node)()), Status::RUNNING);

// Change condition to true, expecting fallback to succeed now
flag = true;
Status result = (*fallback_memory_node)();
ASSERT_EQ(Status::State(result), Status::SUCCESS);
}
// Test the behavior of the FallbackMemory node when conditions are initially met
TEST(BehaviorTreeTest, FallbackMemoryConditionsMet) {
// Create a node that always succeeds
auto true_action_node = action([]() {}, "returns Success");

// Setup three condition nodes that initially return Success, Running, and Failure
bool flag1 = false;
bool flag2 = true;
bool flag3 = false;
auto condition_node1 =
condition([&flag1] { return flag1 ? Status::FAILURE : Status::RUNNING; });
auto condition_node2 =
condition([&flag2] { return flag2 ? Status::FAILURE : Status::RUNNING; });
auto condition_node3 =
condition([&flag3] { return flag3 ? Status::SUCCESS : Status::FAILURE; });

// Create a fallback memory node with all three nodes
auto fallback_memory_node =
fallback_memory("", condition_node1, condition_node2, condition_node3);

// Initially, the condition nodes are satisfied, so the fallback should
// return Success
ASSERT_EQ(Status::State((*fallback_memory_node)()), Status::RUNNING);

flag1 = true;
flag2 = false;
flag3 = false;

ASSERT_EQ(Status::State((*fallback_memory_node)()), Status::RUNNING);

flag1 = true;
flag2 = true;
flag3 = false;

ASSERT_EQ(Status::State((*fallback_memory_node)()), Status::FAILURE);
ASSERT_EQ(Status::State((*fallback_memory_node)()), Status::FAILURE);
ASSERT_EQ(Status::State((*fallback_memory_node)()), Status::FAILURE);

}


// Test the behavior of the FallbackMemory node with multiple action nodes
TEST(BehaviorTreeTest, FallbackMemoryMultipleActions) {
// Create three action nodes, two that always fail and one that always
// succeeds
auto fail_condition_node1 =
condition([]() { return Status::Failure; }, "Condition 1 Fail");
auto fail_condition_node2 =
condition([]() { return Status::Failure; }, "Condition 2 Fail");
auto success_action_node = action([]() {}, "Action Success");

// Create a fallback memory node with the condition nodes and the action node
auto fallback_memory_node = fallback_memory(
"", fail_condition_node1, fail_condition_node2, success_action_node);

// The fallback should succeed because the last action node succeeds
ASSERT_EQ((*fallback_memory_node)(), Status::Success);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ using namespace ::testing;
using namespace evo::behavior;
using namespace evo::behavior::bt_factory;


// Test the behavior of the SequenceMemory node when conditions are initially
// not met
TEST(BehaviorTreeTest, SequenceMemory) {
Expand All @@ -32,32 +33,47 @@ TEST(BehaviorTreeTest, SequenceMemory) {
ASSERT_EQ(Status::State(result), Status::SUCCESS);
}

// Test the behavior of the FallbackMemory node when the primary condition fails
TEST(BehaviorTreeTest, FallbackMemory) {
// Create a node that always returns Success
// Test the behavior of the SequenceMemory node when conditions are initially met
TEST(BehaviorTreeTest, SequenceMemoryConditionsMet) {
// Create a node that always succeeds
auto true_action_node = action([]() {}, "returns Success");

// Setup a condition node that initially returns Failure when the flag is
// true, and Running otherwise
bool flag = false;
auto condition_node =
condition([&flag] { return flag ? Status::FAILURE : Status::RUNNING; });
// Setup three condition nodes that initially return Success, Running, and Failure
bool flag1 = true;
bool flag2 = false;
bool flag3 = true;
auto condition_node1 =
condition([&flag1] { return flag1 ? Status::SUCCESS : Status::RUNNING; });
auto condition_node2 =
condition([&flag2] { return flag2 ? Status::SUCCESS : Status::RUNNING; });
auto condition_node3 =
condition([&flag3] { return flag3 ? Status::SUCCESS : Status::FAILURE; });

// Create a fallback memory node that tries the condition node first, then the
// true action node
auto fallback_memory_node =
fallback_memory("", condition_node, true_action_node);
// Initially, the condition node is running, so the fallback should also
// return Running
ASSERT_EQ((*fallback_memory_node)(), Status::RUNNING);
// Create a sequence memory node with all three nodes
auto sequence_memory_node =
sequence_memory("", condition_node1, condition_node2, condition_node3);

// Initially, the condition nodes are satisfied, so the sequence should
// return Success
ASSERT_EQ(Status::State((*sequence_memory_node)()), Status::RUNNING);

flag1 = false;
flag2 = true;
flag3 = false;

ASSERT_EQ(Status::State((*sequence_memory_node)()), Status::FAILURE);

flag1 = false;
flag2 = true;
flag3 = true;

ASSERT_EQ(Status::State((*sequence_memory_node)()), Status::SUCCESS);
ASSERT_EQ(Status::State((*sequence_memory_node)()), Status::RUNNING);

// Change condition to true, then the condition node will fail and fallback to
// the true action node
flag = true;
Status result = (*fallback_memory_node)();
ASSERT_EQ(result, Status::SUCCESS);
}



// Test the reset behavior of the SequenceMemory node when nodes initially
// return Running
TEST(BehaviorTreeTest, SequenceMemoryReset) {
Expand Down Expand Up @@ -115,39 +131,3 @@ TEST(BehaviorTreeTest, SequenceMemoryMultipleActions) {
// The sequence should fail because the condition node fails
ASSERT_EQ((*sequence_memory_node)(), Status::Failure);
}

// Test the behavior of the FallbackMemory node with multiple action nodes
TEST(BehaviorTreeTest, FallbackMemoryMultipleActions) {
// Create three action nodes, two that always fail and one that always
// succeeds
auto fail_condition_node1 =
condition([]() { return Status::Failure; }, "Condition 1 Fail");
auto fail_condition_node2 =
condition([]() { return Status::Failure; }, "Condition 2 Fail");
auto success_action_node = action([]() {}, "Action Success");

// Create a fallback memory node with the condition nodes and the action node
auto fallback_memory_node = fallback_memory(
"", fail_condition_node1, fail_condition_node2, success_action_node);

// The fallback should succeed because the last action node succeeds
ASSERT_EQ((*fallback_memory_node)(), Status::Success);
}

// Test the behavior of the SequenceMemory node when all conditions are met from
// the start
TEST(BehaviorTreeTest, SequenceMemoryAllConditionsMet) {
// Create a node that always succeeds
auto true_action_node = action([]() {}, "returns Success");

// Setup a condition node that always returns Success
auto condition_node = condition([]() { return Status::Success; });

// Create a sequence memory node with the condition node and the action node
auto sequence_memory_node =
sequence_memory("", condition_node, true_action_node);

// Since the condition is met from the start, the sequence should return
// Success
ASSERT_EQ((*sequence_memory_node)(), Status::Success);
}

0 comments on commit 23def54

Please sign in to comment.