Blender
V5.0
source
blender
blenlib
tests
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
10
namespace
blender::tests
{
11
12
TEST
(
any
, DefaultConstructor)
13
{
14
Any<>
a;
15
EXPECT_FALSE(a.
has_value
());
16
}
17
18
TEST
(
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
43
TEST
(
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
67
TEST
(
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
92
TEST
(
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
101
struct
ExtraSizeInfo
{
102
size_t
size
;
103
104
template
<
typename
T>
static
constexpr
ExtraSizeInfo
get
()
105
{
106
return
{
sizeof
(
T
)};
107
}
108
};
109
110
TEST
(
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
BLI_any.hh
EXPECT_EQ
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
BLI_map.hh
z
SIMD_FORCE_INLINE const btScalar & z() const
Return the z value.
Definition
btQuadWord.h:117
blender::Any
Definition
BLI_any.hh:98
blender::Any::allocate
void * allocate()
Definition
BLI_any.hh:289
blender::Any::has_value
bool has_value() const
Definition
BLI_any.hh:253
blender::Any::reset
void reset()
Definition
BLI_any.hh:238
blender::Any::is
bool is() const
Definition
BLI_any.hh:316
blender::Any::get
void * get()
Definition
BLI_any.hh:322
blender::Map
Definition
BLI_map.hh:129
blender::Map::add
bool add(const Key &key, const Value &value)
Definition
BLI_map.hh:295
blender::Map::lookup
const Value & lookup(const Key &key) const
Definition
BLI_map.hh:545
blender::Map::is_empty
bool is_empty() const
Definition
BLI_map.hh:986
blender::Vector
Definition
BLI_vector.hh:76
blender::Vector::size
int64_t size() const
Definition
BLI_vector.hh:771
b
b
Definition
compositor_morphological_distance_infos.hh:24
any
bool any(VecOp< bool, D >) RET
T
#define T
Definition
mball_tessellate.cc:274
blender::tests
Definition
BLF_tests.cc:9
blender::tests::TEST
TEST(blf_load, load)
Definition
BLF_tests.cc:34
blender::tests::ExtraSizeInfo
Definition
BLI_any_test.cc:101
blender::tests::ExtraSizeInfo::size
size_t size
Definition
BLI_any_test.cc:102
blender::tests::ExtraSizeInfo::get
static constexpr ExtraSizeInfo get()
Definition
BLI_any_test.cc:104
Generated on
for Blender by
doxygen
1.16.1