Blender V5.3
idprop_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include "BLI_string_utf8.hh"
6
7#include "BKE_gtest_base.hh"
8#include "BKE_idprop.hh"
9
10#include "testing/testing.h"
11
12namespace blender::bke::tests {
13
15
16/* U+1D400 is four bytes (0xF0 0x9D 0x90 0x80). */
17static const char *idp_utf8_4byte =
18 "\U0001D400\U0001D400\U0001D400\U0001D400\U0001D400"; /* 5 x 4 bytes = 20 bytes. */
19
20static bool idp_string_is_valid_utf8(const IDProperty *prop)
21{
22 return BLI_str_utf8_invalid_byte(IDP_string_get(prop), size_t(prop->len - 1)) == -1;
23}
24
25TEST_F(IDPropertyTest, NewStringMaxSizeUtf8Truncate)
26{
27 /* A truncated property must never be left as invalid UTF-8. */
28
31 EXPECT_STREQ(IDP_string_get(prop), "\U0001D400");
32 EXPECT_EQ(prop->len, 5);
33 IDP_FreeProperty(prop);
34
35 /* A code-point ending exactly at the limit must not be needlessly dropped. */
36 prop = IDP_NewStringMaxSize(idp_utf8_4byte, 9, "test");
38 EXPECT_STREQ(IDP_string_get(prop), "\U0001D400\U0001D400");
39 EXPECT_EQ(prop->len, 9);
40 IDP_FreeProperty(prop);
41}
42
43TEST_F(IDPropertyTest, AssignStringMaxSizeUtf8Truncate)
44{
45 /* Re-assignment has its own storage path that must equally avoid invalid UTF-8. */
46 IDProperty *prop = IDP_NewString("", "test");
49 EXPECT_STREQ(IDP_string_get(prop), "\U0001D400");
50 EXPECT_EQ(prop->len, 5);
51 IDP_FreeProperty(prop);
52}
53
54TEST_F(IDPropertyTest, AssignStringMaxSizeBytesIgnoreUtf8)
55{
56 /* Binary data must survive truncation byte-for-byte, never truncated as UTF-8 code-points. */
57 IDPropertyTemplate val = {};
58 val.string.subtype = IDP_STRING_SUB_BYTE;
59 IDProperty *prop = IDP_New(IDP_STRING, &val, "test");
60 ASSERT_EQ(prop->subtype, IDP_STRING_SUB_BYTE);
61
63 EXPECT_EQ(prop->len, 3);
64 EXPECT_NE(BLI_str_utf8_invalid_byte(IDP_string_get(prop), size_t(prop->len)), -1);
65
66 IDP_FreeProperty(prop);
67}
68
70{
71 IDProperty *prop = idprop::create_group("test").release();
72 IDP_FreeProperty(prop);
73}
74
76{
77 IDProperty *group = idprop::create_group("test").release();
78 EXPECT_EQ(IDP_GetPropertyFromGroup(group, "a"), nullptr);
79 EXPECT_TRUE(IDP_AddToGroup(group, idprop::create("a", 3.0f).release()));
80 EXPECT_TRUE(IDP_AddToGroup(group, idprop::create("b", 5).release()));
83 IDProperty *duplicate_prop = idprop::create("a", 10).release();
84 EXPECT_FALSE(IDP_AddToGroup(group, duplicate_prop));
86 EXPECT_EQ(IDP_GetPropertyFromGroup(group, "c"), nullptr);
87 IDP_FreeProperty(duplicate_prop);
88 IDP_FreeProperty(group);
89}
90
91TEST_F(IDPropertyTest, ReplaceInGroup)
92{
93 IDProperty *group = idprop::create_group("test").release();
94 EXPECT_TRUE(IDP_AddToGroup(group, idprop::create("a", 3.0f).release()));
96 IDP_ReplaceInGroup(group, idprop::create("a", 5.0f).release());
98 IDP_ReplaceInGroup(group, idprop::create("b", 5).release());
100 IDP_FreeProperty(group);
101}
102
103TEST_F(IDPropertyTest, RemoveFromGroup)
104{
105 IDProperty *group = idprop::create_group("test").release();
106 EXPECT_EQ(IDP_GetPropertyFromGroup(group, "a"), nullptr);
107 IDProperty *prop_a = idprop::create("a", 3.0f).release();
108 EXPECT_TRUE(IDP_AddToGroup(group, prop_a));
110 IDP_RemoveFromGroup(group, prop_a);
111 EXPECT_EQ(IDP_GetPropertyFromGroup(group, "a"), nullptr);
112 IDP_FreeProperty(prop_a);
113 IDP_FreeProperty(group);
114}
115
116TEST_F(IDPropertyTest, ReplaceGroupInGroup)
117{
118 IDProperty *group1 = idprop::create_group("test").release();
119 IDP_AddToGroup(group1, idprop::create("a", 1).release());
120 IDP_AddToGroup(group1, idprop::create("b", 2).release());
121 IDProperty *group2 = idprop::create_group("test2").release();
122 IDP_AddToGroup(group2, idprop::create("b", 3).release());
123 IDP_AddToGroup(group2, idprop::create("c", 4).release());
124
125 IDP_ReplaceGroupInGroup(group1, group2);
129
130 IDP_FreeProperty(group1);
131 IDP_FreeProperty(group2);
132}
133
134TEST_F(IDPropertyTest, SyncGroupValues)
135{
136 IDProperty *group1 = idprop::create_group("test").release();
137 IDProperty *group2 = idprop::create_group("test").release();
138 IDP_AddToGroup(group1, idprop::create("a", 1).release());
139 IDP_AddToGroup(group1, idprop::create("b", 2).release());
140 IDP_AddToGroup(group1, idprop::create("x", 2).release());
141 IDP_AddToGroup(group2, idprop::create("a", 3).release());
142 IDP_AddToGroup(group2, idprop::create("c", 4).release());
143 IDP_AddToGroup(group2, idprop::create("x", "value").release());
144
145 IDP_SyncGroupValues(group1, group2);
149 EXPECT_EQ(IDP_GetPropertyFromGroup(group1, "c"), nullptr);
150
151 IDP_FreeProperty(group1);
152 IDP_FreeProperty(group2);
153}
154
155TEST_F(IDPropertyTest, GroupLookupFloatArray)
156{
157 IDProperty *group = idprop::create_group("test").release();
158 IDP_AddToGroup(group, idprop::create("floats", Span<float>({-0.5f, 0.0f, 0.5f})).release());
159 IDP_AddToGroup(group, idprop::create("doubles", Span<double>({-0.5, 0.0, 0.5})).release());
160 IDP_AddToGroup(group, idprop::create("ints", Span<int32_t>({-1, 0, 1})).release());
161 IDP_AddToGroup(group, idprop::create("scalar", 0.5f).release());
162
163 const std::optional<Span<float>> floats = IDP_group_lookup_float_array(*group, "floats", 3);
164 ASSERT_TRUE(floats.has_value());
165 EXPECT_EQ(*floats, Span<float>({-0.5f, 0.0f, 0.5f}));
166
167 /* A size mismatch is not an error, the caller just does not get a value. */
168 EXPECT_FALSE(IDP_group_lookup_float_array(*group, "floats", 2).has_value());
169
170 /* Arrays of any other element type are rejected rather than reinterpreted. Assigning a float
171 * array through Python stores doubles, so this is reachable from user data. */
172 EXPECT_FALSE(IDP_group_lookup_float_array(*group, "doubles", 3).has_value());
173 EXPECT_FALSE(IDP_group_lookup_float_array(*group, "ints", 3).has_value());
174
175 /* A non-array float of the same name must not be treated as a length-one array. */
176 EXPECT_FALSE(IDP_group_lookup_float_array(*group, "scalar", 1).has_value());
177
178 EXPECT_FALSE(IDP_group_lookup_float_array(*group, "missing", 3).has_value());
179
180 IDP_FreeProperty(group);
181}
182
184{
185 auto repr_fn = [](IDProperty *prop) -> std::string {
186 uint result_len;
187 char *c_str = IDP_reprN(prop, &result_len);
188 std::string result = std::string(c_str, result_len);
189 MEM_delete(c_str);
190 return result;
191 };
192
193 IDProperty *group = idprop::create_group("test").release();
194
195 EXPECT_EQ(repr_fn(group), "{}");
196
197 IDP_AddToGroup(group, idprop::create("a", 1).release());
198 IDP_AddToGroup(group, idprop::create("b", 0.5f).release());
199 IDP_AddToGroup(group, idprop::create_bool("c", true).release());
200 IDP_AddToGroup(group, idprop::create_bool("d", false).release());
201 IDP_AddToGroup(group, idprop::create("e", "ABC (escape \" \\)").release());
202 IDP_AddToGroup(group, idprop::create("f", Span<int32_t>({-1, 0, 1})).release());
203 IDP_AddToGroup(group, idprop::create("g", Span<float>({-0.5f, 0.0f, 0.5f})).release());
204 IDP_AddToGroup(group, idprop::create_group("h").release());
205
206 EXPECT_EQ(repr_fn(group),
207 "{"
208 "\"a\": 1, "
209 "\"b\": 0.5, "
210 "\"c\": True, "
211 "\"d\": False, "
212 "\"e\": \"ABC (escape \\\" \\\\)\", "
213 "\"f\": [-1, 0, 1], "
214 "\"g\": [-0.5, 0, 0.5], "
215 "\"h\": {}"
216 "}");
217
218 IDP_FreeProperty(group);
219}
220
221} // namespace blender::bke::tests
#define IDP_float_get(prop)
#define IDP_int_get(prop)
#define IDP_string_get(prop)
#define EXPECT_FALSE(result)
#define EXPECT_NE(result, expect)
#define EXPECT_TRUE(result)
std::unique_ptr< IDProperty, IDPropertyDeleter > create_bool(StringRef prop_name, bool value, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_BOOLEAN, set its name and value.
std::unique_ptr< IDProperty, IDPropertyDeleter > create(StringRef prop_name, int32_t value, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_INT, set its name and value.
std::unique_ptr< IDProperty, IDPropertyDeleter > create_group(StringRef prop_name, eIDPropertyFlag flags={})
Allocate a new IDProperty of type IDP_GROUP.
static bool idp_string_is_valid_utf8(const IDProperty *prop)
TEST_F(AnimDataTest, BKE_fcurves_id_cb_test)
static const char * idp_utf8_4byte
void IDP_ReplaceGroupInGroup(IDProperty *dest, const IDProperty *src) ATTR_NONNULL()
Definition idprop.cc:705
void IDP_ReplaceInGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:737
ptrdiff_t BLI_str_utf8_invalid_byte(const char *str, size_t str_len) ATTR_NONNULL(1)
unsigned int uint
Definition BKE_text.h:55
bool IDP_AddToGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:788
IDProperty * IDP_GetPropertyFromGroup(const IDProperty *prop, StringRef name) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:818
IDProperty * IDP_NewString(const char *st, StringRef name, eIDPropertyFlag flags={}) ATTR_WARN_UNUSED_RESULT
Definition idprop.cc:443
void IDP_RemoveFromGroup(IDProperty *group, IDProperty *prop) ATTR_NONNULL()
Definition idprop.cc:801
@ IDP_STRING_SUB_BYTE
char * IDP_reprN(const IDProperty *prop, uint *r_len)
void IDP_SyncGroupValues(IDProperty *dest, const IDProperty *src) ATTR_NONNULL()
Definition idprop.cc:655
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
void IDP_FreeProperty(IDProperty *prop)
Definition idprop.cc:1403
std::optional< Span< float > > IDP_group_lookup_float_array(const IDProperty &group, StringRef name, int required_size)
Definition idprop.cc:898
IDProperty * IDP_New(char type, const IDPropertyTemplate *val, StringRef name, eIDPropertyFlag flags={}) ATTR_WARN_UNUSED_RESULT ATTR_NONNULL()
Definition idprop.cc:1132
IDProperty * IDP_NewStringMaxSize(const char *st, size_t st_maxncpy, StringRef name, eIDPropertyFlag flags={}) ATTR_WARN_UNUSED_RESULT
Definition idprop.cc:400
void IDP_AssignStringMaxSize(IDProperty *prop, const char *st, size_t st_maxncpy) ATTR_NONNULL()
Definition idprop.cc:470
struct blender::IDPropertyTemplate::@116303142222164257205355242364302075230051143265 string