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
+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;
}