From 7252a37b2cc2f6d96a47efbfaac1014812e84476 Mon Sep 17 00:00:00 2001 From: Serhii Snitsaruk Date: Tue, 29 Aug 2023 16:56:25 +0200 Subject: [PATCH] Refactor unit tests --- .../{tasks/bt_test_action.h => limbo_test.h} | 15 ++- tests/test_selector.h | 101 ++++-------------- tests/test_sequence.h | 95 +++++----------- 3 files changed, 62 insertions(+), 149 deletions(-) rename tests/{tasks/bt_test_action.h => limbo_test.h} (70%) diff --git a/tests/tasks/bt_test_action.h b/tests/limbo_test.h similarity index 70% rename from tests/tasks/bt_test_action.h rename to tests/limbo_test.h index e18104d..1e2f921 100644 --- a/tests/tasks/bt_test_action.h +++ b/tests/limbo_test.h @@ -1,5 +1,5 @@ /** - * bt_test_action.h + * limbo_test.h * ============================================================================= * Copyright 2021-2023 Serhii Snitsaruk * @@ -9,8 +9,10 @@ * ============================================================================= */ -#ifndef BT_TEST_ACTION_H -#define BT_TEST_ACTION_H +#ifndef LIMBO_TEST_H +#define LIMBO_TEST_H + +#include "tests/test_macros.h" #include "modules/limboai/bt/tasks/bt_action.h" @@ -36,4 +38,9 @@ public: BTTestAction(int p_return_status) { ret_status = p_return_status; } }; -#endif // BT_TEST_ACTION_H +#define CHECK_ENTRIES_TICKS_EXITS(m_task, m_entries, m_ticks, m_exits) \ + 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_selector.h b/tests/test_selector.h index b8fe5bc..97998b1 100644 --- a/tests/test_selector.h +++ b/tests/test_selector.h @@ -12,11 +12,10 @@ #ifndef TEST_SELECTOR_H #define TEST_SELECTOR_H -#include "tests/test_macros.h" +#include "limbo_test.h" #include "modules/limboai/bt/tasks/bt_task.h" #include "modules/limboai/bt/tasks/composites/bt_selector.h" -#include "modules/limboai/tests/tasks/bt_test_action.h" namespace TestSelector { @@ -39,17 +38,9 @@ TEST_CASE("[Modules][LimboAI] BTSelector when all return FAILURE") { CHECK(task2->get_status() == BTTask::FAILURE); CHECK(task3->get_status() == BTTask::FAILURE); - CHECK(task1->num_entries == 1); - CHECK(task1->num_ticks == 1); - CHECK(task1->num_exits == 1); - - CHECK(task2->num_entries == 1); - CHECK(task2->num_ticks == 1); - CHECK(task2->num_exits == 1); - - CHECK(task3->num_entries == 1); - CHECK(task3->num_ticks == 1); - CHECK(task3->num_exits == 1); + CHECK_ENTRIES_TICKS_EXITS(task1, 1, 1, 1); + CHECK_ENTRIES_TICKS_EXITS(task2, 1, 1, 1); + CHECK_ENTRIES_TICKS_EXITS(task3, 1, 1, 1); // * Second execution. CHECK(sel->execute(0.1666) == BTTask::FAILURE); @@ -58,17 +49,9 @@ TEST_CASE("[Modules][LimboAI] BTSelector when all return FAILURE") { CHECK(task2->get_status() == BTTask::FAILURE); CHECK(task3->get_status() == BTTask::FAILURE); - CHECK(task1->num_entries == 2); - CHECK(task1->num_ticks == 2); - CHECK(task1->num_exits == 2); - - CHECK(task2->num_entries == 2); - CHECK(task2->num_ticks == 2); - CHECK(task2->num_exits == 2); - - CHECK(task3->num_entries == 2); - CHECK(task3->num_ticks == 2); - CHECK(task3->num_exits == 2); + CHECK_ENTRIES_TICKS_EXITS(task1, 2, 2, 2); + CHECK_ENTRIES_TICKS_EXITS(task2, 2, 2, 2); + CHECK_ENTRIES_TICKS_EXITS(task3, 2, 2, 2); } TEST_CASE("[Modules][LimboAI] BTSelector when second returns SUCCESS") { @@ -90,17 +73,9 @@ TEST_CASE("[Modules][LimboAI] BTSelector when second returns SUCCESS") { CHECK(task2->get_status() == BTTask::SUCCESS); CHECK(task3->get_status() == BTTask::FRESH); - CHECK(task1->num_entries == 1); - CHECK(task1->num_ticks == 1); - CHECK(task1->num_exits == 1); - - CHECK(task2->num_entries == 1); - CHECK(task2->num_ticks == 1); - CHECK(task2->num_exits == 1); - - CHECK(task3->num_entries == 0); - CHECK(task3->num_ticks == 0); - CHECK(task3->num_exits == 0); + CHECK_ENTRIES_TICKS_EXITS(task1, 1, 1, 1); + CHECK_ENTRIES_TICKS_EXITS(task2, 1, 1, 1); + CHECK_ENTRIES_TICKS_EXITS(task3, 0, 0, 0); // * Second execution. CHECK(sel->execute(0.1666) == BTTask::SUCCESS); @@ -109,17 +84,9 @@ TEST_CASE("[Modules][LimboAI] BTSelector when second returns SUCCESS") { CHECK(task2->get_status() == BTTask::SUCCESS); CHECK(task3->get_status() == BTTask::FRESH); - CHECK(task1->num_entries == 2); - CHECK(task1->num_ticks == 2); - CHECK(task1->num_exits == 2); - - CHECK(task2->num_entries == 2); - CHECK(task2->num_ticks == 2); - CHECK(task2->num_exits == 2); - - CHECK(task3->num_entries == 0); - CHECK(task3->num_ticks == 0); - CHECK(task3->num_exits == 0); + CHECK_ENTRIES_TICKS_EXITS(task1, 2, 2, 2); + CHECK_ENTRIES_TICKS_EXITS(task2, 2, 2, 2); + CHECK_ENTRIES_TICKS_EXITS(task3, 0, 0, 0); } TEST_CASE("[Modules][LimboAI] BTSelector when second returns RUNNING") { @@ -141,17 +108,9 @@ TEST_CASE("[Modules][LimboAI] BTSelector when second returns RUNNING") { CHECK(task2->get_status() == BTTask::RUNNING); CHECK(task3->get_status() == BTTask::FRESH); - CHECK(task1->num_entries == 1); - CHECK(task1->num_ticks == 1); - CHECK(task1->num_exits == 1); - - CHECK(task2->num_entries == 1); - CHECK(task2->num_ticks == 1); - CHECK(task2->num_exits == 0); - - CHECK(task3->num_entries == 0); - CHECK(task3->num_ticks == 0); - CHECK(task3->num_exits == 0); + CHECK_ENTRIES_TICKS_EXITS(task1, 1, 1, 1); + CHECK_ENTRIES_TICKS_EXITS(task2, 1, 1, 0); + CHECK_ENTRIES_TICKS_EXITS(task3, 0, 0, 0); // * Second execution. CHECK(sel->execute(0.1666) == BTTask::RUNNING); @@ -160,17 +119,9 @@ TEST_CASE("[Modules][LimboAI] BTSelector when second returns RUNNING") { CHECK(task2->get_status() == BTTask::RUNNING); CHECK(task3->get_status() == BTTask::FRESH); - CHECK(task1->num_entries == 1); - CHECK(task1->num_ticks == 1); - CHECK(task1->num_exits == 1); - - CHECK(task2->num_entries == 1); - CHECK(task2->num_ticks == 2); - CHECK(task2->num_exits == 0); - - CHECK(task3->num_entries == 0); - CHECK(task3->num_ticks == 0); - CHECK(task3->num_exits == 0); + CHECK_ENTRIES_TICKS_EXITS(task1, 1, 1, 1); + CHECK_ENTRIES_TICKS_EXITS(task2, 1, 2, 0); + CHECK_ENTRIES_TICKS_EXITS(task3, 0, 0, 0); // * Third execution with second task returning FAILURE. task2->ret_status = BTTask::FAILURE; @@ -180,17 +131,9 @@ TEST_CASE("[Modules][LimboAI] BTSelector when second returns RUNNING") { CHECK(task2->get_status() == BTTask::FAILURE); CHECK(task3->get_status() == BTTask::FAILURE); - CHECK(task1->num_entries == 1); - CHECK(task1->num_ticks == 1); - CHECK(task1->num_exits == 1); - - CHECK(task2->num_entries == 1); - CHECK(task2->num_ticks == 3); - CHECK(task2->num_exits == 1); - - CHECK(task3->num_entries == 1); - CHECK(task3->num_ticks == 1); - CHECK(task3->num_exits == 1); + CHECK_ENTRIES_TICKS_EXITS(task1, 1, 1, 1); + CHECK_ENTRIES_TICKS_EXITS(task2, 1, 3, 1); + CHECK_ENTRIES_TICKS_EXITS(task3, 1, 1, 1); } } //namespace TestSelector diff --git a/tests/test_sequence.h b/tests/test_sequence.h index debf5fa..645968f 100644 --- a/tests/test_sequence.h +++ b/tests/test_sequence.h @@ -12,11 +12,10 @@ #ifndef TEST_SEQUENCE_H #define TEST_SEQUENCE_H -#include "tests/test_macros.h" +#include "limbo_test.h" #include "modules/limboai/bt/tasks/bt_task.h" #include "modules/limboai/bt/tasks/composites/bt_sequence.h" -#include "modules/limboai/tests/tasks/bt_test_action.h" namespace TestSequence { @@ -39,17 +38,9 @@ TEST_CASE("[Modules][LimboAI] BTSequence when all return SUCCESS") { CHECK(task2->get_status() == BTTask::SUCCESS); CHECK(task3->get_status() == BTTask::SUCCESS); - CHECK(task1->num_entries == 1); - CHECK(task1->num_ticks == 1); - CHECK(task1->num_exits == 1); - - CHECK(task2->num_entries == 1); - CHECK(task2->num_ticks == 1); - CHECK(task2->num_exits == 1); - - CHECK(task3->num_entries == 1); - CHECK(task3->num_ticks == 1); - CHECK(task3->num_exits == 1); + CHECK_ENTRIES_TICKS_EXITS(task1, 1, 1, 1); + CHECK_ENTRIES_TICKS_EXITS(task2, 1, 1, 1); + CHECK_ENTRIES_TICKS_EXITS(task3, 1, 1, 1); // * Second execution. CHECK(seq->execute(0.1666) == BTTask::SUCCESS); @@ -58,17 +49,9 @@ TEST_CASE("[Modules][LimboAI] BTSequence when all return SUCCESS") { CHECK(task2->get_status() == BTTask::SUCCESS); CHECK(task3->get_status() == BTTask::SUCCESS); - CHECK(task1->num_entries == 2); - CHECK(task1->num_ticks == 2); - CHECK(task1->num_exits == 2); - - CHECK(task2->num_entries == 2); - CHECK(task2->num_ticks == 2); - CHECK(task2->num_exits == 2); - - CHECK(task3->num_entries == 2); - CHECK(task3->num_ticks == 2); - CHECK(task3->num_exits == 2); + CHECK_ENTRIES_TICKS_EXITS(task1, 2, 2, 2); + CHECK_ENTRIES_TICKS_EXITS(task2, 2, 2, 2); + CHECK_ENTRIES_TICKS_EXITS(task3, 2, 2, 2); } TEST_CASE("[Modules][LimboAI] BTSequence when second returns FAILURE") { @@ -90,17 +73,9 @@ TEST_CASE("[Modules][LimboAI] BTSequence when second returns FAILURE") { CHECK(task2->get_status() == BTTask::FAILURE); CHECK(task3->get_status() == BTTask::FRESH); - CHECK(task1->num_entries == 1); - CHECK(task1->num_ticks == 1); - CHECK(task1->num_exits == 1); - - CHECK(task2->num_entries == 1); - CHECK(task2->num_ticks == 1); - CHECK(task2->num_exits == 1); - - CHECK(task3->num_entries == 0); - CHECK(task3->num_ticks == 0); - CHECK(task3->num_exits == 0); + CHECK_ENTRIES_TICKS_EXITS(task1, 1, 1, 1); + CHECK_ENTRIES_TICKS_EXITS(task2, 1, 1, 1); + CHECK_ENTRIES_TICKS_EXITS(task3, 0, 0, 0); // * Second execution. CHECK(seq->execute(0.1666) == BTTask::FAILURE); @@ -109,17 +84,9 @@ TEST_CASE("[Modules][LimboAI] BTSequence when second returns FAILURE") { CHECK(task2->get_status() == BTTask::FAILURE); CHECK(task3->get_status() == BTTask::FRESH); - CHECK(task1->num_entries == 2); - CHECK(task1->num_ticks == 2); - CHECK(task1->num_exits == 2); - - CHECK(task2->num_entries == 2); - CHECK(task2->num_ticks == 2); - CHECK(task2->num_exits == 2); - - CHECK(task3->num_entries == 0); - CHECK(task3->num_ticks == 0); - CHECK(task3->num_exits == 0); + CHECK_ENTRIES_TICKS_EXITS(task1, 2, 2, 2); + CHECK_ENTRIES_TICKS_EXITS(task2, 2, 2, 2); + CHECK_ENTRIES_TICKS_EXITS(task3, 0, 0, 0); } TEST_CASE("[Modules][LimboAI] BTSequence when second returns RUNNING") { @@ -141,32 +108,28 @@ TEST_CASE("[Modules][LimboAI] BTSequence when second returns RUNNING") { CHECK(task2->get_status() == BTTask::RUNNING); CHECK(task3->get_status() == BTTask::FRESH); - CHECK(task1->num_entries == 1); - CHECK(task1->num_exits == 1); - CHECK(task1->num_ticks == 1); - - CHECK(task2->num_entries == 1); - CHECK(task2->num_ticks == 1); - CHECK(task2->num_exits == 0); - - CHECK(task3->num_entries == 0); - CHECK(task3->num_ticks == 0); - CHECK(task3->num_exits == 0); + CHECK_ENTRIES_TICKS_EXITS(task1, 1, 1, 1); + CHECK_ENTRIES_TICKS_EXITS(task2, 1, 1, 0); + CHECK_ENTRIES_TICKS_EXITS(task3, 0, 0, 0); // * Second execution. CHECK(seq->execute(0.1666) == BTTask::RUNNING); - CHECK(task1->num_entries == 1); - CHECK(task1->num_exits == 1); - CHECK(task1->num_ticks == 1); + CHECK_ENTRIES_TICKS_EXITS(task1, 1, 1, 1); + CHECK_ENTRIES_TICKS_EXITS(task2, 1, 2, 0); + CHECK_ENTRIES_TICKS_EXITS(task3, 0, 0, 0); - CHECK(task2->num_entries == 1); - CHECK(task2->num_ticks == 2); - CHECK(task2->num_exits == 0); + // * Third execution with second task returning SUCCESS. + task2->ret_status = BTTask::SUCCESS; + CHECK(seq->execute(0.1666) == BTTask::SUCCESS); - CHECK(task3->num_entries == 0); - CHECK(task3->num_ticks == 0); - CHECK(task3->num_exits == 0); + CHECK(task1->get_status() == BTTask::SUCCESS); + CHECK(task2->get_status() == BTTask::SUCCESS); + CHECK(task3->get_status() == BTTask::SUCCESS); + + CHECK_ENTRIES_TICKS_EXITS(task1, 1, 1, 1); + CHECK_ENTRIES_TICKS_EXITS(task2, 1, 3, 1); + CHECK_ENTRIES_TICKS_EXITS(task3, 1, 1, 1); } TEST_CASE("[Modules][LimboAI] BTSequence with no child tasks") {