[Bf-blender-cvs] [549e2b75398] master: Compositor: Buffer iterators tests

Manuel Castilla noreply at git.blender.org
Fri Jul 30 20:22:04 CEST 2021


Commit: 549e2b7539826ec1d4532b9fd07118ace82195ae
Author: Manuel Castilla
Date:   Fri Jul 30 20:17:50 2021 +0200
Branches: master
https://developer.blender.org/rB549e2b7539826ec1d4532b9fd07118ace82195ae

Compositor: Buffer iterators tests

See D11882 for a description of the iterators.

Reviewed By: jbakker

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

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

M	source/blender/compositor/CMakeLists.txt
A	source/blender/compositor/tests/COM_BufferArea_test.cc
A	source/blender/compositor/tests/COM_BufferRange_test.cc
A	source/blender/compositor/tests/COM_BuffersIterator_test.cc

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

diff --git a/source/blender/compositor/CMakeLists.txt b/source/blender/compositor/CMakeLists.txt
index 830792a2a48..000ba298c2d 100644
--- a/source/blender/compositor/CMakeLists.txt
+++ b/source/blender/compositor/CMakeLists.txt
@@ -635,3 +635,19 @@ if(CXX_WARN_NO_SUGGEST_OVERRIDE)
 endif()
 
 add_dependencies(bf_compositor smaa_areatex_header)
+
+if(WITH_GTESTS)
+  set(TEST_SRC
+    tests/COM_BufferArea_test.cc
+    tests/COM_BufferRange_test.cc
+    tests/COM_BuffersIterator_test.cc
+  )
+  set(TEST_INC
+  )
+  set(TEST_LIB
+    bf_compositor
+  )
+  include(GTestTesting)
+  blender_add_test_lib(bf_compositor_tests "${TEST_SRC}" "${INC};${TEST_INC}" "${INC_SYS}" "${LIB};${TEST_LIB}")
+endif()
+
diff --git a/source/blender/compositor/tests/COM_BufferArea_test.cc b/source/blender/compositor/tests/COM_BufferArea_test.cc
new file mode 100644
index 00000000000..8dad0b0fea2
--- /dev/null
+++ b/source/blender/compositor/tests/COM_BufferArea_test.cc
@@ -0,0 +1,141 @@
+/*
+ * 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.
+ *
+ * Copyright 2021, Blender Foundation.
+ */
+
+#include "testing/testing.h"
+
+#include "COM_BufferArea.h"
+
+namespace blender::compositor::tests {
+
+static rcti create_rect(int width, int height)
+{
+  rcti rect;
+  BLI_rcti_init(&rect, 0, width, 0, height);
+  return rect;
+}
+
+static rcti create_rect(int width, int height, int offset)
+{
+  rcti rect;
+  BLI_rcti_init(&rect, offset, offset + width, offset, offset + height);
+  return rect;
+}
+
+TEST(BufferArea, BufferConstructor)
+{
+  const int width = 2;
+  const int height = 3;
+  BufferArea<float> area(nullptr, width, height, 4);
+  EXPECT_EQ(area.width(), width);
+  EXPECT_EQ(area.height(), height);
+  rcti rect = create_rect(width, height);
+  EXPECT_TRUE(BLI_rcti_compare(&area.get_rect(), &rect));
+}
+
+TEST(BufferArea, AreaConstructor)
+{
+  const int buf_width = 5;
+  const int area_width = 1;
+  const int area_height = 3;
+  rcti area_rect = create_rect(area_width, area_height, 1);
+  BufferArea<float> area(nullptr, buf_width, area_rect, 4);
+  EXPECT_EQ(area.width(), area_width);
+  EXPECT_EQ(area.height(), area_height);
+  EXPECT_TRUE(BLI_rcti_compare(&area.get_rect(), &area_rect));
+}
+
+static void fill_buffer_with_indexes(float *buf, int buf_len)
+{
+  for (int i = 0; i < buf_len; i++) {
+    buf[i] = i;
+  }
+}
+
+static void test_single_elem_iteration(float *buffer, BufferArea<float> area)
+{
+  int elems_count = 0;
+  for (float *elem : area) {
+    EXPECT_EQ(elem, buffer);
+    elems_count++;
+  }
+  EXPECT_EQ(elems_count, 1);
+}
+
+static void test_full_buffer_iteration(
+    float *buf, int buf_width, int buf_len, int num_channels, BufferArea<float> area)
+{
+  fill_buffer_with_indexes(buf, buf_len);
+  rcti rect = area.get_rect();
+  int x = rect.xmin;
+  int y = rect.ymin;
+  for (float *elem : area) {
+    for (int ch = 0; ch < num_channels; ch++) {
+      const int buf_index = y * buf_width * num_channels + x * num_channels + ch;
+      EXPECT_NEAR(elem[ch], buf_index, FLT_EPSILON);
+    }
+    x++;
+    if (x == rect.xmax) {
+      y++;
+      x = rect.xmin;
+    }
+  }
+  EXPECT_EQ(x, rect.xmin);
+  EXPECT_EQ(y, rect.ymax);
+}
+
+TEST(BufferArea, SingleElemBufferIteration)
+{
+  const int buf_width = 4;
+  const int buf_height = 5;
+  const int area_width = 2;
+  const int area_height = 3;
+  const int num_channels = 4;
+  const int stride = 0;
+  float buf[num_channels];
+  {
+    BufferArea area(buf, buf_width, buf_height, stride);
+    test_single_elem_iteration(buf, area);
+  }
+  {
+    rcti area_rect = create_rect(area_width, area_height, 1);
+    BufferArea area(buf, buf_width, area_rect, stride);
+    test_single_elem_iteration(buf, area);
+  }
+}
+
+TEST(BufferArea, FullBufferIteration)
+{
+  const int buf_width = 4;
+  const int area_width = 2;
+  const int area_height = 3;
+  const int buf_height = (area_height + 1);
+  const int num_channels = 4;
+  const int buf_len = buf_height * buf_width * num_channels;
+  float buf[buf_len];
+  {
+    BufferArea area(buf, buf_width, buf_height, num_channels);
+    test_full_buffer_iteration(buf, buf_width, buf_len, num_channels, area);
+  }
+  {
+    rcti area_rect = create_rect(area_width, area_height, 1);
+    BufferArea area(buf, buf_width, area_rect, num_channels);
+    test_full_buffer_iteration(buf, buf_width, buf_len, num_channels, area);
+  }
+}
+
+}  // namespace blender::compositor::tests
diff --git a/source/blender/compositor/tests/COM_BufferRange_test.cc b/source/blender/compositor/tests/COM_BufferRange_test.cc
new file mode 100644
index 00000000000..9ebeb181fa6
--- /dev/null
+++ b/source/blender/compositor/tests/COM_BufferRange_test.cc
@@ -0,0 +1,98 @@
+/*
+ * 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.
+ *
+ * Copyright 2021, Blender Foundation.
+ */
+
+#include "testing/testing.h"
+
+#include "COM_BufferRange.h"
+
+namespace blender::compositor::tests {
+
+TEST(BufferRange, Constructor)
+{
+  const int size = 5;
+  BufferRange<float> range(nullptr, 1, size, 4);
+  EXPECT_EQ(range.size(), size);
+}
+
+static void fill_buffer_with_indexes(float *buf, int buf_len)
+{
+  for (int i = 0; i < buf_len; i++) {
+    buf[i] = i;
+  }
+}
+
+TEST(BufferRange, Subscript)
+{
+  const int start = 2;
+  const int size = 4;
+  const int num_channels = 3;
+  const int buf_len = (start + size) * num_channels;
+  float buf[buf_len];
+
+  BufferRange<float> range(buf, start, size, num_channels);
+
+  fill_buffer_with_indexes(buf, buf_len);
+  int buf_index = start * num_channels;
+  for (int i = 0; i < size; i++) {
+    const float *elem = range[i];
+    for (int ch = 0; ch < num_channels; ch++) {
+      EXPECT_NEAR(elem[ch], buf_index, FLT_EPSILON);
+      buf_index++;
+    }
+  }
+  EXPECT_EQ(buf_index, buf_len);
+}
+
+TEST(BufferRange, SingleElemBufferIteration)
+{
+  const int start = 1;
+  const int size = 3;
+  const int num_channels = 4;
+  float buf[num_channels];
+  const int stride = 0;
+  BufferRange<float> range(buf, start, size, stride);
+
+  int elems_count = 0;
+  for (float *elem : range) {
+    EXPECT_EQ(elem, buf);
+    elems_count++;
+  }
+  EXPECT_EQ(elems_count, 1);
+}
+
+TEST(BufferRange, FullBufferIteration)
+{
+  const int start = 2;
+  const int size = 5;
+  const int num_channels = 4;
+  const int buf_len = (start + size) * num_channels;
+  float buf[buf_len];
+  BufferRange<float> range(buf, start, size, num_channels);
+
+  fill_buffer_with_indexes(buf, buf_len);
+  int buf_index = start * num_channels;
+  for (float *elem : range) {
+    for (int ch = 0; ch < num_channels; ch++) {
+      EXPECT_NEAR(elem[ch], buf_index, FLT_EPSILON);
+      buf_index++;
+    }
+  }
+  EXPECT_EQ(buf_index, buf_len);
+}
+
+}  // namespace blender::compositor::tests
diff --git a/source/blender/compositor/tests/COM_BuffersIterator_test.cc b/source/blender/compositor/tests/COM_BuffersIterator_test.cc
new file mode 100644
index 00000000000..0a288cdc5f0
--- /dev/null
+++ b/source/blender/compositor/tests/COM_BuffersIterator_test.cc
@@ -0,0 +1,299 @@
+/*
+ * 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.
+ *
+ * Copyright 2021, Blender Foundation.
+ */
+
+#include "testing/testing.h"
+
+#include "BLI_array.hh"
+#include "COM_BuffersIterator.h"
+
+namespace blender::compositor::tests {
+
+constexpr int BUFFER_WIDTH = 5;
+constexpr int BUFFER_HEIGHT = 4;
+constexpr int BUFFER_OFFSET_X = 5;
+constexpr int BUFFER_OFFSET_Y = 6;
+constexpr int NUM_CHANNELS = 4;
+constexpr int FULL_BUFFER_LEN = BUFFER_WIDTH * BUFFER_HEIGHT * NUM_CHANNELS;
+constexpr int SINGLE_ELEM_BUFFER_LEN = NUM_CHANNELS;
+constexpr int NUM_INPUTS = 2;
+
+static float *create_buffer(int len)
+{
+  return (float *)MEM_callocN(len * sizeof(float), "COM_BuffersIteratorTest");
+}
+
+static const float *create_input_buffer(int input_idx, bool is_a_single_elem)
+{
+  const int len = is_a_single_elem ? SINGLE_ELEM_BUFFER_LEN : FULL_BUFFER_LEN;
+  float *buf = create_buffer(len);
+  /* Fill buffer with variable data. */
+  for (int i = 0; i < len; i++) {
+    buf[i] = input_idx * 1.5f * (i + 1) + i * 0.9f;
+  }
+  return buf;
+}
+
+using IterFunc = std::function<void(BuffersIterator<float> &it, const rcti &area)>;
+using ValidateElemFunc = 

@@ Diff output truncated at 10240 characters. @@



More information about the Bf-blender-cvs mailing list