diff options
Diffstat (limited to 'source/blender/blenlib/tests')
-rw-r--r-- | source/blender/blenlib/tests/BLI_listbase_test.cc | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/source/blender/blenlib/tests/BLI_listbase_test.cc b/source/blender/blenlib/tests/BLI_listbase_test.cc index 0ba08a0cd48..d66eb214902 100644 --- a/source/blender/blenlib/tests/BLI_listbase_test.cc +++ b/source/blender/blenlib/tests/BLI_listbase_test.cc @@ -96,6 +96,64 @@ TEST(listbase, FindLinkOrIndex) BLI_freelistN(&lb); } +TEST(listbase, FindLinkFromStringOrPointer) +{ + struct TestLink { + struct TestLink *prev, *next; + char name[64]; + const void *ptr; + }; + + const char *const link1_name = "Link1"; + const char *const link2_name = "Link2"; + const void *const link1_ptr = nullptr; + const void *const link2_ptr = link2_name; + + const size_t name_offset = offsetof(struct TestLink, name); + const size_t ptr_offset = offsetof(struct TestLink, ptr); + + ListBase lb; + struct TestLink *link1 = (struct TestLink *)MEM_callocN(sizeof(TestLink), "link1"); + BLI_strncpy(link1->name, link1_name, sizeof(link1->name)); + link1->ptr = link1_ptr; + struct TestLink *link2 = (struct TestLink *)MEM_callocN(sizeof(TestLink), "link2"); + BLI_strncpy(link2->name, link2_name, sizeof(link2->name)); + link2->ptr = link2_ptr; + + /* Empty list */ + BLI_listbase_clear(&lb); + EXPECT_EQ(BLI_findptr(&lb, link1_ptr, ptr_offset), (void *)nullptr); + EXPECT_EQ(BLI_findstring(&lb, link1_name, name_offset), (void *)nullptr); + EXPECT_EQ(BLI_rfindptr(&lb, link1_ptr, ptr_offset), (void *)nullptr); + EXPECT_EQ(BLI_rfindstring(&lb, link1_name, name_offset), (void *)nullptr); + EXPECT_EQ(BLI_listbase_string_or_index_find(&lb, link1_name, name_offset, 0), (void *)nullptr); + + /* One link */ + BLI_addtail(&lb, link1); + EXPECT_EQ(BLI_findptr(&lb, link1_ptr, ptr_offset), (void *)link1); + EXPECT_EQ(BLI_findstring(&lb, link1_name, name_offset), (void *)link1); + EXPECT_EQ(BLI_rfindptr(&lb, link1_ptr, ptr_offset), (void *)link1); + EXPECT_EQ(BLI_rfindstring(&lb, link1_name, name_offset), (void *)link1); + EXPECT_EQ(BLI_listbase_string_or_index_find(&lb, link1_name, name_offset, 0), (void *)link1); + EXPECT_EQ(BLI_listbase_string_or_index_find(&lb, "", name_offset, 0), (void *)link1); + EXPECT_EQ(BLI_listbase_string_or_index_find(&lb, nullptr, name_offset, 0), (void *)link1); + EXPECT_EQ(BLI_listbase_string_or_index_find(&lb, nullptr, name_offset, 1), (void *)nullptr); + + /* Two links */ + BLI_addtail(&lb, link2); + EXPECT_EQ(BLI_findptr(&lb, link1_ptr, ptr_offset), (void *)link1); + EXPECT_EQ(BLI_findstring(&lb, link1_name, name_offset), (void *)link1); + EXPECT_EQ(BLI_rfindptr(&lb, link1_ptr, ptr_offset), (void *)link1); + EXPECT_EQ(BLI_rfindstring(&lb, link1_name, name_offset), (void *)link1); + EXPECT_EQ(BLI_listbase_string_or_index_find(&lb, link1_name, name_offset, 0), (void *)link1); + EXPECT_EQ(BLI_listbase_string_or_index_find(&lb, link2_name, name_offset, 0), (void *)link2); + EXPECT_EQ(BLI_listbase_string_or_index_find(&lb, nullptr, name_offset, 0), (void *)link1); + EXPECT_EQ(BLI_listbase_string_or_index_find(&lb, nullptr, name_offset, 1), (void *)link2); + EXPECT_EQ(BLI_listbase_string_or_index_find(&lb, nullptr, name_offset, -1), (void *)nullptr); + + BLI_freelistN(&lb); +} + /* -------------------------------------------------------------------- */ /* Sort utilities & test */ |