Blender V4.3
COM_BufferArea_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2021 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "testing/testing.h"
6
7#include "COM_BufferArea.h"
8
10
11static rcti create_rect(int width, int height)
12{
13 rcti rect;
14 BLI_rcti_init(&rect, 0, width, 0, height);
15 return rect;
16}
17
18static rcti create_rect(int width, int height, int offset)
19{
20 rcti rect;
21 BLI_rcti_init(&rect, offset, offset + width, offset, offset + height);
22 return rect;
23}
24
25TEST(BufferArea, BufferConstructor)
26{
27 const int width = 2;
28 const int height = 3;
29 BufferArea<float> area(nullptr, width, height, 4);
30 EXPECT_EQ(area.width(), width);
31 EXPECT_EQ(area.height(), height);
32 rcti rect = create_rect(width, height);
33 EXPECT_TRUE(BLI_rcti_compare(&area.get_rect(), &rect));
34}
35
36TEST(BufferArea, AreaConstructor)
37{
38 const int buf_width = 5;
39 const int area_width = 1;
40 const int area_height = 3;
41 rcti area_rect = create_rect(area_width, area_height, 1);
42 BufferArea<float> area(nullptr, buf_width, area_rect, 4);
43 EXPECT_EQ(area.width(), area_width);
44 EXPECT_EQ(area.height(), area_height);
45 EXPECT_TRUE(BLI_rcti_compare(&area.get_rect(), &area_rect));
46}
47
48static void fill_buffer_with_indexes(float *buf, int buf_len)
49{
50 for (int i = 0; i < buf_len; i++) {
51 buf[i] = i;
52 }
53}
54
55static void test_single_elem_iteration(float *buffer, BufferArea<float> area)
56{
57 int elems_count = 0;
58 for (float *elem : area) {
59 EXPECT_EQ(elem, buffer);
60 elems_count++;
61 }
62 EXPECT_EQ(elems_count, 1);
63}
64
66 float *buf, int buf_width, int buf_len, int num_channels, BufferArea<float> area)
67{
68 fill_buffer_with_indexes(buf, buf_len);
69 rcti rect = area.get_rect();
70 int x = rect.xmin;
71 int y = rect.ymin;
72 for (float *elem : area) {
73 for (int ch = 0; ch < num_channels; ch++) {
74 const int buf_index = y * buf_width * num_channels + x * num_channels + ch;
75 EXPECT_NEAR(elem[ch], buf_index, FLT_EPSILON);
76 }
77 x++;
78 if (x == rect.xmax) {
79 y++;
80 x = rect.xmin;
81 }
82 }
83 EXPECT_EQ(x, rect.xmin);
84 EXPECT_EQ(y, rect.ymax);
85}
86
87TEST(BufferArea, SingleElemBufferIteration)
88{
89 const int buf_width = 4;
90 const int buf_height = 5;
91 const int area_width = 2;
92 const int area_height = 3;
93 const int num_channels = 4;
94 const int stride = 0;
95 float buf[num_channels];
96 {
97 BufferArea area(buf, buf_width, buf_height, stride);
99 }
100 {
101 rcti area_rect = create_rect(area_width, area_height, 1);
102 BufferArea area(buf, buf_width, area_rect, stride);
104 }
105}
106
107TEST(BufferArea, FullBufferIteration)
108{
109 const int buf_width = 4;
110 const int area_width = 2;
111 const int area_height = 3;
112 const int buf_height = (area_height + 1);
113 const int num_channels = 4;
114 const int buf_len = buf_height * buf_width * num_channels;
115 float buf[buf_len];
116 {
117 BufferArea area(buf, buf_width, buf_height, num_channels);
118 test_full_buffer_iteration(buf, buf_width, buf_len, num_channels, area);
119 }
120 {
121 rcti area_rect = create_rect(area_width, area_height, 1);
122 BufferArea area(buf, buf_width, area_rect, num_channels);
123 test_full_buffer_iteration(buf, buf_width, buf_len, num_channels, area);
124 }
125}
126
127} // namespace blender::compositor::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
bool BLI_rcti_compare(const struct rcti *rect_a, const struct rcti *rect_b)
void BLI_rcti_init(struct rcti *rect, int xmin, int xmax, int ymin, int ymax)
Definition rct.c:418
static void test_single_elem_iteration(float *buffer, BufferArea< float > area)
static rcti create_rect(int width, int height)
static void fill_buffer_with_indexes(float *buf, int buf_len)
static void test_full_buffer_iteration(float *buf, int buf_width, int buf_len, int num_channels, BufferArea< float > area)
TEST(BufferArea, BufferConstructor)
int ymin
int ymax
int xmin
int xmax