From 117001b8986d46040da5c7b07330b5fb99fc7f70 Mon Sep 17 00:00:00 2001 From: Mitya Selivanov <0x7fffff@guattari.ru> Date: Sun, 14 Aug 2022 04:50:43 +0400 Subject: thread, atomic --- source/test/unittests/CMakeLists.txt | 5 +++-- source/test/unittests/atomic.test.c | 26 ++++++++++++++++++++++++++ source/test/unittests/thread.test.c | 18 ++++++++++++++++++ 3 files changed, 47 insertions(+), 2 deletions(-) create mode 100644 source/test/unittests/atomic.test.c create mode 100644 source/test/unittests/thread.test.c (limited to 'source/test/unittests') diff --git a/source/test/unittests/CMakeLists.txt b/source/test/unittests/CMakeLists.txt index 674f442..853d896 100644 --- a/source/test/unittests/CMakeLists.txt +++ b/source/test/unittests/CMakeLists.txt @@ -2,5 +2,6 @@ target_sources( ${KIT_TEST_SUITE} PRIVATE async_function.test.c test_duration.test.c main.test.c - string_ref.test.c array_ref.test.c input_stream.test.c lower_bound.test.c - input_buffer.test.c dynamic_array.test.c) + string_ref.test.c atomic.test.c thread.test.c array_ref.test.c + input_stream.test.c lower_bound.test.c input_buffer.test.c + dynamic_array.test.c) diff --git a/source/test/unittests/atomic.test.c b/source/test/unittests/atomic.test.c new file mode 100644 index 0000000..9f1a10b --- /dev/null +++ b/source/test/unittests/atomic.test.c @@ -0,0 +1,26 @@ +#include "../../kit/atomic.h" + +#define KIT_TEST_FILE atomic +#include "../../kit_test/test.h" + +TEST("atomic store and load") { + ATOMIC(int) value; + atomic_store_explicit(&value, 20, memory_order_relaxed); + REQUIRE(atomic_load_explicit(&value, memory_order_relaxed) == 20); +} + +TEST("atomic exchange") { + ATOMIC(int) value; + atomic_store_explicit(&value, 20, memory_order_relaxed); + REQUIRE(atomic_exchange_explicit(&value, 42, + memory_order_relaxed) == 20); + REQUIRE(atomic_load_explicit(&value, memory_order_relaxed) == 42); +} + +TEST("atomic fetch add") { + ATOMIC(int) value; + atomic_store_explicit(&value, 20, memory_order_relaxed); + REQUIRE(atomic_fetch_add_explicit(&value, 22, + memory_order_relaxed) == 20); + REQUIRE(atomic_load_explicit(&value, memory_order_relaxed) == 42); +} diff --git a/source/test/unittests/thread.test.c b/source/test/unittests/thread.test.c new file mode 100644 index 0000000..fed564b --- /dev/null +++ b/source/test/unittests/thread.test.c @@ -0,0 +1,18 @@ +#include "../../kit/thread.h" + +#define KIT_TEST_FILE thread +#include "../../kit_test/test.h" + +static void *test_thread_fn(void *data) { + ptrdiff_t *value = (ptrdiff_t *) data; + return (void *) (*value + 20); +} + +TEST("run thread") { + pthread_t t; + ptrdiff_t value = 22; + pthread_create(&t, NULL, test_thread_fn, &value); + void *result; + pthread_join(t, &result); + REQUIRE((ptrdiff_t) result == 42); +} -- cgit v1.2.3