diff options
author | Jacques Lucke <jacques@blender.org> | 2021-05-13 14:39:23 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2021-05-13 14:39:23 +0300 |
commit | d288eeb79abc08d09f535bfd5d64b623d26bd129 (patch) | |
tree | d0e187cdd62fb8d105405b386cf1bf962b4d27eb /source/blender/blenlib/tests/BLI_map_test.cc | |
parent | 522868001c5bc1f6f4a8dc214202896224f2fc70 (diff) |
BLI: support looking up a key stored in Map or VectorSet
Sometimes it is useful to find the key that compares equal
to a known key. Typically that happens when the key itself
has additional data attached that is not part of its hash.
Note that the returned key reference/pointer is const, because
the caller must not change the key in a way that changes its
hash or how it compares to other keys.
Diffstat (limited to 'source/blender/blenlib/tests/BLI_map_test.cc')
-rw-r--r-- | source/blender/blenlib/tests/BLI_map_test.cc | 13 |
1 files changed, 13 insertions, 0 deletions
diff --git a/source/blender/blenlib/tests/BLI_map_test.cc b/source/blender/blenlib/tests/BLI_map_test.cc index 18be456bd20..679a10e9ce0 100644 --- a/source/blender/blenlib/tests/BLI_map_test.cc +++ b/source/blender/blenlib/tests/BLI_map_test.cc @@ -640,6 +640,19 @@ TEST(map, RemoveDuringIteration) EXPECT_EQ(map.lookup(3), 3); } +TEST(map, LookupKey) +{ + Map<std::string, int> map; + map.add("a", 0); + map.add("b", 1); + map.add("c", 2); + EXPECT_EQ(map.lookup_key("a"), "a"); + EXPECT_EQ(map.lookup_key_as("c"), "c"); + EXPECT_EQ(map.lookup_key_ptr_as("d"), nullptr); + EXPECT_EQ(map.lookup_key_ptr_as("b")->size(), 1); + EXPECT_EQ(map.lookup_key_ptr("a"), map.lookup_key_ptr_as("a")); +} + /** * Set this to 1 to activate the benchmark. It is disabled by default, because it prints a lot. */ |