summaryrefslogtreecommitdiff
path: root/source/test/unittests
diff options
context:
space:
mode:
authorMitya Selivanov <0x7fffff@guattari.ru>2022-08-14 04:50:43 +0400
committerMitya Selivanov <0x7fffff@guattari.ru>2022-08-14 04:50:43 +0400
commit117001b8986d46040da5c7b07330b5fb99fc7f70 (patch)
tree96508e21d552d496ebb31baca354b64e6b2eb4ec /source/test/unittests
parent0b1c0f3eb647a7630b473bfec75a3411dab89583 (diff)
downloadkit-117001b8986d46040da5c7b07330b5fb99fc7f70.zip
thread, atomic
Diffstat (limited to 'source/test/unittests')
-rw-r--r--source/test/unittests/CMakeLists.txt5
-rw-r--r--source/test/unittests/atomic.test.c26
-rw-r--r--source/test/unittests/thread.test.c18
3 files changed, 47 insertions, 2 deletions
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);
+}