diff options
author | Jacques Lucke <jacques@blender.org> | 2021-03-20 17:42:35 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2021-03-20 17:42:35 +0300 |
commit | 98721c85431d223c895a25d63dafb9e6637d34c4 (patch) | |
tree | 58a357fcef4d43f8d1e1e93835c9c2339eb66d18 /source/blender/blenlib/tests | |
parent | 59d3ec1eefd30a3f041a9b156b3e01607c2bcd71 (diff) |
BLI: improve support for generic algorithms with c++ containers
Some generic algorithms from the standard library like `std::any_of`
did not work with all container and iterator types. To improve the
situation, this patch adds various type members to containers
and iterators.
Custom iterators for Set, Map and IndexRange now have an iterator
category, which soe algorithms require. IndexRange could become
a random access iterator, but adding all the missing methods can
be done when it is necessary.
Diffstat (limited to 'source/blender/blenlib/tests')
-rw-r--r-- | source/blender/blenlib/tests/BLI_index_range_test.cc | 9 | ||||
-rw-r--r-- | source/blender/blenlib/tests/BLI_map_test.cc | 17 | ||||
-rw-r--r-- | source/blender/blenlib/tests/BLI_set_test.cc | 18 |
3 files changed, 44 insertions, 0 deletions
diff --git a/source/blender/blenlib/tests/BLI_index_range_test.cc b/source/blender/blenlib/tests/BLI_index_range_test.cc index ddaa067f50e..858dc2ee966 100644 --- a/source/blender/blenlib/tests/BLI_index_range_test.cc +++ b/source/blender/blenlib/tests/BLI_index_range_test.cc @@ -147,4 +147,13 @@ TEST(index_range, constexpr_) BLI_STATIC_ASSERT(range.size() == 1, ""); EXPECT_EQ(compiles[0], 1); } + +TEST(index_range, GenericAlgorithms) +{ + IndexRange range{4, 10}; + EXPECT_TRUE(std::any_of(range.begin(), range.end(), [](int v) { return v == 6; })); + EXPECT_FALSE(std::any_of(range.begin(), range.end(), [](int v) { return v == 20; })); + EXPECT_EQ(std::count_if(range.begin(), range.end(), [](int v) { return v < 7; }), 3); +} + } // namespace blender::tests diff --git a/source/blender/blenlib/tests/BLI_map_test.cc b/source/blender/blenlib/tests/BLI_map_test.cc index 70c1887a527..f1ae8fb3921 100644 --- a/source/blender/blenlib/tests/BLI_map_test.cc +++ b/source/blender/blenlib/tests/BLI_map_test.cc @@ -587,6 +587,23 @@ TEST(map, EnumKey) EXPECT_EQ(map.lookup(TestEnum::B), 10); } +TEST(map, GenericAlgorithms) +{ + Map<int, int> map; + map.add(5, 2); + map.add(1, 4); + map.add(2, 2); + map.add(7, 1); + map.add(8, 6); + EXPECT_TRUE(std::any_of(map.keys().begin(), map.keys().end(), [](int v) { return v == 1; })); + EXPECT_TRUE(std::any_of(map.values().begin(), map.values().end(), [](int v) { return v == 1; })); + EXPECT_TRUE(std::any_of( + map.items().begin(), map.items().end(), [](auto item) { return item.value == 1; })); + EXPECT_EQ(std::count(map.values().begin(), map.values().end(), 2), 2); + EXPECT_EQ(std::count(map.values().begin(), map.values().end(), 4), 1); + EXPECT_EQ(std::count(map.keys().begin(), map.keys().end(), 7), 1); +} + /** * Set this to 1 to activate the benchmark. It is disabled by default, because it prints a lot. */ diff --git a/source/blender/blenlib/tests/BLI_set_test.cc b/source/blender/blenlib/tests/BLI_set_test.cc index ea4369d6b8f..56f1d57e4ae 100644 --- a/source/blender/blenlib/tests/BLI_set_test.cc +++ b/source/blender/blenlib/tests/BLI_set_test.cc @@ -526,6 +526,24 @@ TEST(set, AddExceptions) EXPECT_EQ(set.size(), 0); } +TEST(set, ForwardIterator) +{ + Set<int> set = {5, 2, 6, 4, 1}; + Set<int>::iterator iter1 = set.begin(); + int value1 = *iter1; + Set<int>::iterator iter2 = iter1++; + EXPECT_EQ(*iter1, value1); + EXPECT_EQ(*iter2, *(++iter1)); +} + +TEST(set, GenericAlgorithms) +{ + Set<int> set = {1, 20, 30, 40}; + EXPECT_FALSE(std::any_of(set.begin(), set.end(), [](int v) { return v == 5; })); + EXPECT_TRUE(std::any_of(set.begin(), set.end(), [](int v) { return v == 30; })); + EXPECT_EQ(std::count(set.begin(), set.end(), 20), 1); +} + /** * Set this to 1 to activate the benchmark. It is disabled by default, because it prints a lot. */ |