Skip to content

Commit

Permalink
Added a test, new structure
Browse files Browse the repository at this point in the history
  • Loading branch information
Cabooman committed Feb 6, 2024
1 parent 1d09dac commit aca520c
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 23 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/unit-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ jobs:
- name: Run unit tests
run: |
cd build/test
./events_test
find . -type f -executable -exec {} \;
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
1 change: 0 additions & 1 deletion Software/src/devboard/utils/CMakeLists.txt

This file was deleted.

10 changes: 6 additions & 4 deletions Software/src/devboard/utils/timer.cpp
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 3 additions & 1 deletion Software/src/devboard/utils/timer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#ifndef __MYTIMER_H__
#define __MYTIMER_H__

#ifndef UNIT_TEST
#include <Arduino.h>
#endif

class MyTimer {
public:
Expand All @@ -12,7 +14,7 @@ class MyTimer {

private:
unsigned long interval;
unsigned long previousMillis;
unsigned long previous_millis;
};

#endif // __MYTIMER_H__
19 changes: 13 additions & 6 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion test/test_lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

MySerial Serial;

unsigned long test_millis = 0;
unsigned long testlib_millis = 0;
6 changes: 4 additions & 2 deletions test/test_lib.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
12 changes: 6 additions & 6 deletions test/events_test.cpp → test/utils/events_test.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// The test library must be included first!
#include "../../../../test/test_lib.h"
#include "test_lib.h"

#include "events.cpp"

Expand All @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions test/utils/timer_test.cpp
Original file line number Diff line number Diff line change
@@ -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();

0 comments on commit aca520c

Please sign in to comment.