diff options
author | Jacques Lucke <mail@jlucke.com> | 2019-09-13 11:06:02 +0300 |
---|---|---|
committer | Jacques Lucke <mail@jlucke.com> | 2019-09-13 11:06:02 +0300 |
commit | 1c44d08a69eb3e66c7f942d748f549d6b8ca138f (patch) | |
tree | b74784ffdc6f6a66a92d02a985865247101a6093 /tests | |
parent | 8d12c2a83658cb6b1311197c292e5906394c4321 (diff) |
BLI: new C++ hash table data structures
This commit adds some new hashing based data structures to blenlib.
All of them use open addressing with probing currently.
Furthermore, they support small object optimization, but it is not
customizable yet. I'll add support for this when necessary.
The following main data structures are included:
**Set**
A collection of values, where every value must exist at most once.
This is similar to a Python `set`.
**SetVector**
A combination of a Set and a Vector. It supports fast search for
elements and maintains insertion order when there are no deletes.
All elements are stored in a continuous array. So they can be
iterated over using a normal `ArrayRef`.
**Map**
A set of key-value-pairs, where every key must exist at most once.
This is similar to a Python `dict`.
**StringMap**
A special map for the case when the keys are strings. This case is
fairly common and allows for some optimizations. Most importantly,
many unnecessary allocations can be avoided by storing strings in
a single buffer. Furthermore, the interface of this class uses
`StringRef` to avoid unnecessary conversions.
This commit is a continuation of rB369d5e8ad2bb7.
Diffstat (limited to 'tests')
-rw-r--r-- | tests/gtests/blenlib/BLI_map_test.cc | 243 | ||||
-rw-r--r-- | tests/gtests/blenlib/BLI_math_base_test.cc | 30 | ||||
-rw-r--r-- | tests/gtests/blenlib/BLI_set_test.cc | 189 | ||||
-rw-r--r-- | tests/gtests/blenlib/BLI_set_vector_test.cc | 102 | ||||
-rw-r--r-- | tests/gtests/blenlib/BLI_string_map_test.cc | 201 | ||||
-rw-r--r-- | tests/gtests/blenlib/CMakeLists.txt | 4 |
6 files changed, 769 insertions, 0 deletions
diff --git a/tests/gtests/blenlib/BLI_map_test.cc b/tests/gtests/blenlib/BLI_map_test.cc new file mode 100644 index 00000000000..a7711fb3985 --- /dev/null +++ b/tests/gtests/blenlib/BLI_map_test.cc @@ -0,0 +1,243 @@ +#include "testing/testing.h" +#include "BLI_map.h" +#include "BLI_set.h" + +using IntFloatMap = BLI::Map<int, float>; + +TEST(map, DefaultConstructor) +{ + IntFloatMap map; + EXPECT_EQ(map.size(), 0); +} + +TEST(map, AddIncreasesSize) +{ + IntFloatMap map; + EXPECT_EQ(map.size(), 0); + map.add(2, 5.0f); + EXPECT_EQ(map.size(), 1); + map.add(6, 2.0f); + EXPECT_EQ(map.size(), 2); +} + +TEST(map, Contains) +{ + IntFloatMap map; + EXPECT_FALSE(map.contains(4)); + map.add(5, 6.0f); + EXPECT_FALSE(map.contains(4)); + map.add(4, 2.0f); + EXPECT_TRUE(map.contains(4)); +} + +TEST(map, LookupExisting) +{ + IntFloatMap map; + map.add(2, 6.0f); + map.add(4, 1.0f); + EXPECT_EQ(map.lookup(2), 6.0f); + EXPECT_EQ(map.lookup(4), 1.0f); +} + +TEST(map, LookupNotExisting) +{ + IntFloatMap map; + map.add(2, 4.0f); + map.add(1, 1.0f); + EXPECT_EQ(map.lookup_ptr(0), nullptr); + EXPECT_EQ(map.lookup_ptr(5), nullptr); +} + +TEST(map, AddMany) +{ + IntFloatMap map; + for (int i = 0; i < 100; i++) { + map.add(i, i); + } +} + +TEST(map, PopItem) +{ + IntFloatMap map; + map.add(2, 3.0f); + map.add(1, 9.0f); + EXPECT_TRUE(map.contains(2)); + EXPECT_TRUE(map.contains(1)); + + EXPECT_EQ(map.pop(1), 9.0f); + EXPECT_TRUE(map.contains(2)); + EXPECT_FALSE(map.contains(1)); + + EXPECT_EQ(map.pop(2), 3.0f); + EXPECT_FALSE(map.contains(2)); + EXPECT_FALSE(map.contains(1)); +} + +TEST(map, PopItemMany) +{ + IntFloatMap map; + for (uint i = 0; i < 100; i++) { + map.add_new(i, i); + } + for (uint i = 25; i < 80; i++) { + EXPECT_EQ(map.pop(i), i); + } + for (uint i = 0; i < 100; i++) { + EXPECT_EQ(map.contains(i), i < 25 || i >= 80); + } +} + +TEST(map, ValueIterator) +{ + IntFloatMap map; + map.add(3, 5.0f); + map.add(1, 2.0f); + map.add(7, -2.0f); + + BLI::Set<float> values; + + uint iterations = 0; + for (float value : map.values()) { + values.add(value); + iterations++; + } + + EXPECT_EQ(iterations, 3); + EXPECT_TRUE(values.contains(5.0f)); + EXPECT_TRUE(values.contains(-2.0f)); + EXPECT_TRUE(values.contains(2.0f)); +} + +TEST(map, KeyIterator) +{ + IntFloatMap map; + map.add(6, 3.0f); + map.add(2, 4.0f); + map.add(1, 3.0f); + + BLI::Set<int> keys; + + uint iterations = 0; + for (int key : map.keys()) { + keys.add(key); + iterations++; + } + + EXPECT_EQ(iterations, 3); + EXPECT_TRUE(keys.contains(1)); + EXPECT_TRUE(keys.contains(2)); + EXPECT_TRUE(keys.contains(6)); +} + +TEST(map, ItemIterator) +{ + IntFloatMap map; + map.add(5, 3.0f); + map.add(2, 9.0f); + map.add(1, 0.0f); + + BLI::Set<int> keys; + BLI::Set<float> values; + + uint iterations = 0; + for (auto item : map.items()) { + keys.add(item.key); + values.add(item.value); + iterations++; + } + + EXPECT_EQ(iterations, 3); + EXPECT_TRUE(keys.contains(5)); + EXPECT_TRUE(keys.contains(2)); + EXPECT_TRUE(keys.contains(1)); + EXPECT_TRUE(values.contains(3.0f)); + EXPECT_TRUE(values.contains(9.0f)); + EXPECT_TRUE(values.contains(0.0f)); +} + +float return_42() +{ + return 42.0f; +} + +TEST(map, LookupOrAdd_SeparateFunction) +{ + IntFloatMap map; + EXPECT_EQ(map.lookup_or_add(0, return_42), 42.0f); + EXPECT_EQ(map.lookup(0), 42); +} + +TEST(map, LookupOrAdd_Lambdas) +{ + IntFloatMap map; + auto lambda1 = []() { return 11.0f; }; + EXPECT_EQ(map.lookup_or_add(0, lambda1), 11.0f); + auto lambda2 = []() { return 20.0f; }; + EXPECT_EQ(map.lookup_or_add(1, lambda2), 20.0f); + + EXPECT_EQ(map.lookup_or_add(0, lambda2), 11.0f); + EXPECT_EQ(map.lookup_or_add(1, lambda1), 20.0f); +} + +TEST(map, InsertOrModify) +{ + IntFloatMap map; + auto create_func = []() { return 10.0f; }; + auto modify_func = [](float &value) { value += 5; }; + EXPECT_TRUE(map.add_or_modify(1, create_func, modify_func)); + EXPECT_EQ(map.lookup(1), 10.0f); + EXPECT_FALSE(map.add_or_modify(1, create_func, modify_func)); + EXPECT_EQ(map.lookup(1), 15.0f); +} + +TEST(map, AddOverride) +{ + IntFloatMap map; + EXPECT_FALSE(map.contains(3)); + EXPECT_TRUE(map.add_override(3, 6.0f)); + EXPECT_EQ(map.lookup(3), 6.0f); + EXPECT_FALSE(map.add_override(3, 7.0f)); + EXPECT_EQ(map.lookup(3), 7.0f); + EXPECT_FALSE(map.add(3, 8.0f)); + EXPECT_EQ(map.lookup(3), 7.0f); +} + +TEST(map, MoveConstructorSmall) +{ + IntFloatMap map1; + map1.add(1, 2.0f); + map1.add(4, 1.0f); + IntFloatMap map2(std::move(map1)); + EXPECT_EQ(map2.size(), 2); + EXPECT_EQ(map2.lookup(1), 2.0f); + EXPECT_EQ(map2.lookup(4), 1.0f); + EXPECT_EQ(map1.size(), 0); + EXPECT_EQ(map1.lookup_ptr(4), nullptr); +} + +TEST(map, MoveConstructorLarge) +{ + IntFloatMap map1; + for (uint i = 0; i < 100; i++) { + map1.add_new(i, i); + } + IntFloatMap map2(std::move(map1)); + EXPECT_EQ(map2.size(), 100); + EXPECT_EQ(map2.lookup(1), 1.0f); + EXPECT_EQ(map2.lookup(4), 4.0f); + EXPECT_EQ(map1.size(), 0); + EXPECT_EQ(map1.lookup_ptr(4), nullptr); +} + +TEST(map, MoveAssignment) +{ + IntFloatMap map1; + map1.add(1, 2.0f); + map1.add(4, 1.0f); + IntFloatMap map2 = std::move(map1); + EXPECT_EQ(map2.size(), 2); + EXPECT_EQ(map2.lookup(1), 2.0f); + EXPECT_EQ(map2.lookup(4), 1.0f); + EXPECT_EQ(map1.size(), 0); + EXPECT_EQ(map1.lookup_ptr(4), nullptr); +} diff --git a/tests/gtests/blenlib/BLI_math_base_test.cc b/tests/gtests/blenlib/BLI_math_base_test.cc index d62d0ba274d..dc20c75576d 100644 --- a/tests/gtests/blenlib/BLI_math_base_test.cc +++ b/tests/gtests/blenlib/BLI_math_base_test.cc @@ -83,3 +83,33 @@ TEST(math_base, CompareFFRelativeZero) EXPECT_FALSE(compare_ff_relative(fn0, f1, -1.0f, 1024)); EXPECT_FALSE(compare_ff_relative(f1, fn0, -1.0f, 1024)); } + +TEST(math_base, Log2FloorU) +{ + EXPECT_EQ(log2_floor_u(0), 0); + EXPECT_EQ(log2_floor_u(1), 0); + EXPECT_EQ(log2_floor_u(2), 1); + EXPECT_EQ(log2_floor_u(3), 1); + EXPECT_EQ(log2_floor_u(4), 2); + EXPECT_EQ(log2_floor_u(5), 2); + EXPECT_EQ(log2_floor_u(6), 2); + EXPECT_EQ(log2_floor_u(7), 2); + EXPECT_EQ(log2_floor_u(8), 3); + EXPECT_EQ(log2_floor_u(9), 3); + EXPECT_EQ(log2_floor_u(123456), 16); +} + +TEST(math_base, Log2CeilU) +{ + EXPECT_EQ(log2_ceil_u(0), 0); + EXPECT_EQ(log2_ceil_u(1), 0); + EXPECT_EQ(log2_ceil_u(2), 1); + EXPECT_EQ(log2_ceil_u(3), 2); + EXPECT_EQ(log2_ceil_u(4), 2); + EXPECT_EQ(log2_ceil_u(5), 3); + EXPECT_EQ(log2_ceil_u(6), 3); + EXPECT_EQ(log2_ceil_u(7), 3); + EXPECT_EQ(log2_ceil_u(8), 3); + EXPECT_EQ(log2_ceil_u(9), 4); + EXPECT_EQ(log2_ceil_u(123456), 17); +} diff --git a/tests/gtests/blenlib/BLI_set_test.cc b/tests/gtests/blenlib/BLI_set_test.cc new file mode 100644 index 00000000000..f331639b345 --- /dev/null +++ b/tests/gtests/blenlib/BLI_set_test.cc @@ -0,0 +1,189 @@ +#include "testing/testing.h" +#include "BLI_set.h" + +using IntSet = BLI::Set<int>; + +TEST(set, Defaultconstructor) +{ + IntSet set; + EXPECT_EQ(set.size(), 0); +} + +TEST(set, ContainsNotExistant) +{ + IntSet set; + EXPECT_FALSE(set.contains(3)); +} + +TEST(set, ContainsExistant) +{ + IntSet set; + EXPECT_FALSE(set.contains(5)); + set.add(5); + EXPECT_TRUE(set.contains(5)); +} + +TEST(set, AddMany) +{ + IntSet set; + for (int i = 0; i < 100; i++) { + set.add(i); + } + + for (int i = 50; i < 100; i++) { + EXPECT_TRUE(set.contains(i)); + } + for (int i = 100; i < 150; i++) { + EXPECT_FALSE(set.contains(i)); + } +} + +TEST(set, InitializerListConstructor) +{ + IntSet set = {4, 5, 6}; + EXPECT_EQ(set.size(), 3); + EXPECT_TRUE(set.contains(4)); + EXPECT_TRUE(set.contains(5)); + EXPECT_TRUE(set.contains(6)); + EXPECT_FALSE(set.contains(2)); + EXPECT_FALSE(set.contains(3)); +} + +TEST(set, CopyConstructor) +{ + IntSet set = {3}; + EXPECT_TRUE(set.contains(3)); + EXPECT_FALSE(set.contains(4)); + + IntSet set2 = set; + set2.add(4); + EXPECT_TRUE(set2.contains(3)); + EXPECT_TRUE(set2.contains(4)); + + EXPECT_FALSE(set.contains(4)); +} + +TEST(set, MoveConstructor) +{ + IntSet set = {1, 2, 3}; + EXPECT_EQ(set.size(), 3); + IntSet set2 = std::move(set); + EXPECT_EQ(set.size(), 0); + EXPECT_EQ(set2.size(), 3); +} + +TEST(set, Remove) +{ + IntSet set = {3, 4, 5}; + EXPECT_TRUE(set.contains(3)); + EXPECT_TRUE(set.contains(4)); + EXPECT_TRUE(set.contains(5)); + set.remove(4); + EXPECT_TRUE(set.contains(3)); + EXPECT_FALSE(set.contains(4)); + EXPECT_TRUE(set.contains(5)); + set.remove(3); + EXPECT_FALSE(set.contains(3)); + EXPECT_FALSE(set.contains(4)); + EXPECT_TRUE(set.contains(5)); + set.remove(5); + EXPECT_FALSE(set.contains(3)); + EXPECT_FALSE(set.contains(4)); + EXPECT_FALSE(set.contains(5)); +} + +TEST(set, RemoveMany) +{ + IntSet set; + for (uint i = 0; i < 1000; i++) { + set.add(i); + } + for (uint i = 100; i < 1000; i++) { + set.remove(i); + } + for (uint i = 900; i < 1000; i++) { + set.add(i); + } + + for (uint i = 0; i < 1000; i++) { + if (i < 100 || i >= 900) { + EXPECT_TRUE(set.contains(i)); + } + else { + EXPECT_FALSE(set.contains(i)); + } + } +} + +TEST(set, Intersects) +{ + IntSet a = {3, 4, 5, 6}; + IntSet b = {1, 2, 5}; + EXPECT_TRUE(IntSet::Intersects(a, b)); + EXPECT_FALSE(IntSet::Disjoint(a, b)); +} + +TEST(set, Disjoint) +{ + IntSet a = {5, 6, 7, 8}; + IntSet b = {2, 3, 4, 9}; + EXPECT_FALSE(IntSet::Intersects(a, b)); + EXPECT_TRUE(IntSet::Disjoint(a, b)); +} + +TEST(set, AddMultiple) +{ + IntSet a; + a.add_multiple({5, 7}); + EXPECT_TRUE(a.contains(5)); + EXPECT_TRUE(a.contains(7)); + EXPECT_FALSE(a.contains(4)); + a.add_multiple({2, 4, 7}); + EXPECT_TRUE(a.contains(4)); + EXPECT_TRUE(a.contains(2)); + EXPECT_EQ(a.size(), 4); +} + +TEST(set, AddMultipleNew) +{ + IntSet a; + a.add_multiple_new({5, 6}); + EXPECT_TRUE(a.contains(5)); + EXPECT_TRUE(a.contains(6)); +} + +TEST(set, ToSmallVector) +{ + IntSet a = {5, 2, 8}; + BLI::Vector<int> vec = a.to_small_vector(); + EXPECT_EQ(vec.size(), 3); + EXPECT_TRUE(vec.contains(5)); + EXPECT_TRUE(vec.contains(2)); + EXPECT_TRUE(vec.contains(8)); +} + +TEST(set, Iterator) +{ + IntSet set = {1, 3, 2, 5, 4}; + BLI::Vector<int> vec; + for (int value : set) { + vec.append(value); + } + EXPECT_EQ(vec.size(), 5); + EXPECT_TRUE(vec.contains(1)); + EXPECT_TRUE(vec.contains(3)); + EXPECT_TRUE(vec.contains(2)); + EXPECT_TRUE(vec.contains(5)); + EXPECT_TRUE(vec.contains(4)); +} + +TEST(set, OftenAddRemove) +{ + IntSet set; + for (int i = 0; i < 100; i++) { + set.add(42); + EXPECT_EQ(set.size(), 1); + set.remove(42); + EXPECT_EQ(set.size(), 0); + } +} diff --git a/tests/gtests/blenlib/BLI_set_vector_test.cc b/tests/gtests/blenlib/BLI_set_vector_test.cc new file mode 100644 index 00000000000..be6f9a80d7c --- /dev/null +++ b/tests/gtests/blenlib/BLI_set_vector_test.cc @@ -0,0 +1,102 @@ +#include "testing/testing.h" +#include "BLI_set_vector.h" + +using IntSetVector = BLI::SetVector<int>; + +TEST(set_vector, DefaultConstructor) +{ + IntSetVector set; + EXPECT_EQ(set.size(), 0); +} + +TEST(set_vector, InitializerListConstructor_WithoutDuplicates) +{ + IntSetVector set = {1, 4, 5}; + EXPECT_EQ(set.size(), 3); + EXPECT_EQ(set[0], 1); + EXPECT_EQ(set[1], 4); + EXPECT_EQ(set[2], 5); +} + +TEST(set_vector, InitializerListConstructor_WithDuplicates) +{ + IntSetVector set = {1, 3, 3, 2, 1, 5}; + EXPECT_EQ(set.size(), 4); + EXPECT_EQ(set[0], 1); + EXPECT_EQ(set[1], 3); + EXPECT_EQ(set[2], 2); + EXPECT_EQ(set[3], 5); +} + +TEST(set_vector, Copy) +{ + IntSetVector set1 = {1, 2, 3}; + IntSetVector set2 = set1; + EXPECT_EQ(set1.size(), 3); + EXPECT_EQ(set2.size(), 3); + EXPECT_EQ(set1.index(2), 1); + EXPECT_EQ(set2.index(2), 1); +} + +TEST(set_vector, Move) +{ + IntSetVector set1 = {1, 2, 3}; + IntSetVector set2 = std::move(set1); + EXPECT_EQ(set1.size(), 0); + EXPECT_EQ(set2.size(), 3); +} + +TEST(set_vector, AddNewIncreasesSize) +{ + IntSetVector set; + EXPECT_EQ(set.size(), 0); + set.add(5); + EXPECT_EQ(set.size(), 1); +} + +TEST(set_vector, AddExistingDoesNotIncreaseSize) +{ + IntSetVector set; + EXPECT_EQ(set.size(), 0); + set.add(5); + EXPECT_EQ(set.size(), 1); + set.add(5); + EXPECT_EQ(set.size(), 1); +} + +TEST(set_vector, Index) +{ + IntSetVector set = {3, 6, 4}; + EXPECT_EQ(set.index(6), 1); + EXPECT_EQ(set.index(3), 0); + EXPECT_EQ(set.index(4), 2); +} + +TEST(set_vector, IndexTry) +{ + IntSetVector set = {3, 6, 4}; + EXPECT_EQ(set.index_try(5), -1); + EXPECT_EQ(set.index_try(3), 0); + EXPECT_EQ(set.index_try(6), 1); + EXPECT_EQ(set.index_try(2), -1); +} + +TEST(set_vector, Remove) +{ + IntSetVector set = {4, 5, 6, 7}; + EXPECT_EQ(set.size(), 4); + set.remove(5); + EXPECT_EQ(set.size(), 3); + EXPECT_EQ(set[0], 4); + EXPECT_EQ(set[1], 7); + EXPECT_EQ(set[2], 6); + set.remove(6); + EXPECT_EQ(set.size(), 2); + EXPECT_EQ(set[0], 4); + EXPECT_EQ(set[1], 7); + set.remove(4); + EXPECT_EQ(set.size(), 1); + EXPECT_EQ(set[0], 7); + set.remove(7); + EXPECT_EQ(set.size(), 0); +} diff --git a/tests/gtests/blenlib/BLI_string_map_test.cc b/tests/gtests/blenlib/BLI_string_map_test.cc new file mode 100644 index 00000000000..e5e32352161 --- /dev/null +++ b/tests/gtests/blenlib/BLI_string_map_test.cc @@ -0,0 +1,201 @@ +#include "testing/testing.h" +#include "BLI_string_map.h" +#include "BLI_vector.h" + +using namespace BLI; + +TEST(string_map, DefaultConstructor) +{ + StringMap<int> map; + EXPECT_EQ(map.size(), 0); +} + +TEST(string_map, CopyConstructor) +{ + StringMap<Vector<int, 4>> map1; + map1.add_new("A", {1, 2, 3}); + map1.add_new("B", {1, 2, 3, 4, 5, 6}); + + StringMap<Vector<int>> map2(map1); + + EXPECT_EQ(map1.size(), 2); + EXPECT_EQ(map2.size(), 2); + EXPECT_EQ(map1.lookup("A")[1], 2); + EXPECT_EQ(map2.lookup("A")[1], 2); + EXPECT_EQ(map1.lookup("B")[5], 6); + EXPECT_EQ(map2.lookup("B")[5], 6); +} + +TEST(string_map, MoveConstructor) +{ + StringMap<Vector<int, 4>> map1; + map1.add_new("A", {1, 2, 3}); + map1.add_new("B", {1, 2, 3, 4, 5, 6}); + + StringMap<Vector<int>> map2(std::move(map1)); + + EXPECT_EQ(map1.size(), 0); + EXPECT_FALSE(map1.contains("A")); + EXPECT_FALSE(map1.contains("B")); + + EXPECT_EQ(map2.size(), 2); + EXPECT_EQ(map2.lookup("A")[1], 2); + EXPECT_EQ(map2.lookup("B")[5], 6); +} + +TEST(string_map, AddNew) +{ + StringMap<int> map; + EXPECT_EQ(map.size(), 0); + + map.add_new("Why", 5); + EXPECT_EQ(map.size(), 1); + EXPECT_EQ(map.lookup("Why"), 5); + + map.add_new("Where", 6); + EXPECT_EQ(map.size(), 2); + EXPECT_EQ(map.lookup("Where"), 6); +} + +TEST(string_map, AddNew_Many) +{ + StringMap<int> map; + + for (uint i = 0; i < 100; i++) { + map.add_new(std::to_string(i), i); + } + EXPECT_EQ(map.size(), 100); +} + +TEST(string_map, Contains) +{ + StringMap<int> map; + map.add_new("A", 0); + map.add_new("B", 0); + EXPECT_TRUE(map.contains("A")); + EXPECT_TRUE(map.contains("B")); + EXPECT_FALSE(map.contains("C")); +} + +TEST(string_map, Contains_Many) +{ + StringMap<int> map; + for (uint i = 0; i < 50; i++) { + map.add_new(std::to_string(i), i); + } + for (uint i = 100; i < 200; i++) { + map.add_new(std::to_string(i), i); + } + EXPECT_EQ(map.size(), 150); + for (uint i = 0; i < 200; i++) { + if (i < 50 || i >= 100) { + EXPECT_TRUE(map.contains(std::to_string(i))); + } + else { + EXPECT_FALSE(map.contains(std::to_string(i))); + } + } +} + +TEST(string_map, Lookup) +{ + StringMap<int> map; + map.add_new("A", 5); + map.add_new("B", 8); + map.add_new("C", 10); + EXPECT_EQ(map.lookup("A"), 5); + EXPECT_EQ(map.lookup("B"), 8); + EXPECT_EQ(map.lookup("C"), 10); +} + +TEST(string_map, LookupPtr) +{ + StringMap<int> map; + map.add_new("test1", 13); + map.add_new("test2", 14); + map.add_new("test3", 15); + EXPECT_EQ(*map.lookup_ptr("test1"), 13); + EXPECT_EQ(*map.lookup_ptr("test2"), 14); + EXPECT_EQ(*map.lookup_ptr("test3"), 15); + EXPECT_EQ(map.lookup_ptr("test4"), nullptr); +} + +TEST(string_map, LookupDefault) +{ + StringMap<int> map; + EXPECT_EQ(map.lookup_default("test", 42), 42); + map.add_new("test", 5); + EXPECT_EQ(map.lookup_default("test", 42), 5); +} + +TEST(string_map, FindKeyForValue) +{ + StringMap<int> map; + map.add_new("A", 1); + map.add_new("B", 2); + map.add_new("C", 3); + EXPECT_EQ(map.find_key_for_value(1), "A"); + EXPECT_EQ(map.find_key_for_value(2), "B"); + EXPECT_EQ(map.find_key_for_value(3), "C"); +} + +TEST(string_map, ForeachValue) +{ + StringMap<int> map; + map.add_new("A", 4); + map.add_new("B", 5); + map.add_new("C", 1); + + Vector<int> values; + map.foreach_value([&values](int &value) { values.append(value); }); + EXPECT_EQ(values.size(), 3); + EXPECT_TRUE(values.contains(1)); + EXPECT_TRUE(values.contains(4)); + EXPECT_TRUE(values.contains(5)); +} + +TEST(string_map, ForeachKey) +{ + StringMap<int> map; + map.add_new("A", 4); + map.add_new("B", 5); + map.add_new("C", 1); + + Vector<std::string> keys; + map.foreach_key([&keys](StringRefNull key) { keys.append(key); }); + EXPECT_EQ(keys.size(), 3); + EXPECT_TRUE(keys.contains("A")); + EXPECT_TRUE(keys.contains("B")); + EXPECT_TRUE(keys.contains("C")); +} + +TEST(string_map, ForeachKeyValuePair) +{ + StringMap<int> map; + map.add_new("A", 4); + map.add_new("B", 5); + map.add_new("C", 1); + + Vector<std::string> keys; + Vector<int> values; + + map.foreach_key_value_pair([&keys, &values](StringRefNull key, int value) { + keys.append(key); + values.append(value); + }); + + EXPECT_EQ(keys.size(), 3); + EXPECT_EQ(values[keys.index("A")], 4); + EXPECT_EQ(values[keys.index("B")], 5); + EXPECT_EQ(values[keys.index("C")], 1); +} + +TEST(string_map, WithVectors) +{ + StringMap<Vector<int>> map; + map.add_new("A", {1, 2, 3}); + map.add_new("B", {1, 2, 3, 4, 5, 6, 7}); + EXPECT_EQ(map.size(), 2); + EXPECT_EQ(map.lookup("A").size(), 3); + EXPECT_EQ(map.lookup("B").size(), 7); +} diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt index d4281aa77b4..1d4b0b18973 100644 --- a/tests/gtests/blenlib/CMakeLists.txt +++ b/tests/gtests/blenlib/CMakeLists.txt @@ -53,15 +53,19 @@ BLENDER_TEST(BLI_index_range "bf_blenlib") BLENDER_TEST(BLI_kdopbvh "bf_blenlib;bf_intern_numaapi") BLENDER_TEST(BLI_linklist_lockfree "bf_blenlib;bf_intern_numaapi") BLENDER_TEST(BLI_listbase "bf_blenlib") +BLENDER_TEST(BLI_map "bf_blenlib") BLENDER_TEST(BLI_math_base "bf_blenlib") BLENDER_TEST(BLI_math_color "bf_blenlib") BLENDER_TEST(BLI_math_geom "bf_blenlib") BLENDER_TEST(BLI_memiter "bf_blenlib") BLENDER_TEST(BLI_path_util "${BLI_path_util_extra_libs}") BLENDER_TEST(BLI_polyfill_2d "bf_blenlib") +BLENDER_TEST(BLI_set "bf_blenlib") +BLENDER_TEST(BLI_set_vector "bf_blenlib") BLENDER_TEST(BLI_stack "bf_blenlib") BLENDER_TEST(BLI_stack_cxx "bf_blenlib") BLENDER_TEST(BLI_string "bf_blenlib") +BLENDER_TEST(BLI_string_map "bf_blenlib") BLENDER_TEST(BLI_string_ref "bf_blenlib") BLENDER_TEST(BLI_string_utf8 "bf_blenlib") BLENDER_TEST(BLI_task "bf_blenlib;bf_intern_numaapi") |