Skip to content

Commit 00b8f79

Browse files
committed
[test] Adapt all timer and chrono tests
1 parent 0809dcf commit 00b8f79

17 files changed

+427
-414
lines changed

test/modm/architecture/driver/clock_test.cpp

+15-29
Original file line numberDiff line numberDiff line change
@@ -13,43 +13,29 @@
1313
// ----------------------------------------------------------------------------
1414

1515
#include "clock_test.hpp"
16-
#include <modm-test/mock/testing_clock.hpp>
16+
#include <modm/architecture/interface/clock.hpp>
17+
#include <modm-test/mock/clock.hpp>
18+
using test_clock = modm_test::chrono::milli_clock;
1719

1820
void
1921
ClockTest::testClock()
2022
{
21-
// test 16bit timestamp "ShortTimestamp"
22-
TestingClock::time = 0;
23-
TEST_ASSERT_EQUALS(modm::Clock::nowShort(), modm::ShortTimestamp(0));
24-
25-
TestingClock::time = 10;
26-
TEST_ASSERT_EQUALS(modm::Clock::nowShort(), modm::ShortTimestamp(10));
27-
28-
TestingClock::time = 65'000;
29-
TEST_ASSERT_EQUALS(modm::Clock::nowShort(), modm::ShortTimestamp(65'000));
30-
31-
TestingClock::time = 65'535;
32-
TEST_ASSERT_EQUALS(modm::Clock::nowShort(), modm::ShortTimestamp(65'535));
33-
34-
// overflow in timestamp, but not the Clock!
35-
TestingClock::time = 65'536;
36-
TEST_ASSERT_EQUALS(modm::Clock::nowShort(), modm::ShortTimestamp(0));
37-
38-
23+
using time_point = modm::Clock::time_point;
24+
using duration = modm::Clock::duration;
3925
// test 32bit timestamp "Timestamp"
40-
TestingClock::time = 0;
41-
TEST_ASSERT_EQUALS(modm::Clock::now(), modm::Timestamp(0));
26+
test_clock::setTime(0);
27+
TEST_ASSERT_EQUALS(modm::Clock::now(), time_point(duration(0)));
4228

43-
TestingClock::time = 10;
44-
TEST_ASSERT_EQUALS(modm::Clock::now(), modm::Timestamp(10));
29+
test_clock::setTime(10);
30+
TEST_ASSERT_EQUALS(modm::Clock::now(), time_point(duration(10)));
4531

46-
TestingClock::time = 65'536;
47-
TEST_ASSERT_EQUALS(modm::Clock::now(), modm::Timestamp(65'536));
32+
test_clock::setTime(65'536);
33+
TEST_ASSERT_EQUALS(modm::Clock::now(), time_point(duration(65'536)));
4834

49-
TestingClock::time = 4'294'967'295;
50-
TEST_ASSERT_EQUALS(modm::Clock::now(), modm::Timestamp(4'294'967'295));
35+
test_clock::setTime(4'294'967'295);
36+
TEST_ASSERT_EQUALS(modm::Clock::now(), time_point(duration(4'294'967'295)));
5137

5238
// overflow in both timestamp and Clock!
53-
TestingClock::time = uint32_t(4'294'967'296);
54-
TEST_ASSERT_EQUALS(modm::Clock::now(), modm::Timestamp(0));
39+
test_clock::setTime(uint32_t(4'294'967'296));
40+
TEST_ASSERT_EQUALS(modm::Clock::now(), time_point(duration(0)));
5541
}

test/modm/architecture/module.lb

+2-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def init(module):
1717

1818
def prepare(module, options):
1919
module.depends(
20-
":mock:clock.testing",
20+
":mock:clock",
2121
"modm:architecture:accessor",
2222
"modm:architecture:atomic",
2323
"modm:architecture:can",
@@ -28,11 +28,5 @@ def prepare(module, options):
2828

2929
def build(env):
3030
env.outbasepath = "modm-test/src/modm-test/architecture"
31+
env.copy('.')
3132

32-
ignore_patterns = []
33-
target = env[":target"].identifier
34-
if target["platform"] == "hosted":
35-
# Test does not work on hosted
36-
ignore_patterns.append("*driver/clock_test.*")
37-
38-
env.copy('.', ignore=env.ignore_patterns(*ignore_patterns))

test/modm/communication/module.lb

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Xpcc(Module):
2828
module.name = "xpcc"
2929

3030
def prepare(self, module, options):
31-
module.depends("modm:communication:xpcc", ":mock:clock.testing")
31+
module.depends("modm:communication:xpcc", ":mock:clock")
3232
return True
3333

3434
def build(self, env):

test/modm/communication/xpcc/dispatcher_test.cpp

+8-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
#define UNITTEST_RETURN_ON_FAIL
1515
#include "dispatcher_test.hpp"
1616

17-
#include <modm-test/mock/testing_clock.hpp>
17+
#include <modm-test/mock/clock.hpp>
18+
using test_clock = modm_test::chrono::milli_clock;
1819

1920
// ----------------------------------------------------------------------------
2021
void
@@ -399,7 +400,7 @@ DispatcherTest::testActionRetransmissionWithAbort()
399400
backend->messagesSend.removeAll();
400401

401402
// reset time so that the timeout is expired
402-
TestingClock::time += 500;
403+
test_clock::increment(500);
403404

404405
dispatcher->update();
405406
}
@@ -427,7 +428,7 @@ DispatcherTest::testActionRetransmission()
427428

428429
if (i == 0) {
429430
// reset time so that the timeout is expired
430-
TestingClock::time += 500;
431+
test_clock::increment(500);
431432
}
432433

433434
dispatcher->update();
@@ -439,7 +440,7 @@ DispatcherTest::testActionRetransmission()
439440
modm::SmartPointer()));
440441

441442
// reset time so that the timeout is expired
442-
TestingClock::time += 500;
443+
test_clock::increment(500);
443444

444445
dispatcher->update();
445446

@@ -477,7 +478,7 @@ DispatcherTest::testResponseRetransmissionWithAbort()
477478
backend->messagesSend.removeFront();
478479

479480
// reset time so that the timeout is expired
480-
TestingClock::time += 500;
481+
test_clock::increment(500);
481482

482483
dispatcher->update();
483484
}
@@ -509,7 +510,7 @@ DispatcherTest::testResponseRetransmission()
509510

510511
if (i == 0) {
511512
// reset time so that the timeout is expired
512-
TestingClock::time += 500;
513+
test_clock::increment(500);
513514
}
514515

515516
dispatcher->update();
@@ -521,7 +522,7 @@ DispatcherTest::testResponseRetransmission()
521522
modm::SmartPointer()));
522523

523524
// reset time so that the timeout is expired if still active
524-
TestingClock::time += 100;
525+
test_clock::increment(100);
525526

526527
dispatcher->update();
527528

test/modm/mock/clock.cpp

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright (c) 2017-2018, Niklas Hauser
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#include <modm/architecture/interface/clock.hpp>
13+
#include "clock.hpp"
14+
15+
// ----------------------------------------------------------------------------
16+
static uint32_t milli_time{0};
17+
18+
modm::chrono::milli_clock::time_point
19+
modm::chrono::milli_clock::now() noexcept
20+
{
21+
return time_point{duration{milli_time}};
22+
}
23+
24+
void
25+
modm_test::chrono::milli_clock::setTime(uint32_t milliseconds)
26+
{
27+
milli_time = milliseconds;
28+
}
29+
30+
void
31+
modm_test::chrono::milli_clock::increment(uint32_t milliseconds)
32+
{
33+
milli_time += milliseconds;
34+
}
35+
36+
// ----------------------------------------------------------------------------
37+
static uint32_t micro_time{0};
38+
39+
modm::chrono::micro_clock::time_point
40+
modm::chrono::micro_clock::now() noexcept
41+
{
42+
return time_point{duration{micro_time}};
43+
}
44+
45+
void
46+
modm_test::chrono::micro_clock::setTime(uint32_t microseconds)
47+
{
48+
micro_time = microseconds;
49+
}
50+
51+
void
52+
modm_test::chrono::micro_clock::increment(uint32_t microseconds)
53+
{
54+
micro_time += microseconds;
55+
}

test/modm/mock/clock.hpp

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (c) 2020, Niklas Hauser
3+
*
4+
* This file is part of the modm project.
5+
*
6+
* This Source Code Form is subject to the terms of the Mozilla Public
7+
* License, v. 2.0. If a copy of the MPL was not distributed with this
8+
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
9+
*/
10+
// ----------------------------------------------------------------------------
11+
12+
#pragma once
13+
#include <stdint.h>
14+
#include <modm/architecture/interface/clock.hpp>
15+
16+
/// @ingroup modm_test_mock_clock
17+
namespace modm_test::chrono
18+
{
19+
20+
class milli_clock : modm::chrono::milli_clock
21+
{
22+
public:
23+
static inline void setTime(std::chrono::milliseconds time)
24+
{ setTime(time.count()); }
25+
static void setTime(uint32_t milliseconds);
26+
27+
static inline void increment(std::chrono::milliseconds time)
28+
{ increment(time.count()); }
29+
static void increment(uint32_t milliseconds);
30+
};
31+
32+
class micro_clock : modm::chrono::micro_clock
33+
{
34+
public:
35+
static inline void setTime(std::chrono::microseconds time)
36+
{ setTime(time.count()); }
37+
static void setTime(uint32_t microseconds);
38+
39+
static inline void increment(std::chrono::microseconds time)
40+
{ increment(time.count()); }
41+
static void increment(uint32_t microseconds);
42+
};
43+
44+
} // namespace modm_test

test/modm/mock/clock_dummy.cpp

-14
This file was deleted.

test/modm/mock/clock_dummy.hpp

-63
This file was deleted.

test/modm/mock/module.lb

+6-19
Original file line numberDiff line numberDiff line change
@@ -10,31 +10,19 @@
1010
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
1111

1212

13-
class ClockDummy(Module):
13+
class Clock(Module):
1414
def init(self, module):
15-
module.name = "clock.dummy"
15+
module.name = "clock"
16+
module.description = "System Tick Mockup"
1617

1718
def prepare(self, module, options):
1819
module.depends(":architecture:clock")
1920
return True
2021

2122
def build(self, env):
2223
env.outbasepath = "modm-test/src/modm-test/mock"
23-
env.copy("clock_dummy.hpp")
24-
env.copy("clock_dummy.cpp")
25-
26-
class ClockTesting(Module):
27-
def init(self, module):
28-
module.name = "clock.testing"
29-
30-
def prepare(self, module, options):
31-
module.depends(":architecture:clock")
32-
return True
33-
34-
def build(self, env):
35-
env.outbasepath = "modm-test/src/modm-test/mock"
36-
env.copy("testing_clock.hpp")
37-
env.copy("testing_clock.cpp")
24+
env.copy("clock.hpp")
25+
env.copy("clock.cpp")
3826

3927
class SpiDevice(Module):
4028
def init(self, module):
@@ -79,8 +67,7 @@ def init(module):
7967
module.name = ":mock"
8068

8169
def prepare(module, options):
82-
module.add_submodule(ClockDummy())
83-
module.add_submodule(ClockTesting())
70+
module.add_submodule(Clock())
8471
module.add_submodule(SpiDevice())
8572
module.add_submodule(SpiMaster())
8673
module.add_submodule(IoDevice())

0 commit comments

Comments
 (0)