summaryrefslogtreecommitdiff
path: root/source/tests
diff options
context:
space:
mode:
Diffstat (limited to 'source/tests')
-rw-r--r--source/tests/_exe.c1
-rw-r--r--source/tests/atomic.test.c77
-rw-r--r--source/tests/mutex.test.c18
3 files changed, 52 insertions, 44 deletions
diff --git a/source/tests/_exe.c b/source/tests/_exe.c
index 1c59ba2..01add48 100644
--- a/source/tests/_exe.c
+++ b/source/tests/_exe.c
@@ -21,6 +21,7 @@
#include "string_ref.test.c"
#include "duration.test.c"
#include "thread.test.c"
+#include "atomic.test.c"
#include "xml.test.c"
#include "http1.test.c"
#include "bench.test.c"
diff --git a/source/tests/atomic.test.c b/source/tests/atomic.test.c
index d5c172b..ca14959 100644
--- a/source/tests/atomic.test.c
+++ b/source/tests/atomic.test.c
@@ -5,13 +5,13 @@
#include "../kit/test.h"
TEST("atomic store and load") {
- ATOMIC(int) value;
+ int _Atomic value;
atomic_store_explicit(&value, 20, memory_order_relaxed);
REQUIRE(atomic_load_explicit(&value, memory_order_relaxed) == 20);
}
TEST("atomic exchange") {
- ATOMIC(int) value;
+ int _Atomic value;
atomic_store_explicit(&value, 20, memory_order_relaxed);
REQUIRE(atomic_exchange_explicit(&value, 42,
memory_order_relaxed) == 20);
@@ -19,20 +19,23 @@ TEST("atomic exchange") {
}
TEST("atomic fetch add") {
- ATOMIC(int) value;
+ int _Atomic 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);
}
-enum { THREAD_COUNT = 20, TICK_COUNT = 10000 };
+enum {
+ TEST_ATOMIC_THREAD_COUNT = 20,
+ TEST_ATOMIC_TICK_COUNT = 10000,
+};
static int test_8_(void *p) {
ptrdiff_t i;
- ATOMIC(int8_t) *x = (ATOMIC(int8_t) *) p;
+ int8_t _Atomic *x = (int8_t _Atomic *) p;
- for (i = 0; i < TICK_COUNT; i++) {
+ for (i = 0; i < TEST_ATOMIC_TICK_COUNT; i++) {
atomic_fetch_add_explicit(x, 20, memory_order_relaxed);
thrd_yield();
atomic_fetch_add_explicit(x, 22, memory_order_relaxed);
@@ -45,11 +48,11 @@ static int test_8_(void *p) {
}
TEST("atomic types") {
- ATOMIC(int8_t) b_1;
- ATOMIC(int8_t) b_2;
- ATOMIC(int16_t) i16;
- ATOMIC(int32_t) i32;
- ATOMIC(int64_t) i64;
+ int8_t _Atomic b_1;
+ int8_t _Atomic b_2;
+ int16_t _Atomic i16;
+ int32_t _Atomic i32;
+ int64_t _Atomic i64;
atomic_store_explicit(&b_1, 42, memory_order_relaxed);
atomic_store_explicit(&b_2, 43, memory_order_relaxed);
@@ -86,18 +89,18 @@ TEST("atomic types") {
TEST("atomic byte concurrency") {
ptrdiff_t i;
- ATOMIC(int8_t) foo;
- ATOMIC(int8_t) bar;
+ int8_t _Atomic foo;
+ int8_t _Atomic bar;
atomic_store_explicit(&foo, 42, memory_order_relaxed);
atomic_store_explicit(&bar, 43, memory_order_relaxed);
- thrd_t threads[THREAD_COUNT];
- for (i = 0; i < THREAD_COUNT; i++)
+ thrd_t threads[TEST_ATOMIC_THREAD_COUNT];
+ for (i = 0; i < TEST_ATOMIC_THREAD_COUNT; i++)
REQUIRE_EQ(thrd_create(threads + i, test_8_,
(void *) ((i % 2) ? &foo : &bar)),
thrd_success);
- for (i = 0; i < THREAD_COUNT; i++) thrd_join(threads[i], NULL);
+ for (i = 0; i < TEST_ATOMIC_THREAD_COUNT; i++) thrd_join(threads[i], NULL);
REQUIRE(atomic_load_explicit(&foo, memory_order_relaxed) == 42);
REQUIRE(atomic_load_explicit(&bar, memory_order_relaxed) == 43);
@@ -106,9 +109,9 @@ TEST("atomic byte concurrency") {
static int test_16_(void *p) {
ptrdiff_t i;
- ATOMIC(int16_t) *x = (ATOMIC(int16_t) *) p;
+ int16_t _Atomic *x = (int16_t _Atomic *) p;
- for (i = 0; i < TICK_COUNT; i++) {
+ for (i = 0; i < TEST_ATOMIC_TICK_COUNT; i++) {
atomic_fetch_add_explicit(x, 2020, memory_order_relaxed);
thrd_yield();
atomic_fetch_add_explicit(x, 2222, memory_order_relaxed);
@@ -123,18 +126,18 @@ static int test_16_(void *p) {
TEST("atomic int16 concurrency") {
ptrdiff_t i;
- ATOMIC(int16_t) foo;
- ATOMIC(int16_t) bar;
+ int16_t _Atomic foo;
+ int16_t _Atomic bar;
atomic_store_explicit(&foo, 42, memory_order_relaxed);
atomic_store_explicit(&bar, 43, memory_order_relaxed);
- thrd_t threads[THREAD_COUNT];
- for (i = 0; i < THREAD_COUNT; i++)
+ thrd_t threads[TEST_ATOMIC_THREAD_COUNT];
+ for (i = 0; i < TEST_ATOMIC_THREAD_COUNT; i++)
REQUIRE_EQ(thrd_create(threads + i, test_16_,
(void *) ((i % 2) ? &foo : &bar)),
thrd_success);
- for (i = 0; i < THREAD_COUNT; i++) thrd_join(threads[i], NULL);
+ for (i = 0; i < TEST_ATOMIC_THREAD_COUNT; i++) thrd_join(threads[i], NULL);
REQUIRE(atomic_load_explicit(&foo, memory_order_relaxed) == 42);
REQUIRE(atomic_load_explicit(&bar, memory_order_relaxed) == 43);
@@ -143,9 +146,9 @@ TEST("atomic int16 concurrency") {
static int test_32_(void *p) {
ptrdiff_t i;
- ATOMIC(int32_t) *x = (ATOMIC(int32_t) *) p;
+ int32_t _Atomic *x = (int32_t _Atomic *) p;
- for (i = 0; i < TICK_COUNT; i++) {
+ for (i = 0; i < TEST_ATOMIC_TICK_COUNT; i++) {
atomic_fetch_add_explicit(x, 202020, memory_order_relaxed);
thrd_yield();
atomic_fetch_add_explicit(x, 222222, memory_order_relaxed);
@@ -160,18 +163,18 @@ static int test_32_(void *p) {
TEST("atomic int32 concurrency") {
ptrdiff_t i;
- ATOMIC(int32_t) foo;
- ATOMIC(int32_t) bar;
+ int32_t _Atomic foo;
+ int32_t _Atomic bar;
atomic_store_explicit(&foo, 42, memory_order_relaxed);
atomic_store_explicit(&bar, 43, memory_order_relaxed);
- thrd_t threads[THREAD_COUNT];
- for (i = 0; i < THREAD_COUNT; i++)
+ thrd_t threads[TEST_ATOMIC_THREAD_COUNT];
+ for (i = 0; i < TEST_ATOMIC_THREAD_COUNT; i++)
REQUIRE_EQ(thrd_create(threads + i, test_32_,
(void *) ((i % 2) ? &foo : &bar)),
thrd_success);
- for (i = 0; i < THREAD_COUNT; i++) thrd_join(threads[i], NULL);
+ for (i = 0; i < TEST_ATOMIC_THREAD_COUNT; i++) thrd_join(threads[i], NULL);
REQUIRE(atomic_load_explicit(&foo, memory_order_relaxed) == 42);
REQUIRE(atomic_load_explicit(&bar, memory_order_relaxed) == 43);
@@ -180,9 +183,9 @@ TEST("atomic int32 concurrency") {
static int test_64_(void *p) {
ptrdiff_t i;
- ATOMIC(int64_t) *x = (ATOMIC(int64_t) *) p;
+ int64_t _Atomic *x = (int64_t _Atomic *) p;
- for (i = 0; i < TICK_COUNT; i++) {
+ for (i = 0; i < TEST_ATOMIC_TICK_COUNT; i++) {
atomic_fetch_add_explicit(x, 20202020202020ll,
memory_order_relaxed);
thrd_yield();
@@ -200,18 +203,18 @@ static int test_64_(void *p) {
TEST("atomic int64 concurrency") {
ptrdiff_t i;
- ATOMIC(int64_t) foo;
- ATOMIC(int64_t) bar;
+ int64_t _Atomic foo;
+ int64_t _Atomic bar;
atomic_store_explicit(&foo, 42, memory_order_relaxed);
atomic_store_explicit(&bar, 43, memory_order_relaxed);
- thrd_t threads[THREAD_COUNT];
- for (i = 0; i < THREAD_COUNT; i++)
+ thrd_t threads[TEST_ATOMIC_THREAD_COUNT];
+ for (i = 0; i < TEST_ATOMIC_THREAD_COUNT; i++)
REQUIRE_EQ(thrd_create(threads + i, test_64_,
(void *) ((i % 2) ? &foo : &bar)),
thrd_success);
- for (i = 0; i < THREAD_COUNT; i++) thrd_join(threads[i], NULL);
+ for (i = 0; i < TEST_ATOMIC_THREAD_COUNT; i++) thrd_join(threads[i], NULL);
REQUIRE(atomic_load_explicit(&foo, memory_order_relaxed) == 42);
REQUIRE(atomic_load_explicit(&bar, memory_order_relaxed) == 43);
diff --git a/source/tests/mutex.test.c b/source/tests/mutex.test.c
index 61c1caa..9261fb1 100644
--- a/source/tests/mutex.test.c
+++ b/source/tests/mutex.test.c
@@ -3,7 +3,11 @@
#define KIT_TEST_FILE mutex
#include "../kit/test.h"
-enum { SLEEP = 400000000, TICK_COUNT = 200, THREAD_COUNT = 100 };
+enum {
+ TEST_MUTEX_SLEEP = 400000000,
+ TEST_MUTEX_TICK_COUNT = 200,
+ TEST_MUTEX_THREAD_COUNT = 100,
+};
typedef struct {
mtx_t lock;
@@ -13,7 +17,7 @@ typedef struct {
static int mtx_test_run(void *data) {
int i;
mtx_test_data_t *x = (mtx_test_data_t *) data;
- for (i = 0; i < TICK_COUNT; i++) {
+ for (i = 0; i < TEST_MUTEX_TICK_COUNT; i++) {
mtx_lock(&x->lock);
x->value += i;
@@ -33,13 +37,13 @@ TEST("mutex lock") {
ptrdiff_t i;
mtx_test_data_t data;
- thrd_t pool[THREAD_COUNT];
+ thrd_t pool[TEST_MUTEX_THREAD_COUNT];
data.value = 42;
REQUIRE(mtx_init(&data.lock, mtx_plain) == thrd_success);
- for (i = 0; i < THREAD_COUNT; i++)
+ for (i = 0; i < TEST_MUTEX_THREAD_COUNT; i++)
thrd_create(pool + i, mtx_test_run, &data);
- for (i = 0; i < THREAD_COUNT; i++) thrd_join(pool[i], NULL);
+ for (i = 0; i < TEST_MUTEX_THREAD_COUNT; i++) thrd_join(pool[i], NULL);
mtx_destroy(&data.lock);
REQUIRE(data.value == 42);
@@ -49,7 +53,7 @@ static int test_lock(void *data) {
mtx_t *m = (mtx_t *) data;
mtx_lock(m);
- struct timespec sec = { .tv_sec = 0, .tv_nsec = SLEEP };
+ struct timespec sec = { .tv_sec = 0, .tv_nsec = TEST_MUTEX_SLEEP };
thrd_sleep(&sec, NULL);
mtx_unlock(m);
@@ -64,7 +68,7 @@ TEST("mutex try lock") {
thrd_t t;
REQUIRE(thrd_create(&t, test_lock, &m) == thrd_success);
- struct timespec sec = { .tv_sec = 0, .tv_nsec = SLEEP / 2 };
+ struct timespec sec = { .tv_sec = 0, .tv_nsec = TEST_MUTEX_SLEEP / 2 };
REQUIRE(thrd_sleep(&sec, NULL) == thrd_success);
REQUIRE(mtx_trylock(&m) == thrd_busy);