Skip to content

Commit 0999942

Browse files
committed
[test] Mock clock uses real clock by default
1 parent 05f0a99 commit 0999942

File tree

11 files changed

+94
-8
lines changed

11 files changed

+94
-8
lines changed

test/modm/architecture/driver/clock_test.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@
1717
#include <modm-test/mock/clock.hpp>
1818
using test_clock = modm_test::chrono::milli_clock;
1919

20+
void
21+
ClockTest::setUp()
22+
{
23+
test_clock::enable();
24+
}
25+
26+
void
27+
ClockTest::tearDown()
28+
{
29+
test_clock::disable();
30+
}
31+
2032
void
2133
ClockTest::testClock()
2234
{

test/modm/architecture/driver/clock_test.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@
2424
class ClockTest : public unittest::TestSuite
2525
{
2626
public:
27+
void
28+
setUp() override;
29+
30+
void
31+
tearDown() override;
32+
2733
void
2834
testClock();
2935
};

test/modm/communication/xpcc/dispatcher_test.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ using test_clock = modm_test::chrono::milli_clock;
2121
void
2222
DispatcherTest::setUp()
2323
{
24+
test_clock::enable();
2425
backend = new FakeBackend();
2526
postman = new FakePostman();
2627
dispatcher = new xpcc::Dispatcher(backend, postman);
@@ -36,6 +37,7 @@ DispatcherTest::setUp()
3637
void
3738
DispatcherTest::tearDown()
3839
{
40+
test_clock::disable();
3941
delete component2;
4042
delete component1;
4143
delete timeline;

test/modm/mock/clock.cpp

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,13 @@
1414

1515
// ----------------------------------------------------------------------------
1616
static uint32_t milli_time{0};
17+
static bool milli_enabled{false};
1718

1819
modm::chrono::milli_clock::time_point
1920
modm::chrono::milli_clock::now() noexcept
2021
{
21-
return time_point{duration{milli_time}};
22+
if (milli_enabled) return time_point{duration{milli_time}};
23+
return time_point{duration{modm::platform::milli_now()}};
2224
}
2325

2426
void
@@ -33,15 +35,28 @@ modm_test::chrono::milli_clock::increment(uint32_t milliseconds)
3335
milli_time += milliseconds;
3436
}
3537

38+
void
39+
modm_test::chrono::milli_clock::enable()
40+
{
41+
milli_enabled = true;
42+
}
43+
44+
void
45+
modm_test::chrono::milli_clock::disable()
46+
{
47+
milli_enabled = false;
48+
}
49+
3650
// ----------------------------------------------------------------------------
3751
static uint32_t micro_time{0};
52+
static bool micro_enabled{false};
3853

3954
modm::chrono::micro_clock::time_point
4055
modm::chrono::micro_clock::now() noexcept
4156
{
42-
return time_point{duration{micro_time}};
57+
if (micro_enabled) return time_point{duration{micro_time}};
58+
return time_point{duration{modm::platform::micro_now()}};
4359
}
44-
4560
void
4661
modm_test::chrono::micro_clock::setTime(uint32_t microseconds)
4762
{
@@ -53,3 +68,15 @@ modm_test::chrono::micro_clock::increment(uint32_t microseconds)
5368
{
5469
micro_time += microseconds;
5570
}
71+
72+
void
73+
modm_test::chrono::micro_clock::enable()
74+
{
75+
micro_enabled = true;
76+
}
77+
78+
void
79+
modm_test::chrono::micro_clock::disable()
80+
{
81+
micro_enabled = false;
82+
}

test/modm/mock/clock.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ namespace modm_test::chrono
2020
class milli_clock : modm::chrono::milli_clock
2121
{
2222
public:
23+
static void enable();
24+
static void disable();
25+
2326
static inline void setTime(std::chrono::milliseconds time)
2427
{ setTime(time.count()); }
2528
static void setTime(uint32_t milliseconds);
@@ -33,6 +36,9 @@ class milli_clock : modm::chrono::milli_clock
3336
class micro_clock : modm::chrono::micro_clock
3437
{
3538
public:
39+
static void enable();
40+
static void disable();
41+
3642
static inline void setTime(std::chrono::microseconds time)
3743
{ setTime(time.count()); }
3844
static void setTime(uint32_t microseconds);

test/modm/processing/fiber/fiber_test.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,18 @@ using test_clock_us = modm_test::chrono::micro_clock;
2323
void
2424
FiberTest::setUp()
2525
{
26+
test_clock_ms::enable();
27+
test_clock_us::enable();
2628
state = 0;
2729
}
2830

31+
void
32+
FiberTest::tearDown()
33+
{
34+
test_clock_ms::disable();
35+
test_clock_us::disable();
36+
}
37+
2938
// ================================== FIBER ===================================
3039
static void
3140
f1()

test/modm/processing/fiber/fiber_test.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ class FiberTest : public unittest::TestSuite
1818
{
1919
public:
2020
void
21-
setUp();
21+
setUp() override;
22+
23+
void
24+
tearDown() override;
2225

2326
void
2427
testOneFiber();

test/modm/processing/timer/periodic_timer_test.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,16 @@ using test_clock = modm_test::chrono::milli_clock;
2222
void
2323
PeriodicTimerTest::setUp()
2424
{
25+
test_clock::enable();
2526
test_clock::setTime(0);
2627
}
2728

29+
void
30+
PeriodicTimerTest::tearDown()
31+
{
32+
test_clock::disable();
33+
}
34+
2835
void
2936
PeriodicTimerTest::testConstructor()
3037
{

test/modm/processing/timer/periodic_timer_test.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@
1717
class PeriodicTimerTest : public unittest::TestSuite
1818
{
1919
public:
20-
virtual void
21-
setUp();
20+
void
21+
setUp() override;
22+
23+
void
24+
tearDown() override;
2225

2326

2427
void

test/modm/processing/timer/timeout_test.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,17 @@ using test_clock = modm_test::chrono::milli_clock;
2525
void
2626
TimeoutTest::setUp()
2727
{
28+
test_clock::enable();
2829
test_clock::setTime(0);
2930
}
3031

32+
void
33+
TimeoutTest::tearDown()
34+
{
35+
test_clock::disable();
36+
}
37+
38+
3139
void
3240
TimeoutTest::testDefaultConstructor()
3341
{

0 commit comments

Comments
 (0)