Timer fixes & tests added
This commit is contained in:
committed by
GitHub
parent
47377439dd
commit
4f89918e91
@@ -0,0 +1,52 @@
|
||||
#include "doctest.h"
|
||||
#include "bundle.h"
|
||||
#include <cstring>
|
||||
|
||||
TEST_CASE("boolean can be stored and retrieved") {
|
||||
Bundle bundle = tt_bundle_alloc();
|
||||
tt_bundle_put_bool(bundle, "key", true);
|
||||
CHECK(tt_bundle_get_bool(bundle, "key"));
|
||||
bool opt_result = false;
|
||||
CHECK(tt_bundle_opt_bool(bundle, "key", &opt_result));
|
||||
CHECK(tt_bundle_has_bool(bundle, "key"));
|
||||
CHECK_EQ(opt_result, true);
|
||||
tt_bundle_free(bundle);
|
||||
}
|
||||
|
||||
TEST_CASE("int32 can be stored and retrieved") {
|
||||
Bundle bundle = tt_bundle_alloc();
|
||||
tt_bundle_put_int32(bundle, "key", 42);
|
||||
CHECK(tt_bundle_get_int32(bundle, "key"));
|
||||
int32_t opt_result = 0;
|
||||
CHECK(tt_bundle_opt_int32(bundle, "key", &opt_result));
|
||||
CHECK(tt_bundle_has_int32(bundle, "key"));
|
||||
CHECK_EQ(opt_result, 42);
|
||||
tt_bundle_free(bundle);
|
||||
}
|
||||
|
||||
TEST_CASE("string can be stored and retrieved") {
|
||||
Bundle bundle = tt_bundle_alloc();
|
||||
tt_bundle_put_string(bundle, "key", "value");
|
||||
const char* value_from_bundle = tt_bundle_get_string(bundle, "key");
|
||||
CHECK_EQ(strcmp(value_from_bundle, "value"), 0);
|
||||
char* opt_result = NULL;
|
||||
CHECK(tt_bundle_opt_string(bundle, "key", &opt_result));
|
||||
CHECK(tt_bundle_has_string(bundle, "key"));
|
||||
CHECK(opt_result != NULL);
|
||||
tt_bundle_free(bundle);
|
||||
}
|
||||
|
||||
TEST_CASE("bundle copy holds all copied values when original is freed") {
|
||||
Bundle original = tt_bundle_alloc();
|
||||
tt_bundle_put_bool(original, "bool", true);
|
||||
tt_bundle_put_int32(original, "int32", 123);
|
||||
tt_bundle_put_string(original, "string", "text");
|
||||
|
||||
Bundle copy = tt_bundle_alloc_copy(original);
|
||||
tt_bundle_free(original);
|
||||
|
||||
CHECK(tt_bundle_get_bool(copy, "bool") == true);
|
||||
CHECK_EQ(tt_bundle_get_int32(copy, "int32"), 123);
|
||||
CHECK_EQ(strcmp(tt_bundle_get_string(copy, "string"), "text"), 0);
|
||||
tt_bundle_free(copy);
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
#include "doctest.h"
|
||||
#include "tactility_core.h"
|
||||
#include "mutex.h"
|
||||
|
||||
static int thread_with_mutex_parameter(void* parameter) {
|
||||
Mutex mutex = (Mutex)parameter;
|
||||
tt_mutex_acquire(mutex, TtWaitForever);
|
||||
return 0;
|
||||
}
|
||||
|
||||
TEST_CASE("a mutex can block a thread") {
|
||||
Mutex mutex = tt_mutex_alloc(MutexTypeNormal);
|
||||
tt_mutex_acquire(mutex, TtWaitForever);
|
||||
|
||||
Thread* thread = tt_thread_alloc_ex(
|
||||
"thread",
|
||||
1024,
|
||||
&thread_with_mutex_parameter,
|
||||
mutex
|
||||
);
|
||||
tt_thread_start(thread);
|
||||
|
||||
tt_delay_ms(5);
|
||||
CHECK_EQ(tt_thread_get_state(thread), ThreadStateRunning);
|
||||
|
||||
tt_mutex_release(mutex);
|
||||
|
||||
tt_delay_ms(5);
|
||||
CHECK_EQ(tt_thread_get_state(thread), ThreadStateStopped);
|
||||
|
||||
tt_thread_join(thread);
|
||||
tt_thread_free(thread);
|
||||
tt_mutex_free(mutex);
|
||||
}
|
||||
@@ -1,40 +1,39 @@
|
||||
#include "doctest.h"
|
||||
#include "tactility_core.h"
|
||||
#include "thread.h"
|
||||
|
||||
#include "FreeRTOS.h"
|
||||
#include "task.h"
|
||||
|
||||
static int interruptable_thread(TT_UNUSED void* parameter) {
|
||||
static int interruptable_thread(void* parameter) {
|
||||
bool* interrupted = (bool*)parameter;
|
||||
while (!*interrupted) {
|
||||
vTaskDelay(5);
|
||||
tt_delay_ms(5);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
static bool immedate_return_thread_called = false;
|
||||
static int immediate_return_thread(TT_UNUSED void* parameter) {
|
||||
immedate_return_thread_called = true;
|
||||
static int immediate_return_thread(void* parameter) {
|
||||
bool* has_called = (bool*)parameter;
|
||||
*has_called = true;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int thread_with_return_code(TT_UNUSED void* parameter) {
|
||||
static int thread_with_return_code(void* parameter) {
|
||||
int* code = (int*)parameter;
|
||||
return *code;
|
||||
}
|
||||
|
||||
TEST_CASE("when a thread is started then its callback should be called") {
|
||||
bool has_called = false;
|
||||
Thread* thread = tt_thread_alloc_ex(
|
||||
"immediate return task",
|
||||
4096,
|
||||
&immediate_return_thread,
|
||||
NULL
|
||||
&has_called
|
||||
);
|
||||
CHECK(!immedate_return_thread_called);
|
||||
CHECK(!has_called);
|
||||
tt_thread_start(thread);
|
||||
tt_thread_join(thread);
|
||||
tt_thread_free(thread);
|
||||
CHECK(immedate_return_thread_called);
|
||||
CHECK(has_called);
|
||||
}
|
||||
|
||||
TEST_CASE("a thread can be started and stopped") {
|
||||
|
||||
@@ -0,0 +1,63 @@
|
||||
#include "doctest.h"
|
||||
#include "tactility_core.h"
|
||||
#include "timer.h"
|
||||
|
||||
|
||||
void* timer_callback_context = NULL;
|
||||
static void timer_callback_with_context(void* context) {
|
||||
timer_callback_context = context;
|
||||
}
|
||||
|
||||
static void timer_callback_with_counter(void* context) {
|
||||
int* int_ptr = (int*)context;
|
||||
(*int_ptr)++;
|
||||
}
|
||||
|
||||
TEST_CASE("a timer passes the context correctly") {
|
||||
int foo = 1;
|
||||
Timer* timer = tt_timer_alloc(&timer_callback_with_context, TimerTypeOnce, &foo);
|
||||
tt_timer_start(timer, 1);
|
||||
tt_delay_tick(10);
|
||||
tt_timer_stop(timer);
|
||||
tt_timer_free(timer);
|
||||
|
||||
CHECK_EQ(timer_callback_context, &foo);
|
||||
}
|
||||
|
||||
TEST_CASE("TimerTypePeriodic timers can be stopped and restarted") {
|
||||
int counter = 0;
|
||||
Timer* timer = tt_timer_alloc(&timer_callback_with_counter, TimerTypePeriodic, &counter);
|
||||
tt_timer_start(timer, 1);
|
||||
tt_delay_tick(10);
|
||||
tt_timer_stop(timer);
|
||||
tt_delay_tick(10);
|
||||
tt_timer_stop(timer);
|
||||
tt_timer_free(timer);
|
||||
|
||||
CHECK_GE(counter, 2);
|
||||
}
|
||||
|
||||
TEST_CASE("TimerTypePeriodic calls the callback periodically") {
|
||||
int counter = 0;
|
||||
int ticks_to_run = 10;
|
||||
Timer* timer = tt_timer_alloc(&timer_callback_with_counter, TimerTypePeriodic, &counter);
|
||||
tt_timer_start(timer, 1);
|
||||
tt_delay_tick(ticks_to_run);
|
||||
tt_timer_stop(timer);
|
||||
tt_timer_free(timer);
|
||||
|
||||
CHECK_EQ(counter, ticks_to_run);
|
||||
}
|
||||
|
||||
TEST_CASE("restarting TimerTypeOnce timers has no effect") {
|
||||
int counter = 0;
|
||||
Timer* timer = tt_timer_alloc(&timer_callback_with_counter, TimerTypeOnce, &counter);
|
||||
tt_timer_start(timer, 1);
|
||||
tt_delay_tick(10);
|
||||
tt_timer_stop(timer);
|
||||
tt_delay_tick(10);
|
||||
tt_timer_stop(timer);
|
||||
tt_timer_free(timer);
|
||||
|
||||
CHECK_EQ(counter, 1);
|
||||
}
|
||||
Reference in New Issue
Block a user