[3/4] Add tests for C11-like atomic operations.

Message ID 1414619566.10085.49.camel@triegel.csb
State Committed
Headers

Commit Message

Torvald Riegel Oct. 29, 2014, 9:52 p.m. UTC
  This adds tests for the new C11 atomics to the existing test case for
the old glibc-specific atomics.  The old test was single-threaded, so
this remains single-threaded.  Thus, some of the tests such as for
standalone barriers are more or less compilation tests.

	* csu/tst-atomic.c (do_test): Add tests for C11-like atomics.
  

Patch

commit 4f6859b59ed1dff441b2a7e747ad1b59ee7f70a4
Author: Torvald Riegel <triegel@redhat.com>
Date:   Mon Oct 20 20:25:40 2014 +0200

    Add tests for C11-like atomic operations.

diff --git a/csu/tst-atomic.c b/csu/tst-atomic.c
index d16c66d..c6e786d 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;
 }