source: pacpussensors/trunk/Vislab/lib3dv/eigen/test/stdvector.cpp@ 139

Last change on this file since 139 was 136, checked in by ldecherf, 8 years ago

Doc

File size: 4.5 KB
Line 
1// 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);
37 VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(MatrixType));
38
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);
72 VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(TransformType));
73
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);
107 VERIFY((size_t)&(v[22]) == (size_t)&(v[21]) + sizeof(QuaternionType));
108
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
120void test_stdvector()
121{
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()));
126
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()));
132
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)));
138
139 // some Transform
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()));
144
145 // some Quaternion
146 CALL_SUBTEST_5(check_stdvector_quaternion(Quaternionf()));
147 CALL_SUBTEST_5(check_stdvector_quaternion(Quaterniond()));
148}
Note: See TracBrowser for help on using the repository browser.