Blender V4.3
BLI_any_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: Apache-2.0 */
4
5#include "BLI_any.hh"
6#include "BLI_map.hh"
7
8#include "testing/testing.h"
9
10namespace blender::tests {
11
12TEST(any, DefaultConstructor)
13{
14 Any<> a;
15 EXPECT_FALSE(a.has_value());
16}
17
18TEST(any, AssignInt)
19{
20 Any<> a = 5;
21 EXPECT_TRUE(a.has_value());
22 EXPECT_TRUE(a.is<int>());
23 EXPECT_FALSE(a.is<float>());
24 const int &value = a.get<int>();
25 EXPECT_EQ(value, 5);
26 a = 10;
27 EXPECT_EQ(value, 10);
28
29 Any<> b = a;
30 EXPECT_TRUE(b.has_value());
31 EXPECT_EQ(b.get<int>(), 10);
32
33 Any<> c = std::move(a);
34 EXPECT_TRUE(c);
35 EXPECT_EQ(c.get<int>(), 10);
36
37 EXPECT_EQ(a.get<int>(), 10); /* NOLINT: bugprone-use-after-move */
38
39 a.reset();
40 EXPECT_FALSE(a);
41}
42
43TEST(any, AssignMap)
44{
45 Any<> a = Map<int, int>();
46 EXPECT_TRUE(a.has_value());
47 EXPECT_TRUE((a.is<Map<int, int>>()));
48 EXPECT_FALSE((a.is<Map<int, float>>()));
49 Map<int, int> &map = a.get<Map<int, int>>();
50 map.add(4, 2);
51 EXPECT_EQ((a.get<Map<int, int>>().lookup(4)), 2);
52
53 Any<> b = a;
54 EXPECT_TRUE(b);
55 EXPECT_EQ((b.get<Map<int, int>>().lookup(4)), 2);
56
57 Any<> c = std::move(a);
58 /* Test valid state after self assignment. Clang emits `-Wself-assign-overloaded` with `c=c;`.
59 * And `pragma` suppression creates warnings on other compilers. */
60 c = static_cast<decltype(a) &>(c);
61 EXPECT_TRUE(c);
62 EXPECT_EQ((c.get<Map<int, int>>().lookup(4)), 2);
63
64 EXPECT_TRUE((a.get<Map<int, int>>().is_empty())); /* NOLINT: bugprone-use-after-move */
65}
66
67TEST(any, AssignAny)
68{
69 Any<> a = 5;
70 Any<> b = std::string("hello");
71 Any<> c;
72
73 Any<> z;
74 EXPECT_FALSE(z.has_value());
75
76 z = a;
77 EXPECT_TRUE(z.has_value());
78 EXPECT_EQ(z.get<int>(), 5);
79
80 z = b;
81 EXPECT_EQ(z.get<std::string>(), "hello");
82
83 z = c;
84 EXPECT_FALSE(z.has_value());
85
86 z = Any(std::in_place_type<Any<>>, a);
87 EXPECT_FALSE(z.is<int>());
88 EXPECT_TRUE(z.is<Any<>>());
89 EXPECT_EQ(z.get<Any<>>().get<int>(), 5);
90}
91
92TEST(any, Allocate)
93{
94 Any<> a;
95 void *vec_mem = a.allocate<Vector<int>>();
96 Vector<int> &vec = *new (vec_mem) Vector<int>(100);
97 EXPECT_EQ(vec.size(), 100);
98 /* Leak detector checks whether the vector is freed again. */
99}
100
102 size_t size;
103
104 template<typename T> static constexpr ExtraSizeInfo get()
105 {
106 return {sizeof(T)};
107 }
108};
109
110TEST(any, ExtraInfo)
111{
112 using MyAny = Any<ExtraSizeInfo>;
113
114 MyAny a = 5;
115 EXPECT_EQ(a.extra_info().size, sizeof(int));
116
117 a = std::string("hello");
118 EXPECT_EQ(a.extra_info().size, sizeof(std::string));
119}
120
121} // namespace blender::tests
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition btQuadWord.h:117
void * allocate()
Definition BLI_any.hh:291
void * get()
Definition BLI_any.hh:324
bool add(const Key &key, const Value &value)
Definition BLI_map.hh:271
const Value & lookup(const Key &key) const
Definition BLI_map.hh:506
bool is_empty() const
Definition BLI_map.hh:937
int64_t size() const
local_group_size(16, 16) .push_constant(Type b
#define T
TEST(any, DefaultConstructor)
static constexpr ExtraSizeInfo get()