Thread and Timer converted to class (#81)

This commit is contained in:
Ken Van Hoeylandt
2024-11-22 23:08:18 +01:00
committed by GitHub
parent 85e26636a3
commit 854fefa1a1
20 changed files with 550 additions and 679 deletions
+3
View File
@@ -7,6 +7,9 @@ set(CMAKE_CXX_COMPILER g++)
file(GLOB_RECURSE TEST_SOURCES ${PROJECT_SOURCE_DIR}/*.cpp)
add_executable(TactilityCoreTests EXCLUDE_FROM_ALL ${TEST_SOURCES})
add_definitions(-D_Nullable=)
add_definitions(-D_Nonnull=)
target_include_directories(TactilityCoreTests PRIVATE
${DOCTESTINC}
)
+6 -6
View File
@@ -14,23 +14,23 @@ TEST_CASE("a mutex can block a thread") {
auto* mutex = tt_mutex_alloc(MutexTypeNormal);
tt_mutex_acquire(mutex, TtWaitForever);
Thread* thread = thread_alloc_ex(
Thread* thread = new Thread(
"thread",
1024,
&thread_with_mutex_parameter,
mutex
);
thread_start(thread);
thread->start();
delay_ms(5);
CHECK_EQ(thread_get_state(thread), ThreadStateRunning);
CHECK_EQ(thread->getState(), Thread::StateRunning);
tt_mutex_release(mutex);
delay_ms(5);
CHECK_EQ(thread_get_state(thread), ThreadStateStopped);
CHECK_EQ(thread->getState(), Thread::StateStopped);
thread_join(thread);
thread_free(thread);
thread->join();
delete thread;
tt_mutex_free(mutex);
}
+28 -28
View File
@@ -25,79 +25,79 @@ static int thread_with_return_code(void* parameter) {
TEST_CASE("when a thread is started then its callback should be called") {
bool has_called = false;
auto* thread = thread_alloc_ex(
auto* thread = new Thread(
"immediate return task",
4096,
&immediate_return_thread,
&has_called
);
CHECK(!has_called);
thread_start(thread);
thread_join(thread);
thread_free(thread);
thread->start();
thread->join();
delete thread;
CHECK(has_called);
}
TEST_CASE("a thread can be started and stopped") {
bool interrupted = false;
auto* thread = thread_alloc_ex(
auto* thread = new Thread(
"interruptable thread",
4096,
&interruptable_thread,
&interrupted
);
CHECK(thread);
thread_start(thread);
thread->start();
interrupted = true;
thread_join(thread);
thread_free(thread);
thread->join();
delete thread;
}
TEST_CASE("thread id should only be set at when thread is started") {
bool interrupted = false;
auto* thread = thread_alloc_ex(
auto* thread = new Thread(
"interruptable thread",
4096,
&interruptable_thread,
&interrupted
);
CHECK_EQ(thread_get_id(thread), nullptr);
thread_start(thread);
CHECK_NE(thread_get_id(thread), nullptr);
CHECK_EQ(thread->getId(), nullptr);
thread->start();
CHECK_NE(thread->getId(), nullptr);
interrupted = true;
thread_join(thread);
CHECK_EQ(thread_get_id(thread), nullptr);
thread_free(thread);
thread->join();
CHECK_EQ(thread->getId(), nullptr);
delete thread;
}
TEST_CASE("thread state should be correct") {
bool interrupted = false;
auto* thread = thread_alloc_ex(
auto* thread = new Thread(
"interruptable thread",
4096,
&interruptable_thread,
&interrupted
);
CHECK_EQ(thread_get_state(thread), ThreadStateStopped);
thread_start(thread);
ThreadState state = thread_get_state(thread);
CHECK((state == ThreadStateStarting || state == ThreadStateRunning));
CHECK_EQ(thread->getState(), Thread::StateStopped);
thread->start();
Thread::State state = thread->getState();
CHECK((state == Thread::StateStarting || state == Thread::StateRunning));
interrupted = true;
thread_join(thread);
CHECK_EQ(thread_get_state(thread), ThreadStateStopped);
thread_free(thread);
thread->join();
CHECK_EQ(thread->getState(), Thread::StateStopped);
delete thread;
}
TEST_CASE("thread id should only be set at when thread is started") {
int code = 123;
auto* thread = thread_alloc_ex(
auto* thread = new Thread(
"return code",
4096,
&thread_with_return_code,
&code
);
thread_start(thread);
thread_join(thread);
CHECK_EQ(thread_get_return_code(thread), code);
thread_free(thread);
thread->start();
thread->join();
CHECK_EQ(thread->getReturnCode(), code);
delete thread;
}
+22 -20
View File
@@ -16,24 +16,25 @@ static void timer_callback_with_counter(void* context) {
TEST_CASE("a timer passes the context correctly") {
int foo = 1;
auto* timer = timer_alloc(&timer_callback_with_context, TimerTypeOnce, &foo);
timer_start(timer, 1);
auto* timer = new Timer(Timer::TypeOnce, &timer_callback_with_context, &foo);
timer->start(1);
delay_tick(10);
timer_stop(timer);
timer_free(timer);
timer->stop();
delete timer;
CHECK_EQ(timer_callback_context, &foo);
}
TEST_CASE("TimerTypePeriodic timers can be stopped and restarted") {
int counter = 0;
auto* timer = timer_alloc(&timer_callback_with_counter, TimerTypePeriodic, &counter);
timer_start(timer, 1);
auto* timer = new Timer(Timer::TypePeriodic, &timer_callback_with_counter, &counter);
timer->start(1);
delay_tick(10);
timer_stop(timer);
timer->stop();
timer->start(1);
delay_tick(10);
timer_stop(timer);
timer_free(timer);
timer->stop();
delete timer;
CHECK_GE(counter, 2);
}
@@ -41,24 +42,25 @@ TEST_CASE("TimerTypePeriodic timers can be stopped and restarted") {
TEST_CASE("TimerTypePeriodic calls the callback periodically") {
int counter = 0;
int ticks_to_run = 10;
auto* timer = timer_alloc(&timer_callback_with_counter, TimerTypePeriodic, &counter);
timer_start(timer, 1);
auto* timer = new Timer(Timer::TypePeriodic, &timer_callback_with_counter, &counter);
timer->start(1);
delay_tick(ticks_to_run);
timer_stop(timer);
timer_free(timer);
timer->stop();
delete timer;
CHECK_EQ(counter, ticks_to_run);
}
TEST_CASE("restarting TimerTypeOnce timers has no effect") {
TEST_CASE("restarting TimerTypeOnce timers calls the callback again") {
int counter = 0;
auto* timer = timer_alloc(&timer_callback_with_counter, TimerTypeOnce, &counter);
timer_start(timer, 1);
auto* timer = new Timer(Timer::TypeOnce, &timer_callback_with_counter, &counter);
timer->start(1);
delay_tick(10);
timer_stop(timer);
timer->stop();
timer->start(1);
delay_tick(10);
timer_stop(timer);
timer_free(timer);
timer->stop();
delete timer;
CHECK_EQ(counter, 1);
CHECK_EQ(counter, 2);
}