Add tests for BTProbability

This commit is contained in:
Serhii Snitsaruk 2023-08-31 13:41:03 +02:00
parent b9fc99cb2c
commit 6cf4045e0f
2 changed files with 97 additions and 0 deletions

View File

@ -51,4 +51,10 @@ public:
CHECK(m_task->num_ticks <= m_ticks); \ CHECK(m_task->num_ticks <= m_ticks); \
CHECK(m_task->num_exits <= m_exits); CHECK(m_task->num_exits <= m_exits);
#define CHECK_STATUS_ENTRIES_TICKS_EXITS(m_task, m_status, m_entries, m_ticks, m_exits) \
CHECK(m_task->get_status() == m_status); \
CHECK(m_task->num_entries == m_entries); \
CHECK(m_task->num_ticks == m_ticks); \
CHECK(m_task->num_exits == m_exits);
#endif // LIMBO_TEST_H #endif // LIMBO_TEST_H

91
tests/test_probability.h Normal file
View File

@ -0,0 +1,91 @@
/**
* test_probability.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_PROBABILITY_H
#define TEST_PROBABILITY_H
#include "limbo_test.h"
#include "modules/limboai/bt/tasks/bt_task.h"
#include "modules/limboai/bt/tasks/decorators/bt_probability.h"
#include "core/math/math_funcs.h"
namespace TestProbability {
TEST_CASE("[Modules][LimboAI] BTProbability") {
Ref<BTProbability> prob = memnew(BTProbability);
SUBCASE("When empty") {
ERR_PRINT_OFF;
CHECK(prob->execute(0.01666) == BTTask::FAILURE);
ERR_PRINT_ON;
}
Ref<BTTestAction> task = memnew(BTTestAction);
prob->add_child(task);
Math::randomize();
SUBCASE("Check if probability meets expectation") {
task->ret_status = BTTask::SUCCESS;
prob->set_run_chance(0.5);
for (int i = 0; i < 1000; i++) {
prob->execute(0.01666);
}
CHECK(task->num_ticks > 450);
CHECK(task->num_ticks < 550);
}
SUBCASE("When probability is 0") {
task->ret_status = BTTask::SUCCESS;
prob->set_run_chance(0.0);
for (int i = 0; i < 1000; i++) {
prob->execute(0.01666);
}
CHECK(task->num_ticks == 0);
}
SUBCASE("When probability is 1") {
task->ret_status = BTTask::SUCCESS;
prob->set_run_chance(1.0);
for (int i = 0; i < 1000; i++) {
prob->execute(0.01666);
}
CHECK(task->num_ticks == 1000);
}
SUBCASE("Test return status") {
prob->set_run_chance(1.0);
task->ret_status = BTTask::SUCCESS;
CHECK(prob->execute(0.01666) == BTTask::SUCCESS);
CHECK_STATUS_ENTRIES_TICKS_EXITS(task, BTTask::SUCCESS, 1, 1, 1);
task->ret_status = BTTask::RUNNING;
CHECK(prob->execute(0.01666) == BTTask::RUNNING);
CHECK_STATUS_ENTRIES_TICKS_EXITS(task, BTTask::RUNNING, 2, 2, 1);
task->ret_status = BTTask::FAILURE;
CHECK(prob->execute(0.01666) == BTTask::FAILURE);
CHECK_STATUS_ENTRIES_TICKS_EXITS(task, BTTask::FAILURE, 2, 3, 2);
}
}
} //namespace TestProbability
#endif // TEST_PROBABILITY_H