From a53f3d6170b15adf99cd743d8cdd992f95457291 Mon Sep 17 00:00:00 2001 From: Kasparme Date: Fri, 2 Feb 2024 20:16:49 +0200 Subject: [PATCH] Timer tests (#336) --- .gitignore | 1 + src/timers/CTimers.h | 2 +- tests/CMakeLists.txt | 19 +++++++ tests/TestCTimers.cpp | 128 ++++++++++++++++++++++++++++++++++++++++++ tests/TestCTimers.h | 22 ++++++++ 5 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 tests/TestCTimers.cpp create mode 100644 tests/TestCTimers.h diff --git a/.gitignore b/.gitignore index 824ad1882..23ee2ed64 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +cmake-build-debug/ winbuild/ build/ out/ diff --git a/src/timers/CTimers.h b/src/timers/CTimers.h index a45899101..1563bba59 100644 --- a/src/timers/CTimers.h +++ b/src/timers/CTimers.h @@ -40,7 +40,7 @@ class CTimers final : public QObject std::list m_countdowns; QTimer m_timer; -private: +public: std::string getTimers(); std::string getCountdowns(); diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 187395aec..a6dddc9df 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -208,3 +208,22 @@ set_target_properties( UNITY_BUILD ${USE_UNITY_BUILD} ) add_test(NAME TestAdventure COMMAND TestAdventure) + +# Timer +set(timer_qq + ../src/timers/CTimers.cpp + ../src/timers/CTimers.h +) +set(TestTimerQQ TestCTimers.cpp) +add_executable(TestTimer ${timer_qq} ${TestTimerQQ}) +add_dependencies(TestTimer glm) +target_link_libraries(TestTimer Qt5::Test coverage_config) +set_target_properties( + TestTimer PROPERTIES + CXX_STANDARD 17 + CXX_STANDARD_REQUIRED ON + CXX_EXTENSIONS OFF + COMPILE_FLAGS "${WARNING_FLAGS}" + UNITY_BUILD ${USE_UNITY_BUILD} +) +add_test(NAME TestTimer COMMAND TestTimer) \ No newline at end of file diff --git a/tests/TestCTimers.cpp b/tests/TestCTimers.cpp new file mode 100644 index 000000000..265fb88ab --- /dev/null +++ b/tests/TestCTimers.cpp @@ -0,0 +1,128 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +// Copyright (C) 2023 The MMapper Authors + +#include "TestCTimers.h" + +#include + +#include "../src/timers/CTimers.h" + +TestCTimers::TestCTimers() = default; + +TestCTimers::~TestCTimers() = default; + +void TestCTimers::testAddRemoveTimer() +{ + CTimers timers(nullptr); + QString timerName = "TestTimer"; + QString timerDesc = "Test Description"; + + timers.addTimer(timerName.toStdString(), timerDesc.toStdString()); + + // Verify the added timer is present + QString timersList = QString::fromStdString(timers.getTimers()); + QVERIFY(timersList.contains(timerName)); + QVERIFY(timersList.contains(timerDesc)); + + QVERIFY(timers.removeTimer(timerName.toStdString())); + + // Verify the timer is removed + timersList = QString::fromStdString(timers.getTimers()); + QVERIFY(!timersList.contains(timerName)); +} + +void TestCTimers::testAddRemoveCountdown() +{ + CTimers timers(nullptr); + QString countdownName = "TestCountdown"; + QString countdownDesc = "Test Countdown Description"; + int64_t countdownTimeMs = 10000; // 10 seconds + + timers.addCountdown(countdownName.toStdString(), countdownDesc.toStdString(), countdownTimeMs); + + // Verify the added countdown is added + QString countdownsList = QString::fromStdString(timers.getCountdowns()); + QVERIFY(countdownsList.contains(countdownName)); + QVERIFY(countdownsList.contains(countdownDesc)); + + QVERIFY(timers.removeCountdown(countdownName.toStdString())); + + // Verify the countdown is removed + countdownsList = QString::fromStdString(timers.getCountdowns()); + QVERIFY(!countdownsList.contains(countdownName)); +} + +void TestCTimers::testElapsedTime() +{ + CTimers timers(nullptr); + QString timerName = "ElapsedTimeTestTimer"; + QString timerDesc = "Elapsed Time Test Description"; + + timers.addTimer(timerName.toStdString(), timerDesc.toStdString()); + + QString timersList = QString::fromStdString(timers.getTimers()); + QVERIFY(timersList.contains("up for - 0:00")); + + timers.removeTimer(timerName.toStdString()); +} + +void TestCTimers::testCountdownCompletion() +{ + CTimers timers(nullptr); + QString countdownName = "CompletionTestCountdown"; + QString countdownDesc = "Countdown Completion Test"; + int64_t countdownTimeMs = 10000; // 10 seconds + + timers.addCountdown(countdownName.toStdString(), countdownDesc.toStdString(), countdownTimeMs); + QString countdownsListBefore = QString::fromStdString(timers.getCountdowns()); + QVERIFY(countdownsListBefore.contains(countdownName)); // Verify the added countdown is present + QString countdownsList = QString::fromStdString(timers.getCountdowns()); + QVERIFY(countdownsList.contains("(up for - 0:00, left - 0:10)")); +} + +void TestCTimers::testClearFunctionality() +{ + CTimers timers(nullptr); + + // Add multiple timers and countdowns + timers.addTimer("Timer1", "Description1"); + timers.addTimer("Timer2", "Description1"); + timers.addCountdown("Countdown1", "Description1", 5000); + timers.addCountdown("Countdown2", "Description2", 5000); + + // Clear all timers and countdowns + timers.clear(); + + // Verify all timers and countdowns are cleared + QVERIFY(QString::fromStdString(timers.getTimers()).isEmpty()); + QVERIFY(QString::fromStdString(timers.getCountdowns()).isEmpty()); +} + +void TestCTimers::testMultipleTimersAndCountdowns() +{ + CTimers timers(nullptr); + + // Add multiple timers + timers.addTimer("Timer1", "Description1"); + timers.addTimer("Timer2", "Description2"); + + // Add multiple countdowns + timers.addCountdown("Countdown1", "Description1", 5000); + timers.addCountdown("Countdown2", "Description2", 10000); + + // Verify timers and countdowns are added + QVERIFY(!QString::fromStdString(timers.getTimers()).isEmpty()); + QVERIFY(!QString::fromStdString(timers.getCountdowns()).isEmpty()); + + // Remove a timer and a countdown + QVERIFY(timers.removeTimer("Timer1")); + QVERIFY(timers.removeCountdown("Countdown1")); + + // Verify removal + QString timersList = QString::fromStdString(timers.getTimers()); + QString countdownsList = QString::fromStdString(timers.getCountdowns()); + QVERIFY(!timersList.contains("Timer1")); + QVERIFY(!countdownsList.contains("Countdown1")); +} + +QTEST_MAIN(TestCTimers) diff --git a/tests/TestCTimers.h b/tests/TestCTimers.h new file mode 100644 index 000000000..e39b454c6 --- /dev/null +++ b/tests/TestCTimers.h @@ -0,0 +1,22 @@ +#pragma once +// SPDX-License-Identifier: GPL-2.0-or-later +// Copyright (C) 2023 The MMapper Authors + +#include + +class TestCTimers final : public QObject +{ + Q_OBJECT +public: + TestCTimers(); + ~TestCTimers() final; + +private: +private Q_SLOTS: + void testAddRemoveTimer(); + void testAddRemoveCountdown(); + void testElapsedTime(); + void testCountdownCompletion(); + void testClearFunctionality(); + void testMultipleTimersAndCountdowns(); +};