Commit: 8d31f929db42975faa81e43ebd85528d20894dfb
Author: Jacques Lucke
Date:   Wed Sep 11 13:28:40 2019 +0200
Branches: functions
https://developer.blender.org/rB8d31f929db42975faa81e43ebd85528d20894dfb

fix Array and add tests

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

M       source/blender/blenlib/BLI_array.hpp
A       tests/gtests/blenlib/BLI_array_test.cc
M       tests/gtests/blenlib/CMakeLists.txt

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

diff --git a/source/blender/blenlib/BLI_array.hpp 
b/source/blender/blenlib/BLI_array.hpp
index d6ba3152d0d..cf78801b6b4 100644
--- a/source/blender/blenlib/BLI_array.hpp
+++ b/source/blender/blenlib/BLI_array.hpp
@@ -43,6 +43,17 @@ template<typename T, typename Allocator = GuardedAllocator> 
class Array {
     m_size = 0;
   }
 
+  Array(ArrayRef<T> values)
+  {
+    m_size = values.size();
+    m_data = this->allocate(m_size);
+    uninitialized_copy_n(values.begin(), m_size, m_data);
+  }
+
+  Array(const std::initializer_list<T> &values) : Array(ArrayRef<T>(values))
+  {
+  }
+
   explicit Array(uint size)
   {
     m_data = this->allocate(size);
@@ -53,9 +64,16 @@ template<typename T, typename Allocator = GuardedAllocator> 
class Array {
     }
   }
 
+  Array(uint size, const T &value)
+  {
+    m_data = this->allocate(size);
+    m_size = size;
+    uninitialized_fill_n(m_data, m_size, value);
+  }
+
   Array(const Array &other)
   {
-    m_size = 0;
+    m_size = other.size();
     m_allocator = other.m_allocator;
 
     if (m_size == 0) {
@@ -144,6 +162,16 @@ template<typename T, typename Allocator = 
GuardedAllocator> class Array {
     MutableArrayRef<T>(*this).fill_indices(indices, value);
   }
 
+  const T *begin() const
+  {
+    return m_data;
+  }
+
+  const T *end() const
+  {
+    return m_data + m_size;
+  }
+
   T *begin()
   {
     return m_data;
diff --git a/tests/gtests/blenlib/BLI_array_test.cc 
b/tests/gtests/blenlib/BLI_array_test.cc
new file mode 100644
index 00000000000..d14dfa42514
--- /dev/null
+++ b/tests/gtests/blenlib/BLI_array_test.cc
@@ -0,0 +1,103 @@
+#include "testing/testing.h"
+#include "BLI_array.hpp"
+
+using namespace BLI;
+
+TEST(array, DefaultConstructor)
+{
+  Array<int> array;
+  EXPECT_EQ(array.size(), 0);
+}
+
+TEST(array, SizeConstructor)
+{
+  Array<int> array(5);
+  EXPECT_EQ(array.size(), 5);
+}
+
+TEST(array, FillConstructor)
+{
+  Array<int> array(5, 8);
+  EXPECT_EQ(array.size(), 5);
+  EXPECT_EQ(array[0], 8);
+  EXPECT_EQ(array[1], 8);
+  EXPECT_EQ(array[2], 8);
+  EXPECT_EQ(array[3], 8);
+  EXPECT_EQ(array[4], 8);
+}
+
+TEST(array, InitializerListConstructor)
+{
+  Array<int> array = {4, 5, 6, 7};
+  EXPECT_EQ(array.size(), 4);
+  EXPECT_EQ(array[0], 4);
+  EXPECT_EQ(array[1], 5);
+  EXPECT_EQ(array[2], 6);
+  EXPECT_EQ(array[3], 7);
+}
+
+TEST(array, ArrayRefConstructor)
+{
+  int stackarray[4] = {6, 7, 8, 9};
+  ArrayRef<int> array_ref(stackarray, ARRAY_SIZE(stackarray));
+  Array<int> array(array_ref);
+  EXPECT_EQ(array.size(), 4);
+  EXPECT_EQ(array[0], 6);
+  EXPECT_EQ(array[1], 7);
+  EXPECT_EQ(array[2], 8);
+  EXPECT_EQ(array[3], 9);
+}
+
+TEST(array, CopyConstructor)
+{
+  Array<int> array = {5, 6, 7, 8};
+  Array<int> new_array(array);
+
+  EXPECT_EQ(array.size(), 4);
+  EXPECT_EQ(new_array.size(), 4);
+  EXPECT_NE(array.begin(), new_array.begin());
+  EXPECT_EQ(new_array[0], 5);
+  EXPECT_EQ(new_array[1], 6);
+  EXPECT_EQ(new_array[2], 7);
+  EXPECT_EQ(new_array[3], 8);
+}
+
+TEST(array, MoveConstructor)
+{
+  Array<int> array = {5, 6, 7, 8};
+  Array<int> new_array(std::move(array));
+
+  EXPECT_EQ(array.size(), 0);
+  EXPECT_EQ(new_array.size(), 4);
+  EXPECT_EQ(new_array[0], 5);
+  EXPECT_EQ(new_array[1], 6);
+  EXPECT_EQ(new_array[2], 7);
+  EXPECT_EQ(new_array[3], 8);
+}
+
+TEST(array, CopyAssignment)
+{
+  Array<int> array = {1, 2, 3};
+  Array<int> new_array = {4};
+  EXPECT_EQ(new_array.size(), 1);
+  new_array = array;
+  EXPECT_EQ(new_array.size(), 3);
+  EXPECT_EQ(array.size(), 3);
+  EXPECT_NE(array.begin(), new_array.begin());
+  EXPECT_EQ(new_array[0], 1);
+  EXPECT_EQ(new_array[1], 2);
+  EXPECT_EQ(new_array[2], 3);
+}
+
+TEST(array, MoveAssignment)
+{
+  Array<int> array = {1, 2, 3};
+  Array<int> new_array = {4};
+  EXPECT_EQ(new_array.size(), 1);
+  new_array = std::move(array);
+  EXPECT_EQ(new_array.size(), 3);
+  EXPECT_EQ(array.size(), 0);
+  EXPECT_EQ(new_array[0], 1);
+  EXPECT_EQ(new_array[1], 2);
+  EXPECT_EQ(new_array[2], 3);
+}
diff --git a/tests/gtests/blenlib/CMakeLists.txt 
b/tests/gtests/blenlib/CMakeLists.txt
index 483406ccace..984fe59d7df 100644
--- a/tests/gtests/blenlib/CMakeLists.txt
+++ b/tests/gtests/blenlib/CMakeLists.txt
@@ -38,6 +38,7 @@ else()
   set(BLI_path_util_extra_libs "bf_blenlib;extern_wcwidth;${ZLIB_LIBRARIES}")
 endif()
 
+BLENDER_TEST(BLI_array "bf_blenlib")
 BLENDER_TEST(BLI_array_store "bf_blenlib")
 BLENDER_TEST(BLI_array_utils "bf_blenlib")
 BLENDER_TEST(BLI_array_ref "bf_blenlib")

_______________________________________________
Bf-blender-cvs mailing list
Bf-blender-cvs@blender.org
https://lists.blender.org/mailman/listinfo/bf-blender-cvs

Reply via email to