1 // This file is part of Eigen, a lightweight C++ template library
4 // Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6 // This Source Code Form is subject to the terms of the Mozilla
7 // Public License v. 2.0. If a copy of the MPL was not distributed
8 // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 #include <Eigen/StdVector>
12 #include <Eigen/Geometry>
14 template<typename MatrixType
>
15 void check_stdvector_matrix(const MatrixType
& m
)
17 typename
MatrixType::Index rows
= m
.rows();
18 typename
MatrixType::Index cols
= m
.cols();
19 MatrixType x
= MatrixType::Random(rows
,cols
), y
= MatrixType::Random(rows
,cols
);
20 std::vector
<MatrixType
,Eigen::aligned_allocator
<MatrixType
> > v(10, MatrixType(rows
,cols
)), w(20, y
);
23 VERIFY_IS_APPROX(w
[6], v
[5]);
25 for(int i
= 0; i
< 20; i
++)
27 VERIFY_IS_APPROX(w
[i
], v
[i
]);
32 VERIFY_IS_APPROX(v
[20], x
);
34 VERIFY_IS_APPROX(v
[21], y
);
36 VERIFY_IS_APPROX(v
[22], x
);
37 VERIFY((size_t)&(v
[22]) == (size_t)&(v
[21]) + sizeof(MatrixType
));
39 // do a lot of push_back such that the vector gets internally resized
40 // (with memory reallocation)
41 MatrixType
* ref
= &w
[0];
42 for(int i
=0; i
<30 || ((ref
==&w
[0]) && i
<300); ++i
)
43 v
.push_back(w
[i
%w
.size()]);
44 for(unsigned int i
=23; i
<v
.size(); ++i
)
46 VERIFY(v
[i
]==w
[(i
-23)%w
.size()]);
50 template<typename TransformType
>
51 void check_stdvector_transform(const TransformType
&)
53 typedef typename
TransformType::MatrixType MatrixType
;
54 TransformType
x(MatrixType::Random()), y(MatrixType::Random());
55 std::vector
<TransformType
,Eigen::aligned_allocator
<TransformType
> > v(10), w(20, y
);
58 VERIFY_IS_APPROX(w
[6], v
[5]);
60 for(int i
= 0; i
< 20; i
++)
62 VERIFY_IS_APPROX(w
[i
], v
[i
]);
67 VERIFY_IS_APPROX(v
[20], x
);
69 VERIFY_IS_APPROX(v
[21], y
);
71 VERIFY_IS_APPROX(v
[22], x
);
72 VERIFY((size_t)&(v
[22]) == (size_t)&(v
[21]) + sizeof(TransformType
));
74 // do a lot of push_back such that the vector gets internally resized
75 // (with memory reallocation)
76 TransformType
* ref
= &w
[0];
77 for(int i
=0; i
<30 || ((ref
==&w
[0]) && i
<300); ++i
)
78 v
.push_back(w
[i
%w
.size()]);
79 for(unsigned int i
=23; i
<v
.size(); ++i
)
81 VERIFY(v
[i
].matrix()==w
[(i
-23)%w
.size()].matrix());
85 template<typename QuaternionType
>
86 void check_stdvector_quaternion(const QuaternionType
&)
88 typedef typename
QuaternionType::Coefficients Coefficients
;
89 QuaternionType
x(Coefficients::Random()), y(Coefficients::Random());
90 std::vector
<QuaternionType
,Eigen::aligned_allocator
<QuaternionType
> > v(10), w(20, y
);
93 VERIFY_IS_APPROX(w
[6], v
[5]);
95 for(int i
= 0; i
< 20; i
++)
97 VERIFY_IS_APPROX(w
[i
], v
[i
]);
102 VERIFY_IS_APPROX(v
[20], x
);
104 VERIFY_IS_APPROX(v
[21], y
);
106 VERIFY_IS_APPROX(v
[22], x
);
107 VERIFY((size_t)&(v
[22]) == (size_t)&(v
[21]) + sizeof(QuaternionType
));
109 // do a lot of push_back such that the vector gets internally resized
110 // (with memory reallocation)
111 QuaternionType
* ref
= &w
[0];
112 for(int i
=0; i
<30 || ((ref
==&w
[0]) && i
<300); ++i
)
113 v
.push_back(w
[i
%w
.size()]);
114 for(unsigned int i
=23; i
<v
.size(); ++i
)
116 VERIFY(v
[i
].coeffs()==w
[(i
-23)%w
.size()].coeffs());
120 void test_stdvector()
122 // some non vectorizable fixed sizes
123 CALL_SUBTEST_1(check_stdvector_matrix(Vector2f()));
124 CALL_SUBTEST_1(check_stdvector_matrix(Matrix3f()));
125 CALL_SUBTEST_2(check_stdvector_matrix(Matrix3d()));
127 // some vectorizable fixed sizes
128 CALL_SUBTEST_1(check_stdvector_matrix(Matrix2f()));
129 CALL_SUBTEST_1(check_stdvector_matrix(Vector4f()));
130 CALL_SUBTEST_1(check_stdvector_matrix(Matrix4f()));
131 CALL_SUBTEST_2(check_stdvector_matrix(Matrix4d()));
133 // some dynamic sizes
134 CALL_SUBTEST_3(check_stdvector_matrix(MatrixXd(1,1)));
135 CALL_SUBTEST_3(check_stdvector_matrix(VectorXd(20)));
136 CALL_SUBTEST_3(check_stdvector_matrix(RowVectorXf(20)));
137 CALL_SUBTEST_3(check_stdvector_matrix(MatrixXcf(10,10)));
140 CALL_SUBTEST_4(check_stdvector_transform(Projective2f()));
141 CALL_SUBTEST_4(check_stdvector_transform(Projective3f()));
142 CALL_SUBTEST_4(check_stdvector_transform(Projective3d()));
143 //CALL_SUBTEST(heck_stdvector_transform(Projective4d()));
146 CALL_SUBTEST_5(check_stdvector_quaternion(Quaternionf()));
147 CALL_SUBTEST_5(check_stdvector_quaternion(Quaterniond()));