Blender V5.3
lib_id_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2020 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4#include "testing/testing.h"
5
6#include "BLI_listbase.hh"
7#include "BLI_string.hh"
8#include "BLI_string_ref.hh"
9
10#include "BKE_gtest_base.hh"
11#include "BKE_idtype.hh"
12#include "BKE_lib_id.hh"
13#include "BKE_main.hh"
14#include "BKE_main_namemap.hh"
15
16#include "DNA_ID.h"
17
18#include <string>
19
20namespace blender::bke::tests {
21
34
35static void test_lib_id_main_sort_check_order(std::initializer_list<ID *> list)
36{
37 ID *prev_id = nullptr;
38 for (ID *id : list) {
39 EXPECT_EQ(id->prev, prev_id);
40 if (prev_id != nullptr) {
41 EXPECT_EQ(prev_id->next, id);
42 }
43 prev_id = id;
44 }
45 EXPECT_EQ(prev_id->next, nullptr);
46}
47
49
51{
53 EXPECT_TRUE(ctx.bmain->libraries.is_empty());
54
55 ID *id_c = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_C"));
56 ID *id_a = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_A"));
57 ID *id_b = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_B"));
58 EXPECT_TRUE(ctx.bmain->objects.first_as<ID>() == id_a);
59 EXPECT_TRUE(ctx.bmain->objects.last_as<ID>() == id_c);
60 test_lib_id_main_sort_check_order({id_a, id_b, id_c});
61
62 EXPECT_EQ(ctx.bmain->name_map_global, nullptr);
63}
64
65static void change_lib(Main *bmain, ID *id, Library *lib)
66{
67 if (id->lib == lib) {
68 return;
69 }
70 BKE_main_namemap_remove_id(*bmain, *id);
71 id->lib = lib;
72 BKE_main_namemap_get_unique_name(*bmain, *id, id->name + 2);
73}
74
75static IDNewNameResult change_name(Main *bmain, ID *id, const char *name, const IDNewNameMode mode)
76{
77 return BKE_libblock_rename(*bmain, *id, name, mode);
78}
79
81{
83 EXPECT_TRUE(ctx.bmain->libraries.is_empty());
84
85 Library *lib_a = static_cast<Library *>(BKE_id_new(ctx.bmain, ID_LI, "LI_A"));
86 Library *lib_b = static_cast<Library *>(BKE_id_new(ctx.bmain, ID_LI, "LI_B"));
87 ID *id_c = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_C"));
88 ID *id_a = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_A"));
89 ID *id_b = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_B"));
90
91 change_lib(ctx.bmain, id_a, lib_a);
92 id_sort_by_name(&ctx.bmain->objects.cast<ID>(), id_a, nullptr);
93 change_lib(ctx.bmain, id_b, lib_a);
94 id_sort_by_name(&ctx.bmain->objects.cast<ID>(), id_b, nullptr);
95 EXPECT_TRUE(ctx.bmain->objects.first_as<ID>() == id_c);
96 EXPECT_TRUE(ctx.bmain->objects.last_as<ID>() == id_b);
97 test_lib_id_main_sort_check_order({id_c, id_a, id_b});
98
99 change_lib(ctx.bmain, id_a, lib_b);
100 id_sort_by_name(&ctx.bmain->objects.cast<ID>(), id_a, nullptr);
101 EXPECT_TRUE(ctx.bmain->objects.first_as<ID>() == id_c);
102 EXPECT_TRUE(ctx.bmain->objects.last_as<ID>() == id_a);
103 test_lib_id_main_sort_check_order({id_c, id_b, id_a});
104
105 change_lib(ctx.bmain, id_b, lib_b);
106 id_sort_by_name(&ctx.bmain->objects.cast<ID>(), id_b, nullptr);
107 EXPECT_TRUE(ctx.bmain->objects.first_as<ID>() == id_c);
108 EXPECT_TRUE(ctx.bmain->objects.last_as<ID>() == id_b);
109 test_lib_id_main_sort_check_order({id_c, id_a, id_b});
110
112
113 EXPECT_EQ(ctx.bmain->name_map_global, nullptr);
114}
115
117
118TEST_F(LibIDMainUniqueNameTest, local_ids_rename_existing_never)
119{
121 EXPECT_TRUE(ctx.bmain->libraries.is_empty());
122
123 ID *id_c = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_C"));
124 ID *id_a = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_A"));
125 ID *id_b = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_B"));
126 test_lib_id_main_sort_check_order({id_a, id_b, id_c});
127
129
131
132 /* Rename to different root name. */
134
136 // EXPECT_EQ(result.other_id, id_a); /* other_id purposely not looked-up currently. */
137 EXPECT_EQ(result.other_id, nullptr);
138 EXPECT_STREQ(id_c->name + 2, "OB_A.001");
139 EXPECT_STREQ(id_a->name + 2, "OB_A");
140 EXPECT_TRUE(ctx.bmain->objects.first_as<ID>() == id_a);
141 EXPECT_TRUE(ctx.bmain->objects.last_as<ID>() == id_b);
142 test_lib_id_main_sort_check_order({id_a, id_c, id_b});
143
145
146 /* Rename to same root name. */
148
150 // EXPECT_EQ(result.other_id, id_a); /* other_id purposely not looked-up currently. */
151 EXPECT_EQ(result.other_id, nullptr);
152 EXPECT_STREQ(id_c->name + 2, "OB_A.001");
153 EXPECT_STREQ(id_a->name + 2, "OB_A");
154 EXPECT_TRUE(ctx.bmain->objects.first_as<ID>() == id_a);
155 EXPECT_TRUE(ctx.bmain->objects.last_as<ID>() == id_b);
156 test_lib_id_main_sort_check_order({id_a, id_c, id_b});
157
159
160 EXPECT_EQ(ctx.bmain->name_map_global, nullptr);
161
162 /* Test lower-level #BKE_main_namemap_get_unique_name itself. */
163 /* Name already in use, needs additional numeric suffix. */
164 char future_name[MAX_ID_NAME - 2];
165 STRNCPY(future_name, "OB_B");
166 EXPECT_TRUE(BKE_main_namemap_get_unique_name(*ctx.bmain, *id_c, future_name));
167 EXPECT_STREQ(future_name, "OB_B.001");
168 /* Name not already in use, no need to alter it. */
169 STRNCPY(future_name, "OB_BBBB");
170 EXPECT_FALSE(BKE_main_namemap_get_unique_name(*ctx.bmain, *id_c, future_name));
171 EXPECT_STREQ(future_name, "OB_BBBB");
172 constexpr char long_name[] =
173 "OB_BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
174 "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
175 "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
176 "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
177 BLI_STATIC_ASSERT(std::string::traits_type::length(long_name) == MAX_ID_NAME - 2 - 1,
178 "Wrong 'max length' name");
179 constexpr char long_name_shorten[] =
180 "OB_BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
181 "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
182 "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"
183 "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB";
184 BLI_STATIC_ASSERT(std::string::traits_type::length(long_name_shorten) == MAX_ID_NAME - 2 - 2,
185 "Wrong 'max length' name");
186 /* Name too long, needs to be truncated. */
187 STRNCPY(future_name, long_name);
189 EXPECT_STREQ(id_a->name + 2, future_name);
190 EXPECT_STREQ(future_name, long_name);
191 EXPECT_TRUE(BKE_main_namemap_get_unique_name(*ctx.bmain, *id_c, future_name));
192 EXPECT_STREQ(future_name, long_name_shorten);
193}
194
195TEST_F(LibIDMainUniqueNameTest, local_ids_rename_existing_always)
196{
198 EXPECT_TRUE(ctx.bmain->libraries.is_empty());
199
200 ID *id_c = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_C"));
201 ID *id_a = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_A"));
202 ID *id_b = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_B"));
203 test_lib_id_main_sort_check_order({id_a, id_b, id_c});
204
206
208
209 /* Rename to different root name. */
211
213 EXPECT_EQ(result.other_id, id_a);
214 EXPECT_STREQ(id_c->name + 2, "OB_A");
215 EXPECT_STREQ(id_a->name + 2, "OB_A.001");
216 EXPECT_TRUE(ctx.bmain->objects.first_as<ID>() == id_c);
217 EXPECT_TRUE(ctx.bmain->objects.last_as<ID>() == id_b);
218 test_lib_id_main_sort_check_order({id_c, id_a, id_b});
219
221
222 /* Rename to same root name. */
224
226 EXPECT_EQ(result.other_id, id_c);
227 EXPECT_STREQ(id_c->name + 2, "OB_A.001");
228 EXPECT_STREQ(id_a->name + 2, "OB_A");
229 EXPECT_TRUE(ctx.bmain->objects.first_as<ID>() == id_a);
230 EXPECT_TRUE(ctx.bmain->objects.last_as<ID>() == id_b);
231 test_lib_id_main_sort_check_order({id_a, id_c, id_b});
232
234
235 EXPECT_EQ(ctx.bmain->name_map_global, nullptr);
236}
237
238TEST_F(LibIDMainUniqueNameTest, local_ids_rename_existing_same_root)
239{
241 EXPECT_TRUE(ctx.bmain->libraries.is_empty());
242
243 ID *id_c = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_C"));
244 ID *id_a = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_A"));
245 ID *id_b = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_B"));
246 test_lib_id_main_sort_check_order({id_a, id_b, id_c});
247
249
251
252 /* Rename to different root name. */
254
256 // EXPECT_EQ(result.other_id, id_a); /* other_id purposely not looked-up currently. */
257 EXPECT_EQ(result.other_id, nullptr);
258 EXPECT_STREQ(id_c->name + 2, "OB_A.001");
259 EXPECT_STREQ(id_a->name + 2, "OB_A");
260 EXPECT_TRUE(ctx.bmain->objects.first_as<ID>() == id_a);
261 EXPECT_TRUE(ctx.bmain->objects.last_as<ID>() == id_b);
262 test_lib_id_main_sort_check_order({id_a, id_c, id_b});
263
265
266 /* Rename to same root name. */
268
270 EXPECT_EQ(result.other_id, id_a);
271 EXPECT_STREQ(id_c->name + 2, "OB_A");
272 EXPECT_STREQ(id_a->name + 2, "OB_A.001");
273 EXPECT_TRUE(ctx.bmain->objects.first_as<ID>() == id_c);
274 EXPECT_TRUE(ctx.bmain->objects.last_as<ID>() == id_b);
275 test_lib_id_main_sort_check_order({id_c, id_a, id_b});
276
278
279 EXPECT_EQ(ctx.bmain->name_map_global, nullptr);
280}
281
283{
285 EXPECT_TRUE(ctx.bmain->libraries.is_empty());
286
287 Library *lib_a = static_cast<Library *>(BKE_id_new(ctx.bmain, ID_LI, "LI_A"));
288 Library *lib_b = static_cast<Library *>(BKE_id_new(ctx.bmain, ID_LI, "LI_B"));
289 ID *id_c = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_C"));
290 ID *id_a = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_A"));
291 ID *id_b = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_B"));
292
294
295 change_lib(ctx.bmain, id_a, lib_a);
296 id_sort_by_name(&ctx.bmain->objects.cast<ID>(), id_a, nullptr);
297 change_lib(ctx.bmain, id_b, lib_a);
298 id_sort_by_name(&ctx.bmain->objects.cast<ID>(), id_b, nullptr);
299
301 EXPECT_STREQ(id_b->name + 2, "OB_A.001");
302 EXPECT_STREQ(id_a->name + 2, "OB_A");
303 EXPECT_TRUE(ctx.bmain->objects.first_as<ID>() == id_c);
304 EXPECT_TRUE(ctx.bmain->objects.last_as<ID>() == id_b);
305 test_lib_id_main_sort_check_order({id_c, id_a, id_b});
306
308
309 change_lib(ctx.bmain, id_b, lib_b);
310 id_sort_by_name(&ctx.bmain->objects.cast<ID>(), id_b, nullptr);
312 EXPECT_STREQ(id_b->name + 2, "OB_A");
313 EXPECT_STREQ(id_a->name + 2, "OB_A");
314 EXPECT_TRUE(ctx.bmain->objects.first_as<ID>() == id_c);
315 EXPECT_TRUE(ctx.bmain->objects.last_as<ID>() == id_b);
316 test_lib_id_main_sort_check_order({id_c, id_a, id_b});
317
319
320 EXPECT_EQ(ctx.bmain->name_map_global, nullptr);
321}
322
323static void change_name_global(Main *bmain, ID *id, const char *name)
324{
325 BKE_main_namemap_remove_id(*bmain, *id);
326 BLI_strncpy(id->name + 2, name, MAX_ID_NAME - 2);
327
328 BKE_main_global_namemap_get_unique_name(*bmain, *id, id->name + 2);
329
330 id_sort_by_name(&bmain->objects.cast<ID>(), id, nullptr);
331}
332
334
336{
338 EXPECT_TRUE(ctx.bmain->libraries.is_empty());
339
340 Library *lib_a = static_cast<Library *>(BKE_id_new(ctx.bmain, ID_LI, "LI_A"));
341 Library *lib_b = static_cast<Library *>(BKE_id_new(ctx.bmain, ID_LI, "LI_B"));
342 ID *id_c = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_C"));
343 ID *id_a = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_A"));
344 ID *id_b = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "OB_B"));
345
347
348 change_lib(ctx.bmain, id_a, lib_a);
349 id_sort_by_name(&ctx.bmain->objects.cast<ID>(), id_a, nullptr);
350 change_lib(ctx.bmain, id_b, lib_b);
351 id_sort_by_name(&ctx.bmain->objects.cast<ID>(), id_b, nullptr);
352
353 change_name_global(ctx.bmain, id_b, "OB_A");
354 EXPECT_NE(ctx.bmain->name_map_global, nullptr);
355 EXPECT_STREQ(id_b->name + 2, "OB_A.001");
356 EXPECT_STREQ(id_a->name + 2, "OB_A");
357 EXPECT_TRUE(ctx.bmain->objects.first_as<ID>() == id_c);
358 EXPECT_TRUE(ctx.bmain->objects.last_as<ID>() == id_b);
359 test_lib_id_main_sort_check_order({id_c, id_a, id_b});
360
362
363 change_lib(ctx.bmain, id_b, lib_a);
364 id_sort_by_name(&ctx.bmain->objects.cast<ID>(), id_b, nullptr);
365 change_name_global(ctx.bmain, id_b, "OB_C");
366 EXPECT_STREQ(id_b->name + 2, "OB_C.001");
367 EXPECT_STREQ(id_a->name + 2, "OB_A");
368 EXPECT_STREQ(id_c->name + 2, "OB_C");
369 change_name_global(ctx.bmain, id_a, "OB_C");
370 EXPECT_STREQ(id_b->name + 2, "OB_C.001");
371 EXPECT_STREQ(id_a->name + 2, "OB_C.002");
372 EXPECT_STREQ(id_c->name + 2, "OB_C");
373 EXPECT_TRUE(ctx.bmain->objects.first_as<ID>() == id_c);
374 EXPECT_TRUE(ctx.bmain->objects.last_as<ID>() == id_a);
375 test_lib_id_main_sort_check_order({id_c, id_b, id_a});
376
378
380 EXPECT_STREQ(id_b->name + 2, "OB_C");
381 EXPECT_STREQ(id_a->name + 2, "OB_C.002");
382 EXPECT_STREQ(id_c->name + 2, "OB_C");
383 EXPECT_TRUE(ctx.bmain->objects.first_as<ID>() == id_c);
384 EXPECT_TRUE(ctx.bmain->objects.last_as<ID>() == id_a);
385 test_lib_id_main_sort_check_order({id_c, id_b, id_a});
386
388}
389
390TEST_F(LibIDMainUniqueNameTest, ids_sorted_by_default)
391{
393
394 ID *id_foo = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
395 ID *id_bar = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Bar"));
396 ID *id_baz = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Baz"));
397 ID *id_yes = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Yes"));
398 test_lib_id_main_sort_check_order({id_bar, id_baz, id_foo, id_yes});
399
401
402 EXPECT_EQ(ctx.bmain->name_map_global, nullptr);
403}
404
405static ID *add_id_in_library(Main *bmain, const char *name, Library *lib)
406{
407 ID *id = static_cast<ID *>(BKE_id_new(bmain, ID_OB, name));
408 change_lib(bmain, id, lib);
409 id_sort_by_name(&bmain->objects.cast<ID>(), id, nullptr);
410 return id;
411}
412
413TEST_F(LibIDMainUniqueNameTest, ids_sorted_by_default_with_libraries)
414{
416
417 Library *lib_one = static_cast<Library *>(BKE_id_new(ctx.bmain, ID_LI, "LibOne"));
418 Library *lib_two = static_cast<Library *>(BKE_id_new(ctx.bmain, ID_LI, "LibTwo"));
419
420 ID *id_foo = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
421 ID *id_bar = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Bar"));
422
423 ID *id_l1c = add_id_in_library(ctx.bmain, "C", lib_one);
424 ID *id_l2b = add_id_in_library(ctx.bmain, "B", lib_two);
425 ID *id_l1a = add_id_in_library(ctx.bmain, "A", lib_one);
426
427 ID *id_baz = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Baz"));
428 ID *id_yes = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Yes"));
429
430 test_lib_id_main_sort_check_order({id_bar, id_baz, id_foo, id_yes, id_l1a, id_l1c, id_l2b});
431
433
434 EXPECT_EQ(ctx.bmain->name_map_global, nullptr);
435}
436
437TEST_F(LibIDMainUniqueNameTest, name_too_long_handling)
438{
440 constexpr char name_a[] =
441 "Long_Name_That_Does_Not_Fit_Into_Max_Name_Limit_And_Should_Get_Truncated_"
442 "Long_Name_That_Does_Not_Fit_Into_Max_Name_Limit_And_Should_Get_Truncated_"
443 "Long_Name_That_Does_Not_Fit_Into_Max_Name_Limit_And_Should_Get_Truncated_"
444 "Long_Name_That_Does_Not_Fit_Into_Max_Name_Limit_And_Should_Get_Truncated";
445 BLI_STATIC_ASSERT(std::string::traits_type::length(name_a) > MAX_ID_NAME - 2,
446 "Wrong 'max length' name");
447 constexpr char name_a_shorten[] =
448 "Long_Name_That_Does_Not_Fit_Into_Max_Name_Limit_And_Should_Get_Truncated_"
449 "Long_Name_That_Does_Not_Fit_Into_Max_Name_Limit_And_Should_Get_Truncated_"
450 "Long_Name_That_Does_Not_Fit_Into_Max_Name_Limit_And_Should_Get_Truncated_"
451 "Long_Name_That_Does_Not_Fit_Into_Max";
452 BLI_STATIC_ASSERT(std::string::traits_type::length(name_a_shorten) == MAX_ID_NAME - 2 - 1,
453 "Wrong 'max length' name");
454 constexpr char name_b[] =
455 "Another_Long_Name_That_Does_Not_Fit_And_Has_A_Number_Suffix_____"
456 "Another_Long_Name_That_Does_Not_Fit_And_Has_A_Number_Suffix_____"
457 "Another_Long_Name_That_Does_Not_Fit_And_Has_A_Number_Suffix_____"
458 "Another_Long_Name_That_Does_Not_Fit_And_Has_A_Number_Suffix.123456";
459 BLI_STATIC_ASSERT(std::string::traits_type::length(name_b) > MAX_ID_NAME - 2,
460 "Wrong 'max length' name");
461 constexpr char name_b_shorten[] =
462 "Another_Long_Name_That_Does_Not_Fit_And_Has_A_Number_Suffix_____"
463 "Another_Long_Name_That_Does_Not_Fit_And_Has_A_Number_Suffix_____"
464 "Another_Long_Name_That_Does_Not_Fit_And_Has_A_Number_Suffix_____"
465 "Another_Long_Name_That_Does_Not_Fit_And_Has_A_Number_Suffix.123";
466 BLI_STATIC_ASSERT(std::string::traits_type::length(name_b_shorten) == MAX_ID_NAME - 2 - 1,
467 "Wrong 'max length' name");
468 constexpr char name_c[] = "Name_That_Has_Too_Long_Number_Suffix.1234567890";
469 BLI_STATIC_ASSERT(std::string::traits_type::length(name_c) < MAX_ID_NAME - 2,
470 "Wrong 'max length' name");
471
472 ID *id_a = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, name_a));
473 ID *id_b = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, name_b));
474 ID *id_c = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, name_c));
475
476 EXPECT_STREQ(BKE_id_name(*id_a), name_a_shorten);
477 EXPECT_STREQ(BKE_id_name(*id_b), name_b_shorten);
478 EXPECT_STREQ(BKE_id_name(*id_c), name_c); /* Unchanged */
479
481
482 EXPECT_EQ(ctx.bmain->name_map_global, nullptr);
483}
484
485TEST_F(LibIDMainUniqueNameTest, create_equivalent_numeric_suffixes)
486{
488
489 /* Create names where many of their numeric suffixes are
490 * the same number, yet the names are different and thus
491 * should be allowed as-is. */
492 ID *id_a = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.123"));
493 ID *id_b = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.000"));
494 ID *id_c = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.003"));
495 ID *id_d = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.3"));
496 ID *id_e = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.0"));
497 ID *id_f = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo."));
498 ID *id_g = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.0123"));
499 ID *id_h = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
500 ID *id_i = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.."));
501 ID *id_j = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo..001"));
502 ID *id_k = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo..000"));
503
504 EXPECT_STREQ(id_a->name + 2, "Foo.123");
505 EXPECT_STREQ(id_b->name + 2, "Foo.000");
506 EXPECT_STREQ(id_c->name + 2, "Foo.003");
507 EXPECT_STREQ(id_d->name + 2, "Foo.3");
508 EXPECT_STREQ(id_e->name + 2, "Foo.0");
509 EXPECT_STREQ(id_f->name + 2, "Foo.");
510 EXPECT_STREQ(id_g->name + 2, "Foo.0123");
511 EXPECT_STREQ(id_h->name + 2, "Foo");
512 EXPECT_STREQ(id_i->name + 2, "Foo..");
513 EXPECT_STREQ(id_j->name + 2, "Foo..001");
514 EXPECT_STREQ(id_k->name + 2, "Foo..000");
515
517
518 /* Now create their exact duplicates again, and check what happens. */
519 id_a = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.123"));
520 id_b = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.000"));
521 id_c = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.003"));
522 id_d = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.3"));
523 id_e = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.0"));
524 id_f = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo."));
525 id_g = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.0123"));
526 id_h = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
527 id_i = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.."));
528 id_j = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo..001"));
529 id_k = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo..000"));
530
531 EXPECT_STREQ(id_a->name + 2, "Foo.001");
532 EXPECT_STREQ(id_b->name + 2, "Foo.002");
533 EXPECT_STREQ(id_c->name + 2, "Foo.004");
534 EXPECT_STREQ(id_d->name + 2, "Foo.005");
535 EXPECT_STREQ(id_e->name + 2, "Foo.006");
536 EXPECT_STREQ(id_f->name + 2, "Foo..002");
537 EXPECT_STREQ(id_g->name + 2, "Foo.007");
538 EXPECT_STREQ(id_h->name + 2, "Foo.008");
539 EXPECT_STREQ(id_i->name + 2, "Foo...001");
540 EXPECT_STREQ(id_j->name + 2, "Foo..003");
541 EXPECT_STREQ(id_k->name + 2, "Foo..004");
542
544
545 EXPECT_EQ(ctx.bmain->name_map_global, nullptr);
546}
547
548TEST_F(LibIDMainUniqueNameTest, re_create_equivalent_numeric_suffixes)
549{
551
552 /* Create names where many of their numeric suffixes are
553 * the same number, yet the names are different and thus
554 * should be allowed as-is. */
555 ID *id_a = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.123"));
556 ID *id_b = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.001"));
557 ID *id_c = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.01"));
558 ID *id_d = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.1"));
559 ID *id_e = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
560
561 EXPECT_STREQ(BKE_id_name(*id_a), "Foo.123");
562 EXPECT_STREQ(BKE_id_name(*id_b), "Foo.001");
563 EXPECT_STREQ(BKE_id_name(*id_c), "Foo.01");
564 EXPECT_STREQ(BKE_id_name(*id_d), "Foo.1");
565 EXPECT_STREQ(BKE_id_name(*id_e), "Foo");
566
567 /* Deleting 'Foo.1' will _not_ mark number `1` as available, since its internal multi-usages
568 * counter will still be at `2`, for the 'Foo.01' and 'Foo.001' IDs still present.
569 *
570 * So the number `1` is not available, and since `123` is also used, the next free value is `2`.
571 */
572 BKE_id_delete(ctx.bmain, id_d);
573 id_d = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.123"));
574 EXPECT_STREQ(BKE_id_name(*id_d), "Foo.002");
575
576 /* However, while deleting 'Foo.001' will _not_ mark number `1` as available, it _will_ remove
577 * the exact name from the full names map.
578 *
579 * So adding again 'Foo.001' will succeed and not modify the name at all. */
580 BKE_id_delete(ctx.bmain, id_b);
581 id_b = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.001"));
582 EXPECT_STREQ(BKE_id_name(*id_b), "Foo.001");
583
584 /* Finally, removing the last two users of number `1` makes it available again. */
585 BKE_id_delete(ctx.bmain, id_b);
586 BKE_id_delete(ctx.bmain, id_c);
587 id_b = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
588 EXPECT_STREQ(BKE_id_name(*id_b), "Foo.001");
589 id_c = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.01"));
590 EXPECT_STREQ(BKE_id_name(*id_c), "Foo.01");
591
593
594 EXPECT_EQ(ctx.bmain->name_map_global, nullptr);
595}
596
597TEST_F(LibIDMainUniqueNameTest, zero_suffix_is_never_assigned)
598{
600
601 /* Creating these should assign 002 to the first one, but the next
602 * ones should start numbers starting from 1: 001 and 003. */
603 ID *id_002 = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.002"));
604 ID *id_001 = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.002"));
605 ID *id_003 = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.002"));
606
607 EXPECT_STREQ(id_002->name + 2, "Foo.002");
608 EXPECT_STREQ(id_001->name + 2, "Foo.001");
609 EXPECT_STREQ(id_003->name + 2, "Foo.003");
610
612
613 EXPECT_EQ(ctx.bmain->name_map_global, nullptr);
614}
615
616TEST_F(LibIDMainUniqueNameTest, remove_after_dup_get_original_name)
617{
619
620 ID *id_a = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
621 ID *id_b = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
622
623 EXPECT_STREQ(id_a->name + 2, "Foo");
624 EXPECT_STREQ(id_b->name + 2, "Foo.001");
625 BKE_id_free(ctx.bmain, id_a);
626
628
629 id_a = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
630 EXPECT_STREQ(id_a->name + 2, "Foo");
631
633
634 EXPECT_EQ(ctx.bmain->name_map_global, nullptr);
635}
636
637TEST_F(LibIDMainUniqueNameTest, name_number_suffix_assignment)
638{
640
641 /* Create <1k objects first. */
642 const int total_object_count = 1200;
643 ID *ids[total_object_count] = {};
644 for (int i = 0; i < total_object_count / 2; ++i) {
645 ids[i] = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
646 }
647
648 /* They should get assigned sequential numeric suffixes. */
649 EXPECT_STREQ(ids[0]->name + 2, "Foo");
650 EXPECT_STREQ(ids[1]->name + 2, "Foo.001");
651 EXPECT_STREQ(ids[total_object_count / 2 - 1]->name + 2, "Foo.599");
652
654
655 /* Free some of the objects. */
656 BKE_id_free(ctx.bmain, ids[10]);
657 BKE_id_free(ctx.bmain, ids[20]);
658 BKE_id_free(ctx.bmain, ids[30]);
659
661
662 /* Create objects again; they should get suffixes that were just freed up. */
663 ID *id_010 = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
664 EXPECT_STREQ(id_010->name + 2, "Foo.010");
665 ID *id_020 = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.123"));
666 EXPECT_STREQ(id_020->name + 2, "Foo.020");
667 /* Suffixes >1k do not get the "use the most proper free one" treatment. */
668 ID *id_2000 = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.2000"));
669 EXPECT_STREQ(id_2000->name + 2, "Foo.2000");
670 /* But smaller than 1k suffixes do get proper empty spots. */
671 ID *id_030 = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
672 EXPECT_STREQ(id_030->name + 2, "Foo.030");
673 ID *id_600 = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
674 EXPECT_STREQ(id_600->name + 2, "Foo.600");
675
677
678 /* Max possible numeric suffix. */
679 ID *id_max = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.999999999"));
680 EXPECT_STREQ(id_max->name + 2, "Foo.999999999");
681 /* Try with max. possible suffix again: will assign free suffix under 1k. */
682 ID *id_max1 = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.999999999"));
683 EXPECT_STREQ(id_max1->name + 2, "Foo.601");
684
686
687 /* Now create the rest of objects, to use all the suffixes up to 1k.
688 * Once all the ones up to 1k are used, the logic will fall back to
689 * "use largest number seen + 1", but the largest one is already the max
690 * possible. So it will modify the name part and restart the counter,
691 * i.e. "Foo_001.001". */
692 for (int i = total_object_count / 2; i < total_object_count; ++i) {
693 ids[i] = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
694 }
695 /* At this point creating "Foo" based objects will always result in names extended with a 3 or
696 * more digits numeric suffix, e.g. "Foo_001.001".
697 *
698 * NOTE: The random 3-digits suffix added to the base name is expected to be stable, as the
699 * requested base name remains the same. This is why the added numeric suffixes can be predicted
700 * here. */
701 ID *id_foo_001_178 = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
702 EXPECT_STREQ(BKE_id_name(*id_foo_001_178), "Foo_001.178");
703 ID *id_foo_001_179 = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.2000"));
704 EXPECT_STREQ(BKE_id_name(*id_foo_001_179), "Foo_001.179");
705 ID *id_foo_001_180 = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo.999999999"));
706 EXPECT_STREQ(BKE_id_name(*id_foo_001_180), "Foo_001.180");
707
708 /* Longer names will be shortened, when no more numeric suffixes are available. */
709 for (int i = 0; i < total_object_count; ++i) {
710 ids[i] = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "ALongerName"));
711 }
712 /* Max possible numeric suffix. */
713 id_max = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "ALongerName.999999999"));
714 EXPECT_STREQ(BKE_id_name(*id_max), "ALongerName.999999999");
715
716 ID *id_alongernam = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "ALongerName"));
717 EXPECT_STREQ(BKE_id_name(*id_alongernam), "ALongerNam");
718 ID *id_alongernam001 = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "ALongerName"));
719 EXPECT_STREQ(BKE_id_name(*id_alongernam001), "ALongerNam.001");
720
722
723 EXPECT_EQ(ctx.bmain->name_map_global, nullptr);
724}
725
726TEST_F(LibIDMainUniqueNameTest, renames_with_duplicates)
727{
729
730 ID *id_a = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
731 ID *id_b = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
732 ID *id_c = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Bar"));
733
734 EXPECT_STREQ(id_a->name + 2, "Foo");
735 EXPECT_STREQ(id_b->name + 2, "Foo.001");
736 EXPECT_STREQ(id_c->name + 2, "Bar");
737
739
740 BKE_libblock_rename(*ctx.bmain, *id_a, "Foo.002");
741 EXPECT_STREQ(id_a->name + 2, "Foo.002");
742 BKE_libblock_rename(*ctx.bmain, *id_b, "Bar");
743 EXPECT_STREQ(id_b->name + 2, "Bar.001");
744 BKE_libblock_rename(*ctx.bmain, *id_c, "Foo");
745 EXPECT_STREQ(id_c->name + 2, "Foo");
746 BKE_libblock_rename(*ctx.bmain, *id_b, "Bar");
747 EXPECT_STREQ(id_b->name + 2, "Bar");
748
750
751 EXPECT_EQ(ctx.bmain->name_map_global, nullptr);
752}
753
754TEST_F(LibIDMainUniqueNameTest, names_are_unique_per_id_type)
755{
757
758 ID *id_a = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
759 ID *id_b = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_CA, "Foo"));
760 ID *id_c = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "Foo"));
761
762 EXPECT_STREQ(id_a->name + 2, "Foo");
763 EXPECT_STREQ(id_b->name + 2, "Foo"); /* Different types (OB & CA) can have the same name. */
764 EXPECT_STREQ(id_c->name + 2, "Foo.001");
765
767
768 EXPECT_EQ(ctx.bmain->name_map_global, nullptr);
769}
770
771TEST_F(LibIDMainUniqueNameTest, name_huge_number_suffix)
772{
774
775 /* Use numeric suffix that is really large: should come through
776 * fine, since no duplicates with other names. */
777 ID *id_a = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "SuperLong.1234567890"));
778 EXPECT_STREQ(id_a->name + 2, "SuperLong.1234567890");
779 /* Now create with the same name again: should get 001 suffix. */
780 ID *id_b = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_OB, "SuperLong.1234567890"));
781 EXPECT_STREQ(id_b->name + 2, "SuperLong.001");
782
784
785 EXPECT_EQ(ctx.bmain->name_map_global, nullptr);
786}
787
789
791{
793
794 Library *lib_a = static_cast<Library *>(BKE_id_new(ctx.bmain, ID_LI, "LI_A"));
795 ID *br_a = static_cast<ID *>(BKE_id_new(ctx.bmain, ID_BR, "BR_A"));
796
797 change_lib(ctx.bmain, br_a, lib_a);
798
800 EXPECT_NE(br_a->newid, nullptr);
801
803 EXPECT_EQ(br_a->newid->us, 1);
804}
805
806} // namespace blender::bke::tests
ID and Library types, which are fundamental for SDNA.
#define MAX_ID_NAME
Definition DNA_ID.h:367
#define EXPECT_FALSE(result)
#define EXPECT_NE(result, expect)
#define EXPECT_TRUE(result)
static ID * add_id_in_library(Main *bmain, const char *name, Library *lib)
static void change_lib(Main *bmain, ID *id, Library *lib)
static void change_name_global(Main *bmain, ID *id, const char *name)
static IDNewNameResult change_name(Main *bmain, ID *id, const char *name, const IDNewNameMode mode)
TEST_F(AnimDataTest, BKE_fcurves_id_cb_test)
static void test_lib_id_main_sort_check_order(std::initializer_list< ID * > list)
BLI_STATIC_ASSERT(ARRAY_SIZE(data_mode_strings)==CTX_MODE_NUM+1, "Must have a string for each context mode") const char *CTX_data_mode_string(const bContext *C)
char * STRNCPY(char(&dst)[N], const char *src)
void BKE_id_delete(Main *bmain, void *idv, const BKEIDDeleteOptions &options={}) ATTR_NONNULL()
Main * BKE_main_new()
Definition main.cc:93
bool BKE_main_namemap_get_unique_name(Main &bmain, ID &id, char *r_name)
const char * BKE_id_name(const ID &id)
void BKE_main_namemap_remove_id(Main &bmain, ID &id)
void id_sort_by_name(ListBaseT< ID > *lb, ID *id, ID *id_sorting_hint)
Definition lib_id.cc:1888
char * BLI_strncpy(char *__restrict dst, const char *__restrict src, size_t dst_maxncpy) ATTR_NONNULL(1
void BKE_id_free(Main *bmain, void *idv)
bool BKE_main_namemap_validate(Main &bmain)
void BKE_main_free(Main *bmain)
Definition main.cc:202
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
IDNewNameResult BKE_libblock_rename(Main &bmain, ID &id, StringRefNull name, const IDNewNameMode mode=IDNewNameMode::RenameExistingNever)
Definition lib_id.cc:2632
bool BKE_main_global_namemap_get_unique_name(Main &bmain, ID &id, char *r_name)
@ ID_FLAG_FAKEUSER
Definition DNA_ID.h:770
@ LIB_ID_MAKELOCAL_FORCE_COPY
bool BKE_lib_id_make_local(Main *bmain, ID *id, int flags)
Definition lib_id.cc:672
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
char name[258]
Definition DNA_ID.h:433
struct ID * newid
Definition DNA_ID.h:419
short flag
Definition DNA_ID.h:439
void * next
Definition DNA_ID.h:418
UniqueName_Map * name_map_global
Definition BKE_main.hh:449
ListBaseT< Library > libraries
Definition BKE_main.hh:387
ListBaseT< Object > objects
Definition BKE_main.hh:388