diff --git a/tactility-core/src/timer.c b/tactility-core/src/timer.c index fa98228b..afaf5a71 100644 --- a/tactility-core/src/timer.c +++ b/tactility-core/src/timer.c @@ -16,13 +16,7 @@ typedef struct { } TimerCallback_t; static void timer_callback(TimerHandle_t hTimer) { - TimerCallback_t* callb; - - /* Retrieve pointer to callback function and context */ - callb = (TimerCallback_t*)pvTimerGetTimerID(hTimer); - - /* Remove dynamic allocation flag */ - callb = (TimerCallback_t*)((uint32_t)callb & ~1U); + TimerCallback_t* callb = (TimerCallback_t*)pvTimerGetTimerID(hTimer); if (callb != NULL) { callb->func(callb->context); @@ -31,36 +25,26 @@ static void timer_callback(TimerHandle_t hTimer) { Timer* tt_timer_alloc(TimerCallback func, TimerType type, void* context) { tt_assert((tt_kernel_is_irq() == 0U) && (func != NULL)); - - TimerHandle_t hTimer; - TimerCallback_t* callb; - UBaseType_t reload; - - hTimer = NULL; - - /* Dynamic memory allocation is available: if memory for callback and */ - /* its context is not provided, allocate it from dynamic memory pool */ - callb = (TimerCallback_t*)malloc(sizeof(TimerCallback_t)); + TimerCallback_t* callb = (TimerCallback_t*)malloc(sizeof(TimerCallback_t)); callb->func = func; callb->context = context; + UBaseType_t reload; if (type == TimerTypeOnce) { reload = pdFALSE; } else { reload = pdTRUE; } - /* Store callback memory dynamic allocation flag */ - callb = (TimerCallback_t*)((uint32_t)callb | 1U); // TimerCallback function is always provided as a callback and is used to call application // specified function with its context both stored in structure callb. // TODO: should we use pointer to function or function directly as-is? - hTimer = xTimerCreate(NULL, portMAX_DELAY, reload, callb, timer_callback); + TimerHandle_t hTimer = xTimerCreate(NULL, portMAX_DELAY, reload, callb, timer_callback); tt_check(hTimer); /* Return timer ID */ - return ((Timer*)hTimer); + return (Timer*)hTimer; } void tt_timer_free(Timer* instance) { @@ -68,21 +52,14 @@ void tt_timer_free(Timer* instance) { tt_assert(instance); TimerHandle_t hTimer = (TimerHandle_t)instance; - TimerCallback_t* callb; - - callb = (TimerCallback_t*)pvTimerGetTimerID(hTimer); + TimerCallback_t* callb = (TimerCallback_t*)pvTimerGetTimerID(hTimer); tt_check(xTimerDelete(hTimer, portMAX_DELAY) == pdPASS); while (tt_timer_is_running(instance)) tt_delay_tick(2); - if ((uint32_t)callb & 1U) { - /* Callback memory was allocated from dynamic pool, clear flag */ - callb = (TimerCallback_t*)((uint32_t)callb & ~1U); - - /* Return allocated memory to dynamic pool */ - free(callb); - } + /* Return allocated memory to dynamic pool */ + free(callb); } TtStatus tt_timer_start(Timer* instance, uint32_t ticks) { diff --git a/tests/tactility-core/bundle_test.cpp b/tests/tactility-core/bundle_test.cpp new file mode 100644 index 00000000..fd094fe7 --- /dev/null +++ b/tests/tactility-core/bundle_test.cpp @@ -0,0 +1,52 @@ +#include "doctest.h" +#include "bundle.h" +#include + +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); +} diff --git a/tests/tactility-core/mutex_test.cpp b/tests/tactility-core/mutex_test.cpp new file mode 100644 index 00000000..a0ea7b70 --- /dev/null +++ b/tests/tactility-core/mutex_test.cpp @@ -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); +} diff --git a/tests/tactility-core/thread_test.cpp b/tests/tactility-core/thread_test.cpp index 3b2aac36..24d6fa5c 100644 --- a/tests/tactility-core/thread_test.cpp +++ b/tests/tactility-core/thread_test.cpp @@ -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") { diff --git a/tests/tactility-core/timer_test.cpp b/tests/tactility-core/timer_test.cpp new file mode 100644 index 00000000..95ea914c --- /dev/null +++ b/tests/tactility-core/timer_test.cpp @@ -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); +}