mirror of
https://github.com/ByteWelder/Tactility.git
synced 2026-02-18 19:03:16 +00:00
* Remove version from artifact name * Target C++ 20 and higher * Use cpp string * Better crash implementation * String utils in cpp style * Replace parameter methods with start() method * MutexType to Mutex::Type * Kernel c to cpp style * Cleanup event flag * More cpp conversions * Test fixes * Updated ideas docs
69 lines
1.9 KiB
C++
69 lines
1.9 KiB
C++
#include "doctest.h"
|
|
#include "TactilityCore.h"
|
|
#include "Timer.h"
|
|
|
|
#include <utility>
|
|
|
|
using namespace tt;
|
|
|
|
std::shared_ptr<void> timer_callback_context = NULL;
|
|
static void timer_callback_with_context(std::shared_ptr<void> context) {
|
|
timer_callback_context = std::move(context);
|
|
}
|
|
|
|
static void timer_callback_with_counter(std::shared_ptr<void> context) {
|
|
auto int_ptr = std::static_pointer_cast<int>(context);
|
|
(*int_ptr)++;
|
|
}
|
|
|
|
TEST_CASE("a timer passes the context correctly") {
|
|
auto foo = std::make_shared<int>(1);
|
|
auto* timer = new Timer(Timer::TypeOnce, &timer_callback_with_context, foo);
|
|
timer->start(1);
|
|
kernel::delayTicks(10);
|
|
timer->stop();
|
|
delete timer;
|
|
|
|
CHECK_EQ(*std::static_pointer_cast<int>(timer_callback_context), *foo);
|
|
}
|
|
|
|
TEST_CASE("TimerTypePeriodic timers can be stopped and restarted") {
|
|
auto counter = std::make_shared<int>(0);
|
|
auto* timer = new Timer(Timer::TypePeriodic, &timer_callback_with_counter, counter);
|
|
timer->start(1);
|
|
kernel::delayTicks(10);
|
|
timer->stop();
|
|
timer->start(1);
|
|
kernel::delayTicks(10);
|
|
timer->stop();
|
|
delete timer;
|
|
|
|
CHECK_GE(*counter, 2);
|
|
}
|
|
|
|
TEST_CASE("TimerTypePeriodic calls the callback periodically") {
|
|
auto counter = std::make_shared<int>(0);
|
|
int ticks_to_run = 10;
|
|
auto* timer = new Timer(Timer::TypePeriodic, &timer_callback_with_counter, counter);
|
|
timer->start(1);
|
|
kernel::delayTicks(ticks_to_run);
|
|
timer->stop();
|
|
delete timer;
|
|
|
|
CHECK_EQ(*counter, ticks_to_run);
|
|
}
|
|
|
|
TEST_CASE("restarting TimerTypeOnce timers calls the callback again") {
|
|
auto counter = std::make_shared<int>(0);
|
|
auto* timer = new Timer(Timer::TypeOnce, &timer_callback_with_counter, counter);
|
|
timer->start(1);
|
|
kernel::delayTicks(10);
|
|
timer->stop();
|
|
timer->start(1);
|
|
kernel::delayTicks(10);
|
|
timer->stop();
|
|
delete timer;
|
|
|
|
CHECK_EQ(*counter, 2);
|
|
}
|