diff options
author | Jacques Lucke <jacques@blender.org> | 2020-05-21 15:35:19 +0300 |
---|---|---|
committer | Jacques Lucke <jacques@blender.org> | 2020-05-21 15:35:19 +0300 |
commit | 0c9e73845cc771fae14c0458299c3d218aef5689 (patch) | |
tree | 5ea919b016d3e4b755d302e082feb153a2e6a27e /tests/gtests/blenlib | |
parent | 03d8783db021dfd962e0bc7cbdc1b7a750170a97 (diff) |
BLI: improve Vector test
Diffstat (limited to 'tests/gtests/blenlib')
-rw-r--r-- | tests/gtests/blenlib/BLI_type_construct_mock.hh | 63 | ||||
-rw-r--r-- | tests/gtests/blenlib/BLI_vector_test.cc | 149 |
2 files changed, 170 insertions, 42 deletions
diff --git a/tests/gtests/blenlib/BLI_type_construct_mock.hh b/tests/gtests/blenlib/BLI_type_construct_mock.hh new file mode 100644 index 00000000000..72767631608 --- /dev/null +++ b/tests/gtests/blenlib/BLI_type_construct_mock.hh @@ -0,0 +1,63 @@ +/* + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU General Public License + * as published by the Free Software Foundation; either version 2 + * of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +#ifndef __ANY_TYPE_MOCK_TEST_H__ +#define __ANY_TYPE_MOCK_TEST_H__ + +#include "BLI_sys_types.h" + +class TypeConstructMock { + public: + bool default_constructed = false; + bool copy_constructed = false; + bool move_constructed = false; + bool copy_assigned = false; + bool move_assigned = false; + + TypeConstructMock() : default_constructed(true) + { + } + + TypeConstructMock(const TypeConstructMock &other) : copy_constructed(true) + { + } + + TypeConstructMock(TypeConstructMock &&other) : move_constructed(true) + { + } + + TypeConstructMock &operator=(const TypeConstructMock &other) + { + if (this == &other) { + return *this; + } + + copy_assigned = true; + return *this; + } + + TypeConstructMock &operator=(TypeConstructMock &&other) + { + if (this == &other) { + return *this; + } + + move_assigned = true; + return *this; + } +}; + +#endif /* __ANY_TYPE_MOCK_TEST_H__ */ diff --git a/tests/gtests/blenlib/BLI_vector_test.cc b/tests/gtests/blenlib/BLI_vector_test.cc index e5a1ca2b200..90180feba1f 100644 --- a/tests/gtests/blenlib/BLI_vector_test.cc +++ b/tests/gtests/blenlib/BLI_vector_test.cc @@ -1,19 +1,19 @@ +#include "BLI_type_construct_mock.hh" #include "BLI_vector.hh" #include "testing/testing.h" #include <forward_list> using BLI::Vector; -using IntVector = Vector<int>; TEST(vector, DefaultConstructor) { - IntVector vec; + Vector<int> vec; EXPECT_EQ(vec.size(), 0); } TEST(vector, SizeConstructor) { - IntVector vec(3); + Vector<int> vec(3); EXPECT_EQ(vec.size(), 3); EXPECT_EQ(vec[0], 0); EXPECT_EQ(vec[1], 0); @@ -22,7 +22,7 @@ TEST(vector, SizeConstructor) TEST(vector, SizeValueConstructor) { - IntVector vec(4, 10); + Vector<int> vec(4, 10); EXPECT_EQ(vec.size(), 4); EXPECT_EQ(vec[0], 10); EXPECT_EQ(vec[1], 10); @@ -32,7 +32,7 @@ TEST(vector, SizeValueConstructor) TEST(vector, InitializerListConstructor) { - IntVector vec = {1, 3, 4, 6}; + Vector<int> vec = {1, 3, 4, 6}; EXPECT_EQ(vec.size(), 4); EXPECT_EQ(vec[0], 1); EXPECT_EQ(vec[1], 3); @@ -74,7 +74,7 @@ TEST(vector, ContainerConstructor) list.push_front(1); list.push_front(5); - IntVector vec = IntVector::FromContainer(list); + Vector<int> vec = Vector<int>::FromContainer(list); EXPECT_EQ(vec.size(), 3); EXPECT_EQ(vec[0], 5); EXPECT_EQ(vec[1], 1); @@ -83,8 +83,8 @@ TEST(vector, ContainerConstructor) TEST(vector, CopyConstructor) { - IntVector vec1 = {1, 2, 3}; - IntVector vec2(vec1); + Vector<int> vec1 = {1, 2, 3}; + Vector<int> vec2(vec1); EXPECT_EQ(vec2.size(), 3); EXPECT_EQ(vec2[0], 1); EXPECT_EQ(vec2[1], 2); @@ -133,8 +133,8 @@ TEST(vector, CopyConstructor4) TEST(vector, MoveConstructor) { - IntVector vec1 = {1, 2, 3, 4}; - IntVector vec2(std::move(vec1)); + Vector<int> vec1 = {1, 2, 3, 4}; + Vector<int> vec2(std::move(vec1)); EXPECT_EQ(vec1.size(), 0); EXPECT_EQ(vec2.size(), 4); @@ -179,20 +179,20 @@ TEST(vector, MoveConstructor4) TEST(vector, MoveAssignment) { - IntVector vec = {1, 2}; + Vector<int> vec = {1, 2}; EXPECT_EQ(vec.size(), 2); EXPECT_EQ(vec[0], 1); EXPECT_EQ(vec[1], 2); - vec = IntVector({5}); + vec = Vector<int>({5}); EXPECT_EQ(vec.size(), 1); EXPECT_EQ(vec[0], 5); } TEST(vector, CopyAssignment) { - IntVector vec1 = {1, 2, 3}; - IntVector vec2 = {4, 5}; + Vector<int> vec1 = {1, 2, 3}; + Vector<int> vec2 = {4, 5}; EXPECT_EQ(vec1.size(), 3); EXPECT_EQ(vec2.size(), 2); @@ -206,7 +206,7 @@ TEST(vector, CopyAssignment) TEST(vector, Append) { - IntVector vec; + Vector<int> vec; vec.append(3); vec.append(6); vec.append(7); @@ -218,7 +218,7 @@ TEST(vector, Append) TEST(vector, AppendAndGetIndex) { - IntVector vec; + Vector<int> vec; EXPECT_EQ(vec.append_and_get_index(10), 0); EXPECT_EQ(vec.append_and_get_index(10), 1); EXPECT_EQ(vec.append_and_get_index(10), 2); @@ -228,7 +228,7 @@ TEST(vector, AppendAndGetIndex) TEST(vector, AppendNonDuplicates) { - IntVector vec; + Vector<int> vec; vec.append_non_duplicates(4); EXPECT_EQ(vec.size(), 1); vec.append_non_duplicates(5); @@ -239,7 +239,7 @@ TEST(vector, AppendNonDuplicates) TEST(vector, ExtendNonDuplicates) { - IntVector vec; + Vector<int> vec; vec.extend_non_duplicates({1, 2}); EXPECT_EQ(vec.size(), 2); vec.extend_non_duplicates({3, 4}); @@ -250,7 +250,7 @@ TEST(vector, ExtendNonDuplicates) TEST(vector, Fill) { - IntVector vec(5); + Vector<int> vec(5); vec.fill(3); EXPECT_EQ(vec.size(), 5); EXPECT_EQ(vec[0], 3); @@ -262,7 +262,7 @@ TEST(vector, Fill) TEST(vector, FillIndices) { - IntVector vec(5, 0); + Vector<int> vec(5, 0); vec.fill_indices({1, 2}, 4); EXPECT_EQ(vec[0], 0); EXPECT_EQ(vec[1], 4); @@ -273,7 +273,7 @@ TEST(vector, FillIndices) TEST(vector, Iterator) { - IntVector vec({1, 4, 9, 16}); + Vector<int> vec({1, 4, 9, 16}); int i = 1; for (int value : vec) { EXPECT_EQ(value, i * i); @@ -293,14 +293,14 @@ TEST(vector, BecomeLarge) } } -static IntVector return_by_value_helper() +static Vector<int> return_by_value_helper() { - return IntVector({3, 5, 1}); + return Vector<int>({3, 5, 1}); } TEST(vector, ReturnByValue) { - IntVector vec = return_by_value_helper(); + Vector<int> vec = return_by_value_helper(); EXPECT_EQ(vec.size(), 3); EXPECT_EQ(vec[0], 3); EXPECT_EQ(vec[1], 5); @@ -309,10 +309,10 @@ TEST(vector, ReturnByValue) TEST(vector, VectorOfVectors_Append) { - Vector<IntVector> vec; + Vector<Vector<int>> vec; EXPECT_EQ(vec.size(), 0); - IntVector v({1, 2}); + Vector<int> v({1, 2}); vec.append(v); vec.append({7, 8}); EXPECT_EQ(vec.size(), 2); @@ -324,7 +324,7 @@ TEST(vector, VectorOfVectors_Append) TEST(vector, VectorOfVectors_Fill) { - Vector<IntVector> vec(3); + Vector<Vector<int>> vec(3); vec.fill({4, 5}); EXPECT_EQ(vec[0][0], 4); @@ -337,7 +337,7 @@ TEST(vector, VectorOfVectors_Fill) TEST(vector, RemoveLast) { - IntVector vec = {5, 6}; + Vector<int> vec = {5, 6}; EXPECT_EQ(vec.size(), 2); vec.remove_last(); EXPECT_EQ(vec.size(), 1); @@ -347,7 +347,7 @@ TEST(vector, RemoveLast) TEST(vector, IsEmpty) { - IntVector vec; + Vector<int> vec; EXPECT_TRUE(vec.is_empty()); vec.append(1); EXPECT_FALSE(vec.is_empty()); @@ -357,7 +357,7 @@ TEST(vector, IsEmpty) TEST(vector, RemoveReorder) { - IntVector vec = {4, 5, 6, 7}; + Vector<int> vec = {4, 5, 6, 7}; vec.remove_and_reorder(1); EXPECT_EQ(vec[0], 4); EXPECT_EQ(vec[1], 7); @@ -373,7 +373,7 @@ TEST(vector, RemoveReorder) TEST(vector, RemoveFirstOccurrenceAndReorder) { - IntVector vec = {4, 5, 6, 7}; + Vector<int> vec = {4, 5, 6, 7}; vec.remove_first_occurrence_and_reorder(5); EXPECT_EQ(vec[0], 4); EXPECT_EQ(vec[1], 7); @@ -389,24 +389,24 @@ TEST(vector, RemoveFirstOccurrenceAndReorder) TEST(vector, AllEqual_False) { - IntVector a = {1, 2, 3}; - IntVector b = {1, 2, 4}; - bool result = IntVector::all_equal(a, b); + Vector<int> a = {1, 2, 3}; + Vector<int> b = {1, 2, 4}; + bool result = Vector<int>::all_equal(a, b); EXPECT_FALSE(result); } TEST(vector, AllEqual_True) { - IntVector a = {4, 5, 6}; - IntVector b = {4, 5, 6}; - bool result = IntVector::all_equal(a, b); + Vector<int> a = {4, 5, 6}; + Vector<int> b = {4, 5, 6}; + bool result = Vector<int>::all_equal(a, b); EXPECT_TRUE(result); } TEST(vector, ExtendSmallVector) { - IntVector a = {2, 3, 4}; - IntVector b = {11, 12}; + Vector<int> a = {2, 3, 4}; + Vector<int> b = {11, 12}; b.extend(a); EXPECT_EQ(b.size(), 5); EXPECT_EQ(b[0], 11); @@ -420,7 +420,7 @@ TEST(vector, ExtendArray) { int array[] = {3, 4, 5, 6}; - IntVector a; + Vector<int> a; a.extend(array, 2); EXPECT_EQ(a.size(), 2); @@ -430,13 +430,13 @@ TEST(vector, ExtendArray) TEST(vector, Last) { - IntVector a{3, 5, 7}; + Vector<int> a{3, 5, 7}; EXPECT_EQ(a.last(), 7); } TEST(vector, AppendNTimes) { - IntVector a; + Vector<int> a; a.append_n_times(5, 3); a.append_n_times(2, 2); EXPECT_EQ(a.size(), 5); @@ -460,3 +460,68 @@ TEST(vector, UniquePtrValue) UNUSED_VARS(a, b); } + +TEST(vector, SizeConstructorCallsDefaultConstructor) +{ + Vector<TypeConstructMock> vec(3); + EXPECT_TRUE(vec[0].default_constructed); + EXPECT_TRUE(vec[1].default_constructed); + EXPECT_TRUE(vec[2].default_constructed); +} + +TEST(vector, SizeValueConstructorCallsCopyConstructor) +{ + Vector<TypeConstructMock> vec(3, TypeConstructMock()); + EXPECT_TRUE(vec[0].copy_constructed); + EXPECT_TRUE(vec[1].copy_constructed); + EXPECT_TRUE(vec[2].copy_constructed); +} + +TEST(vector, AppendCallsCopyConstructor) +{ + Vector<TypeConstructMock> vec; + TypeConstructMock value; + vec.append(value); + EXPECT_TRUE(vec[0].copy_constructed); +} + +TEST(vector, AppendCallsMoveConstructor) +{ + Vector<TypeConstructMock> vec; + vec.append(TypeConstructMock()); + EXPECT_TRUE(vec[0].move_constructed); +} + +TEST(vector, SmallVectorCopyCallsCopyConstructor) +{ + Vector<TypeConstructMock, 2> src(2); + Vector<TypeConstructMock, 2> dst(src); + EXPECT_TRUE(dst[0].copy_constructed); + EXPECT_TRUE(dst[1].copy_constructed); +} + +TEST(vector, LargeVectorCopyCallsCopyConstructor) +{ + Vector<TypeConstructMock, 2> src(5); + Vector<TypeConstructMock, 2> dst(src); + EXPECT_TRUE(dst[0].copy_constructed); + EXPECT_TRUE(dst[1].copy_constructed); +} + +TEST(vector, SmallVectorMoveCallsMoveConstructor) +{ + Vector<TypeConstructMock, 2> src(2); + Vector<TypeConstructMock, 2> dst(std::move(src)); + EXPECT_TRUE(dst[0].move_constructed); + EXPECT_TRUE(dst[1].move_constructed); +} + +TEST(vector, LargeVectorMoveCallsNoConstructor) +{ + Vector<TypeConstructMock, 2> src(5); + Vector<TypeConstructMock, 2> dst(std::move(src)); + + EXPECT_TRUE(dst[0].default_constructed); + EXPECT_FALSE(dst[0].move_constructed); + EXPECT_FALSE(dst[0].copy_constructed); +} |