[Bf-blender-cvs] [72a199e148b] master: Atomic: Cover with unit tests

Sergey Sharybin noreply at git.blender.org
Thu Nov 19 17:19:51 CET 2020


Commit: 72a199e148b20c36a63cdf6c6af067c9bcdb3f92
Author: Sergey Sharybin
Date:   Tue Nov 17 12:06:35 2020 +0100
Branches: master
https://developer.blender.org/rB72a199e148b20c36a63cdf6c6af067c9bcdb3f92

Atomic: Cover with unit tests

Cover all atomic functions with unit tests.

The tests are quite simple, nothing special so far. The goal is to:

- Make sure implementation exists.
- Implementation behaves the same way on all platforms
  (We had issue when MSVC and GCC were behaving differently in the
  past).
- Proper bitness is used for implementation and non-fixed-size
  function implementation.

The tests can be extended further to make sure, for example, that
CAS operations do not cast arguments to a more narrow type for
comparison. Considering it a possible further improvement, as it is
better be done being focused on that specific task.

There is an annoying ifdef around 64bit implementation, which uses
same internal ifdef as the header does. This check is aimed to be
removed, so is easier to simply accept such duplication for now.

The tests seems somewhat duplicate for signed/unsigned variants and
things like this. The reason for that is to keep test code as simple
as possible: attempting to do something smart/tricky in the test code
often causes the test code to be a subject of being covered with its
own unit tests.

Differential Revision: https://developer.blender.org/D9590

===================================================================

M	intern/CMakeLists.txt
A	intern/atomic/CMakeLists.txt
A	intern/atomic/tests/atomic_test.cc

===================================================================

diff --git a/intern/CMakeLists.txt b/intern/CMakeLists.txt
index c1792a312b2..dbd939e64b7 100644
--- a/intern/CMakeLists.txt
+++ b/intern/CMakeLists.txt
@@ -19,6 +19,7 @@
 # ***** END GPL LICENSE BLOCK *****
 
 # add_subdirectory(atomic)  # header only
+add_subdirectory(atomic)
 add_subdirectory(clog)
 add_subdirectory(ghost)
 add_subdirectory(guardedalloc)
diff --git a/intern/atomic/CMakeLists.txt b/intern/atomic/CMakeLists.txt
new file mode 100644
index 00000000000..afcafc4c8ab
--- /dev/null
+++ b/intern/atomic/CMakeLists.txt
@@ -0,0 +1,38 @@
+# ***** BEGIN GPL LICENSE BLOCK *****
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software Foundation,
+# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+#
+# The Original Code is Copyright (C) 2020, Blender Foundation
+# All rights reserved.
+# ***** END GPL LICENSE BLOCK *****
+
+set(INC
+  .
+)
+
+set(INC_SYS
+)
+
+if(WITH_GTESTS)
+  set(TEST_SRC
+    tests/atomic_test.cc
+  )
+  set(TEST_INC
+  )
+  set(TEST_LIB
+  )
+  include(GTestTesting)
+  blender_add_test_executable(atomic "${TEST_SRC}" "${INC};${TEST_INC}" "${INC_SYS}" "${LIB};${TEST_LIB}")
+endif()
diff --git a/intern/atomic/tests/atomic_test.cc b/intern/atomic/tests/atomic_test.cc
new file mode 100644
index 00000000000..f52422d0d30
--- /dev/null
+++ b/intern/atomic/tests/atomic_test.cc
@@ -0,0 +1,883 @@
+/* Apache License, Version 2.0 */
+
+#include <limits>
+
+#include "atomic_ops.h"
+#include "testing/testing.h"
+
+#ifdef __GNUC__
+#  if (__GNUC__ * 100 + __GNUC_MINOR__) >= 406 /* gcc4.6+ only */
+#    pragma GCC diagnostic error "-Wsign-compare"
+#  endif
+#  if (__GNUC__ * 100 + __GNUC_MINOR__) >= 408
+#    pragma GCC diagnostic error "-Wsign-conversion"
+#  endif
+#endif
+
+/* NOTE: it is suboptimal to duplicate same check as in API, but this check is
+ * planned to be removed, making it so 64bit intrinsics are available on 32bit
+ * platforms. */
+#if (LG_SIZEOF_PTR == 8 || LG_SIZEOF_INT == 8)
+
+/* -------------------------------------------------------------------- */
+/** \name 64 bit unsigned int atomics
+ * \{ */
+
+TEST(atomic, atomic_add_and_fetch_uint64)
+{
+  {
+    uint64_t value = 1;
+    EXPECT_EQ(atomic_add_and_fetch_uint64(&value, 2), 3);
+    EXPECT_EQ(value, 3);
+  }
+
+  {
+    uint64_t value = 0x1020304050607080;
+    EXPECT_EQ(atomic_add_and_fetch_uint64(&value, 0x0807060504030201), 0x1827364554637281);
+    EXPECT_EQ(value, 0x1827364554637281);
+  }
+
+  {
+    uint64_t value = 0x9020304050607080;
+    EXPECT_EQ(atomic_add_and_fetch_uint64(&value, 0x0807060504030201), 0x9827364554637281);
+    EXPECT_EQ(value, 0x9827364554637281);
+  }
+}
+
+TEST(atomic, atomic_sub_and_fetch_uint64)
+{
+  {
+    uint64_t value = 3;
+    EXPECT_EQ(atomic_sub_and_fetch_uint64(&value, 2), 1);
+    EXPECT_EQ(value, 1);
+  }
+
+  {
+    uint64_t value = 0x1827364554637281;
+    EXPECT_EQ(atomic_sub_and_fetch_uint64(&value, 0x0807060504030201), 0x1020304050607080);
+    EXPECT_EQ(value, 0x1020304050607080);
+  }
+
+  {
+    uint64_t value = 0x9827364554637281;
+    EXPECT_EQ(atomic_sub_and_fetch_uint64(&value, 0x0807060504030201), 0x9020304050607080);
+    EXPECT_EQ(value, 0x9020304050607080);
+  }
+
+  {
+    uint64_t value = 1;
+    EXPECT_EQ(atomic_sub_and_fetch_uint64(&value, 2), 0xffffffffffffffff);
+    EXPECT_EQ(value, 0xffffffffffffffff);
+  }
+}
+
+TEST(atomic, atomic_fetch_and_add_uint64)
+{
+  {
+    uint64_t value = 1;
+    EXPECT_EQ(atomic_fetch_and_add_uint64(&value, 2), 1);
+    EXPECT_EQ(value, 3);
+  }
+
+  {
+    uint64_t value = 0x1020304050607080;
+    EXPECT_EQ(atomic_fetch_and_add_uint64(&value, 0x0807060504030201), 0x1020304050607080);
+    EXPECT_EQ(value, 0x1827364554637281);
+  }
+
+  {
+    uint64_t value = 0x9020304050607080;
+    EXPECT_EQ(atomic_fetch_and_add_uint64(&value, 0x0807060504030201), 0x9020304050607080);
+    EXPECT_EQ(value, 0x9827364554637281);
+  }
+}
+
+TEST(atomic, atomic_fetch_and_sub_uint64)
+{
+  {
+    uint64_t value = 3;
+    EXPECT_EQ(atomic_fetch_and_sub_uint64(&value, 2), 3);
+    EXPECT_EQ(value, 1);
+  }
+
+  {
+    uint64_t value = 0x1827364554637281;
+    EXPECT_EQ(atomic_fetch_and_sub_uint64(&value, 0x0807060504030201), 0x1827364554637281);
+    EXPECT_EQ(value, 0x1020304050607080);
+  }
+
+  {
+    uint64_t value = 0x9827364554637281;
+    EXPECT_EQ(atomic_fetch_and_sub_uint64(&value, 0x0807060504030201), 0x9827364554637281);
+    EXPECT_EQ(value, 0x9020304050607080);
+  }
+
+  {
+    uint64_t value = 1;
+    EXPECT_EQ(atomic_fetch_and_sub_uint64(&value, 2), 1);
+    EXPECT_EQ(value, 0xffffffffffffffff);
+  }
+}
+
+TEST(atomic, atomic_cas_uint64)
+{
+  {
+    uint64_t value = 1;
+    EXPECT_EQ(atomic_cas_uint64(&value, 1, 2), 1);
+    EXPECT_EQ(value, 2);
+  }
+
+  {
+    uint64_t value = 1;
+    EXPECT_EQ(atomic_cas_uint64(&value, 2, 3), 1);
+    EXPECT_EQ(value, 1);
+  }
+
+  {
+    uint64_t value = 0x1234567890abcdef;
+    EXPECT_EQ(atomic_cas_uint64(&value, 0x1234567890abcdef, 0xfedcba0987654321),
+              0x1234567890abcdef);
+    EXPECT_EQ(value, 0xfedcba0987654321);
+  }
+
+  {
+    uint64_t value = 0x1234567890abcdef;
+    EXPECT_EQ(atomic_cas_uint64(&value, 0xdeadbeefefefefef, 0xfedcba0987654321),
+              0x1234567890abcdef);
+    EXPECT_EQ(value, 0x1234567890abcdef);
+  }
+}
+
+/** \} */
+
+/* -------------------------------------------------------------------- */
+/** \name 64 bit signed int atomics
+ * \{ */
+
+TEST(atomic, atomic_add_and_fetch_int64)
+{
+  {
+    int64_t value = 1;
+    EXPECT_EQ(atomic_add_and_fetch_int64(&value, 2), 3);
+    EXPECT_EQ(value, 3);
+  }
+
+  {
+    int64_t value = 0x1020304050607080;
+    EXPECT_EQ(atomic_add_and_fetch_int64(&value, 0x0807060504030201), 0x1827364554637281);
+    EXPECT_EQ(value, 0x1827364554637281);
+  }
+
+  {
+    int64_t value = -0x1020304050607080;
+    EXPECT_EQ(atomic_add_and_fetch_int64(&value, -0x0807060504030201), -0x1827364554637281);
+    EXPECT_EQ(value, -0x1827364554637281);
+  }
+}
+
+TEST(atomic, atomic_sub_and_fetch_int64)
+{
+  {
+    int64_t value = 3;
+    EXPECT_EQ(atomic_sub_and_fetch_int64(&value, 2), 1);
+    EXPECT_EQ(value, 1);
+  }
+
+  {
+    int64_t value = 0x1827364554637281;
+    EXPECT_EQ(atomic_sub_and_fetch_int64(&value, 0x0807060504030201), 0x1020304050607080);
+    EXPECT_EQ(value, 0x1020304050607080);
+  }
+
+  {
+    int64_t value = -0x1827364554637281;
+    EXPECT_EQ(atomic_sub_and_fetch_int64(&value, -0x0807060504030201), -0x1020304050607080);
+    EXPECT_EQ(value, -0x1020304050607080);
+  }
+
+  {
+    int64_t value = 1;
+    EXPECT_EQ(atomic_sub_and_fetch_int64(&value, 2), -1);
+    EXPECT_EQ(value, -1);
+  }
+}
+
+TEST(atomic, atomic_fetch_and_add_int64)
+{
+  {
+    int64_t value = 1;
+    EXPECT_EQ(atomic_fetch_and_add_int64(&value, 2), 1);
+    EXPECT_EQ(value, 3);
+  }
+
+  {
+    int64_t value = 0x1020304050607080;
+    EXPECT_EQ(atomic_fetch_and_add_int64(&value, 0x0807060504030201), 0x1020304050607080);
+    EXPECT_EQ(value, 0x1827364554637281);
+  }
+
+  {
+    int64_t value = -0x1020304050607080;
+    EXPECT_EQ(atomic_fetch_and_add_int64(&value, -0x0807060504030201), -0x1020304050607080);
+    EXPECT_EQ(value, -0x1827364554637281);
+  }
+}
+
+TEST(atomic, atomic_fetch_and_sub_int64)
+{
+  {
+    int64_t value = 3;
+    EXPECT_EQ(atomic_fetch_and_sub_int64(&value, 2), 3);
+    EXPECT_EQ(value, 1);
+  }
+
+  {
+    int64_t value = 0x1827364554637281;
+    EXPECT_EQ(atomic_fetch_and_sub_int64(&value, 0x0807060504030201), 0x1827364554637281);
+    EXPECT_EQ(value, 0x1020304050607080);
+  }
+
+  {
+    int64_t value = -0x1827364554637281;
+    EXPECT_EQ(atomic_fetch_and_sub_int64(&value, -0x0807060504030201), -0x1827364554637281);
+    EXPECT_EQ(value, -0x1020304050607080);
+  }
+
+  {
+    int64_t value = 1;
+    EXPECT_EQ(atomic_fetch_and_sub_int64(&value, 2), 1);
+    EXPECT_EQ(value, -1);
+  }
+}
+
+TEST(atomic, atomic_cas_int64)
+{
+  {
+    int64_t value = 1;
+    EXPECT_EQ(atomic_cas_int64(&value, 1, 2), 1);
+    EXPECT_EQ(value, 2);
+  }
+
+  {
+    int64_t value = 1;
+    EXPECT_EQ(atomic_cas_int64(&value, 2, 3), 1);
+    EXPECT_EQ(value, 1);
+  }
+
+  // 0xfedcba0987654321 is -0x012345f6789abcdf
+  // 0xdeadbeefefefefef is -0x2152411010101011
+
+  {
+    int64_t value = 0x1234567890abcdef;
+    EXPECT_EQ(atomic_cas_int64(&value, 0x1234567890abcdef, -0x012345f6789abcdf),
+              0x1234567890abcdef);
+    EXPECT_EQ(value, -0x012345f6789abcdf);
+  }
+
+  {
+    int64_t value = 0x1234567890abcdef;
+    EXPECT_EQ(atomic_cas_int64(&value, 0x2152411010101011, -0x012345f6789abcdf),
+              0x1234567890abcdef);
+    EXPECT_EQ(value, 0x1234567890abcdef);
+  }
+}
+
+/** \} */
+
+#endif
+
+/* -------------------------------------------------------------------- */
+/** \name 32 bit unsigned int atomics
+ * \{ */
+
+TEST(atomic, atomic_add_and_fetch_uint32)
+{
+  {
+    uint32_t value = 1;
+    EXPECT_EQ(atomic_add_and_fetch_uint32(&value, 2), 3);
+    EXPECT_EQ(value, 3);
+  }
+
+  {
+    uint32_t value = 0x10203040;
+    EXPECT_EQ(atomic_add_and_fetch_uint32(&value, 0x04030201), 0x14233241);
+    EXPECT_EQ(value, 0x14233241);
+  }
+
+  {
+    uint32_t value = 0x90203040;
+    EXPECT_EQ(atomic_add_and_fetch_uint32(&value, 0x04030201), 0x94233241);
+    EXPECT_EQ(value, 0x94233241);
+  }
+}
+
+TEST(atomic, atomic_sub_and_fetch_uint32)
+{
+  {
+    uint32_t value = 3;
+    EXPECT_EQ(atomic_sub_and_fetch_uint32(&value, 2), 1);
+    EXPECT_EQ(value, 1);
+  }
+
+  {
+    uint32_t value = 0x14233241;
+    EXPECT_EQ(atomic_sub_and_fetch_uint32(&value, 0x04030201), 0x10203040);
+    EXPECT_EQ(value, 0x10203040);
+  }
+
+  {
+    uint32_t value = 0x94233241;
+   

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list