blob: e2b7bd061adfbabe099f0cc2953092f4d3d4825b [file] [log] [blame]
Benoit Jacob335d3bc2009-01-09 23:26:45 +00001// This file is part of Eigen, a lightweight C++ template library
Benoit Jacob6347b1d2009-05-22 20:25:33 +02002// for linear algebra.
Benoit Jacob335d3bc2009-01-09 23:26:45 +00003//
Gael Guennebaudf645d1f2009-01-20 16:50:47 +00004// Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
Benoit Jacob335d3bc2009-01-09 23:26:45 +00005//
Benoit Jacob69124cf2012-07-13 14:42:47 -04006// 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/.
Benoit Jacob335d3bc2009-01-09 23:26:45 +00009
Gael Guennebaud3009d792009-02-07 11:16:15 +000010#include "main.h"
Benoit Jacobeac79b62009-05-09 03:41:17 +000011#include <Eigen/StdVector>
Benoit Jacob4d44ca22009-01-12 16:06:04 +000012#include <Eigen/Geometry>
Benoit Jacob335d3bc2009-01-09 23:26:45 +000013
14template<typename MatrixType>
Benoit Jacob4d44ca22009-01-12 16:06:04 +000015void check_stdvector_matrix(const MatrixType& m)
Benoit Jacob335d3bc2009-01-09 23:26:45 +000016{
Gael Guennebaud12e1ebb2018-07-12 17:16:40 +020017 Index rows = m.rows();
18 Index cols = m.cols();
Benoit Jacob0c1ef2f2009-01-10 13:10:23 +000019 MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
Gael Guennebaud469b8aa2009-05-03 15:39:37 +000020 std::vector<MatrixType,Eigen::aligned_allocator<MatrixType> > v(10, MatrixType(rows,cols)), w(20, y);
Benoit Jacob335d3bc2009-01-09 23:26:45 +000021 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 }
Gael Guennebaud9e8f4372009-01-11 21:59:04 +000029
Benoit Jacob335d3bc2009-01-09 23:26:45 +000030 v.resize(21);
Benoit Jacob5f43a422009-01-21 17:10:23 +000031 v[20] = x;
Benoit Jacob335d3bc2009-01-09 23:26:45 +000032 VERIFY_IS_APPROX(v[20], x);
33 v.resize(22,y);
Gael Guennebaud9e8f4372009-01-11 21:59:04 +000034 VERIFY_IS_APPROX(v[21], y);
Benoit Jacob335d3bc2009-01-09 23:26:45 +000035 v.push_back(x);
36 VERIFY_IS_APPROX(v[22], x);
Gael Guennebaudfdcad682016-05-26 17:41:28 +020037 VERIFY((internal::UIntPtr)&(v[22]) == (internal::UIntPtr)&(v[21]) + sizeof(MatrixType));
Gael Guennebaud9e8f4372009-01-11 21:59:04 +000038
39 // do a lot of push_back such that the vector gets internally resized
40 // (with memory reallocation)
41 MatrixType* ref = &w[0];
Benoit Jacob336ad582009-01-12 13:41:40 +000042 for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
Gael Guennebaud9e8f4372009-01-11 21:59:04 +000043 v.push_back(w[i%w.size()]);
Benoit Jacob336ad582009-01-12 13:41:40 +000044 for(unsigned int i=23; i<v.size(); ++i)
Gael Guennebaud9e8f4372009-01-11 21:59:04 +000045 {
46 VERIFY(v[i]==w[(i-23)%w.size()]);
47 }
Benoit Jacob335d3bc2009-01-09 23:26:45 +000048}
49
Benoit Jacob4d44ca22009-01-12 16:06:04 +000050template<typename TransformType>
51void check_stdvector_transform(const TransformType&)
52{
53 typedef typename TransformType::MatrixType MatrixType;
54 TransformType x(MatrixType::Random()), y(MatrixType::Random());
Gael Guennebaud469b8aa2009-05-03 15:39:37 +000055 std::vector<TransformType,Eigen::aligned_allocator<TransformType> > v(10), w(20, y);
Benoit Jacob4d44ca22009-01-12 16:06:04 +000056 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);
Gael Guennebaudfdcad682016-05-26 17:41:28 +020072 VERIFY((internal::UIntPtr)&(v[22]) == (internal::UIntPtr)&(v[21]) + sizeof(TransformType));
Benoit Jacob4d44ca22009-01-12 16:06:04 +000073
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());
Gael Guennebaud469b8aa2009-05-03 15:39:37 +000090 std::vector<QuaternionType,Eigen::aligned_allocator<QuaternionType> > v(10), w(20, y);
Benoit Jacob4d44ca22009-01-12 16:06:04 +000091 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);
Gael Guennebaudfdcad682016-05-26 17:41:28 +0200107 VERIFY((internal::UIntPtr)&(v[22]) == (internal::UIntPtr)&(v[21]) + sizeof(QuaternionType));
Benoit Jacob4d44ca22009-01-12 16:06:04 +0000108
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
Gael Guennebaud82f0ce22018-07-17 14:46:15 +0200120EIGEN_DECLARE_TEST(stdvector)
Benoit Jacob335d3bc2009-01-09 23:26:45 +0000121{
122 // some non vectorizable fixed sizes
Benoit Jacob2840ac72009-10-28 18:19:29 -0400123 CALL_SUBTEST_1(check_stdvector_matrix(Vector2f()));
124 CALL_SUBTEST_1(check_stdvector_matrix(Matrix3f()));
125 CALL_SUBTEST_2(check_stdvector_matrix(Matrix3d()));
Benoit Jacob335d3bc2009-01-09 23:26:45 +0000126
127 // some vectorizable fixed sizes
Benoit Jacob2840ac72009-10-28 18:19:29 -0400128 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()));
Benoit Jacob0c1ef2f2009-01-10 13:10:23 +0000132
133 // some dynamic sizes
Benoit Jacob2840ac72009-10-28 18:19:29 -0400134 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)));
Benoit Jacob4d44ca22009-01-12 16:06:04 +0000138
139 // some Transform
Hauke Heibel85fdcdf2010-08-17 20:03:50 +0200140 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()));
Benoit Jacob4d44ca22009-01-12 16:06:04 +0000144
145 // some Quaternion
Benoit Jacob2840ac72009-10-28 18:19:29 -0400146 CALL_SUBTEST_5(check_stdvector_quaternion(Quaternionf()));
147 CALL_SUBTEST_5(check_stdvector_quaternion(Quaterniond()));
Benoit Jacob335d3bc2009-01-09 23:26:45 +0000148}