From 1eccfecd40a6b7e228154e4c616902e22ca10c41 Mon Sep 17 00:00:00 2001 From: Torvald Riegel Date: Mon, 20 Oct 2014 20:25:40 +0200 Subject: Add tests for C11-like atomic operations. --- csu/tst-atomic.c | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 129 insertions(+), 1 deletion(-) (limited to 'csu') diff --git a/csu/tst-atomic.c b/csu/tst-atomic.c index d16c66dc31..c6e786d92e 100644 --- a/csu/tst-atomic.c +++ b/csu/tst-atomic.c @@ -28,7 +28,7 @@ static int do_test (void) { - atomic_t mem; + atomic_t mem, expected; int ret = 0; #ifdef atomic_compare_and_exchange_val_acq @@ -489,6 +489,134 @@ do_test (void) ret = 1; } + /* Tests for C11-like atomics. */ + mem = 11; + if (atomic_load_relaxed (&mem) != 11 || atomic_load_acquire (&mem) != 11) + { + puts ("atomic_load_{relaxed,acquire} test failed"); + ret = 1; + } + + atomic_store_relaxed (&mem, 12); + if (mem != 12) + { + puts ("atomic_store_relaxed test failed"); + ret = 1; + } + atomic_store_release (&mem, 13); + if (mem != 13) + { + puts ("atomic_store_release test failed"); + ret = 1; + } + + mem = 14; + expected = 14; + if (!atomic_compare_exchange_weak_relaxed (&mem, &expected, 25) + || mem != 25 || expected != 14) + { + puts ("atomic_compare_exchange_weak_relaxed test 1 failed"); + ret = 1; + } + if (atomic_compare_exchange_weak_relaxed (&mem, &expected, 14) + || mem != 25 || expected != 25) + { + puts ("atomic_compare_exchange_weak_relaxed test 2 failed"); + ret = 1; + } + mem = 14; + expected = 14; + if (!atomic_compare_exchange_weak_acquire (&mem, &expected, 25) + || mem != 25 || expected != 14) + { + puts ("atomic_compare_exchange_weak_acquire test 1 failed"); + ret = 1; + } + if (atomic_compare_exchange_weak_acquire (&mem, &expected, 14) + || mem != 25 || expected != 25) + { + puts ("atomic_compare_exchange_weak_acquire test 2 failed"); + ret = 1; + } + mem = 14; + expected = 14; + if (!atomic_compare_exchange_weak_release (&mem, &expected, 25) + || mem != 25 || expected != 14) + { + puts ("atomic_compare_exchange_weak_release test 1 failed"); + ret = 1; + } + if (atomic_compare_exchange_weak_release (&mem, &expected, 14) + || mem != 25 || expected != 25) + { + puts ("atomic_compare_exchange_weak_release test 2 failed"); + ret = 1; + } + + mem = 23; + if (atomic_exchange_acquire (&mem, 42) != 23 || mem != 42) + { + puts ("atomic_exchange_acquire test failed"); + ret = 1; + } + mem = 23; + if (atomic_exchange_release (&mem, 42) != 23 || mem != 42) + { + puts ("atomic_exchange_release test failed"); + ret = 1; + } + + mem = 23; + if (atomic_fetch_add_relaxed (&mem, 1) != 23 || mem != 24) + { + puts ("atomic_fetch_add_relaxed test failed"); + ret = 1; + } + mem = 23; + if (atomic_fetch_add_acquire (&mem, 1) != 23 || mem != 24) + { + puts ("atomic_fetch_add_acquire test failed"); + ret = 1; + } + mem = 23; + if (atomic_fetch_add_release (&mem, 1) != 23 || mem != 24) + { + puts ("atomic_fetch_add_release test failed"); + ret = 1; + } + mem = 23; + if (atomic_fetch_add_acq_rel (&mem, 1) != 23 || mem != 24) + { + puts ("atomic_fetch_add_acq_rel test failed"); + ret = 1; + } + + mem = 3; + if (atomic_fetch_and_acquire (&mem, 2) != 3 || mem != 2) + { + puts ("atomic_fetch_and_acquire test failed"); + ret = 1; + } + + mem = 4; + if (atomic_fetch_or_relaxed (&mem, 2) != 4 || mem != 6) + { + puts ("atomic_fetch_or_relaxed test failed"); + ret = 1; + } + mem = 4; + if (atomic_fetch_or_acquire (&mem, 2) != 4 || mem != 6) + { + puts ("atomic_fetch_or_acquire test failed"); + ret = 1; + } + + /* This is a single-threaded test, so we can't test the effects of the + fences. */ + atomic_thread_fence_acquire (); + atomic_thread_fence_release (); + atomic_thread_fence_seq_cst (); + return ret; } -- cgit 1.4.1