Added a test, new structure

This commit is contained in:
Cabooman 2024-02-06 07:55:00 +01:00
parent 1d09dacccb
commit aca520c506
10 changed files with 66 additions and 23 deletions

View file

@ -20,4 +20,4 @@ jobs:
- name: Run unit tests
run: |
cd build/test
./events_test
find . -type f -executable -exec {} \;

View file

@ -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)

View file

@ -1 +0,0 @@
# add_library(utils_library events.cpp)

View file

@ -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;

View file

@ -1,7 +1,9 @@
#ifndef __MYTIMER_H__
#define __MYTIMER_H__
#ifndef UNIT_TEST
#include <Arduino.h>
#endif
class MyTimer {
public:
@ -12,7 +14,7 @@ class MyTimer {
private:
unsigned long interval;
unsigned long previousMillis;
unsigned long previous_millis;
};
#endif // __MYTIMER_H__

View file

@ -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()

View file

@ -3,4 +3,4 @@
MySerial Serial;
unsigned long test_millis = 0;
unsigned long testlib_millis = 0;

View file

@ -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) {

View file

@ -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);

31
test/utils/timer_test.cpp Normal file
View file

@ -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();