xref: /aosp_15_r20/external/eigen/test/stddeque.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1*bf2c3715SXin Li // This file is part of Eigen, a lightweight C++ template library
2*bf2c3715SXin Li // for linear algebra.
3*bf2c3715SXin Li //
4*bf2c3715SXin Li // Copyright (C) 2008 Benoit Jacob <[email protected]>
5*bf2c3715SXin Li // Copyright (C) 2010 Hauke Heibel <[email protected]>
6*bf2c3715SXin Li //
7*bf2c3715SXin Li // This Source Code Form is subject to the terms of the Mozilla
8*bf2c3715SXin Li // Public License v. 2.0. If a copy of the MPL was not distributed
9*bf2c3715SXin Li // with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
10*bf2c3715SXin Li 
11*bf2c3715SXin Li #include "main.h"
12*bf2c3715SXin Li #include <Eigen/StdDeque>
13*bf2c3715SXin Li #include <Eigen/Geometry>
14*bf2c3715SXin Li 
15*bf2c3715SXin Li template<typename MatrixType>
check_stddeque_matrix(const MatrixType & m)16*bf2c3715SXin Li void check_stddeque_matrix(const MatrixType& m)
17*bf2c3715SXin Li {
18*bf2c3715SXin Li   Index rows = m.rows();
19*bf2c3715SXin Li   Index cols = m.cols();
20*bf2c3715SXin Li   MatrixType x = MatrixType::Random(rows,cols), y = MatrixType::Random(rows,cols);
21*bf2c3715SXin Li   std::deque<MatrixType,Eigen::aligned_allocator<MatrixType> > v(10, MatrixType::Zero(rows,cols)), w(20, y);
22*bf2c3715SXin Li   v.front() = x;
23*bf2c3715SXin Li   w.front() = w.back();
24*bf2c3715SXin Li   VERIFY_IS_APPROX(w.front(), w.back());
25*bf2c3715SXin Li   v = w;
26*bf2c3715SXin Li 
27*bf2c3715SXin Li   typename std::deque<MatrixType,Eigen::aligned_allocator<MatrixType> >::iterator vi = v.begin();
28*bf2c3715SXin Li   typename std::deque<MatrixType,Eigen::aligned_allocator<MatrixType> >::iterator wi = w.begin();
29*bf2c3715SXin Li   for(int i = 0; i < 20; i++)
30*bf2c3715SXin Li   {
31*bf2c3715SXin Li     VERIFY_IS_APPROX(*vi, *wi);
32*bf2c3715SXin Li     ++vi;
33*bf2c3715SXin Li     ++wi;
34*bf2c3715SXin Li   }
35*bf2c3715SXin Li 
36*bf2c3715SXin Li   v.resize(21,MatrixType::Zero(rows,cols));
37*bf2c3715SXin Li   v.back() = x;
38*bf2c3715SXin Li   VERIFY_IS_APPROX(v.back(), x);
39*bf2c3715SXin Li   v.resize(22,y);
40*bf2c3715SXin Li   VERIFY_IS_APPROX(v.back(), y);
41*bf2c3715SXin Li   v.push_back(x);
42*bf2c3715SXin Li   VERIFY_IS_APPROX(v.back(), x);
43*bf2c3715SXin Li }
44*bf2c3715SXin Li 
45*bf2c3715SXin Li template<typename TransformType>
check_stddeque_transform(const TransformType &)46*bf2c3715SXin Li void check_stddeque_transform(const TransformType&)
47*bf2c3715SXin Li {
48*bf2c3715SXin Li   typedef typename TransformType::MatrixType MatrixType;
49*bf2c3715SXin Li   TransformType x(MatrixType::Random()), y(MatrixType::Random()), ti=TransformType::Identity();
50*bf2c3715SXin Li   std::deque<TransformType,Eigen::aligned_allocator<TransformType> > v(10,ti), w(20, y);
51*bf2c3715SXin Li   v.front() = x;
52*bf2c3715SXin Li   w.front() = w.back();
53*bf2c3715SXin Li   VERIFY_IS_APPROX(w.front(), w.back());
54*bf2c3715SXin Li   v = w;
55*bf2c3715SXin Li 
56*bf2c3715SXin Li   typename std::deque<TransformType,Eigen::aligned_allocator<TransformType> >::iterator vi = v.begin();
57*bf2c3715SXin Li   typename std::deque<TransformType,Eigen::aligned_allocator<TransformType> >::iterator wi = w.begin();
58*bf2c3715SXin Li   for(int i = 0; i < 20; i++)
59*bf2c3715SXin Li   {
60*bf2c3715SXin Li     VERIFY_IS_APPROX(*vi, *wi);
61*bf2c3715SXin Li     ++vi;
62*bf2c3715SXin Li     ++wi;
63*bf2c3715SXin Li   }
64*bf2c3715SXin Li 
65*bf2c3715SXin Li   v.resize(21,ti);
66*bf2c3715SXin Li   v.back() = x;
67*bf2c3715SXin Li   VERIFY_IS_APPROX(v.back(), x);
68*bf2c3715SXin Li   v.resize(22,y);
69*bf2c3715SXin Li   VERIFY_IS_APPROX(v.back(), y);
70*bf2c3715SXin Li   v.push_back(x);
71*bf2c3715SXin Li   VERIFY_IS_APPROX(v.back(), x);
72*bf2c3715SXin Li }
73*bf2c3715SXin Li 
74*bf2c3715SXin Li template<typename QuaternionType>
check_stddeque_quaternion(const QuaternionType &)75*bf2c3715SXin Li void check_stddeque_quaternion(const QuaternionType&)
76*bf2c3715SXin Li {
77*bf2c3715SXin Li   typedef typename QuaternionType::Coefficients Coefficients;
78*bf2c3715SXin Li   QuaternionType x(Coefficients::Random()), y(Coefficients::Random()), qi=QuaternionType::Identity();
79*bf2c3715SXin Li   std::deque<QuaternionType,Eigen::aligned_allocator<QuaternionType> > v(10,qi), w(20, y);
80*bf2c3715SXin Li   v.front() = x;
81*bf2c3715SXin Li   w.front() = w.back();
82*bf2c3715SXin Li   VERIFY_IS_APPROX(w.front(), w.back());
83*bf2c3715SXin Li   v = w;
84*bf2c3715SXin Li 
85*bf2c3715SXin Li   typename std::deque<QuaternionType,Eigen::aligned_allocator<QuaternionType> >::iterator vi = v.begin();
86*bf2c3715SXin Li   typename std::deque<QuaternionType,Eigen::aligned_allocator<QuaternionType> >::iterator wi = w.begin();
87*bf2c3715SXin Li   for(int i = 0; i < 20; i++)
88*bf2c3715SXin Li   {
89*bf2c3715SXin Li     VERIFY_IS_APPROX(*vi, *wi);
90*bf2c3715SXin Li     ++vi;
91*bf2c3715SXin Li     ++wi;
92*bf2c3715SXin Li   }
93*bf2c3715SXin Li 
94*bf2c3715SXin Li   v.resize(21,qi);
95*bf2c3715SXin Li   v.back() = x;
96*bf2c3715SXin Li   VERIFY_IS_APPROX(v.back(), x);
97*bf2c3715SXin Li   v.resize(22,y);
98*bf2c3715SXin Li   VERIFY_IS_APPROX(v.back(), y);
99*bf2c3715SXin Li   v.push_back(x);
100*bf2c3715SXin Li   VERIFY_IS_APPROX(v.back(), x);
101*bf2c3715SXin Li }
102*bf2c3715SXin Li 
EIGEN_DECLARE_TEST(stddeque)103*bf2c3715SXin Li EIGEN_DECLARE_TEST(stddeque)
104*bf2c3715SXin Li {
105*bf2c3715SXin Li   // some non vectorizable fixed sizes
106*bf2c3715SXin Li   CALL_SUBTEST_1(check_stddeque_matrix(Vector2f()));
107*bf2c3715SXin Li   CALL_SUBTEST_1(check_stddeque_matrix(Matrix3f()));
108*bf2c3715SXin Li   CALL_SUBTEST_2(check_stddeque_matrix(Matrix3d()));
109*bf2c3715SXin Li 
110*bf2c3715SXin Li   // some vectorizable fixed sizes
111*bf2c3715SXin Li   CALL_SUBTEST_1(check_stddeque_matrix(Matrix2f()));
112*bf2c3715SXin Li   CALL_SUBTEST_1(check_stddeque_matrix(Vector4f()));
113*bf2c3715SXin Li   CALL_SUBTEST_1(check_stddeque_matrix(Matrix4f()));
114*bf2c3715SXin Li   CALL_SUBTEST_2(check_stddeque_matrix(Matrix4d()));
115*bf2c3715SXin Li 
116*bf2c3715SXin Li   // some dynamic sizes
117*bf2c3715SXin Li   CALL_SUBTEST_3(check_stddeque_matrix(MatrixXd(1,1)));
118*bf2c3715SXin Li   CALL_SUBTEST_3(check_stddeque_matrix(VectorXd(20)));
119*bf2c3715SXin Li   CALL_SUBTEST_3(check_stddeque_matrix(RowVectorXf(20)));
120*bf2c3715SXin Li   CALL_SUBTEST_3(check_stddeque_matrix(MatrixXcf(10,10)));
121*bf2c3715SXin Li 
122*bf2c3715SXin Li   // some Transform
123*bf2c3715SXin Li   CALL_SUBTEST_4(check_stddeque_transform(Affine2f()));
124*bf2c3715SXin Li   CALL_SUBTEST_4(check_stddeque_transform(Affine3f()));
125*bf2c3715SXin Li   CALL_SUBTEST_4(check_stddeque_transform(Affine3d()));
126*bf2c3715SXin Li 
127*bf2c3715SXin Li   // some Quaternion
128*bf2c3715SXin Li   CALL_SUBTEST_5(check_stddeque_quaternion(Quaternionf()));
129*bf2c3715SXin Li   CALL_SUBTEST_5(check_stddeque_quaternion(Quaterniond()));
130*bf2c3715SXin Li }
131