From 12a086ac0bfae90ebede548a405ab054d39089e7 Mon Sep 17 00:00:00 2001 From: Kaspar-Metsa Date: Wed, 31 Jan 2024 14:37:38 +0200 Subject: [PATCH 1/5] Timer tests --- .gitignore | 1 + src/timers/CTimers.h | 2 +- tests/CMakeLists.txt | 19 ++++++ tests/TestCTimers.cpp | 132 ++++++++++++++++++++++++++++++++++++++++++ tests/TestCTimers.h | 20 +++++++ 5 files changed, 173 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..9ca5ec29a --- /dev/null +++ b/tests/TestCTimers.cpp @@ -0,0 +1,132 @@ +#include "TestCTimers.h" + +#include +#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()); + + QTest::qWait(1100); // Wait 1.1 seconds + + QString timersList = QString::fromStdString(timers.getTimers()); + QVERIFY(timersList.contains("up for - 0:01")); + + timers.removeTimer(timerName.toStdString()); +} + +void TestCTimers::testCountdownCompletion() +{ + CTimers timers(nullptr); + QString countdownName = "CompletionTestCountdown"; + QString countdownDesc = "Countdown Completion Test"; + int64_t countdownTimeMs = 1; // 1 millisecond + + timers.addCountdown(countdownName.toStdString(), countdownDesc.toStdString(), countdownTimeMs); + QString countdownsListBefore = QString::fromStdString(timers.getCountdowns()); + // Verify the added countdown is present + QVERIFY(countdownsListBefore.contains(countdownName)); + QTest::qWait(50); // Wait longer than the countdown time to ensure it completes + + // Verify the countdown has been removed because it's completed + QString countdownsListAfter = QString::fromStdString(timers.getCountdowns()); + QVERIFY(!countdownsListAfter.contains(countdownName)); +} + +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..f74980087 --- /dev/null +++ b/tests/TestCTimers.h @@ -0,0 +1,20 @@ +#pragma once + +#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(); +}; From 5fbb04ccbb8105342644b598a8e4ae96d53e191a Mon Sep 17 00:00:00 2001 From: Kaspar-Metsa Date: Wed, 31 Jan 2024 21:04:29 +0200 Subject: [PATCH 2/5] Timer tests --- tests/TestCTimers.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/TestCTimers.cpp b/tests/TestCTimers.cpp index 9ca5ec29a..15ea9bd03 100644 --- a/tests/TestCTimers.cpp +++ b/tests/TestCTimers.cpp @@ -71,13 +71,13 @@ void TestCTimers::testCountdownCompletion() CTimers timers(nullptr); QString countdownName = "CompletionTestCountdown"; QString countdownDesc = "Countdown Completion Test"; - int64_t countdownTimeMs = 1; // 1 millisecond + int64_t countdownTimeMs = 100; // 1 millisecond timers.addCountdown(countdownName.toStdString(), countdownDesc.toStdString(), countdownTimeMs); QString countdownsListBefore = QString::fromStdString(timers.getCountdowns()); // Verify the added countdown is present QVERIFY(countdownsListBefore.contains(countdownName)); - QTest::qWait(50); // Wait longer than the countdown time to ensure it completes + QTest::qWait(500); // Wait longer than the countdown time to ensure it completes // Verify the countdown has been removed because it's completed QString countdownsListAfter = QString::fromStdString(timers.getCountdowns()); From f1156cd45fe5bbea0066d82fabd89b5d72cae091 Mon Sep 17 00:00:00 2001 From: Kaspar-Metsa Date: Wed, 31 Jan 2024 21:15:38 +0200 Subject: [PATCH 3/5] Timer tests --- tests/TestCTimers.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/tests/TestCTimers.cpp b/tests/TestCTimers.cpp index 15ea9bd03..e279bc591 100644 --- a/tests/TestCTimers.cpp +++ b/tests/TestCTimers.cpp @@ -58,10 +58,10 @@ void TestCTimers::testElapsedTime() timers.addTimer(timerName.toStdString(), timerDesc.toStdString()); - QTest::qWait(1100); // Wait 1.1 seconds +// QTest::qWait(1100); // Wait 1.1 seconds QString timersList = QString::fromStdString(timers.getTimers()); - QVERIFY(timersList.contains("up for - 0:01")); + QVERIFY(timersList.contains("up for - 0:00")); timers.removeTimer(timerName.toStdString()); } @@ -71,17 +71,14 @@ void TestCTimers::testCountdownCompletion() CTimers timers(nullptr); QString countdownName = "CompletionTestCountdown"; QString countdownDesc = "Countdown Completion Test"; - int64_t countdownTimeMs = 100; // 1 millisecond + int64_t countdownTimeMs = 10000; // 10 seconds timers.addCountdown(countdownName.toStdString(), countdownDesc.toStdString(), countdownTimeMs); QString countdownsListBefore = QString::fromStdString(timers.getCountdowns()); - // Verify the added countdown is present - QVERIFY(countdownsListBefore.contains(countdownName)); - QTest::qWait(500); // Wait longer than the countdown time to ensure it completes - - // Verify the countdown has been removed because it's completed - QString countdownsListAfter = QString::fromStdString(timers.getCountdowns()); - QVERIFY(!countdownsListAfter.contains(countdownName)); + QVERIFY(countdownsListBefore.contains(countdownName)); // Verify the added countdown is present + QString countdownsList = QString::fromStdString(timers.getCountdowns()); + qDebug() << "Timers List:" << countdownsList; + QVERIFY(countdownsList.contains("(up for - 0:00, left - 0:10)")); } void TestCTimers::testClearFunctionality() From dbf33bb1a8aee8aa34ed0ed53483ffd98e7f7a27 Mon Sep 17 00:00:00 2001 From: Kaspar-Metsa Date: Fri, 2 Feb 2024 18:56:05 +0200 Subject: [PATCH 4/5] Timer bugfix --- tests/TestCTimers.cpp | 5 +++-- tests/TestCTimers.h | 2 ++ 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/TestCTimers.cpp b/tests/TestCTimers.cpp index e279bc591..e8358e472 100644 --- a/tests/TestCTimers.cpp +++ b/tests/TestCTimers.cpp @@ -1,6 +1,8 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +// Copyright (C) 2023 The MMapper Authors + #include "TestCTimers.h" -#include #include #include "../src/timers/CTimers.h" @@ -77,7 +79,6 @@ void TestCTimers::testCountdownCompletion() QString countdownsListBefore = QString::fromStdString(timers.getCountdowns()); QVERIFY(countdownsListBefore.contains(countdownName)); // Verify the added countdown is present QString countdownsList = QString::fromStdString(timers.getCountdowns()); - qDebug() << "Timers List:" << countdownsList; QVERIFY(countdownsList.contains("(up for - 0:00, left - 0:10)")); } diff --git a/tests/TestCTimers.h b/tests/TestCTimers.h index f74980087..e39b454c6 100644 --- a/tests/TestCTimers.h +++ b/tests/TestCTimers.h @@ -1,4 +1,6 @@ #pragma once +// SPDX-License-Identifier: GPL-2.0-or-later +// Copyright (C) 2023 The MMapper Authors #include From fa01906c47d35cfefa028f346f32b895e677a9d5 Mon Sep 17 00:00:00 2001 From: Kaspar-Metsa Date: Fri, 2 Feb 2024 19:00:55 +0200 Subject: [PATCH 5/5] Timer bugfix --- tests/TestCTimers.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/TestCTimers.cpp b/tests/TestCTimers.cpp index e8358e472..265fb88ab 100644 --- a/tests/TestCTimers.cpp +++ b/tests/TestCTimers.cpp @@ -60,8 +60,6 @@ void TestCTimers::testElapsedTime() timers.addTimer(timerName.toStdString(), timerDesc.toStdString()); -// QTest::qWait(1100); // Wait 1.1 seconds - QString timersList = QString::fromStdString(timers.getTimers()); QVERIFY(timersList.contains("up for - 0:00"));