blob: fa928ea4fe95700c97198015e4fdb26ff4a87bdc [file] [log] [blame]
Brian Silverman72890c22015-09-19 14:37:37 -04001// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
5//
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/.
9
10#include "main.h"
11#include <Eigen/StdVector>
12#include <Eigen/Geometry>
13
14template<typename MatrixType>
15void check_stdvector_matrix(const MatrixType& m)
16{
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);
21 v[5] = x;
22 w[6] = v[5];
23 VERIFY_IS_APPROX(w[6], v[5]);
24 v = w;
25 for(int i = 0; i < 20; i++)
26 {
27 VERIFY_IS_APPROX(w[i], v[i]);
28 }
29
30 v.resize(21);
31 v[20] = x;
32 VERIFY_IS_APPROX(v[20], x);
33 v.resize(22,y);
34 VERIFY_IS_APPROX(v[21], y);
35 v.push_back(x);
36 VERIFY_IS_APPROX(v[22], x);
Austin Schuh189376f2018-12-20 22:11:15 +110037 VERIFY((internal::UIntPtr)&(v[22]) == (internal::UIntPtr)&(v[21]) + sizeof(MatrixType));
Brian Silverman72890c22015-09-19 14:37:37 -040038
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)
45 {
46 VERIFY(v[i]==w[(i-23)%w.size()]);
47 }
48}
49
50template<typename TransformType>
51void check_stdvector_transform(const TransformType&)
52{
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);
56 v[5] = x;
57 w[6] = v[5];
58 VERIFY_IS_APPROX(w[6], v[5]);
59 v = w;
60 for(int i = 0; i < 20; i++)
61 {
62 VERIFY_IS_APPROX(w[i], v[i]);
63 }
64
65 v.resize(21);
66 v[20] = x;
67 VERIFY_IS_APPROX(v[20], x);
68 v.resize(22,y);
69 VERIFY_IS_APPROX(v[21], y);
70 v.push_back(x);
71 VERIFY_IS_APPROX(v[22], x);
Austin Schuh189376f2018-12-20 22:11:15 +110072 VERIFY((internal::UIntPtr)&(v[22]) == (internal::UIntPtr)&(v[21]) + sizeof(TransformType));
Brian Silverman72890c22015-09-19 14:37:37 -040073
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)
80 {
81 VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix());
82 }
83}
84
85template<typename QuaternionType>
86void check_stdvector_quaternion(const QuaternionType&)
87{
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);
91 v[5] = x;
92 w[6] = v[5];
93 VERIFY_IS_APPROX(w[6], v[5]);
94 v = w;
95 for(int i = 0; i < 20; i++)
96 {
97 VERIFY_IS_APPROX(w[i], v[i]);
98 }
99
100 v.resize(21);
101 v[20] = x;
102 VERIFY_IS_APPROX(v[20], x);
103 v.resize(22,y);
104 VERIFY_IS_APPROX(v[21], y);
105 v.push_back(x);
106 VERIFY_IS_APPROX(v[22], x);
Austin Schuh189376f2018-12-20 22:11:15 +1100107 VERIFY((internal::UIntPtr)&(v[22]) == (internal::UIntPtr)&(v[21]) + sizeof(QuaternionType));
Brian Silverman72890c22015-09-19 14:37:37 -0400108
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)
115 {
116 VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs());
117 }
118}
119
Austin Schuh189376f2018-12-20 22:11:15 +1100120// the code below triggered an invalid warning with gcc >= 7
121// eigen/Eigen/src/Core/util/Memory.h:189:12: warning: argument 1 value '18446744073709551612' exceeds maximum object size 9223372036854775807
122// This has been reported to gcc there: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87544
123void std_vector_gcc_warning()
124{
125 typedef Eigen::Vector3f T;
126 std::vector<T, Eigen::aligned_allocator<T> > v;
127 v.push_back(T());
128}
129
Brian Silverman72890c22015-09-19 14:37:37 -0400130void test_stdvector()
131{
132 // some non vectorizable fixed sizes
133 CALL_SUBTEST_1(check_stdvector_matrix(Vector2f()));
134 CALL_SUBTEST_1(check_stdvector_matrix(Matrix3f()));
135 CALL_SUBTEST_2(check_stdvector_matrix(Matrix3d()));
136
137 // some vectorizable fixed sizes
138 CALL_SUBTEST_1(check_stdvector_matrix(Matrix2f()));
139 CALL_SUBTEST_1(check_stdvector_matrix(Vector4f()));
140 CALL_SUBTEST_1(check_stdvector_matrix(Matrix4f()));
141 CALL_SUBTEST_2(check_stdvector_matrix(Matrix4d()));
142
143 // some dynamic sizes
144 CALL_SUBTEST_3(check_stdvector_matrix(MatrixXd(1,1)));
145 CALL_SUBTEST_3(check_stdvector_matrix(VectorXd(20)));
146 CALL_SUBTEST_3(check_stdvector_matrix(RowVectorXf(20)));
147 CALL_SUBTEST_3(check_stdvector_matrix(MatrixXcf(10,10)));
148
149 // some Transform
150 CALL_SUBTEST_4(check_stdvector_transform(Projective2f()));
151 CALL_SUBTEST_4(check_stdvector_transform(Projective3f()));
152 CALL_SUBTEST_4(check_stdvector_transform(Projective3d()));
153 //CALL_SUBTEST(heck_stdvector_transform(Projective4d()));
154
155 // some Quaternion
156 CALL_SUBTEST_5(check_stdvector_quaternion(Quaternionf()));
157 CALL_SUBTEST_5(check_stdvector_quaternion(Quaterniond()));
158}