Skip to content

Commit

Permalink
[test] Adapt all timer and chrono tests
Browse files Browse the repository at this point in the history
  • Loading branch information
salkinium committed Apr 8, 2020
1 parent 0809dcf commit 00b8f79
Show file tree
Hide file tree
Showing 17 changed files with 427 additions and 414 deletions.
44 changes: 15 additions & 29 deletions test/modm/architecture/driver/clock_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,43 +13,29 @@
// ----------------------------------------------------------------------------

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

void
ClockTest::testClock()
{
// test 16bit timestamp "ShortTimestamp"
TestingClock::time = 0;
TEST_ASSERT_EQUALS(modm::Clock::nowShort(), modm::ShortTimestamp(0));

TestingClock::time = 10;
TEST_ASSERT_EQUALS(modm::Clock::nowShort(), modm::ShortTimestamp(10));

TestingClock::time = 65'000;
TEST_ASSERT_EQUALS(modm::Clock::nowShort(), modm::ShortTimestamp(65'000));

TestingClock::time = 65'535;
TEST_ASSERT_EQUALS(modm::Clock::nowShort(), modm::ShortTimestamp(65'535));

// overflow in timestamp, but not the Clock!
TestingClock::time = 65'536;
TEST_ASSERT_EQUALS(modm::Clock::nowShort(), modm::ShortTimestamp(0));


using time_point = modm::Clock::time_point;
using duration = modm::Clock::duration;
// test 32bit timestamp "Timestamp"
TestingClock::time = 0;
TEST_ASSERT_EQUALS(modm::Clock::now(), modm::Timestamp(0));
test_clock::setTime(0);
TEST_ASSERT_EQUALS(modm::Clock::now(), time_point(duration(0)));

TestingClock::time = 10;
TEST_ASSERT_EQUALS(modm::Clock::now(), modm::Timestamp(10));
test_clock::setTime(10);
TEST_ASSERT_EQUALS(modm::Clock::now(), time_point(duration(10)));

TestingClock::time = 65'536;
TEST_ASSERT_EQUALS(modm::Clock::now(), modm::Timestamp(65'536));
test_clock::setTime(65'536);
TEST_ASSERT_EQUALS(modm::Clock::now(), time_point(duration(65'536)));

TestingClock::time = 4'294'967'295;
TEST_ASSERT_EQUALS(modm::Clock::now(), modm::Timestamp(4'294'967'295));
test_clock::setTime(4'294'967'295);
TEST_ASSERT_EQUALS(modm::Clock::now(), time_point(duration(4'294'967'295)));

// overflow in both timestamp and Clock!
TestingClock::time = uint32_t(4'294'967'296);
TEST_ASSERT_EQUALS(modm::Clock::now(), modm::Timestamp(0));
test_clock::setTime(uint32_t(4'294'967'296));
TEST_ASSERT_EQUALS(modm::Clock::now(), time_point(duration(0)));
}
10 changes: 2 additions & 8 deletions test/modm/architecture/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def init(module):

def prepare(module, options):
module.depends(
":mock:clock.testing",
":mock:clock",
"modm:architecture:accessor",
"modm:architecture:atomic",
"modm:architecture:can",
Expand All @@ -28,11 +28,5 @@ def prepare(module, options):

def build(env):
env.outbasepath = "modm-test/src/modm-test/architecture"
env.copy('.')

ignore_patterns = []
target = env[":target"].identifier
if target["platform"] == "hosted":
# Test does not work on hosted
ignore_patterns.append("*driver/clock_test.*")

env.copy('.', ignore=env.ignore_patterns(*ignore_patterns))
2 changes: 1 addition & 1 deletion test/modm/communication/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class Xpcc(Module):
module.name = "xpcc"

def prepare(self, module, options):
module.depends("modm:communication:xpcc", ":mock:clock.testing")
module.depends("modm:communication:xpcc", ":mock:clock")
return True

def build(self, env):
Expand Down
15 changes: 8 additions & 7 deletions test/modm/communication/xpcc/dispatcher_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
#define UNITTEST_RETURN_ON_FAIL
#include "dispatcher_test.hpp"

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

// ----------------------------------------------------------------------------
void
Expand Down Expand Up @@ -399,7 +400,7 @@ DispatcherTest::testActionRetransmissionWithAbort()
backend->messagesSend.removeAll();

// reset time so that the timeout is expired
TestingClock::time += 500;
test_clock::increment(500);

dispatcher->update();
}
Expand Down Expand Up @@ -427,7 +428,7 @@ DispatcherTest::testActionRetransmission()

if (i == 0) {
// reset time so that the timeout is expired
TestingClock::time += 500;
test_clock::increment(500);
}

dispatcher->update();
Expand All @@ -439,7 +440,7 @@ DispatcherTest::testActionRetransmission()
modm::SmartPointer()));

// reset time so that the timeout is expired
TestingClock::time += 500;
test_clock::increment(500);

dispatcher->update();

Expand Down Expand Up @@ -477,7 +478,7 @@ DispatcherTest::testResponseRetransmissionWithAbort()
backend->messagesSend.removeFront();

// reset time so that the timeout is expired
TestingClock::time += 500;
test_clock::increment(500);

dispatcher->update();
}
Expand Down Expand Up @@ -509,7 +510,7 @@ DispatcherTest::testResponseRetransmission()

if (i == 0) {
// reset time so that the timeout is expired
TestingClock::time += 500;
test_clock::increment(500);
}

dispatcher->update();
Expand All @@ -521,7 +522,7 @@ DispatcherTest::testResponseRetransmission()
modm::SmartPointer()));

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

dispatcher->update();

Expand Down
55 changes: 55 additions & 0 deletions test/modm/mock/clock.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/*
* Copyright (c) 2017-2018, Niklas Hauser
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#include <modm/architecture/interface/clock.hpp>
#include "clock.hpp"

// ----------------------------------------------------------------------------
static uint32_t milli_time{0};

modm::chrono::milli_clock::time_point
modm::chrono::milli_clock::now() noexcept
{
return time_point{duration{milli_time}};
}

void
modm_test::chrono::milli_clock::setTime(uint32_t milliseconds)
{
milli_time = milliseconds;
}

void
modm_test::chrono::milli_clock::increment(uint32_t milliseconds)
{
milli_time += milliseconds;
}

// ----------------------------------------------------------------------------
static uint32_t micro_time{0};

modm::chrono::micro_clock::time_point
modm::chrono::micro_clock::now() noexcept
{
return time_point{duration{micro_time}};
}

void
modm_test::chrono::micro_clock::setTime(uint32_t microseconds)
{
micro_time = microseconds;
}

void
modm_test::chrono::micro_clock::increment(uint32_t microseconds)
{
micro_time += microseconds;
}
44 changes: 44 additions & 0 deletions test/modm/mock/clock.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2020, Niklas Hauser
*
* This file is part of the modm project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
// ----------------------------------------------------------------------------

#pragma once
#include <stdint.h>
#include <modm/architecture/interface/clock.hpp>

/// @ingroup modm_test_mock_clock
namespace modm_test::chrono
{

class milli_clock : modm::chrono::milli_clock
{
public:
static inline void setTime(std::chrono::milliseconds time)
{ setTime(time.count()); }
static void setTime(uint32_t milliseconds);

static inline void increment(std::chrono::milliseconds time)
{ increment(time.count()); }
static void increment(uint32_t milliseconds);
};

class micro_clock : modm::chrono::micro_clock
{
public:
static inline void setTime(std::chrono::microseconds time)
{ setTime(time.count()); }
static void setTime(uint32_t microseconds);

static inline void increment(std::chrono::microseconds time)
{ increment(time.count()); }
static void increment(uint32_t microseconds);
};

} // namespace modm_test
14 changes: 0 additions & 14 deletions test/modm/mock/clock_dummy.cpp

This file was deleted.

63 changes: 0 additions & 63 deletions test/modm/mock/clock_dummy.hpp

This file was deleted.

25 changes: 6 additions & 19 deletions test/modm/mock/module.lb
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,19 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.


class ClockDummy(Module):
class Clock(Module):
def init(self, module):
module.name = "clock.dummy"
module.name = "clock"
module.description = "System Tick Mockup"

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

def build(self, env):
env.outbasepath = "modm-test/src/modm-test/mock"
env.copy("clock_dummy.hpp")
env.copy("clock_dummy.cpp")

class ClockTesting(Module):
def init(self, module):
module.name = "clock.testing"

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

def build(self, env):
env.outbasepath = "modm-test/src/modm-test/mock"
env.copy("testing_clock.hpp")
env.copy("testing_clock.cpp")
env.copy("clock.hpp")
env.copy("clock.cpp")

class SpiDevice(Module):
def init(self, module):
Expand Down Expand Up @@ -79,8 +67,7 @@ def init(module):
module.name = ":mock"

def prepare(module, options):
module.add_submodule(ClockDummy())
module.add_submodule(ClockTesting())
module.add_submodule(Clock())
module.add_submodule(SpiDevice())
module.add_submodule(SpiMaster())
module.add_submodule(IoDevice())
Expand Down
Loading

0 comments on commit 00b8f79

Please sign in to comment.