diff options
Diffstat (limited to 'tests/gtests')
-rw-r--r-- | tests/gtests/blenlib/BLI_array_ref_test.cc | 266 | ||||
-rw-r--r-- | tests/gtests/blenlib/BLI_array_test.cc | 103 | ||||
-rw-r--r-- | tests/gtests/blenlib/BLI_index_range_test.cc | 131 | ||||
-rw-r--r-- | tests/gtests/blenlib/BLI_stack_cxx_test.cc | 52 | ||||
-rw-r--r-- | tests/gtests/blenlib/BLI_vector_test.cc | 400 | ||||
-rw-r--r-- | tests/gtests/blenlib/CMakeLists.txt | 7 |
6 files changed, 958 insertions, 1 deletions
diff --git a/tests/gtests/blenlib/BLI_array_ref_test.cc b/tests/gtests/blenlib/BLI_array_ref_test.cc new file mode 100644 index 00000000000..4507d9e6e84 --- /dev/null +++ b/tests/gtests/blenlib/BLI_array_ref_test.cc @@ -0,0 +1,266 @@ +#include "testing/testing.h" +#include "BLI_array_ref.h" +#include "BLI_vector.h" + +using BLI::IndexRange; +using IntVector = BLI::Vector<int>; +using IntArrayRef = BLI::ArrayRef<int>; +using MutableIntArrayRef = BLI::MutableArrayRef<int>; + +TEST(array_ref, FromSmallVector) +{ + IntVector a = {1, 2, 3}; + IntArrayRef a_ref = a; + EXPECT_EQ(a_ref.size(), 3); + EXPECT_EQ(a_ref[0], 1); + EXPECT_EQ(a_ref[1], 2); + EXPECT_EQ(a_ref[2], 3); +} + +TEST(array_ref, IsReferencing) +{ + int array[] = {3, 5, 8}; + MutableIntArrayRef ref(array, ARRAY_SIZE(array)); + EXPECT_EQ(ref.size(), 3); + EXPECT_EQ(ref[1], 5); + array[1] = 10; + EXPECT_EQ(ref[1], 10); +} + +TEST(array_ref, DropBack) +{ + IntVector a = {4, 5, 6, 7}; + auto slice = IntArrayRef(a).drop_back(2); + EXPECT_EQ(slice.size(), 2); + EXPECT_EQ(slice[0], 4); + EXPECT_EQ(slice[1], 5); +} + +TEST(array_ref, DropBackAll) +{ + IntVector a = {4, 5, 6, 7}; + auto slice = IntArrayRef(a).drop_back(a.size()); + EXPECT_EQ(slice.size(), 0); +} + +TEST(array_ref, DropFront) +{ + IntVector a = {4, 5, 6, 7}; + auto slice = IntArrayRef(a).drop_front(1); + EXPECT_EQ(slice.size(), 3); + EXPECT_EQ(slice[0], 5); + EXPECT_EQ(slice[1], 6); + EXPECT_EQ(slice[2], 7); +} + +TEST(array_ref, DropFrontAll) +{ + IntVector a = {4, 5, 6, 7}; + auto slice = IntArrayRef(a).drop_front(a.size()); + EXPECT_EQ(slice.size(), 0); +} + +TEST(array_ref, TakeFront) +{ + IntVector a = {4, 5, 6, 7}; + auto slice = IntArrayRef(a).take_front(2); + EXPECT_EQ(slice.size(), 2); + EXPECT_EQ(slice[0], 4); + EXPECT_EQ(slice[1], 5); +} + +TEST(array_ref, TakeBack) +{ + IntVector a = {5, 6, 7, 8}; + auto slice = IntArrayRef(a).take_back(2); + EXPECT_EQ(slice.size(), 2); + EXPECT_EQ(slice[0], 7); + EXPECT_EQ(slice[1], 8); +} + +TEST(array_ref, Slice) +{ + IntVector a = {4, 5, 6, 7}; + auto slice = IntArrayRef(a).slice(1, 2); + EXPECT_EQ(slice.size(), 2); + EXPECT_EQ(slice[0], 5); + EXPECT_EQ(slice[1], 6); +} + +TEST(array_ref, SliceEmpty) +{ + IntVector a = {4, 5, 6, 7}; + auto slice = IntArrayRef(a).slice(2, 0); + EXPECT_EQ(slice.size(), 0); +} + +TEST(array_ref, SliceRange) +{ + IntVector a = {1, 2, 3, 4, 5}; + auto slice = IntArrayRef(a).slice(IndexRange(2, 2)); + EXPECT_EQ(slice.size(), 2); + EXPECT_EQ(slice[0], 3); + EXPECT_EQ(slice[1], 4); +} + +TEST(array_ref, Contains) +{ + IntVector a = {4, 5, 6, 7}; + IntArrayRef a_ref = a; + EXPECT_TRUE(a_ref.contains(4)); + EXPECT_TRUE(a_ref.contains(5)); + EXPECT_TRUE(a_ref.contains(6)); + EXPECT_TRUE(a_ref.contains(7)); + EXPECT_FALSE(a_ref.contains(3)); + EXPECT_FALSE(a_ref.contains(8)); +} + +TEST(array_ref, Count) +{ + IntVector a = {2, 3, 4, 3, 3, 2, 2, 2, 2}; + IntArrayRef a_ref = a; + EXPECT_EQ(a_ref.count(1), 0); + EXPECT_EQ(a_ref.count(2), 5); + EXPECT_EQ(a_ref.count(3), 3); + EXPECT_EQ(a_ref.count(4), 1); + EXPECT_EQ(a_ref.count(5), 0); +} + +TEST(array_ref, ToSmallVector) +{ + IntVector a = {1, 2, 3, 4}; + IntArrayRef a_ref = a; + IntVector b = a_ref; + IntVector::all_equal(a, b); +} + +static void test_ref_from_initializer_list(IntArrayRef ref) +{ + EXPECT_EQ(ref.size(), 4); + EXPECT_EQ(ref[0], 3); + EXPECT_EQ(ref[1], 6); + EXPECT_EQ(ref[2], 8); + EXPECT_EQ(ref[3], 9); +} + +TEST(array_ref, FromInitializerList) +{ + test_ref_from_initializer_list({3, 6, 8, 9}); +} + +TEST(array_ref, FromVector) +{ + std::vector<int> a = {1, 2, 3, 4}; + IntArrayRef a_ref(a); + EXPECT_EQ(a_ref.size(), 4); + EXPECT_EQ(a_ref[0], 1); + EXPECT_EQ(a_ref[1], 2); + EXPECT_EQ(a_ref[2], 3); + EXPECT_EQ(a_ref[3], 4); +} + +TEST(array_ref, FromArray) +{ + std::array<int, 2> a = {5, 6}; + IntArrayRef a_ref(a); + EXPECT_EQ(a_ref.size(), 2); + EXPECT_EQ(a_ref[0], 5); + EXPECT_EQ(a_ref[1], 6); +} + +TEST(array_ref, Fill) +{ + std::array<int, 5> a = {4, 5, 6, 7, 8}; + MutableIntArrayRef a_ref(a); + a_ref.fill(1); + EXPECT_EQ(a[0], 1); + EXPECT_EQ(a[1], 1); + EXPECT_EQ(a[2], 1); + EXPECT_EQ(a[3], 1); + EXPECT_EQ(a[4], 1); +} + +TEST(array_ref, FillIndices) +{ + std::array<int, 5> a = {0, 0, 0, 0, 0}; + MutableIntArrayRef a_ref(a); + a_ref.fill_indices({0, 2, 3}, 1); + EXPECT_EQ(a[0], 1); + EXPECT_EQ(a[1], 0); + EXPECT_EQ(a[2], 1); + EXPECT_EQ(a[3], 1); + EXPECT_EQ(a[4], 0); +} + +TEST(array_ref, CopyFrom) +{ + std::array<int, 3> a = {3, 4, 5}; + MutableIntArrayRef a_ref(a); + EXPECT_EQ(a[0], 3); + EXPECT_EQ(a[1], 4); + EXPECT_EQ(a[2], 5); + a_ref.copy_from({1, 2, 3}); + EXPECT_EQ(a[0], 1); + EXPECT_EQ(a[1], 2); + EXPECT_EQ(a[2], 3); +} + +TEST(array_ref, ByteSize) +{ + std::array<int, 10> a; + IntArrayRef a_ref(a); + EXPECT_EQ(a_ref.byte_size(), sizeof(a)); + EXPECT_EQ(a_ref.byte_size(), 40); +} + +TEST(array_ref, CopyTo) +{ + std::array<int, 3> a = {5, 6, 7}; + int b[3] = {0}; + IntArrayRef a_ref(a); + a_ref.copy_to(b); + + EXPECT_EQ(b[0], 5); + EXPECT_EQ(b[1], 6); + EXPECT_EQ(b[2], 7); +} + +TEST(array_ref, FirstLast) +{ + std::array<int, 4> a = {6, 7, 8, 9}; + IntArrayRef a_ref(a); + EXPECT_EQ(a_ref.first(), 6); + EXPECT_EQ(a_ref.last(), 9); +} + +TEST(array_ref, FirstLast_OneElement) +{ + int a = 3; + IntArrayRef a_ref(&a, 1); + EXPECT_EQ(a_ref.first(), 3); + EXPECT_EQ(a_ref.last(), 3); +} + +TEST(array_ref, Get) +{ + std::array<int, 3> a = {5, 6, 7}; + IntArrayRef a_ref(a); + EXPECT_EQ(a_ref.get(0, 42), 5); + EXPECT_EQ(a_ref.get(1, 42), 6); + EXPECT_EQ(a_ref.get(2, 42), 7); + EXPECT_EQ(a_ref.get(3, 42), 42); + EXPECT_EQ(a_ref.get(4, 42), 42); +} + +TEST(array_ref, ContainsPtr) +{ + std::array<int, 3> a = {5, 6, 7}; + int other = 10; + IntArrayRef a_ref(a); + EXPECT_TRUE(a_ref.contains_ptr(&a[0] + 0)); + EXPECT_TRUE(a_ref.contains_ptr(&a[0] + 1)); + EXPECT_TRUE(a_ref.contains_ptr(&a[0] + 2)); + EXPECT_FALSE(a_ref.contains_ptr(&a[0] + 3)); + EXPECT_FALSE(a_ref.contains_ptr(&a[0] - 1)); + EXPECT_FALSE(a_ref.contains_ptr(&other)); +} diff --git a/tests/gtests/blenlib/BLI_array_test.cc b/tests/gtests/blenlib/BLI_array_test.cc new file mode 100644 index 00000000000..33f84928d3d --- /dev/null +++ b/tests/gtests/blenlib/BLI_array_test.cc @@ -0,0 +1,103 @@ +#include "testing/testing.h" +#include "BLI_array_cxx.h" + +using namespace BLI; + +TEST(array, DefaultConstructor) +{ + Array<int> array; + EXPECT_EQ(array.size(), 0); +} + +TEST(array, SizeConstructor) +{ + Array<int> array(5); + EXPECT_EQ(array.size(), 5); +} + +TEST(array, FillConstructor) +{ + Array<int> array(5, 8); + EXPECT_EQ(array.size(), 5); + EXPECT_EQ(array[0], 8); + EXPECT_EQ(array[1], 8); + EXPECT_EQ(array[2], 8); + EXPECT_EQ(array[3], 8); + EXPECT_EQ(array[4], 8); +} + +TEST(array, InitializerListConstructor) +{ + Array<int> array = {4, 5, 6, 7}; + EXPECT_EQ(array.size(), 4); + EXPECT_EQ(array[0], 4); + EXPECT_EQ(array[1], 5); + EXPECT_EQ(array[2], 6); + EXPECT_EQ(array[3], 7); +} + +TEST(array, ArrayRefConstructor) +{ + int stackarray[4] = {6, 7, 8, 9}; + ArrayRef<int> array_ref(stackarray, ARRAY_SIZE(stackarray)); + Array<int> array(array_ref); + EXPECT_EQ(array.size(), 4); + EXPECT_EQ(array[0], 6); + EXPECT_EQ(array[1], 7); + EXPECT_EQ(array[2], 8); + EXPECT_EQ(array[3], 9); +} + +TEST(array, CopyConstructor) +{ + Array<int> array = {5, 6, 7, 8}; + Array<int> new_array(array); + + EXPECT_EQ(array.size(), 4); + EXPECT_EQ(new_array.size(), 4); + EXPECT_NE(array.begin(), new_array.begin()); + EXPECT_EQ(new_array[0], 5); + EXPECT_EQ(new_array[1], 6); + EXPECT_EQ(new_array[2], 7); + EXPECT_EQ(new_array[3], 8); +} + +TEST(array, MoveConstructor) +{ + Array<int> array = {5, 6, 7, 8}; + Array<int> new_array(std::move(array)); + + EXPECT_EQ(array.size(), 0); + EXPECT_EQ(new_array.size(), 4); + EXPECT_EQ(new_array[0], 5); + EXPECT_EQ(new_array[1], 6); + EXPECT_EQ(new_array[2], 7); + EXPECT_EQ(new_array[3], 8); +} + +TEST(array, CopyAssignment) +{ + Array<int> array = {1, 2, 3}; + Array<int> new_array = {4}; + EXPECT_EQ(new_array.size(), 1); + new_array = array; + EXPECT_EQ(new_array.size(), 3); + EXPECT_EQ(array.size(), 3); + EXPECT_NE(array.begin(), new_array.begin()); + EXPECT_EQ(new_array[0], 1); + EXPECT_EQ(new_array[1], 2); + EXPECT_EQ(new_array[2], 3); +} + +TEST(array, MoveAssignment) +{ + Array<int> array = {1, 2, 3}; + Array<int> new_array = {4}; + EXPECT_EQ(new_array.size(), 1); + new_array = std::move(array); + EXPECT_EQ(new_array.size(), 3); + EXPECT_EQ(array.size(), 0); + EXPECT_EQ(new_array[0], 1); + EXPECT_EQ(new_array[1], 2); + EXPECT_EQ(new_array[2], 3); +} diff --git a/tests/gtests/blenlib/BLI_index_range_test.cc b/tests/gtests/blenlib/BLI_index_range_test.cc new file mode 100644 index 00000000000..1944279999d --- /dev/null +++ b/tests/gtests/blenlib/BLI_index_range_test.cc @@ -0,0 +1,131 @@ +#include "testing/testing.h" +#include "BLI_index_range.h" +#include "BLI_vector.h" + +using BLI::ArrayRef; +using BLI::IndexRange; +using IntVector = BLI::Vector<int>; + +TEST(index_range, DefaultConstructor) +{ + IndexRange range; + EXPECT_EQ(range.size(), 0); + + IntVector vector; + for (int value : range) { + vector.append(value); + } + EXPECT_EQ(vector.size(), 0); +} + +TEST(index_range, SingleElementRange) +{ + IndexRange range(4, 1); + EXPECT_EQ(range.size(), 1); + EXPECT_EQ(*range.begin(), 4); + + IntVector vector; + for (int value : range) { + vector.append(value); + } + + EXPECT_EQ(vector.size(), 1); + EXPECT_EQ(vector[0], 4); +} + +TEST(index_range, MultipleElementRange) +{ + IndexRange range(6, 4); + EXPECT_EQ(range.size(), 4); + + IntVector vector; + for (int value : range) { + vector.append(value); + } + + EXPECT_EQ(vector.size(), 4); + for (uint i = 0; i < 4; i++) { + EXPECT_EQ(vector[i], i + 6); + } +} + +TEST(index_range, SubscriptOperator) +{ + IndexRange range(5, 5); + EXPECT_EQ(range[0], 5); + EXPECT_EQ(range[1], 6); + EXPECT_EQ(range[2], 7); +} + +TEST(index_range, Before) +{ + IndexRange range = IndexRange(5, 5).before(3); + EXPECT_EQ(range[0], 2); + EXPECT_EQ(range[1], 3); + EXPECT_EQ(range[2], 4); + EXPECT_EQ(range.size(), 3); +} + +TEST(index_range, After) +{ + IndexRange range = IndexRange(5, 5).after(4); + EXPECT_EQ(range[0], 10); + EXPECT_EQ(range[1], 11); + EXPECT_EQ(range[2], 12); + EXPECT_EQ(range[3], 13); + EXPECT_EQ(range.size(), 4); +} + +TEST(index_range, Contains) +{ + IndexRange range = IndexRange(5, 3); + EXPECT_TRUE(range.contains(5)); + EXPECT_TRUE(range.contains(6)); + EXPECT_TRUE(range.contains(7)); + EXPECT_FALSE(range.contains(4)); + EXPECT_FALSE(range.contains(8)); +} + +TEST(index_range, First) +{ + IndexRange range = IndexRange(5, 3); + EXPECT_EQ(range.first(), 5); +} + +TEST(index_range, Last) +{ + IndexRange range = IndexRange(5, 3); + EXPECT_EQ(range.last(), 7); +} + +TEST(index_range, OneAfterEnd) +{ + IndexRange range = IndexRange(5, 3); + EXPECT_EQ(range.one_after_last(), 8); +} + +TEST(index_range, Start) +{ + IndexRange range = IndexRange(6, 2); + EXPECT_EQ(range.start(), 6); +} + +TEST(index_range, Slice) +{ + IndexRange range = IndexRange(5, 15); + IndexRange slice = range.slice(2, 6); + EXPECT_EQ(slice.size(), 6); + EXPECT_EQ(slice.first(), 7); + EXPECT_EQ(slice.last(), 12); +} + +TEST(index_range, AsArrayRef) +{ + IndexRange range = IndexRange(4, 6); + ArrayRef<uint> ref = range.as_array_ref(); + EXPECT_EQ(ref.size(), 6); + EXPECT_EQ(ref[0], 4); + EXPECT_EQ(ref[1], 5); + EXPECT_EQ(ref[2], 6); + EXPECT_EQ(ref[3], 7); +} diff --git a/tests/gtests/blenlib/BLI_stack_cxx_test.cc b/tests/gtests/blenlib/BLI_stack_cxx_test.cc new file mode 100644 index 00000000000..02c5407fda3 --- /dev/null +++ b/tests/gtests/blenlib/BLI_stack_cxx_test.cc @@ -0,0 +1,52 @@ +#include "testing/testing.h" +#include "BLI_stack_cxx.h" + +using IntStack = BLI::Stack<int>; + +TEST(stack, DefaultConstructor) +{ + IntStack stack; + EXPECT_EQ(stack.size(), 0); + EXPECT_TRUE(stack.empty()); +} + +TEST(stack, ArrayRefConstructor) +{ + std::array<int, 3> array = {4, 7, 2}; + IntStack stack(array); + EXPECT_EQ(stack.size(), 3); + EXPECT_EQ(stack.pop(), 2); + EXPECT_EQ(stack.pop(), 7); + EXPECT_EQ(stack.pop(), 4); + EXPECT_TRUE(stack.empty()); +} + +TEST(stack, Push) +{ + IntStack stack; + EXPECT_EQ(stack.size(), 0); + stack.push(3); + EXPECT_EQ(stack.size(), 1); + stack.push(5); + EXPECT_EQ(stack.size(), 2); +} + +TEST(stack, Pop) +{ + IntStack stack; + stack.push(4); + stack.push(6); + EXPECT_EQ(stack.pop(), 6); + EXPECT_EQ(stack.pop(), 4); +} + +TEST(stack, Peek) +{ + IntStack stack; + stack.push(3); + stack.push(4); + EXPECT_EQ(stack.peek(), 4); + EXPECT_EQ(stack.peek(), 4); + stack.pop(); + EXPECT_EQ(stack.peek(), 3); +} diff --git a/tests/gtests/blenlib/BLI_vector_test.cc b/tests/gtests/blenlib/BLI_vector_test.cc new file mode 100644 index 00000000000..60f78025269 --- /dev/null +++ b/tests/gtests/blenlib/BLI_vector_test.cc @@ -0,0 +1,400 @@ +#include "testing/testing.h" +#include "BLI_vector.h" +#include <forward_list> + +using BLI::Vector; +using IntVector = Vector<int>; + +TEST(vector, DefaultConstructor) +{ + IntVector vec; + EXPECT_EQ(vec.size(), 0); +} + +TEST(vector, SizeConstructor) +{ + IntVector vec(3); + EXPECT_EQ(vec.size(), 3); + EXPECT_EQ(vec[0], 0); + EXPECT_EQ(vec[1], 0); + EXPECT_EQ(vec[2], 0); +} + +TEST(vector, SizeValueConstructor) +{ + IntVector vec(4, 10); + EXPECT_EQ(vec.size(), 4); + EXPECT_EQ(vec[0], 10); + EXPECT_EQ(vec[1], 10); + EXPECT_EQ(vec[2], 10); + EXPECT_EQ(vec[3], 10); +} + +TEST(vector, InitializerListConstructor) +{ + IntVector vec = {1, 3, 4, 6}; + EXPECT_EQ(vec.size(), 4); + EXPECT_EQ(vec[0], 1); + EXPECT_EQ(vec[1], 3); + EXPECT_EQ(vec[2], 4); + EXPECT_EQ(vec[3], 6); +} + +struct TestListValue { + TestListValue *next, *prev; + int value; +}; + +TEST(vector, IntrusiveListBaseConstructor) +{ + TestListValue *value1 = new TestListValue{0, 0, 4}; + TestListValue *value2 = new TestListValue{0, 0, 5}; + TestListValue *value3 = new TestListValue{0, 0, 6}; + + ListBase list = {NULL, NULL}; + BLI_addtail(&list, value1); + BLI_addtail(&list, value2); + BLI_addtail(&list, value3); + Vector<TestListValue *> vec(list, true); + + EXPECT_EQ(vec.size(), 3); + EXPECT_EQ(vec[0]->value, 4); + EXPECT_EQ(vec[1]->value, 5); + EXPECT_EQ(vec[2]->value, 6); + + delete value1; + delete value2; + delete value3; +} + +TEST(vector, ContainerConstructor) +{ + std::forward_list<int> list; + list.push_front(3); + list.push_front(1); + list.push_front(5); + + IntVector vec = IntVector::FromContainer(list); + EXPECT_EQ(vec.size(), 3); + EXPECT_EQ(vec[0], 5); + EXPECT_EQ(vec[1], 1); + EXPECT_EQ(vec[2], 3); +} + +TEST(vector, CopyConstructor) +{ + IntVector vec1 = {1, 2, 3}; + IntVector vec2(vec1); + EXPECT_EQ(vec2.size(), 3); + EXPECT_EQ(vec2[0], 1); + EXPECT_EQ(vec2[1], 2); + EXPECT_EQ(vec2[2], 3); + + vec1[1] = 5; + EXPECT_EQ(vec1[1], 5); + EXPECT_EQ(vec2[1], 2); +} + +TEST(vector, CopyConstructor2) +{ + Vector<int, 2> vec1 = {1, 2, 3, 4}; + Vector<int, 3> vec2(vec1); + + EXPECT_EQ(vec1.size(), 4); + EXPECT_EQ(vec2.size(), 4); + EXPECT_NE(vec1.begin(), vec2.begin()); + EXPECT_EQ(vec2[0], 1); + EXPECT_EQ(vec2[1], 2); + EXPECT_EQ(vec2[2], 3); + EXPECT_EQ(vec2[3], 4); +} + +TEST(vector, CopyConstructor3) +{ + Vector<int, 20> vec1 = {1, 2, 3, 4}; + Vector<int, 1> vec2(vec1); + + EXPECT_EQ(vec1.size(), 4); + EXPECT_EQ(vec2.size(), 4); + EXPECT_NE(vec1.begin(), vec2.begin()); + EXPECT_EQ(vec2[2], 3); +} + +TEST(vector, CopyConstructor4) +{ + Vector<int, 5> vec1 = {1, 2, 3, 4}; + Vector<int, 6> vec2(vec1); + + EXPECT_EQ(vec1.size(), 4); + EXPECT_EQ(vec2.size(), 4); + EXPECT_NE(vec1.begin(), vec2.begin()); + EXPECT_EQ(vec2[3], 4); +} + +TEST(vector, MoveConstructor) +{ + IntVector vec1 = {1, 2, 3, 4}; + IntVector vec2(std::move(vec1)); + + EXPECT_EQ(vec1.size(), 0); + EXPECT_EQ(vec2.size(), 4); + EXPECT_EQ(vec2[0], 1); + EXPECT_EQ(vec2[1], 2); + EXPECT_EQ(vec2[2], 3); + EXPECT_EQ(vec2[3], 4); +} + +TEST(vector, MoveConstructor2) +{ + Vector<int, 2> vec1 = {1, 2, 3, 4}; + Vector<int, 3> vec2(std::move(vec1)); + + EXPECT_EQ(vec1.size(), 0); + EXPECT_EQ(vec2.size(), 4); + EXPECT_EQ(vec2[0], 1); + EXPECT_EQ(vec2[1], 2); + EXPECT_EQ(vec2[2], 3); + EXPECT_EQ(vec2[3], 4); +} + +TEST(vector, MoveConstructor3) +{ + Vector<int, 20> vec1 = {1, 2, 3, 4}; + Vector<int, 1> vec2(std::move(vec1)); + + EXPECT_EQ(vec1.size(), 0); + EXPECT_EQ(vec2.size(), 4); + EXPECT_EQ(vec2[2], 3); +} + +TEST(vector, MoveConstructor4) +{ + Vector<int, 5> vec1 = {1, 2, 3, 4}; + Vector<int, 6> vec2(std::move(vec1)); + + EXPECT_EQ(vec1.size(), 0); + EXPECT_EQ(vec2.size(), 4); + EXPECT_EQ(vec2[3], 4); +} + +TEST(vector, MoveAssignment) +{ + IntVector vec = {1, 2}; + EXPECT_EQ(vec.size(), 2); + EXPECT_EQ(vec[0], 1); + EXPECT_EQ(vec[1], 2); + + vec = IntVector({5}); + EXPECT_EQ(vec.size(), 1); + EXPECT_EQ(vec[0], 5); +} + +TEST(vector, CopyAssignment) +{ + IntVector vec1 = {1, 2, 3}; + IntVector vec2 = {4, 5}; + EXPECT_EQ(vec1.size(), 3); + EXPECT_EQ(vec2.size(), 2); + + vec2 = vec1; + EXPECT_EQ(vec2.size(), 3); + + vec1[0] = 7; + EXPECT_EQ(vec1[0], 7); + EXPECT_EQ(vec2[0], 1); +} + +TEST(vector, Append) +{ + IntVector vec; + vec.append(3); + vec.append(6); + vec.append(7); + EXPECT_EQ(vec.size(), 3); + EXPECT_EQ(vec[0], 3); + EXPECT_EQ(vec[1], 6); + EXPECT_EQ(vec[2], 7); +} + +TEST(vector, Fill) +{ + IntVector vec(5); + vec.fill(3); + EXPECT_EQ(vec.size(), 5); + EXPECT_EQ(vec[0], 3); + EXPECT_EQ(vec[1], 3); + EXPECT_EQ(vec[2], 3); + EXPECT_EQ(vec[3], 3); + EXPECT_EQ(vec[4], 3); +} + +TEST(vector, FillIndices) +{ + IntVector vec(5, 0); + vec.fill_indices({1, 2}, 4); + EXPECT_EQ(vec[0], 0); + EXPECT_EQ(vec[1], 4); + EXPECT_EQ(vec[2], 4); + EXPECT_EQ(vec[3], 0); + EXPECT_EQ(vec[4], 0); +} + +TEST(vector, Iterator) +{ + IntVector vec({1, 4, 9, 16}); + int i = 1; + for (int value : vec) { + EXPECT_EQ(value, i * i); + i++; + } +} + +TEST(vector, BecomeLarge) +{ + Vector<int, 4> vec; + for (int i = 0; i < 100; i++) { + vec.append(i * 5); + } + EXPECT_EQ(vec.size(), 100); + for (int i = 0; i < 100; i++) { + EXPECT_EQ(vec[i], i * 5); + } +} + +IntVector return_by_value_helper() +{ + return IntVector({3, 5, 1}); +} + +TEST(vector, ReturnByValue) +{ + IntVector vec = return_by_value_helper(); + EXPECT_EQ(vec.size(), 3); + EXPECT_EQ(vec[0], 3); + EXPECT_EQ(vec[1], 5); + EXPECT_EQ(vec[2], 1); +} + +TEST(vector, VectorOfVectors_Append) +{ + Vector<IntVector> vec; + EXPECT_EQ(vec.size(), 0); + + IntVector v({1, 2}); + vec.append(v); + vec.append({7, 8}); + EXPECT_EQ(vec.size(), 2); + EXPECT_EQ(vec[0][0], 1); + EXPECT_EQ(vec[0][1], 2); + EXPECT_EQ(vec[1][0], 7); + EXPECT_EQ(vec[1][1], 8); +} + +TEST(vector, VectorOfVectors_Fill) +{ + Vector<IntVector> vec(3); + vec.fill({4, 5}); + + EXPECT_EQ(vec[0][0], 4); + EXPECT_EQ(vec[0][1], 5); + EXPECT_EQ(vec[1][0], 4); + EXPECT_EQ(vec[1][1], 5); + EXPECT_EQ(vec[2][0], 4); + EXPECT_EQ(vec[2][1], 5); +} + +TEST(vector, RemoveLast) +{ + IntVector vec = {5, 6}; + EXPECT_EQ(vec.size(), 2); + vec.remove_last(); + EXPECT_EQ(vec.size(), 1); + vec.remove_last(); + EXPECT_EQ(vec.size(), 0); +} + +TEST(vector, Empty) +{ + IntVector vec; + EXPECT_TRUE(vec.empty()); + vec.append(1); + EXPECT_FALSE(vec.empty()); + vec.remove_last(); + EXPECT_TRUE(vec.empty()); +} + +TEST(vector, RemoveReorder) +{ + IntVector vec = {4, 5, 6, 7}; + vec.remove_and_reorder(1); + EXPECT_EQ(vec[0], 4); + EXPECT_EQ(vec[1], 7); + EXPECT_EQ(vec[2], 6); + vec.remove_and_reorder(2); + EXPECT_EQ(vec[0], 4); + EXPECT_EQ(vec[1], 7); + vec.remove_and_reorder(0); + EXPECT_EQ(vec[0], 7); + vec.remove_and_reorder(0); + EXPECT_TRUE(vec.empty()); +} + +TEST(vector, AllEqual_False) +{ + IntVector a = {1, 2, 3}; + IntVector b = {1, 2, 4}; + bool result = IntVector::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); + EXPECT_TRUE(result); +} + +TEST(vector, ExtendSmallVector) +{ + IntVector a = {2, 3, 4}; + IntVector b = {11, 12}; + b.extend(a); + EXPECT_EQ(b.size(), 5); + EXPECT_EQ(b[0], 11); + EXPECT_EQ(b[1], 12); + EXPECT_EQ(b[2], 2); + EXPECT_EQ(b[3], 3); + EXPECT_EQ(b[4], 4); +} + +TEST(vector, ExtendArray) +{ + int array[] = {3, 4, 5, 6}; + + IntVector a; + a.extend(array, 2); + + EXPECT_EQ(a.size(), 2); + EXPECT_EQ(a[0], 3); + EXPECT_EQ(a[1], 4); +} + +TEST(vector, Last) +{ + IntVector a{3, 5, 7}; + EXPECT_EQ(a.last(), 7); +} + +TEST(vector, AppendNTimes) +{ + IntVector a; + a.append_n_times(5, 3); + a.append_n_times(2, 2); + EXPECT_EQ(a.size(), 5); + EXPECT_EQ(a[0], 5); + EXPECT_EQ(a[1], 5); + EXPECT_EQ(a[2], 5); + EXPECT_EQ(a[3], 2); + EXPECT_EQ(a[4], 2); +} diff --git a/tests/gtests/blenlib/CMakeLists.txt b/tests/gtests/blenlib/CMakeLists.txt index 4e5811d140e..d2d7b76df6c 100644 --- a/tests/gtests/blenlib/CMakeLists.txt +++ b/tests/gtests/blenlib/CMakeLists.txt @@ -38,15 +38,18 @@ else() set(BLI_path_util_extra_libs "bf_blenlib;extern_wcwidth;${ZLIB_LIBRARIES}") endif() +BLENDER_TEST(BLI_array "bf_blenlib") +BLENDER_TEST(BLI_array_ref "bf_blenlib") BLENDER_TEST(BLI_array_store "bf_blenlib") BLENDER_TEST(BLI_array_utils "bf_blenlib") BLENDER_TEST(BLI_delaunay_2d "bf_blenlib") -BLENDER_TEST(BLI_expr_pylike_eval "bf_blenlib") BLENDER_TEST(BLI_edgehash "bf_blenlib") +BLENDER_TEST(BLI_expr_pylike_eval "bf_blenlib") BLENDER_TEST(BLI_ghash "bf_blenlib") BLENDER_TEST(BLI_hash_mm2a "bf_blenlib") BLENDER_TEST(BLI_heap "bf_blenlib") BLENDER_TEST(BLI_heap_simple "bf_blenlib") +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") @@ -57,9 +60,11 @@ 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_stack "bf_blenlib") +BLENDER_TEST(BLI_stack_cxx "bf_blenlib") BLENDER_TEST(BLI_string "bf_blenlib") BLENDER_TEST(BLI_string_utf8 "bf_blenlib") BLENDER_TEST(BLI_task "bf_blenlib;bf_intern_numaapi") +BLENDER_TEST(BLI_vector "bf_blenlib") BLENDER_TEST_PERFORMANCE(BLI_ghash_performance "bf_blenlib") BLENDER_TEST_PERFORMANCE(BLI_task_performance "bf_blenlib") |