From 6baa6d5686827c5d8c4c6c7d4a6b67292c133601 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Fri, 8 Sep 2023 11:18:24 +0200 Subject: [PATCH] Add tests for wait actions --- tests/test_wait_actions.h | 120 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 120 insertions(+) create mode 100644 tests/test_wait_actions.h diff --git a/tests/test_wait_actions.h b/tests/test_wait_actions.h new file mode 100644 index 0000000..ee15506 --- /dev/null +++ b/tests/test_wait_actions.h @@ -0,0 +1,120 @@ +/** + * test_wait_actions.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_WAIT_ACTIONS_H +#define TEST_WAIT_ACTIONS_H + +#include "limbo_test.h" + +#include "modules/limboai/bt/tasks/bt_task.h" +#include "modules/limboai/bt/tasks/utility/bt_random_wait.h" +#include "modules/limboai/bt/tasks/utility/bt_wait.h" +#include "modules/limboai/bt/tasks/utility/bt_wait_ticks.h" + +#include "core/math/math_funcs.h" + +namespace TestWaitActions { + +TEST_CASE("[Modules][LimboAI] BTWait") { + Ref wait = memnew(BTWait); + + SUBCASE("With zero duration") { + wait->set_duration(0.0); + CHECK(wait->execute(0.0) == BTWait::SUCCESS); + } + SUBCASE("With one second duration") { + wait->set_duration(1.0); + CHECK(wait->execute(0.0) == BTWait::RUNNING); + CHECK(wait->execute(0.5) == BTWait::RUNNING); // * elapsed 0.5 + CHECK(wait->execute(0.5) == BTWait::SUCCESS); // * elapsed 1.0 + } +} + +TEST_CASE("[Modules][LimboAI] BTWaitTicks") { + Ref wait = memnew(BTWaitTicks); + + SUBCASE("With zero ticks") { + wait->set_num_ticks(0); + CHECK(wait->execute(0.01666) == BTWait::SUCCESS); // * elapsed 0 ticks + } + SUBCASE("With 1 tick") { + wait->set_num_ticks(1); + CHECK(wait->execute(0.01666) == BTWait::RUNNING); // * elapsed 0 ticks + CHECK(wait->execute(0.01666) == BTWait::SUCCESS); // * elapsed 1 tick + } + SUBCASE("With 2 ticks") { + wait->set_num_ticks(2); + CHECK(wait->execute(0.01666) == BTWait::RUNNING); // * elapsed 0 ticks + CHECK(wait->execute(0.01666) == BTWait::RUNNING); // * elapsed 1 tick + CHECK(wait->execute(0.01666) == BTWait::SUCCESS); // * elapsed 2 ticks + } +} + +TEST_CASE("[Modules][LimboAI] BTRandomWait") { + Ref wait = memnew(BTRandomWait); + + Math::randomize(); + + SUBCASE("With duration range [0, 0]") { + wait->set_min_duration(0.0); + wait->set_max_duration(0.0); + CHECK(wait->execute(0.01666) == BTWait::SUCCESS); + } + SUBCASE("With certain SUCCESS") { + wait->set_min_duration(0.5); + wait->set_max_duration(1.0); + + CHECK(wait->execute(0.00) == BTWait::RUNNING); // * elapsed 0.00 + CHECK(wait->execute(0.25) == BTWait::RUNNING); // * elapsed 0.25 + CHECK(wait->execute(0.76) == BTWait::SUCCESS); // * elapsed 1.01 + } + SUBCASE("With duration range [0.5, 1.0]") { + wait->set_min_duration(0.5); + wait->set_max_duration(1.0); + + int num_successes = 0; + int num_running = 0; + int num_failures = 0; + int num_undefined = 0; + + for (int i = 0; i < 1000; i++) { + wait->execute(0.00); // * elapsed 0.00 + wait->execute(0.75); // * elapsed 0.75 + switch (wait->get_status()) { + case BTTask::RUNNING: { + num_running += 1; + } break; + case BTTask::SUCCESS: { + num_successes += 1; + } break; + case BTTask::FAILURE: { + num_failures += 1; + } break; + default: { + num_undefined += 1; + } break; + } + wait->cancel(); + } + + // * Expected ~500/500 SUCCESS/RUNNING. + CHECK(num_successes > 450); + CHECK(num_successes < 550); + CHECK(num_running > 450); + CHECK(num_running < 550); + CHECK(num_failures == 0); + CHECK(num_undefined == 0); + } +} + +} //namespace TestWaitActions + +#endif // TEST_WAIT_ACTIONS_H