Bug 732043 - part 2 - add tests for mfbt/Atomics.h; r=Waldo; original-author=jcranmer
authorNathan Froyd <froydnj@mozilla.com>
Thu, 25 Apr 2013 15:10:38 -0400
changeset 132285 eab6eac1a371adde17745a0a338d1a68be1cb23c
parent 132284 8096f5bdab923055e627e73bd04db3c855ac2c8b
child 132286 0152d497fbefdc95761a240a935d49b1076d7011
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersWaldo
bugs732043
milestone24.0a1
Bug 732043 - part 2 - add tests for mfbt/Atomics.h; r=Waldo; original-author=jcranmer
mfbt/tests/Makefile.in
mfbt/tests/TestAtomics.cpp
--- a/mfbt/tests/Makefile.in
+++ b/mfbt/tests/Makefile.in
@@ -7,16 +7,17 @@ topsrcdir = @top_srcdir@
 srcdir = @srcdir@
 VPATH = @srcdir@
 
 include $(DEPTH)/config/autoconf.mk
 
 STL_FLAGS =
 
 CPP_UNIT_TESTS = \
+  TestAtomics.cpp \
   TestBloomFilter.cpp \
   TestCasting.cpp \
   TestCheckedInt.cpp \
   TestEndian.cpp \
   TestEnumSet.cpp \
   TestSHA1.cpp \
   TestTypeTraits.cpp \
   TestWeakPtr.cpp \
new file mode 100644
--- /dev/null
+++ b/mfbt/tests/TestAtomics.cpp
@@ -0,0 +1,140 @@
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "mozilla/Assertions.h"
+#include "mozilla/Atomics.h"
+#include "mozilla/DebugOnly.h"
+
+#include <stdint.h>
+
+using mozilla::Atomic;
+using mozilla::DebugOnly;
+using mozilla::MemoryOrdering;
+using mozilla::Relaxed;
+using mozilla::ReleaseAcquire;
+using mozilla::SequentiallyConsistent;
+
+template <typename T, MemoryOrdering Order>
+static void
+TestTypeWithOrdering()
+{
+  Atomic<T, Order> atomic(5);
+  MOZ_ASSERT(atomic == 5, "Atomic variable did not initialize");
+
+  // Test atomic increment
+  MOZ_ASSERT(++atomic == T(6), "Atomic increment did not work");
+  MOZ_ASSERT(atomic++ == T(6), "Atomic post-increment did not work");
+  MOZ_ASSERT(atomic == T(7), "Atomic post-increment did not work");
+
+  // Test atomic decrement
+  MOZ_ASSERT(--atomic == 6, "Atomic decrement did not work");
+  MOZ_ASSERT(atomic-- == 6, "Atomic post-decrement did not work");
+  MOZ_ASSERT(atomic == 5, "Atomic post-decrement did not work");
+
+  // Test other arithmetic.
+  DebugOnly<T> result;
+  result = (atomic += T(5));
+  MOZ_ASSERT(atomic == T(10), "Atomic += did not work");
+  MOZ_ASSERT(result == T(10), "Atomic += returned the wrong value");
+  result = (atomic -= T(3));
+  MOZ_ASSERT(atomic == T(7), "Atomic -= did not work");
+  MOZ_ASSERT(result == T(7), "Atomic -= returned the wrong value");
+
+  // Test assignment
+  result = (atomic = T(5));
+  MOZ_ASSERT(atomic == T(5), "Atomic assignment failed");
+  MOZ_ASSERT(result == T(5), "Atomic assignment returned the wrong value");
+
+  // Test logical operations.
+  result = (atomic ^= T(2));
+  MOZ_ASSERT(atomic == T(7), "Atomic ^= did not work");
+  MOZ_ASSERT(result == T(7), "Atomic ^= returned the wrong value");
+  result = (atomic ^= T(4));
+  MOZ_ASSERT(atomic == T(3), "Atomic ^= did not work");
+  MOZ_ASSERT(result == T(3), "Atomic ^= returned the wrong value");
+  result = (atomic |= T(8));
+  MOZ_ASSERT(atomic == T(11), "Atomic |= did not work");
+  MOZ_ASSERT(result == T(11), "Atomic |= returned the wrong value");
+  result = (atomic |= T(8));
+  MOZ_ASSERT(atomic == T(11), "Atomic |= did not work");
+  MOZ_ASSERT(result == T(11), "Atomic |= returned the wrong value");
+  result = (atomic &= T(12));
+  MOZ_ASSERT(atomic == T(8), "Atomic &= did not work");
+  MOZ_ASSERT(result == T(8), "Atomic &= returned the wrong value");
+
+  // Test exchange.
+  atomic = T(30);
+  result = atomic.exchange(42);
+  MOZ_ASSERT(atomic == T(42), "Atomic exchange did not work");
+  MOZ_ASSERT(result == T(30), "Atomic exchange returned the wrong value");
+}
+
+template<typename T, MemoryOrdering Order>
+static void
+TestPointerWithOrdering()
+{
+  T array1[10];
+  Atomic<T*, Order> atomic(array1);
+  MOZ_ASSERT(atomic == array1, "Atomic variable did not initialize");
+
+  // Test atomic increment
+  MOZ_ASSERT(++atomic == array1 + 1, "Atomic increment did not work");
+  MOZ_ASSERT(atomic++ == array1 + 1, "Atomic post-increment did not work");
+  MOZ_ASSERT(atomic == array1 + 2, "Atomic post-increment did not work");
+
+  // Test atomic decrement
+  MOZ_ASSERT(--atomic == array1 + 1, "Atomic decrement did not work");
+  MOZ_ASSERT(atomic-- == array1 + 1, "Atomic post-decrement did not work");
+  MOZ_ASSERT(atomic == array1, "Atomic post-decrement did not work");
+
+  // Test other arithmetic operations
+  DebugOnly<T*> result;
+  result = (atomic += 2);
+  MOZ_ASSERT(atomic == array1 + 2, "Atomic += did not work");
+  MOZ_ASSERT(result == array1 + 2, "Atomic += returned the wrong value");
+  result = (atomic -= 1);
+  MOZ_ASSERT(atomic == array1 + 1, "Atomic -= did not work");
+  MOZ_ASSERT(result == array1 + 1, "Atomic -= returned the wrong value");
+
+  // Test stores
+  result = (atomic = array1);
+  MOZ_ASSERT(atomic == array1, "Atomic assignment did not work");
+  MOZ_ASSERT(result == array1, "Atomic assignment returned the wrong value");
+
+  // Test exchange
+  atomic = array1 + 2;
+  result = atomic.exchange(array1);
+  MOZ_ASSERT(atomic == array1, "Atomic exchange did not work");
+  MOZ_ASSERT(result == array1 + 2, "Atomic exchange returned the wrong value");
+}
+
+template <typename T>
+static void
+TestType()
+{
+  TestTypeWithOrdering<T, SequentiallyConsistent>();
+  TestTypeWithOrdering<T, ReleaseAcquire>();
+  TestTypeWithOrdering<T, Relaxed>();
+}
+
+template<typename T>
+static void
+TestPointer()
+{
+  TestPointerWithOrdering<T, SequentiallyConsistent>();
+  TestPointerWithOrdering<T, ReleaseAcquire>();
+  TestPointerWithOrdering<T, Relaxed>();
+}
+
+int main()
+{
+  TestType<uint32_t>();
+  TestType<int32_t>();
+  TestType<intptr_t>();
+  TestType<uintptr_t>();
+  TestPointer<int>();
+  TestPointer<float>();
+  TestPointer<uint16_t*>();
+  TestPointer<uint32_t*>();
+}