Welcome to mirror list, hosted at ThFree Co, Russian Federation.

COM_BufferArea_test.cc « tests « compositor « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8dad0b0fea26f98f339acbc7a88a206b047b7dd4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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