blob: 024f97287544adcc0250d5c5da64f3755889bfa4 [file] [log] [blame]
Benoit Jacob335d3bc2009-01-09 23:26:45 +00001// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra. Eigen itself is part of the KDE project.
3//
4// Copyright (C) 2008 Gael Guennebaud <g.gael@free.fr>
5//
6// Eigen is free software; you can redistribute it and/or
7// modify it under the terms of the GNU Lesser General Public
8// License as published by the Free Software Foundation; either
9// version 3 of the License, or (at your option) any later version.
10//
11// Alternatively, you can redistribute it and/or
12// modify it under the terms of the GNU General Public License as
13// published by the Free Software Foundation; either version 2 of
14// the License, or (at your option) any later version.
15//
16// Eigen is distributed in the hope that it will be useful, but WITHOUT ANY
17// WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18// FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License or the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU Lesser General Public
22// License and a copy of the GNU General Public License along with
23// Eigen. If not, see <http://www.gnu.org/licenses/>.
24
25#include "main.h"
26#include <Eigen/StdVector>
27
28template<typename MatrixType>
Benoit Jacob0c1ef2f2009-01-10 13:10:23 +000029void check_stdvector(const MatrixType& m)
Benoit Jacob335d3bc2009-01-09 23:26:45 +000030{
Benoit Jacob0c1ef2f2009-01-10 13:10:23 +000031 int rows = m.rows();
32 int cols = m.cols();
33 MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
34 std::vector<MatrixType> v(10, MatrixType(rows,cols)), w(20, y);
Benoit Jacob335d3bc2009-01-09 23:26:45 +000035 v[5] = x;
36 w[6] = v[5];
37 VERIFY_IS_APPROX(w[6], v[5]);
38 v = w;
39 for(int i = 0; i < 20; i++)
40 {
41 VERIFY_IS_APPROX(w[i], v[i]);
42 }
Gael Guennebaud9e8f4372009-01-11 21:59:04 +000043
Benoit Jacob335d3bc2009-01-09 23:26:45 +000044 v.resize(21);
Benoit Jacob0c1ef2f2009-01-10 13:10:23 +000045 v[20].set(x);
Benoit Jacob335d3bc2009-01-09 23:26:45 +000046 VERIFY_IS_APPROX(v[20], x);
47 v.resize(22,y);
Gael Guennebaud9e8f4372009-01-11 21:59:04 +000048 VERIFY_IS_APPROX(v[21], y);
Benoit Jacob335d3bc2009-01-09 23:26:45 +000049 v.push_back(x);
50 VERIFY_IS_APPROX(v[22], x);
Benoit Jacob0c1ef2f2009-01-10 13:10:23 +000051 VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(MatrixType));
Gael Guennebaud9e8f4372009-01-11 21:59:04 +000052
53 // do a lot of push_back such that the vector gets internally resized
54 // (with memory reallocation)
55 MatrixType* ref = &w[0];
Benoit Jacob336ad582009-01-12 13:41:40 +000056 for(int i=0; i<30 || ((ref==&w[0]) && i<300); ++i)
Gael Guennebaud9e8f4372009-01-11 21:59:04 +000057 v.push_back(w[i%w.size()]);
Benoit Jacob336ad582009-01-12 13:41:40 +000058 for(unsigned int i=23; i<v.size(); ++i)
Gael Guennebaud9e8f4372009-01-11 21:59:04 +000059 {
60 VERIFY(v[i]==w[(i-23)%w.size()]);
61 }
Benoit Jacob335d3bc2009-01-09 23:26:45 +000062}
63
Benoit Jacob335d3bc2009-01-09 23:26:45 +000064void test_stdvector()
65{
66 // some non vectorizable fixed sizes
Benoit Jacob0c1ef2f2009-01-10 13:10:23 +000067 CALL_SUBTEST(check_stdvector(Vector2f()));
68 CALL_SUBTEST(check_stdvector(Matrix3f()));
69 CALL_SUBTEST(check_stdvector(Matrix3d()));
Benoit Jacob335d3bc2009-01-09 23:26:45 +000070
71 // some vectorizable fixed sizes
Benoit Jacob0c1ef2f2009-01-10 13:10:23 +000072 CALL_SUBTEST(check_stdvector(Matrix2f()));
73 CALL_SUBTEST(check_stdvector(Vector4f()));
74 CALL_SUBTEST(check_stdvector(Matrix4f()));
Gael Guennebaud9e8f4372009-01-11 21:59:04 +000075 CALL_SUBTEST(check_stdvector(Matrix4d()));
Benoit Jacob0c1ef2f2009-01-10 13:10:23 +000076
77 // some dynamic sizes
78 CALL_SUBTEST(check_stdvector(MatrixXd(1,1)));
79 CALL_SUBTEST(check_stdvector(VectorXd(20)));
80 CALL_SUBTEST(check_stdvector(RowVectorXf(20)));
81 CALL_SUBTEST(check_stdvector(MatrixXcf(10,10)));
Benoit Jacob335d3bc2009-01-09 23:26:45 +000082}