Blender  V3.3
BLI_index_mask_test.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 
3 #include "BLI_index_mask.hh"
4 #include "testing/testing.h"
5 
6 namespace blender::tests {
7 
8 TEST(index_mask, DefaultConstructor)
9 {
11  EXPECT_EQ(mask.min_array_size(), 0);
12  EXPECT_EQ(mask.size(), 0);
13 }
14 
15 TEST(index_mask, ArrayConstructor)
16 {
17  [](IndexMask mask) {
18  EXPECT_EQ(mask.size(), 4);
19  EXPECT_EQ(mask.min_array_size(), 8);
20  EXPECT_FALSE(mask.is_range());
21  EXPECT_EQ(mask[0], 3);
22  EXPECT_EQ(mask[1], 5);
23  EXPECT_EQ(mask[2], 6);
24  EXPECT_EQ(mask[3], 7);
25  }({3, 5, 6, 7});
26 }
27 
28 TEST(index_mask, RangeConstructor)
29 {
30  IndexMask mask = IndexRange(3, 5);
31  EXPECT_EQ(mask.size(), 5);
32  EXPECT_EQ(mask.min_array_size(), 8);
33  EXPECT_EQ(mask.last(), 7);
34  EXPECT_TRUE(mask.is_range());
35  EXPECT_EQ(mask.as_range().first(), 3);
36  EXPECT_EQ(mask.as_range().last(), 7);
37  Span<int64_t> indices = mask.indices();
38  EXPECT_EQ(indices[0], 3);
39  EXPECT_EQ(indices[1], 4);
40  EXPECT_EQ(indices[2], 5);
41 }
42 
43 TEST(index_mask, SliceAndOffset)
44 {
46  {
48  IndexMask new_mask = mask.slice_and_offset(IndexRange(3, 5), indices);
49  EXPECT_TRUE(new_mask.is_range());
50  EXPECT_EQ(new_mask.size(), 5);
51  EXPECT_EQ(new_mask[0], 0);
52  EXPECT_EQ(new_mask[1], 1);
53  }
54  {
55  Vector<int64_t> original_indices = {2, 3, 5, 7, 8, 9, 10};
56  IndexMask mask{original_indices.as_span()};
57  IndexMask new_mask = mask.slice_and_offset(IndexRange(1, 4), indices);
58  EXPECT_FALSE(new_mask.is_range());
59  EXPECT_EQ(new_mask.size(), 4);
60  EXPECT_EQ(new_mask[0], 0);
61  EXPECT_EQ(new_mask[1], 2);
62  EXPECT_EQ(new_mask[2], 4);
63  EXPECT_EQ(new_mask[3], 5);
64  }
65 }
66 
67 TEST(index_mask, ExtractRanges)
68 {
69  {
70  Vector<int64_t> indices = {1, 2, 3, 5, 7, 8};
72  EXPECT_EQ(ranges.size(), 3);
73  EXPECT_EQ(ranges[0], IndexRange(1, 3));
74  EXPECT_EQ(ranges[1], IndexRange(5, 1));
75  EXPECT_EQ(ranges[2], IndexRange(7, 2));
76  }
77  {
80  EXPECT_EQ(ranges.size(), 0);
81  }
82  {
83  Vector<int64_t> indices = {5, 6, 7, 8, 9, 10};
85  EXPECT_EQ(ranges.size(), 1);
86  EXPECT_EQ(ranges[0], IndexRange(5, 6));
87  }
88  {
89  Vector<int64_t> indices = {1, 3, 6, 8};
91  EXPECT_EQ(ranges.size(), 4);
92  EXPECT_EQ(ranges[0], IndexRange(1, 1));
93  EXPECT_EQ(ranges[1], IndexRange(3, 1));
94  EXPECT_EQ(ranges[2], IndexRange(6, 1));
95  EXPECT_EQ(ranges[3], IndexRange(8, 1));
96  }
97  {
99  IndexRange range1{4, 10};
100  IndexRange range2{20, 30};
101  IndexRange range3{100, 1};
102  IndexRange range4{150, 100};
103  for (const IndexRange &range : {range1, range2, range3, range4}) {
104  for (const int64_t i : range) {
105  indices.append(i);
106  }
107  }
109  EXPECT_EQ(ranges.size(), 4);
110  EXPECT_EQ(ranges[0], range1);
111  EXPECT_EQ(ranges[1], range2);
112  EXPECT_EQ(ranges[2], range3);
113  EXPECT_EQ(ranges[3], range4);
114  }
115  {
116  const int64_t max_test_range_size = 50;
118  int64_t offset = 0;
119  for (const int64_t range_size : IndexRange(1, max_test_range_size)) {
120  for (const int i : IndexRange(range_size)) {
121  indices.append(offset + i);
122  }
123  offset += range_size + 1;
124  }
126  EXPECT_EQ(ranges.size(), max_test_range_size);
127  for (const int64_t range_size : IndexRange(1, max_test_range_size)) {
128  const IndexRange range = ranges[range_size - 1];
129  EXPECT_EQ(range.size(), range_size);
130  }
131  }
132 }
133 
134 TEST(index_mask, Invert)
135 {
136  {
138  Vector<int64_t> new_indices;
139  IndexMask inverted_mask = IndexMask(indices).invert(IndexRange(10), new_indices);
140  EXPECT_EQ(inverted_mask.size(), 10);
141  EXPECT_TRUE(new_indices.is_empty());
142  }
143  {
144  Vector<int64_t> indices = {3, 4, 5, 6};
145  Vector<int64_t> new_indices;
146  IndexMask inverted_mask = IndexMask(indices).invert(IndexRange(3, 4), new_indices);
147  EXPECT_TRUE(inverted_mask.is_empty());
148  }
149  {
150  Vector<int64_t> indices = {5};
151  Vector<int64_t> new_indices;
152  IndexMask inverted_mask = IndexMask(indices).invert(IndexRange(10), new_indices);
153  EXPECT_EQ(inverted_mask.size(), 9);
154  EXPECT_EQ(inverted_mask.indices(), Span<int64_t>({0, 1, 2, 3, 4, 6, 7, 8, 9}));
155  }
156  {
157  Vector<int64_t> indices = {0, 1, 2, 6, 7, 9};
158  Vector<int64_t> new_indices;
159  IndexMask inverted_mask = IndexMask(indices).invert(IndexRange(10), new_indices);
160  EXPECT_EQ(inverted_mask.size(), 4);
161  EXPECT_EQ(inverted_mask.indices(), Span<int64_t>({3, 4, 5, 8}));
162  }
163 }
164 
165 TEST(index_mask, ExtractRangesInvert)
166 {
167  {
170  EXPECT_EQ(ranges.size(), 1);
171  EXPECT_EQ(ranges[0], IndexRange(10));
172  }
173  {
174  Vector<int64_t> indices = {1, 2, 3, 6, 7};
175  Vector<int64_t> skip_amounts;
177  &skip_amounts);
178  EXPECT_EQ(ranges.size(), 3);
179  EXPECT_EQ(ranges[0], IndexRange(0, 1));
180  EXPECT_EQ(ranges[1], IndexRange(4, 2));
181  EXPECT_EQ(ranges[2], IndexRange(8, 2));
182  EXPECT_EQ(skip_amounts[0], 0);
183  EXPECT_EQ(skip_amounts[1], 3);
184  EXPECT_EQ(skip_amounts[2], 5);
185  }
186  {
187  Vector<int64_t> indices = {0, 1, 2, 3, 4};
188  Vector<int64_t> skip_amounts;
190  &skip_amounts);
191  EXPECT_TRUE(ranges.is_empty());
192  EXPECT_TRUE(skip_amounts.is_empty());
193  }
194  {
195  Vector<int64_t> indices = {5, 6, 7, 10, 11};
196  Vector<int64_t> skip_amounts;
198  &skip_amounts);
199  EXPECT_EQ(ranges.size(), 2);
200  EXPECT_EQ(ranges[0], IndexRange(8, 2));
201  EXPECT_EQ(ranges[1], IndexRange(12, 13));
202  EXPECT_EQ(skip_amounts[0], 3);
203  EXPECT_EQ(skip_amounts[1], 5);
204  }
205 }
206 
207 TEST(index_mask, ContainedIn)
208 {
209  EXPECT_TRUE(IndexMask({3, 4, 5}).contained_in(IndexRange(10)));
210  EXPECT_TRUE(IndexMask().contained_in(IndexRange(5, 0)));
211  EXPECT_FALSE(IndexMask({3}).contained_in(IndexRange(3)));
212  EXPECT_FALSE(IndexMask({4, 5, 6}).contained_in(IndexRange(5, 10)));
213  EXPECT_FALSE(IndexMask({5, 6}).contained_in(IndexRange()));
214 }
215 
216 } // namespace blender::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
Group Output data from inside of a node group A color picker Mix two input colors RGB to Convert a color s luminance to a grayscale value Generate a normal vector and a dot product Bright Control the brightness and contrast of the input color Vector Map an input vectors to used to fine tune the interpolation of the input Camera Retrieve information about the camera and how it relates to the current shading point s position Clamp a value between a minimum and a maximum Vector Perform vector math operation Invert
int64_t size() const
bool is_empty() const
bool is_range() const
Vector< IndexRange > extract_ranges_invert(const IndexRange full_range, Vector< int64_t > *r_skip_amounts=nullptr) const
Definition: index_mask.cc:100
Span< int64_t > indices() const
Vector< IndexRange > extract_ranges() const
Definition: index_mask.cc:59
IndexMask invert(const IndexRange full_range, Vector< int64_t > &r_new_indices) const
Definition: index_mask.cc:39
constexpr int64_t size() const
int64_t size() const
Definition: BLI_vector.hh:694
Span< T > as_span() const
Definition: BLI_vector.hh:325
bool is_empty() const
Definition: BLI_vector.hh:706
ccl_gpu_kernel_postfix ccl_global float int int int int float bool int offset
ccl_gpu_kernel_postfix int ccl_global int * indices
ccl_device_inline float4 mask(const int4 &mask, const float4 &a)
Definition: math_float4.h:513
TEST(any, DefaultConstructor)
Definition: BLI_any_test.cc:10
__int64 int64_t
Definition: stdint.h:89