From 79e1165bd7488850e896112c2b0f8bf1e6b25db9 Mon Sep 17 00:00:00 2001 From: Jacques Lucke Date: Sat, 14 Sep 2019 12:11:14 +0200 Subject: BLI: Improve forwarding semantics of some data structures This makes it possible to use e.g. `std::unique_ptr` in a map. --- tests/gtests/blenlib/BLI_map_test.cc | 24 +++++++++++++++++++++++- tests/gtests/blenlib/BLI_set_test.cc | 16 +++++++++++++++- tests/gtests/blenlib/BLI_set_vector_test.cc | 13 ++++++++++++- tests/gtests/blenlib/BLI_stack_cxx_test.cc | 13 ++++++++++++- tests/gtests/blenlib/BLI_string_map_test.cc | 9 +++++++++ tests/gtests/blenlib/BLI_vector_test.cc | 14 ++++++++++++++ 6 files changed, 85 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/gtests/blenlib/BLI_map_test.cc b/tests/gtests/blenlib/BLI_map_test.cc index 8d5b178aea6..3acb76e09f0 100644 --- a/tests/gtests/blenlib/BLI_map_test.cc +++ b/tests/gtests/blenlib/BLI_map_test.cc @@ -2,7 +2,8 @@ #include "BLI_map.h" #include "BLI_set.h" -using IntFloatMap = BLI::Map; +using BLI::Map; +using IntFloatMap = Map; TEST(map, DefaultConstructor) { @@ -258,3 +259,24 @@ TEST(map, Clear) EXPECT_FALSE(map.contains(1)); EXPECT_FALSE(map.contains(2)); } + +TEST(map, UniquePtrValue) +{ + auto value1 = std::unique_ptr(new int()); + auto value2 = std::unique_ptr(new int()); + auto value3 = std::unique_ptr(new int()); + + int *value1_ptr = value1.get(); + + Map> map; + map.add_new(1, std::move(value1)); + map.add(2, std::move(value2)); + map.add_override(3, std::move(value3)); + map.lookup_or_add(4, []() { return std::unique_ptr(new int()); }); + map.add_new(5, std::unique_ptr(new int())); + map.add(6, std::unique_ptr(new int())); + map.add_override(7, std::unique_ptr(new int())); + + EXPECT_EQ(map.lookup(1).get(), value1_ptr); + EXPECT_EQ(map.lookup_ptr(100), nullptr); +} diff --git a/tests/gtests/blenlib/BLI_set_test.cc b/tests/gtests/blenlib/BLI_set_test.cc index f331639b345..5baf069557e 100644 --- a/tests/gtests/blenlib/BLI_set_test.cc +++ b/tests/gtests/blenlib/BLI_set_test.cc @@ -1,7 +1,10 @@ #include "testing/testing.h" #include "BLI_set.h" +#include "BLI_vector.h" -using IntSet = BLI::Set; +using BLI::Set; +using BLI::Vector; +using IntSet = Set; TEST(set, Defaultconstructor) { @@ -187,3 +190,14 @@ TEST(set, OftenAddRemove) EXPECT_EQ(set.size(), 0); } } + +TEST(set, UniquePtrValues) +{ + Set> set; + set.add_new(std::unique_ptr(new int())); + auto value1 = std::unique_ptr(new int()); + set.add_new(std::move(value1)); + set.add(std::unique_ptr(new int())); + + EXPECT_EQ(set.size(), 3); +} diff --git a/tests/gtests/blenlib/BLI_set_vector_test.cc b/tests/gtests/blenlib/BLI_set_vector_test.cc index be6f9a80d7c..b135e31914c 100644 --- a/tests/gtests/blenlib/BLI_set_vector_test.cc +++ b/tests/gtests/blenlib/BLI_set_vector_test.cc @@ -1,7 +1,8 @@ #include "testing/testing.h" #include "BLI_set_vector.h" -using IntSetVector = BLI::SetVector; +using BLI::SetVector; +using IntSetVector = SetVector; TEST(set_vector, DefaultConstructor) { @@ -100,3 +101,13 @@ TEST(set_vector, Remove) set.remove(7); EXPECT_EQ(set.size(), 0); } + +TEST(set_vector, UniquePtrValue) +{ + SetVector> set; + set.add_new(std::unique_ptr(new int())); + set.add(std::unique_ptr(new int())); + set.index_try(std::unique_ptr(new int())); + std::unique_ptr value = set.pop(); + UNUSED_VARS(value); +} diff --git a/tests/gtests/blenlib/BLI_stack_cxx_test.cc b/tests/gtests/blenlib/BLI_stack_cxx_test.cc index 02c5407fda3..436f1f307b9 100644 --- a/tests/gtests/blenlib/BLI_stack_cxx_test.cc +++ b/tests/gtests/blenlib/BLI_stack_cxx_test.cc @@ -1,7 +1,8 @@ #include "testing/testing.h" #include "BLI_stack_cxx.h" -using IntStack = BLI::Stack; +using BLI::Stack; +using IntStack = Stack; TEST(stack, DefaultConstructor) { @@ -50,3 +51,13 @@ TEST(stack, Peek) stack.pop(); EXPECT_EQ(stack.peek(), 3); } + +TEST(stack, UniquePtrValues) +{ + Stack> stack; + stack.push(std::unique_ptr(new int())); + stack.push(std::unique_ptr(new int())); + std::unique_ptr a = stack.pop(); + std::unique_ptr &b = stack.peek(); + UNUSED_VARS(a, b); +} diff --git a/tests/gtests/blenlib/BLI_string_map_test.cc b/tests/gtests/blenlib/BLI_string_map_test.cc index e5e32352161..cc02a54e0c8 100644 --- a/tests/gtests/blenlib/BLI_string_map_test.cc +++ b/tests/gtests/blenlib/BLI_string_map_test.cc @@ -199,3 +199,12 @@ TEST(string_map, WithVectors) EXPECT_EQ(map.lookup("A").size(), 3); EXPECT_EQ(map.lookup("B").size(), 7); } + +TEST(string_map, UniquePtrValues) +{ + StringMap> map; + map.add_new("A", std::unique_ptr(new int())); + std::unique_ptr &a = map.lookup("A"); + std::unique_ptr *b = map.lookup_ptr("A"); + EXPECT_EQ(a.get(), b->get()); +} diff --git a/tests/gtests/blenlib/BLI_vector_test.cc b/tests/gtests/blenlib/BLI_vector_test.cc index 60f78025269..9486c9c0ef2 100644 --- a/tests/gtests/blenlib/BLI_vector_test.cc +++ b/tests/gtests/blenlib/BLI_vector_test.cc @@ -398,3 +398,17 @@ TEST(vector, AppendNTimes) EXPECT_EQ(a[3], 2); EXPECT_EQ(a[4], 2); } + +TEST(vector, UniquePtrValue) +{ + Vector> vec; + vec.append(std::unique_ptr(new int())); + vec.append(std::unique_ptr(new int())); + vec.append(std::unique_ptr(new int())); + + std::unique_ptr &a = vec.last(); + std::unique_ptr b = vec.pop_last(); + vec.remove_and_reorder(0); + + UNUSED_VARS(a, b); +} -- cgit v1.2.3