Skip to content

Commit

Permalink
timers unit testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Strooom committed Oct 29, 2024
1 parent 0b05ca5 commit df48364
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 11 deletions.
5 changes: 3 additions & 2 deletions lib/timer/intervaltimer.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
#include <intervaltimer.hpp>
#ifndef generic
#include <Arduino.h>
#else
unsigned long intervalTimer::mockMillis{0};
#endif


void intervalTimer::set(unsigned long interval) {
intervalDuration = interval;
}
Expand Down Expand Up @@ -71,7 +72,7 @@ void intervalTimer::stop() {
timerIsRunning = false;
}

bool intervalTimer::isRunning() const{
bool intervalTimer::isRunning() const {
return timerIsRunning;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/timer/intervaltimer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ class intervalTimer {
unsigned long intervalStartTime{0};
unsigned long intervalDuration;
#ifdef generic
unsigned long mockMillis{0}; // mock millis() function for unit testing
static unsigned long mockMillis; // mock millis() function for unit testing
#endif
};
6 changes: 3 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,9 @@ test_framework = unity
test_filter =
;generic/test_tag
;generic/test_logging
;generic/test_singletimer
;generic/test_intervaltimer
generic/test_nci
generic/test_singletimer
generic/test_intervaltimer
;generic/test_nci
;generic/test_ncistate
;generic/test_ncipacket
;generic/test_pn7160interface
Expand Down
26 changes: 23 additions & 3 deletions test/generic/test_intervaltimer/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,46 @@ void test_set() {
TEST_ASSERT_FALSE(testTimer.isRunning());
}

void test_start() {
void test_start_stop() {
intervalTimer testTimer;
testTimer.start();
TEST_ASSERT_TRUE(testTimer.isRunning());
TEST_ASSERT_TRUE(testTimer.timerIsRunning);
testTimer.stop();
TEST_ASSERT_FALSE(testTimer.isRunning());
}

void test_start_or_continue() {
intervalTimer testTimer;
testTimer.start();
testTimer.start(100);
TEST_ASSERT_TRUE(testTimer.isRunning());
TEST_ASSERT_TRUE(testTimer.timerIsRunning);
intervalTimer::mockMillis += 50;
TEST_ASSERT_EQUAL(50, testTimer.value());
testTimer.startOrContinue();
TEST_ASSERT_TRUE(testTimer.isRunning());
TEST_ASSERT_EQUAL(0, testTimer.intervalStartTime);
testTimer.stop();
testTimer.startOrContinue();
TEST_ASSERT_EQUAL(50, testTimer.intervalStartTime);
}

void test_expired() {
intervalTimer testTimer;
testTimer.start(100);
TEST_ASSERT_FALSE(testTimer.expired());
intervalTimer::mockMillis += 101;
TEST_ASSERT_TRUE(testTimer.expired());
TEST_ASSERT_FALSE(testTimer.expired());
}

int main(int argc, char** argv) {
UNITY_BEGIN();
RUN_TEST(test_initialize);
RUN_TEST(test_set);
RUN_TEST(test_start);
RUN_TEST(test_start_stop);
RUN_TEST(test_start_or_continue);
RUN_TEST(test_expired);

UNITY_END();
}
37 changes: 35 additions & 2 deletions test/generic/test_singletimer/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,37 @@ void test_initialize() {
}

void test_start() {
singleTimer::mockMillis = 500;
singleTimer::mockMillis = 100;
singleTimer testTimer;
testTimer.start(200);
TEST_ASSERT_TRUE(testTimer.isRunning());
TEST_ASSERT_TRUE(testTimer.timerIsRunning);
TEST_ASSERT_EQUAL(500, testTimer.startTime);
TEST_ASSERT_EQUAL(100, testTimer.startTime);
TEST_ASSERT_EQUAL(200, testTimer.timerDuration);
TEST_ASSERT_EQUAL(200, testTimer.duration());
TEST_ASSERT_EQUAL(0, testTimer.value());
singleTimer::mockMillis += 100;
TEST_ASSERT_EQUAL(100, testTimer.value());
}

void test_start_or_continue() {
singleTimer::mockMillis = 100;
singleTimer testTimer;
testTimer.start(200);
singleTimer::mockMillis += 100;
testTimer.startOrContinue(400);
TEST_ASSERT_TRUE(testTimer.isRunning());
TEST_ASSERT_EQUAL(100, testTimer.startTime);
TEST_ASSERT_EQUAL(200, testTimer.timerDuration);
TEST_ASSERT_EQUAL(200, testTimer.duration());
testTimer.stop();
singleTimer::mockMillis += 100;
testTimer.startOrContinue(400);
TEST_ASSERT_EQUAL(300, testTimer.startTime);
TEST_ASSERT_EQUAL(400, testTimer.timerDuration);
}


void test_stop() {
singleTimer::mockMillis = 500;
singleTimer testTimer;
Expand All @@ -42,13 +63,25 @@ void test_expired() {
TEST_ASSERT_TRUE(testTimer.expired());
}

void test_expired_and_continue() {
singleTimer::mockMillis = 500;
singleTimer testTimer;
testTimer.start(200);
singleTimer::mockMillis = 700;
TEST_ASSERT_FALSE(testTimer.expiredAndContinue());
singleTimer::mockMillis = 701;
TEST_ASSERT_TRUE(testTimer.expiredAndContinue());
TEST_ASSERT_TRUE(testTimer.isRunning());
}


int main(int argc, char** argv) {
UNITY_BEGIN();
RUN_TEST(test_initialize);
RUN_TEST(test_start);
RUN_TEST(test_start_or_continue);
RUN_TEST(test_stop);
RUN_TEST(test_expired);
RUN_TEST(test_expired_and_continue);
UNITY_END();
}

0 comments on commit df48364

Please sign in to comment.