limboai/tests/test_run_limit.h

102 lines
3.3 KiB
C
Raw Permalink Normal View History

2023-08-31 13:45:07 +00:00
/**
* test_run_limit.h
* =============================================================================
2024-03-21 20:38:57 +00:00
* Copyright 2021-2024 Serhii Snitsaruk
2023-08-31 13:45:07 +00:00
*
* 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<BTRunLimit> lim = memnew(BTRunLimit);
SUBCASE("When empty") {
ERR_PRINT_OFF;
CHECK(lim->execute(0.01666) == BTTask::FAILURE);
ERR_PRINT_ON;
}
Ref<BTTestAction> 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;
2024-02-10 19:14:49 +00:00
lim->set_count_policy(BTRunLimit::COUNT_FAILED);
2023-08-31 13:45:07 +00:00
CHECK(lim->execute(0.01666) == BTTask::SUCCESS);
CHECK_STATUS_ENTRIES_TICKS_EXITS(task, BTTask::SUCCESS, 2, 2, 2); // * task executed
2024-02-10 19:14:49 +00:00
lim->set_count_policy(BTRunLimit::COUNT_SUCCESSFUL);
CHECK(lim->execute(0.01666) == BTTask::SUCCESS);
CHECK_STATUS_ENTRIES_TICKS_EXITS(task, BTTask::SUCCESS, 3, 3, 3); // * task executed
2023-08-31 13:45:07 +00:00
}
SUBCASE("When the child task fails") {
task->ret_status = BTTask::FAILURE;
2024-02-10 19:14:49 +00:00
lim->set_count_policy(BTRunLimit::COUNT_SUCCESSFUL);
2023-08-31 13:45:07 +00:00
CHECK(lim->execute(0.01666) == BTTask::FAILURE);
CHECK_STATUS_ENTRIES_TICKS_EXITS(task, BTTask::FAILURE, 2, 2, 2); // * task executed
2024-02-10 19:14:49 +00:00
lim->set_count_policy(BTRunLimit::COUNT_FAILED);
CHECK(lim->execute(0.01666) == BTTask::FAILURE);
CHECK_STATUS_ENTRIES_TICKS_EXITS(task, BTTask::FAILURE, 3, 3, 3); // * task executed
2023-08-31 13:45:07 +00:00
}
task->ret_status = BTTask::SUCCESS;
2024-02-10 19:14:49 +00:00
lim->set_count_policy(BTRunLimit::COUNT_SUCCESSFUL);
2023-08-31 13:45:07 +00:00
CHECK(lim->execute(0.01666) == BTTask::FAILURE);
2024-02-10 19:14:49 +00:00
CHECK_ENTRIES_TICKS_EXITS(task, 3, 3, 3); // * task not executed
2023-08-31 13:45:07 +00:00
CHECK(lim->execute(0.01666) == BTTask::FAILURE);
2024-02-10 19:14:49 +00:00
CHECK_ENTRIES_TICKS_EXITS(task, 3, 3, 3); // * task not executed
2023-08-31 13:45:07 +00:00
}
SUBCASE("When the child task takes more than one tick to finish") {
lim->set_run_limit(2);
2024-02-10 19:14:49 +00:00
lim->set_count_policy(BTRunLimit::COUNT_ALL);
2023-08-31 13:45:07 +00:00
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