Skip to content

Commit bc3354a

Browse files
committed
[test] Mock clock uses real clock by default
1 parent 7d7d27c commit bc3354a

File tree

12 files changed

+104
-8
lines changed

12 files changed

+104
-8
lines changed

test/config/lbuild.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,8 @@
1212
<modules>
1313
<module>modm:build:scons</module>
1414
</modules>
15+
<collectors>
16+
<collect name="modm:build:cppdefines">MODM_CHRONO_MILLI_CLOCK_NOW=modm_platform_milli_now</collect>
17+
<collect name="modm:build:cppdefines">MODM_CHRONO_MICRO_CLOCK_NOW=modm_platform_micro_now</collect>
18+
</collectors>
1519
</library>

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: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,16 @@
1414

1515
// ----------------------------------------------------------------------------
1616
static uint32_t milli_time{0};
17+
static bool milli_enabled{false};
18+
19+
modm::chrono::milli_clock::time_point
20+
modm_platform_milli_now() noexcept;
1721

1822
modm::chrono::milli_clock::time_point
1923
modm::chrono::milli_clock::now() noexcept
2024
{
21-
return time_point{duration{milli_time}};
25+
if (milli_enabled) return time_point{duration{milli_time}};
26+
return modm_platform_milli_now();
2227
}
2328

2429
void
@@ -33,15 +38,31 @@ modm_test::chrono::milli_clock::increment(uint32_t milliseconds)
3338
milli_time += milliseconds;
3439
}
3540

41+
void
42+
modm_test::chrono::milli_clock::enable()
43+
{
44+
milli_enabled = true;
45+
}
46+
47+
void
48+
modm_test::chrono::milli_clock::disable()
49+
{
50+
milli_enabled = false;
51+
}
52+
3653
// ----------------------------------------------------------------------------
3754
static uint32_t micro_time{0};
55+
static bool micro_enabled{false};
56+
57+
modm::chrono::micro_clock::time_point
58+
modm_platform_micro_now() noexcept;
3859

3960
modm::chrono::micro_clock::time_point
4061
modm::chrono::micro_clock::now() noexcept
4162
{
42-
return time_point{duration{micro_time}};
63+
if (micro_enabled) return time_point{duration{micro_time}};
64+
return modm_platform_micro_now();
4365
}
44-
4566
void
4667
modm_test::chrono::micro_clock::setTime(uint32_t microseconds)
4768
{
@@ -53,3 +74,15 @@ modm_test::chrono::micro_clock::increment(uint32_t microseconds)
5374
{
5475
micro_time += microseconds;
5576
}
77+
78+
void
79+
modm_test::chrono::micro_clock::enable()
80+
{
81+
micro_enabled = true;
82+
}
83+
84+
void
85+
modm_test::chrono::micro_clock::disable()
86+
{
87+
micro_enabled = false;
88+
}

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

0 commit comments

Comments
 (0)