diff --git a/tests/limbo_test.h b/tests/limbo_test.h index 4e85b24..20b354c 100644 --- a/tests/limbo_test.h +++ b/tests/limbo_test.h @@ -51,4 +51,10 @@ public: CHECK(m_task->num_ticks <= m_ticks); \ 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 diff --git a/tests/test_probability.h b/tests/test_probability.h new file mode 100644 index 0000000..df3dc1a --- /dev/null +++ b/tests/test_probability.h @@ -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 prob = memnew(BTProbability); + + SUBCASE("When empty") { + ERR_PRINT_OFF; + CHECK(prob->execute(0.01666) == BTTask::FAILURE); + ERR_PRINT_ON; + } + + Ref 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