Blender  V3.3
BLI_index_range_test.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 
3 #include "BLI_index_range.hh"
4 #include "BLI_strict_flags.h"
5 #include "BLI_vector.hh"
6 #include "testing/testing.h"
7 
8 namespace blender::tests {
9 
10 TEST(index_range, DefaultConstructor)
11 {
12  IndexRange range;
13  EXPECT_EQ(range.size(), 0);
14 
16  for (int64_t value : range) {
17  vector.append(value);
18  }
19  EXPECT_EQ(vector.size(), 0);
20 }
21 
22 TEST(index_range, SingleElementRange)
23 {
24  IndexRange range(4, 1);
25  EXPECT_EQ(range.size(), 1);
26  EXPECT_EQ(*range.begin(), 4);
27 
29  for (int64_t value : range) {
30  vector.append(value);
31  }
32 
33  EXPECT_EQ(vector.size(), 1);
34  EXPECT_EQ(vector[0], 4);
35 }
36 
37 TEST(index_range, MultipleElementRange)
38 {
39  IndexRange range(6, 4);
40  EXPECT_EQ(range.size(), 4);
41 
43  for (int64_t value : range) {
44  vector.append(value);
45  }
46 
47  EXPECT_EQ(vector.size(), 4);
48  for (int i = 0; i < 4; i++) {
49  EXPECT_EQ(vector[i], i + 6);
50  }
51 }
52 
53 TEST(index_range, SubscriptOperator)
54 {
55  IndexRange range(5, 5);
56  EXPECT_EQ(range[0], 5);
57  EXPECT_EQ(range[1], 6);
58  EXPECT_EQ(range[2], 7);
59 }
60 
61 TEST(index_range, Before)
62 {
63  IndexRange range = IndexRange(5, 5).before(3);
64  EXPECT_EQ(range[0], 2);
65  EXPECT_EQ(range[1], 3);
66  EXPECT_EQ(range[2], 4);
67  EXPECT_EQ(range.size(), 3);
68 }
69 
70 TEST(index_range, After)
71 {
72  IndexRange range = IndexRange(5, 5).after(4);
73  EXPECT_EQ(range[0], 10);
74  EXPECT_EQ(range[1], 11);
75  EXPECT_EQ(range[2], 12);
76  EXPECT_EQ(range[3], 13);
77  EXPECT_EQ(range.size(), 4);
78 }
79 
80 TEST(index_range, Contains)
81 {
82  IndexRange range = IndexRange(5, 3);
83  EXPECT_TRUE(range.contains(5));
84  EXPECT_TRUE(range.contains(6));
85  EXPECT_TRUE(range.contains(7));
86  EXPECT_FALSE(range.contains(4));
87  EXPECT_FALSE(range.contains(8));
88 }
89 
90 TEST(index_range, First)
91 {
92  IndexRange range = IndexRange(5, 3);
93  EXPECT_EQ(range.first(), 5);
94 }
95 
96 TEST(index_range, Last)
97 {
98  IndexRange range = IndexRange(5, 3);
99  EXPECT_EQ(range.last(), 7);
100 }
101 
102 TEST(index_range, OneAfterEnd)
103 {
104  IndexRange range = IndexRange(5, 3);
105  EXPECT_EQ(range.one_after_last(), 8);
106 }
107 
108 TEST(index_range, OneBeforeStart)
109 {
110  IndexRange range = IndexRange(5, 3);
111  EXPECT_EQ(range.one_before_start(), 4);
112 }
113 
114 TEST(index_range, Start)
115 {
116  IndexRange range = IndexRange(6, 2);
117  EXPECT_EQ(range.start(), 6);
118 }
119 
120 TEST(index_range, Slice)
121 {
122  IndexRange range = IndexRange(5, 15);
123  IndexRange slice = range.slice(2, 6);
124  EXPECT_EQ(slice.size(), 6);
125  EXPECT_EQ(slice.first(), 7);
126  EXPECT_EQ(slice.last(), 12);
127 }
128 
129 TEST(index_range, SliceRange)
130 {
131  IndexRange range = IndexRange(5, 15);
132  IndexRange slice = range.slice(IndexRange(3, 5));
133  EXPECT_EQ(slice.size(), 5);
134  EXPECT_EQ(slice.first(), 8);
135  EXPECT_EQ(slice.last(), 12);
136 }
137 
138 TEST(index_range, DropBack)
139 {
140  IndexRange a(4, 4);
141  auto slice = a.drop_back(2);
142  EXPECT_EQ(slice.size(), 2);
143  EXPECT_EQ(slice.start(), 4);
144  EXPECT_EQ(slice[1], 5);
145 }
146 
147 TEST(index_range, DropBackAll)
148 {
149  IndexRange a(4, 4);
150  auto slice = a.drop_back(a.size());
151  EXPECT_TRUE(slice.is_empty());
152 }
153 
154 TEST(index_range, DropFront)
155 {
156  IndexRange a(4, 4);
157  auto slice = a.drop_front(1);
158  EXPECT_EQ(slice.size(), 3);
159  EXPECT_EQ(slice[0], 5);
160  EXPECT_EQ(slice[1], 6);
161  EXPECT_EQ(slice.last(), 7);
162 }
163 
164 TEST(index_range, DropFrontLargeN)
165 {
166  IndexRange a(1, 5);
167  IndexRange slice = a.drop_front(100);
168  EXPECT_TRUE(slice.is_empty());
169 }
170 
171 TEST(index_range, DropFrontAll)
172 {
173  IndexRange a(50);
174  IndexRange slice = a.drop_front(a.size());
175  EXPECT_TRUE(slice.is_empty());
176 }
177 
178 TEST(index_range, TakeFront)
179 {
180  IndexRange a(4, 4);
181  IndexRange slice = a.take_front(2);
182  EXPECT_EQ(slice.size(), 2);
183  EXPECT_EQ(slice[0], 4);
184  EXPECT_EQ(slice[1], 5);
185 }
186 
187 TEST(index_range, TakeFrontLargeN)
188 {
189  IndexRange a(4, 4);
190  IndexRange slice = a.take_front(100);
191  EXPECT_EQ(slice.size(), 4);
192 }
193 
194 TEST(index_range, TakeBack)
195 {
196  IndexRange a(4, 4);
197  auto slice = a.take_back(2);
198  EXPECT_EQ(slice.size(), 2);
199  EXPECT_EQ(slice[0], 6);
200  EXPECT_EQ(slice[1], 7);
201 }
202 
203 TEST(index_range, TakeBackLargeN)
204 {
205  IndexRange a(3, 4);
206  IndexRange slice = a.take_back(100);
207  EXPECT_EQ(slice.size(), 4);
208  EXPECT_EQ(slice.size(), 4);
209 }
210 
211 TEST(index_range, AsSpan)
212 {
213  IndexRange range = IndexRange(4, 6);
214  Span<int64_t> span = range.as_span();
215  EXPECT_EQ(span.size(), 6);
216  EXPECT_EQ(span[0], 4);
217  EXPECT_EQ(span[1], 5);
218  EXPECT_EQ(span[2], 6);
219  EXPECT_EQ(span[3], 7);
220 }
221 
222 TEST(index_range, constexpr_)
223 {
224  constexpr IndexRange range = IndexRange(1, 1);
225  std::array<int, range[0]> compiles = {1};
226  BLI_STATIC_ASSERT(range.size() == 1, "");
227  EXPECT_EQ(compiles[0], 1);
228 }
229 
230 TEST(index_range, GenericAlgorithms)
231 {
232  IndexRange range{4, 10};
233  EXPECT_TRUE(std::any_of(range.begin(), range.end(), [](int v) { return v == 6; }));
234  EXPECT_FALSE(std::any_of(range.begin(), range.end(), [](int v) { return v == 20; }));
235  EXPECT_EQ(std::count_if(range.begin(), range.end(), [](int v) { return v < 7; }), 3);
236 }
237 
238 } // namespace blender::tests
#define BLI_STATIC_ASSERT(a, msg)
Definition: BLI_assert.h:83
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
Strict compiler flags for areas of code we want to ensure don't do conversions without us knowing abo...
in reality light always falls off quadratically Particle Retrieve the data of the particle that spawned the object for example to give variation to multiple instances of an object Point Retrieve information about points in a point cloud Retrieve the edges of an object as it appears to Cycles topology will always appear triangulated Convert a blackbody temperature to an RGB value Normal Generate a perturbed normal from an RGB normal map image Typically used for faking highly detailed surfaces Generate an OSL shader from a file or text data block Image Sample an image file as a texture Sky Generate a procedural sky texture Noise Generate fractal Perlin noise Wave Generate procedural bands or rings with noise Voronoi Generate Worley noise based on the distance to random points Typically used to generate textures such as or biological cells Brick Generate a procedural texture producing bricks Texture Retrieve multiple types of texture coordinates nTypically used as inputs for texture nodes Vector Convert a vector
ATTR_WARN_UNUSED_RESULT const BMVert * v
constexpr int64_t one_before_start() const
constexpr int64_t first() const
constexpr int64_t one_after_last() const
Span< int64_t > as_span() const
Definition: BLI_span.hh:726
constexpr int64_t last(const int64_t n=0) const
constexpr int64_t size() const
constexpr bool is_empty() const
constexpr IndexRange after(int64_t n) const
constexpr int64_t start() const
constexpr IndexRange before(int64_t n) const
constexpr Iterator begin() const
constexpr IndexRange slice(int64_t start, int64_t size) const
constexpr bool contains(int64_t value) const
constexpr int64_t size() const
Definition: BLI_span.hh:240
static unsigned a[3]
Definition: RandGen.cpp:78
TEST(any, DefaultConstructor)
Definition: BLI_any_test.cc:10
__int64 int64_t
Definition: stdint.h:89