diff --git a/.github/workflows/unit-tests.yml b/.github/workflows/unit-tests.yml index df92a4e6..1381259a 100644 --- a/.github/workflows/unit-tests.yml +++ b/.github/workflows/unit-tests.yml @@ -20,4 +20,4 @@ jobs: - name: Run unit tests run: | cd build/test - ./events_test + find . -type f -executable -exec {} \; diff --git a/CMakeLists.txt b/CMakeLists.txt index 3239de2d..6f0c4af6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -2,5 +2,5 @@ cmake_minimum_required(VERSION 3.10) project(BatteryEmulator) -add_subdirectory(Software/src/devboard/utils) +# add_subdirectory(Software/src/devboard/utils) add_subdirectory(test) diff --git a/Software/src/devboard/utils/CMakeLists.txt b/Software/src/devboard/utils/CMakeLists.txt deleted file mode 100644 index bded7cb0..00000000 --- a/Software/src/devboard/utils/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -# add_library(utils_library events.cpp) diff --git a/Software/src/devboard/utils/timer.cpp b/Software/src/devboard/utils/timer.cpp index 5b4d59bf..ff051a03 100644 --- a/Software/src/devboard/utils/timer.cpp +++ b/Software/src/devboard/utils/timer.cpp @@ -1,11 +1,13 @@ #include "timer.h" -MyTimer::MyTimer(unsigned long interval) : interval(interval), previousMillis(0) {} +MyTimer::MyTimer(unsigned long interval) : interval(interval) { + previous_millis = millis(); +} bool MyTimer::elapsed() { - unsigned long currentMillis = millis(); - if (currentMillis - previousMillis >= interval) { - previousMillis = currentMillis; + unsigned long current_millis = millis(); + if (current_millis - previous_millis >= interval) { + previous_millis = current_millis; return true; } return false; diff --git a/Software/src/devboard/utils/timer.h b/Software/src/devboard/utils/timer.h index 829ea1e5..e330eb2f 100644 --- a/Software/src/devboard/utils/timer.h +++ b/Software/src/devboard/utils/timer.h @@ -1,7 +1,9 @@ #ifndef __MYTIMER_H__ #define __MYTIMER_H__ +#ifndef UNIT_TEST #include +#endif class MyTimer { public: @@ -12,7 +14,7 @@ class MyTimer { private: unsigned long interval; - unsigned long previousMillis; + unsigned long previous_millis; }; #endif // __MYTIMER_H__ diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b445ece9..6d63e2e3 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,11 +1,18 @@ # Include the directory with your source files -include_directories(${CMAKE_SOURCE_DIR}/Software/src/devboard/utils) +include_directories(${CMAKE_SOURCE_DIR}/Software/src/devboard/utils .) -add_executable(events_test events_test.cpp test_lib.cpp) +# Create a variable to store the list of test files +file(GLOB TEST_SOURCES utils/*.cpp) -target_compile_definitions(events_test PRIVATE UNIT_TEST) +# Loop through each test source file and create an executable +foreach(TEST_SOURCE ${TEST_SOURCES}) + # Extract the test name without extension + get_filename_component(TEST_NAME ${TEST_SOURCE} NAME_WE) -target_link_libraries(events_test) # Link to the library from devboard/utils + # Create an executable for the test + add_executable(${TEST_NAME} ${TEST_SOURCE} test_lib.cpp) -# Register your tests with CTest -add_test(NAME MyProjectTests COMMAND events_test) + # Apply the target_compile_definitions for the test + target_compile_definitions(${TEST_NAME} PRIVATE UNIT_TEST) + +endforeach() diff --git a/test/test_lib.cpp b/test/test_lib.cpp index 372e037c..8ed9c2f5 100644 --- a/test/test_lib.cpp +++ b/test/test_lib.cpp @@ -3,4 +3,4 @@ MySerial Serial; -unsigned long test_millis = 0; +unsigned long testlib_millis = 0; diff --git a/test/test_lib.h b/test/test_lib.h index 87296830..ccca3ca4 100644 --- a/test/test_lib.h +++ b/test/test_lib.h @@ -9,12 +9,14 @@ class MySerial; -extern unsigned long test_millis; +extern unsigned long testlib_millis; +/* Mock millis() */ static inline unsigned long millis(void) { - return test_millis; + return testlib_millis; } +/* Mock Serial class */ class MySerial { public: size_t println(const char* s) { diff --git a/test/events_test.cpp b/test/utils/events_test.cpp similarity index 92% rename from test/events_test.cpp rename to test/utils/events_test.cpp index 1c38a888..1d6c0748 100644 --- a/test/events_test.cpp +++ b/test/utils/events_test.cpp @@ -1,5 +1,5 @@ // The test library must be included first! -#include "../../../../test/test_lib.h" +#include "test_lib.h" #include "events.cpp" @@ -22,28 +22,28 @@ TEST(update_event_timestamps_test) { time_seconds = 0; // No delta, so time shouldn't increase - test_millis = 0; + testlib_millis = 0; update_event_timestamps(); ASSERT_EQ(time_seconds, 0); // Almost time to bump the seconds - test_millis = 999; + testlib_millis = 999; update_event_timestamps(); ASSERT_EQ(time_seconds, 0); ASSERT_EQ(previous_millis, 0); // millis == 1000, so we should add a second - test_millis = 1000; + testlib_millis = 1000; update_event_timestamps(); ASSERT_EQ(time_seconds, 1); ASSERT_EQ(previous_millis, 1000); // We shouldn't add more seconds until 2000 now - test_millis = 1999; + testlib_millis = 1999; update_event_timestamps(); ASSERT_EQ(time_seconds, 1); ASSERT_EQ(previous_millis, 1000); - test_millis = 2000; + testlib_millis = 2000; update_event_timestamps(); ASSERT_EQ(time_seconds, 2); ASSERT_EQ(previous_millis, 2000); diff --git a/test/utils/timer_test.cpp b/test/utils/timer_test.cpp new file mode 100644 index 00000000..613bf739 --- /dev/null +++ b/test/utils/timer_test.cpp @@ -0,0 +1,31 @@ +// The test library must be included first! +#include "timer.cpp" +#include "../test_lib.h" + +/* Helper functions */ + +/* Test functions */ + +TEST(timer_test) { + unsigned long test_interval = 10; + + testlib_millis = 0; + MyTimer timer(test_interval); + ASSERT_EQ(timer.elapsed(), false); + + testlib_millis = test_interval - 1; + ASSERT_EQ(timer.elapsed(), false); + + testlib_millis = test_interval; + ASSERT_EQ(timer.elapsed(), true); + ASSERT_EQ(timer.elapsed(), false); + + testlib_millis = 2 * test_interval - 1; + ASSERT_EQ(timer.elapsed(), false); + + testlib_millis = 2 * test_interval; + ASSERT_EQ(timer.elapsed(), true); + ASSERT_EQ(timer.elapsed(), false); +} + +TEST_MAIN();