Blender  V3.3
BLI_memory_utils_test.cc
Go to the documentation of this file.
1 /* SPDX-License-Identifier: Apache-2.0 */
2 
3 #include "BLI_math_vec_types.hh"
4 #include "BLI_memory_utils.hh"
5 #include "BLI_strict_flags.h"
6 #include "testing/testing.h"
7 
8 namespace blender::tests {
9 
10 namespace {
11 struct MyValue {
12  static inline int alive = 0;
13 
14  MyValue()
15  {
16  if (alive == 15) {
17  throw std::exception();
18  }
19 
20  alive++;
21  }
22 
23  MyValue(const MyValue &UNUSED(other))
24  {
25  if (alive == 15) {
26  throw std::exception();
27  }
28 
29  alive++;
30  }
31 
32  ~MyValue()
33  {
34  alive--;
35  }
36 };
37 } // namespace
38 
39 TEST(memory_utils, DefaultConstructN_ActuallyCallsConstructor)
40 {
41  constexpr int amount = 10;
43 
45  default_construct_n(buffer.ptr(), amount);
46  EXPECT_EQ(MyValue::alive, amount);
47  destruct_n(buffer.ptr(), amount);
49 }
50 
51 TEST(memory_utils, DefaultConstructN_StrongExceptionSafety)
52 {
53  constexpr int amount = 20;
55 
57  EXPECT_THROW(default_construct_n(buffer.ptr(), amount), std::exception);
59 }
60 
61 TEST(memory_utils, UninitializedCopyN_ActuallyCopies)
62 {
63  constexpr int amount = 5;
66 
68  default_construct_n(buffer1.ptr(), amount);
69  EXPECT_EQ(MyValue::alive, amount);
70  uninitialized_copy_n(buffer1.ptr(), amount, buffer2.ptr());
71  EXPECT_EQ(MyValue::alive, 2 * amount);
72  destruct_n(buffer1.ptr(), amount);
73  EXPECT_EQ(MyValue::alive, amount);
74  destruct_n(buffer2.ptr(), amount);
76 }
77 
78 TEST(memory_utils, UninitializedCopyN_StrongExceptionSafety)
79 {
80  constexpr int amount = 10;
83 
85  default_construct_n(buffer1.ptr(), amount);
86  EXPECT_EQ(MyValue::alive, amount);
87  EXPECT_THROW(uninitialized_copy_n(buffer1.ptr(), amount, buffer2.ptr()), std::exception);
88  EXPECT_EQ(MyValue::alive, amount);
89  destruct_n(buffer1.ptr(), amount);
91 }
92 
93 TEST(memory_utils, UninitializedFillN_ActuallyCopies)
94 {
95  constexpr int amount = 10;
97 
99  {
100  MyValue value;
102  uninitialized_fill_n(buffer.ptr(), amount, value);
103  EXPECT_EQ(MyValue::alive, 1 + amount);
104  destruct_n(buffer.ptr(), amount);
106  }
108 }
109 
110 TEST(memory_utils, UninitializedFillN_StrongExceptionSafety)
111 {
112  constexpr int amount = 20;
114 
116  {
117  MyValue value;
119  EXPECT_THROW(uninitialized_fill_n(buffer.ptr(), amount, value), std::exception);
121  }
123 }
124 
126  virtual void mymethod(){};
127 };
128 
130  void mymethod() override
131  {
132  }
133 };
134 
135 static_assert(is_convertible_pointer_v<int *, int *>);
136 static_assert(is_convertible_pointer_v<int *, const int *>);
137 static_assert(is_convertible_pointer_v<int *, int *const>);
138 static_assert(is_convertible_pointer_v<int *, const int *const>);
139 static_assert(!is_convertible_pointer_v<const int *, int *>);
140 static_assert(!is_convertible_pointer_v<int, int *>);
141 static_assert(!is_convertible_pointer_v<int *, int>);
142 static_assert(is_convertible_pointer_v<TestBaseClass *, const TestBaseClass *>);
143 static_assert(!is_convertible_pointer_v<const TestBaseClass *, TestBaseClass *>);
144 static_assert(is_convertible_pointer_v<TestChildClass *, TestBaseClass *>);
145 static_assert(!is_convertible_pointer_v<TestBaseClass *, TestChildClass *>);
146 static_assert(is_convertible_pointer_v<const TestChildClass *, const TestBaseClass *>);
147 static_assert(!is_convertible_pointer_v<TestBaseClass, const TestChildClass *>);
148 static_assert(!is_convertible_pointer_v<float3, float *>);
149 static_assert(!is_convertible_pointer_v<float *, float3>);
150 static_assert(!is_convertible_pointer_v<int **, int *>);
151 static_assert(!is_convertible_pointer_v<int *, int **>);
152 static_assert(is_convertible_pointer_v<int **, int **>);
153 static_assert(is_convertible_pointer_v<const int **, const int **>);
154 static_assert(!is_convertible_pointer_v<const int **, int **>);
155 static_assert(!is_convertible_pointer_v<int *const *, int **>);
156 static_assert(!is_convertible_pointer_v<int *const *const, int **>);
157 static_assert(is_convertible_pointer_v<int **, int **const>);
158 static_assert(is_convertible_pointer_v<int **, int *const *>);
159 static_assert(is_convertible_pointer_v<int **, int const *const *>);
160 
161 static_assert(is_span_convertible_pointer_v<int *, int *>);
162 static_assert(is_span_convertible_pointer_v<int *, const int *>);
163 static_assert(!is_span_convertible_pointer_v<const int *, int *>);
164 static_assert(is_span_convertible_pointer_v<const int *, const int *>);
165 static_assert(is_span_convertible_pointer_v<const int *, const void *>);
166 static_assert(!is_span_convertible_pointer_v<const int *, void *>);
167 static_assert(is_span_convertible_pointer_v<int *, void *>);
168 static_assert(is_span_convertible_pointer_v<int *, const void *>);
169 static_assert(!is_span_convertible_pointer_v<TestBaseClass *, TestChildClass *>);
170 static_assert(!is_span_convertible_pointer_v<TestChildClass *, TestBaseClass *>);
171 
172 static_assert(is_same_any_v<int, float, bool, int>);
173 static_assert(is_same_any_v<int, int, float>);
174 static_assert(is_same_any_v<int, int>);
175 static_assert(!is_same_any_v<int, float, bool>);
176 static_assert(!is_same_any_v<int, float>);
177 static_assert(!is_same_any_v<int>);
178 
179 TEST(memory_utils, ScopedDefer1)
180 {
181  int a = 0;
182  {
183  BLI_SCOPED_DEFER([&]() { a -= 5; });
184  {
185  BLI_SCOPED_DEFER([&]() { a *= 10; });
186  a = 5;
187  }
188  }
189  EXPECT_EQ(a, 45);
190 }
191 
192 TEST(memory_utils, ScopedDefer2)
193 {
194  std::string s;
195  {
196  BLI_SCOPED_DEFER([&]() { s += "A"; });
197  BLI_SCOPED_DEFER([&]() { s += "B"; });
198  BLI_SCOPED_DEFER([&]() { s += "C"; });
199  BLI_SCOPED_DEFER([&]() { s += "D"; });
200  }
201  EXPECT_EQ(s, "DCBA");
202 }
203 
204 } // namespace blender::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
#define BLI_SCOPED_DEFER(function_to_defer)
static int alive
Strict compiler flags for areas of code we want to ensure don't do conversions without us knowing abo...
#define UNUSED(x)
ccl_global float * buffer
static unsigned a[3]
Definition: RandGen.cpp:78
TEST(any, DefaultConstructor)
Definition: BLI_any_test.cc:10
void default_construct_n(T *ptr, int64_t n)
void uninitialized_fill_n(T *dst, int64_t n, const T &value)
void destruct_n(T *ptr, int64_t n)
void uninitialized_copy_n(const T *src, int64_t n, T *dst)