Blender V4.3
vector_test.cc
Go to the documentation of this file.
1// Copyright (c) 2009 libmv authors.
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to
5// deal in the Software without restriction, including without limitation the
6// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7// sell copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19// IN THE SOFTWARE.
20
21#include "libmv/base/vector.h"
22#include <algorithm>
24#include "testing/testing.h"
25
26namespace {
27using namespace libmv;
28
29// This uses a Vec2d which is a fixed-size vectorizable Eigen type. It is
30// necessary to test vectorizable types to ensure that the alignment asserts
31// trigger if the alignment is not correct.
32TEST(VectorAlignmentTest, PushBack) {
33 Vec2 x1, x2;
34 x1 << 1, 2;
35 x2 << 3, 4;
36
37 vector<Vec2> vs;
38 vs.push_back(x1);
39 EXPECT_EQ(1, vs.size());
40
41 vs.push_back(x2);
42 EXPECT_EQ(2, vs.size());
43
44 // The following is necessary because of some bug in gtest; the expected
45 // parameter can't be a fixed size vectorizable type with alignment
46 // requirements.
47 Vec x1r = x1;
48 Vec x2r = x2;
49 EXPECT_EQ(x1r, vs[0]);
50 EXPECT_EQ(x2r, vs[1]);
51
52 vs.push_back(x2);
53 vs.push_back(x2);
54 vs.push_back(x2);
55 EXPECT_EQ(5, vs.size());
56}
57
58// Count the number of destruct calls to test that the destructor gets called.
59int foo_construct_calls = 0;
60int foo_destruct_calls = 0;
61
62struct Foo {
63 public:
64 Foo() : value(5) { foo_construct_calls++; }
65 ~Foo() { foo_destruct_calls++; }
66 int value;
67};
68
69struct VectorTest : public testing::Test {
70 VectorTest() {
71 foo_construct_calls = 0;
72 foo_destruct_calls = 0;
73 }
74};
75
76TEST_F(VectorTest, EmptyVectorDoesNotConstruct) {
77 {
79 EXPECT_EQ(0, v.size());
80 }
81 EXPECT_EQ(0, foo_construct_calls);
82 EXPECT_EQ(0, foo_destruct_calls);
83}
84
85TEST_F(VectorTest, DestructorGetsCalled) {
86 {
88 v.resize(5);
89 }
90 EXPECT_EQ(5, foo_construct_calls);
91 EXPECT_EQ(5, foo_destruct_calls);
92}
93
94TEST_F(VectorTest, ReserveDoesNotCallConstructorsOrDestructors) {
96 EXPECT_EQ(0, v.size());
97 EXPECT_EQ(0, foo_construct_calls);
98 EXPECT_EQ(0, foo_destruct_calls);
99
100 v.reserve(5);
101 EXPECT_EQ(0, v.size());
102 EXPECT_EQ(0, foo_construct_calls);
103 EXPECT_EQ(0, foo_destruct_calls);
104}
105
106TEST_F(VectorTest, ResizeConstructsAndDestructsAsExpected) {
108
109 // Create one object.
110 v.resize(1);
111 EXPECT_EQ(1, v.size());
112 EXPECT_EQ(1, foo_construct_calls);
113 EXPECT_EQ(5, v[0].value);
114
115 // Create two more.
116 v.resize(3);
117 EXPECT_EQ(3, v.size());
118 EXPECT_EQ(3, foo_construct_calls);
119
120 // Delete the last one.
121 v.resize(2);
122 EXPECT_EQ(2, v.size());
123 EXPECT_EQ(3, foo_construct_calls);
124
125 // Delete the remaining two.
126 v.resize(0);
127 EXPECT_EQ(0, v.size());
128 EXPECT_EQ(3, foo_construct_calls);
129}
130
131TEST_F(VectorTest, PushPopBack) {
133
134 Foo foo;
135 foo.value = 10;
136 v.push_back(foo);
137 EXPECT_EQ(1, v.size());
138 EXPECT_EQ(10, v.back().value);
139
140 v.pop_back();
141 EXPECT_EQ(0, v.size());
142 EXPECT_EQ(1, foo_construct_calls);
143 EXPECT_EQ(1, foo_destruct_calls);
144}
145
146TEST_F(VectorTest, CopyConstructor) {
147 vector<int> a;
148 a.push_back(1);
149 a.push_back(5);
150 a.push_back(3);
151
152 vector<int> b(a);
153 EXPECT_EQ(a.size(), b.size());
154 for (int i = 0; i < a.size(); ++i) {
155 EXPECT_EQ(a[i], b[i]);
156 }
157}
158
159TEST_F(VectorTest, OperatorEquals) {
160 vector<int> a, b;
161 a.push_back(1);
162 a.push_back(5);
163 a.push_back(3);
164
165 b = a;
166
167 EXPECT_EQ(a.size(), b.size());
168 for (int i = 0; i < a.size(); ++i) {
169 EXPECT_EQ(a[i], b[i]);
170 }
171}
172
173TEST_F(VectorTest, STLFind) {
174 vector<int> a;
175 a.push_back(1);
176 a.push_back(5);
177 a.push_back(3);
178
179 // Find returns an int *
180 EXPECT_EQ(std::find(&a[0], &a[2], 1) == &a[0], true);
181 EXPECT_EQ(std::find(&a[0], &a[2], 5) == &a[1], true);
182 EXPECT_EQ(std::find(&a[0], &a[2], 3) == &a[2], true);
183
184 // Find returns an interator
185 EXPECT_EQ(std::find(a.begin(), a.end(), 1) == std::next(a.begin(), 0), true);
186 EXPECT_EQ(std::find(a.begin(), a.end(), 5) == std::next(a.begin(), 1), true);
187 EXPECT_EQ(std::find(a.begin(), a.end(), 3) == std::next(a.begin(), 2), true);
188
189 // Search value that are not in the vector
190 EXPECT_EQ(std::find(a.begin(), a.end(), 0) == a.end(), true);
191 EXPECT_EQ(std::find(a.begin(), a.end(), 52) == a.end(), true);
192}
193
194TEST(Vector, swap) {
195 vector<int> a, b;
196 a.push_back(1);
197 a.push_back(2);
198 b.push_back(3);
199 a.swap(b);
200 EXPECT_EQ(1, a.size());
201 EXPECT_EQ(3, a[0]);
202 EXPECT_EQ(2, b.size());
203 EXPECT_EQ(1, b[0]);
204 EXPECT_EQ(2, b[1]);
205}
206
207} // namespace
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
TEST_F(StringFindSplitWords, Simple)
ATTR_WARN_UNUSED_RESULT const BMVert * v
local_group_size(16, 16) .push_constant(Type b
Eigen::VectorXd Vec
Definition numeric.h:61
Eigen::Vector2d Vec2
Definition numeric.h:105
TEST(PolynomialCameraIntrinsics2, ApplyOnFocalCenter)