From 915ccdc964b886dd38b6036c608ff7cc29a97aab Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Thu, 31 Aug 2023 15:45:07 +0200 Subject: [PATCH] Add tests for BTRunLimit --- tests/test_run_limit.h | 87 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 tests/test_run_limit.h diff --git a/tests/test_run_limit.h b/tests/test_run_limit.h new file mode 100644 index 0000000..b5d4a2c --- /dev/null +++ b/tests/test_run_limit.h @@ -0,0 +1,87 @@ +/** + * test_run_limit.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_RUN_LIMIT_H +#define TEST_RUN_LIMIT_H + +#include "limbo_test.h" + +#include "modules/limboai/bt/tasks/bt_task.h" +#include "modules/limboai/bt/tasks/decorators/bt_run_limit.h" + +namespace TestRunLimit { + +TEST_CASE("[Modules][LimboAI] BTRunLimit") { + Ref lim = memnew(BTRunLimit); + + SUBCASE("When empty") { + ERR_PRINT_OFF; + CHECK(lim->execute(0.01666) == BTTask::FAILURE); + ERR_PRINT_ON; + } + + Ref task = memnew(BTTestAction); + lim->add_child(task); + + SUBCASE("With run limit set to 2") { + lim->set_run_limit(2); + + task->ret_status = BTTask::SUCCESS; + CHECK(lim->execute(0.01666) == BTTask::SUCCESS); + CHECK_STATUS_ENTRIES_TICKS_EXITS(task, BTTask::SUCCESS, 1, 1, 1); // * task executed + + SUBCASE("When the child task succeeds") { + task->ret_status = BTTask::SUCCESS; + CHECK(lim->execute(0.01666) == BTTask::SUCCESS); + CHECK_STATUS_ENTRIES_TICKS_EXITS(task, BTTask::SUCCESS, 2, 2, 2); // * task executed + } + SUBCASE("When the child task fails") { + task->ret_status = BTTask::FAILURE; + CHECK(lim->execute(0.01666) == BTTask::FAILURE); + CHECK_STATUS_ENTRIES_TICKS_EXITS(task, BTTask::FAILURE, 2, 2, 2); // * task executed + } + + task->ret_status = BTTask::SUCCESS; + + CHECK(lim->execute(0.01666) == BTTask::FAILURE); + CHECK_ENTRIES_TICKS_EXITS(task, 2, 2, 2); // * task not executed + + CHECK(lim->execute(0.01666) == BTTask::FAILURE); + CHECK_ENTRIES_TICKS_EXITS(task, 2, 2, 2); // * task not executed + } + + SUBCASE("When the child task takes more than one tick to finish") { + lim->set_run_limit(2); + + task->ret_status = BTTask::RUNNING; + CHECK(lim->execute(0.01666) == BTTask::RUNNING); + CHECK_STATUS_ENTRIES_TICKS_EXITS(task, BTTask::RUNNING, 1, 1, 0); + + task->ret_status = BTTask::SUCCESS; + CHECK(lim->execute(0.01666) == BTTask::SUCCESS); + CHECK_STATUS_ENTRIES_TICKS_EXITS(task, BTTask::SUCCESS, 1, 2, 1); + + task->ret_status = BTTask::RUNNING; + CHECK(lim->execute(0.01666) == BTTask::RUNNING); + CHECK_STATUS_ENTRIES_TICKS_EXITS(task, BTTask::RUNNING, 2, 3, 1); + + task->ret_status = BTTask::SUCCESS; + CHECK(lim->execute(0.01666) == BTTask::SUCCESS); + CHECK_STATUS_ENTRIES_TICKS_EXITS(task, BTTask::SUCCESS, 2, 4, 2); + + CHECK(lim->execute(0.01666) == BTTask::FAILURE); + CHECK_ENTRIES_TICKS_EXITS(task, 2, 4, 2); + } +} + +} //namespace TestRunLimit + +#endif // TEST_RUN_LIMIT_H