Testing: Add tests for BTSequence

This commit is contained in:
Serhii Snitsaruk 2023-08-29 16:12:09 +02:00
parent a625173786
commit 108526ec07
2 changed files with 222 additions and 0 deletions

View File

@ -0,0 +1,41 @@
/**
* bt_test_action.h
* =============================================================================
* Copyright 2021-2023 Serhii Snitsaruk
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
#ifndef BT_TEST_ACTION_H
#define BT_TEST_ACTION_H
#include "modules/limboai/bt/tasks/bt_action.h"
class BTTestAction : public BTAction {
GDCLASS(BTTestAction, BTAction);
private:
int ret_status = BTTask::SUCCESS;
public:
int num_entries = 0;
int num_ticks = 0;
int num_exits = 0;
protected:
virtual void _enter() override { num_entries += 1; }
virtual void _exit() override { num_exits += 1; }
virtual int _tick(double p_delta) override {
num_ticks += 1;
return ret_status;
}
public:
BTTestAction(int p_return_status) { ret_status = p_return_status; }
};
#endif // BT_TEST_ACTION_H

181
tests/test_sequence.h Normal file
View File

@ -0,0 +1,181 @@
/**
* test_sequence.h
* =============================================================================
* Copyright 2021-2023 Serhii Snitsaruk
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
* =============================================================================
*/
#ifndef TEST_SEQUENCE_H
#define TEST_SEQUENCE_H
#include "tests/test_macros.h"
#include "modules/limboai/bt/tasks/bt_task.h"
#include "modules/limboai/bt/tasks/composites/bt_sequence.h"
#include "modules/limboai/tests/tasks/bt_test_action.h"
namespace TestSequence {
TEST_CASE("[Modules][LimboAI] BTSequence when all return SUCCESS") {
Ref<BTSequence> seq = memnew(BTSequence);
Ref<BTTestAction> task1 = memnew(BTTestAction(BTTask::SUCCESS));
Ref<BTTestAction> task2 = memnew(BTTestAction(BTTask::SUCCESS));
Ref<BTTestAction> task3 = memnew(BTTestAction(BTTask::SUCCESS));
seq->add_child(task1);
seq->add_child(task2);
seq->add_child(task3);
REQUIRE(seq->get_child_count() == 3);
// * First execution.
CHECK(seq->execute(0.1666) == BTTask::SUCCESS);
CHECK(task1->get_status() == BTTask::SUCCESS);
CHECK(task2->get_status() == BTTask::SUCCESS);
CHECK(task3->get_status() == BTTask::SUCCESS);
CHECK(task1->num_entries == 1);
CHECK(task1->num_ticks == 1);
CHECK(task1->num_exits == 1);
CHECK(task2->num_entries == 1);
CHECK(task2->num_ticks == 1);
CHECK(task2->num_exits == 1);
CHECK(task3->num_entries == 1);
CHECK(task3->num_ticks == 1);
CHECK(task3->num_exits == 1);
// * Second execution.
CHECK(seq->execute(0.1666) == BTTask::SUCCESS);
CHECK(task1->get_status() == BTTask::SUCCESS);
CHECK(task2->get_status() == BTTask::SUCCESS);
CHECK(task3->get_status() == BTTask::SUCCESS);
CHECK(task1->num_entries == 2);
CHECK(task1->num_ticks == 2);
CHECK(task1->num_exits == 2);
CHECK(task2->num_entries == 2);
CHECK(task2->num_ticks == 2);
CHECK(task2->num_exits == 2);
CHECK(task3->num_entries == 2);
CHECK(task3->num_ticks == 2);
CHECK(task3->num_exits == 2);
}
TEST_CASE("[Modules][LimboAI] BTSequence when second returns FAILURE") {
Ref<BTSequence> seq = memnew(BTSequence);
Ref<BTTestAction> task1 = memnew(BTTestAction(BTTask::SUCCESS));
Ref<BTTestAction> task2 = memnew(BTTestAction(BTTask::FAILURE));
Ref<BTTestAction> task3 = memnew(BTTestAction(BTTask::SUCCESS));
seq->add_child(task1);
seq->add_child(task2);
seq->add_child(task3);
REQUIRE(seq->get_child_count() == 3);
// * First execution.
CHECK(seq->execute(0.1666) == BTTask::FAILURE);
CHECK(task1->get_status() == BTTask::SUCCESS);
CHECK(task2->get_status() == BTTask::FAILURE);
CHECK(task3->get_status() == BTTask::FRESH);
CHECK(task1->num_entries == 1);
CHECK(task1->num_ticks == 1);
CHECK(task1->num_exits == 1);
CHECK(task2->num_entries == 1);
CHECK(task2->num_ticks == 1);
CHECK(task2->num_exits == 1);
CHECK(task3->num_entries == 0);
CHECK(task3->num_ticks == 0);
CHECK(task3->num_exits == 0);
// * Second execution.
CHECK(seq->execute(0.1666) == BTTask::FAILURE);
CHECK(task1->get_status() == BTTask::SUCCESS);
CHECK(task2->get_status() == BTTask::FAILURE);
CHECK(task3->get_status() == BTTask::FRESH);
CHECK(task1->num_entries == 2);
CHECK(task1->num_ticks == 2);
CHECK(task1->num_exits == 2);
CHECK(task2->num_entries == 2);
CHECK(task2->num_ticks == 2);
CHECK(task2->num_exits == 2);
CHECK(task3->num_entries == 0);
CHECK(task3->num_ticks == 0);
CHECK(task3->num_exits == 0);
}
TEST_CASE("[Modules][LimboAI] BTSequence when second returns RUNNING") {
Ref<BTSequence> seq = memnew(BTSequence);
Ref<BTTestAction> task1 = memnew(BTTestAction(BTTask::SUCCESS));
Ref<BTTestAction> task2 = memnew(BTTestAction(BTTask::RUNNING));
Ref<BTTestAction> task3 = memnew(BTTestAction(BTTask::SUCCESS));
seq->add_child(task1);
seq->add_child(task2);
seq->add_child(task3);
REQUIRE(seq->get_child_count() == 3);
// * First execution.
CHECK(seq->execute(0.1666) == BTTask::RUNNING);
CHECK(task1->get_status() == BTTask::SUCCESS);
CHECK(task2->get_status() == BTTask::RUNNING);
CHECK(task3->get_status() == BTTask::FRESH);
CHECK(task1->num_entries == 1);
CHECK(task1->num_exits == 1);
CHECK(task1->num_ticks == 1);
CHECK(task2->num_entries == 1);
CHECK(task2->num_ticks == 1);
CHECK(task2->num_exits == 0);
CHECK(task3->num_entries == 0);
CHECK(task3->num_ticks == 0);
CHECK(task3->num_exits == 0);
// * Second execution.
CHECK(seq->execute(0.1666) == BTTask::RUNNING);
CHECK(task1->num_entries == 1);
CHECK(task1->num_exits == 1);
CHECK(task1->num_ticks == 1);
CHECK(task2->num_entries == 1);
CHECK(task2->num_ticks == 2);
CHECK(task2->num_exits == 0);
CHECK(task3->num_entries == 0);
CHECK(task3->num_ticks == 0);
CHECK(task3->num_exits == 0);
}
TEST_CASE("[Modules][LimboAI] BTSequence with no child tasks") {
Ref<BTSequence> seq = memnew(BTSequence);
REQUIRE(seq->get_child_count() == 0);
CHECK(seq->execute(0.1666) == BTTask::SUCCESS);
}
} //namespace TestSequence
#endif // TEST_SEQUENCE_H