From 4259db0b8f5972ecc0b368560e52294a1b5f1a8b Mon Sep 17 00:00:00 2001 From: Lars Toenning Date: Thu, 26 May 2022 15:23:51 +0200 Subject: [PATCH] Add unittest for SGTimer/SGTimerQueue --- simgear/structure/CMakeLists.txt | 1 + simgear/structure/event_mgr_test.cxx | 118 +++++++++++++++++++++++++++ 2 files changed, 119 insertions(+) create mode 100644 simgear/structure/event_mgr_test.cxx diff --git a/simgear/structure/CMakeLists.txt b/simgear/structure/CMakeLists.txt index bfcc0e9d..22cc332b 100644 --- a/simgear/structure/CMakeLists.txt +++ b/simgear/structure/CMakeLists.txt @@ -47,6 +47,7 @@ simgear_component(structure structure "${SOURCES}" "${HEADERS}") if(ENABLE_TESTS) add_simgear_autotest(test_subsystems subsystem_test.cxx) add_simgear_autotest(test_state_machine state_machine_test.cxx) + add_simgear_autotest(test_event_mgr event_mgr_test.cxx) add_simgear_autotest(test_expressions expression_test.cxx) add_simgear_autotest(test_shared_ptr shared_ptr_test.cpp) add_simgear_autotest(test_commands test_commands.cxx) diff --git a/simgear/structure/event_mgr_test.cxx b/simgear/structure/event_mgr_test.cxx new file mode 100644 index 00000000..be15ad79 --- /dev/null +++ b/simgear/structure/event_mgr_test.cxx @@ -0,0 +1,118 @@ +// Unit tests for SGTimer and SGTimerQueue +// SPDX-FileCopyrightText: (C) 2022 Lars Toenning +// SPDX-License-Identifier: LGPL-2.0-or-later + +#include + +#include + +#include "event_mgr.hxx" + +void testSGTimer() { + int call_counter = 0; + SGTimer timer; + timer.callback = [&call_counter]() { ++call_counter; }; + timer.repeat = false; + timer.running = false; + timer.interval = 0.1; + timer.name = "TestTimer"; + + // Check single run + timer.run(); + SG_CHECK_EQUAL(call_counter, 1); + + // Check multiple runs + call_counter = 0; + for (int i = 0; i < 5; i++) { + timer.run(); + } + SG_CHECK_EQUAL(call_counter, 5); + + // run shouldn't have side effects on members + SG_CHECK_EQUAL(timer.repeat, false); + SG_CHECK_EQUAL(timer.running, false); + SG_CHECK_EQUAL(timer.interval, 0.1); + SG_CHECK_EQUAL(timer.name, "TestTimer"); +} + +void testSGTimerQueueClear() { + SGTimerQueue queue; + int call_counter = 0; + std::map stats; + + auto timer = std::make_unique(); + timer->callback = [&call_counter]() { ++call_counter; }; + timer->repeat = true; + timer->interval = 0.5; + + queue.insert(std::move(timer), 1); + + SG_CHECK_EQUAL(call_counter, 0); + queue.update(0.5, stats); + SG_CHECK_EQUAL(call_counter, 0); + queue.update(0.5, stats); + SG_CHECK_EQUAL(call_counter, 1); + queue.update(0.4, stats); + SG_CHECK_EQUAL(call_counter, 1); + queue.update(0.1, stats); + SG_CHECK_EQUAL(call_counter, 2); + queue.update(42.0, stats); + SG_CHECK_EQUAL(call_counter, 3); + + queue.clear(); + queue.update(0.6, stats); + SG_CHECK_EQUAL(call_counter, 3); +} + +void testSGTimerQueueRemoveByName() { + SGTimerQueue queue; + int call_counter = 0; + std::map stats; + + auto timer = std::make_unique(); + timer->callback = [&call_counter]() { ++call_counter; }; + timer->name = "TestTimer1"; + timer->repeat = true; + timer->interval = 1; + queue.insert(std::move(timer), 0); + + SG_CHECK_EQUAL(call_counter, 0); + queue.update(1.0, stats); + SG_CHECK_EQUAL(call_counter, 1); + queue.update(1.0, stats); + SG_CHECK_EQUAL(call_counter, 2); + SG_CHECK_EQUAL(queue.removeByName("TestTimer1"), true); + queue.update(1.0, stats); + SG_CHECK_EQUAL(call_counter, 2); +} + +void testSGTimerQueueOneShot() { + SGTimerQueue queue; + int call_counter = 0; + std::map stats; + + auto timer = std::make_unique(); + timer->callback = [&call_counter]() { ++call_counter; }; + timer->name = "TestTimer1"; + timer->repeat = false; + timer->interval = 1; + queue.insert(std::move(timer), 0); + + SG_CHECK_EQUAL(call_counter, 0); + queue.update(1.0, stats); + SG_CHECK_EQUAL(call_counter, 1); + queue.update(1.0, stats); + SG_CHECK_EQUAL(call_counter, 1); + SG_CHECK_EQUAL(queue.removeByName("TestTimer1"), false); + queue.update(1.0, stats); + SG_CHECK_EQUAL(call_counter, 1); +} + +int main(int argc, char *argv[]) { + testSGTimer(); + testSGTimerQueueClear(); + testSGTimerQueueRemoveByName(); + testSGTimerQueueOneShot(); + + return EXIT_SUCCESS; +}