blob: bc780b45bed4048e368b7b1be4b860de272efaa3 [file] [log] [blame]
Gael Guennebaudf645d1f2009-01-20 16:50:47 +00001// This file is part of Eigen, a lightweight C++ template library
Benoit Jacob6347b1d2009-05-22 20:25:33 +02002// for linear algebra.
Gael Guennebaudf645d1f2009-01-20 16:50:47 +00003//
4// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
5// Copyright (C) 2008 Benoit Jacob <jacob.benoit.1@gmail.com>
6//
7// Eigen is free software; you can redistribute it and/or
8// modify it under the terms of the GNU Lesser General Public
9// License as published by the Free Software Foundation; either
10// version 3 of the License, or (at your option) any later version.
11//
12// Alternatively, you can redistribute it and/or
13// modify it under the terms of the GNU General Public License as
14// published by the Free Software Foundation; either version 2 of
15// the License, or (at your option) any later version.
16//
17// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
18// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
19// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
20// GNU General Public License for more details.
21//
22// You should have received a copy of the GNU Lesser General Public
23// License and a copy of the GNU General Public License along with
24// Eigen. If not, see <http://www.gnu.org/licenses/>.
25
26#define EIGEN_WORK_AROUND_QT_BUG_CALLING_WRONG_OPERATOR_NEW_FIXED_IN_QT_4_5
27
28#include "main.h"
29#include <QtCore/QVector>
30#include <Eigen/Geometry>
31#include <Eigen/QtAlignedMalloc>
32
33template<typename MatrixType>
34void check_qtvector_matrix(const MatrixType& m)
35{
36 int rows = m.rows();
37 int cols = m.cols();
38 MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
39 QVector<MatrixType> v(10, MatrixType(rows,cols)), w(20, y);
Gael Guennebaud98499312009-01-23 16:31:03 +000040 for(int i = 0; i < 20; i++)
41 {
42 VERIFY_IS_APPROX(w[i], y);
43 }
Gael Guennebaudf645d1f2009-01-20 16:50:47 +000044 v[5] = x;
45 w[6] = v[5];
46 VERIFY_IS_APPROX(w[6], v[5]);
47 v = w;
48 for(int i = 0; i < 20; i++)
49 {
50 VERIFY_IS_APPROX(w[i], v[i]);
51 }
52
53 v.resize(21);
Benoit Jacob5f43a422009-01-21 17:10:23 +000054 v[20] = x;
Gael Guennebaudf645d1f2009-01-20 16:50:47 +000055 VERIFY_IS_APPROX(v[20], x);
56 v.fill(y,22);
Gael Guennebaudf645d1f2009-01-20 16:50:47 +000057 VERIFY_IS_APPROX(v[21], y);
58 v.push_back(x);
59 VERIFY_IS_APPROX(v[22], x);
60 VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(MatrixType));
61
62 // do a lot of push_back such that the vector gets internally resized
63 // (with memory reallocation)
64 MatrixType* ref = &w[0];
65 for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
66 v.push_back(w[i%w.size()]);
67 for(int i=23; i<v.size(); ++i)
68 {
69 VERIFY(v[i]==w[(i-23)%w.size()]);
70 }
71}
72
73template<typename TransformType>
74void check_qtvector_transform(const TransformType&)
75{
76 typedef typename TransformType::MatrixType MatrixType;
77 TransformType x(MatrixType::Random()), y(MatrixType::Random());
78 QVector<TransformType> v(10), w(20, y);
79 v[5] = x;
80 w[6] = v[5];
81 VERIFY_IS_APPROX(w[6], v[5]);
82 v = w;
83 for(int i = 0; i < 20; i++)
84 {
85 VERIFY_IS_APPROX(w[i], v[i]);
86 }
87
88 v.resize(21);
89 v[20] = x;
90 VERIFY_IS_APPROX(v[20], x);
91 v.fill(y,22);
Gael Guennebaudf645d1f2009-01-20 16:50:47 +000092 VERIFY_IS_APPROX(v[21], y);
93 v.push_back(x);
94 VERIFY_IS_APPROX(v[22], x);
95 VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(TransformType));
96
97 // do a lot of push_back such that the vector gets internally resized
98 // (with memory reallocation)
99 TransformType* ref = &w[0];
100 for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
101 v.push_back(w[i%w.size()]);
Benoit Jacob5f43a422009-01-21 17:10:23 +0000102 for(unsigned int i=23; int(i)<v.size(); ++i)
Gael Guennebaudf645d1f2009-01-20 16:50:47 +0000103 {
104 VERIFY(v[i].matrix()==w[(i-23)%w.size()].matrix());
105 }
106}
107
108template<typename QuaternionType>
109void check_qtvector_quaternion(const QuaternionType&)
110{
111 typedef typename QuaternionType::Coefficients Coefficients;
112 QuaternionType x(Coefficients::Random()), y(Coefficients::Random());
113 QVector<QuaternionType> v(10), w(20, y);
114 v[5] = x;
115 w[6] = v[5];
116 VERIFY_IS_APPROX(w[6], v[5]);
117 v = w;
118 for(int i = 0; i < 20; i++)
119 {
120 VERIFY_IS_APPROX(w[i], v[i]);
121 }
122
123 v.resize(21);
124 v[20] = x;
125 VERIFY_IS_APPROX(v[20], x);
126 v.fill(y,22);
Gael Guennebaudf645d1f2009-01-20 16:50:47 +0000127 VERIFY_IS_APPROX(v[21], y);
128 v.push_back(x);
129 VERIFY_IS_APPROX(v[22], x);
130 VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(QuaternionType));
131
132 // do a lot of push_back such that the vector gets internally resized
133 // (with memory reallocation)
134 QuaternionType* ref = &w[0];
135 for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
136 v.push_back(w[i%w.size()]);
Benoit Jacob5f43a422009-01-21 17:10:23 +0000137 for(unsigned int i=23; int(i)<v.size(); ++i)
Gael Guennebaudf645d1f2009-01-20 16:50:47 +0000138 {
139 VERIFY(v[i].coeffs()==w[(i-23)%w.size()].coeffs());
140 }
141}
142
143void test_qtvector()
144{
145 // some non vectorizable fixed sizes
146 CALL_SUBTEST(check_qtvector_matrix(Vector2f()));
147 CALL_SUBTEST(check_qtvector_matrix(Matrix3f()));
148 CALL_SUBTEST(check_qtvector_matrix(Matrix3d()));
149
150 // some vectorizable fixed sizes
151 CALL_SUBTEST(check_qtvector_matrix(Matrix2f()));
152 CALL_SUBTEST(check_qtvector_matrix(Vector4f()));
153 CALL_SUBTEST(check_qtvector_matrix(Matrix4f()));
154 CALL_SUBTEST(check_qtvector_matrix(Matrix4d()));
155
156 // some dynamic sizes
157 CALL_SUBTEST(check_qtvector_matrix(MatrixXd(1,1)));
158 CALL_SUBTEST(check_qtvector_matrix(VectorXd(20)));
159 CALL_SUBTEST(check_qtvector_matrix(RowVectorXf(20)));
160 CALL_SUBTEST(check_qtvector_matrix(MatrixXcf(10,10)));
161
162 // some Transform
163 CALL_SUBTEST(check_qtvector_transform(Transform2f()));
164 CALL_SUBTEST(check_qtvector_transform(Transform3f()));
165 CALL_SUBTEST(check_qtvector_transform(Transform3d()));
166 //CALL_SUBTEST(check_qtvector_transform(Transform4d()));
167
168 // some Quaternion
169 CALL_SUBTEST(check_qtvector_quaternion(Quaternionf()));
170 CALL_SUBTEST(check_qtvector_quaternion(Quaternionf()));
171}