xref: /aosp_15_r20/external/eigen/test/constructor.cpp (revision bf2c37156dfe67e5dfebd6d394bad8b2ab5804d4)
1 // This file is part of Eigen, a lightweight C++ template library
2 // for linear algebra.
3 //
4 // Copyright (C) 2017 Gael Guennebaud <[email protected]>
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 
11 #define TEST_ENABLE_TEMPORARY_TRACKING
12 
13 #include "main.h"
14 
15 template<typename MatrixType> struct Wrapper
16 {
17   MatrixType m_mat;
WrapperWrapper18   inline Wrapper(const MatrixType &x) : m_mat(x) {}
operator const MatrixType&Wrapper19   inline operator const MatrixType& () const { return m_mat; }
operator MatrixType&Wrapper20   inline operator MatrixType& () { return m_mat; }
21 };
22 
23 enum my_sizes { M = 12, N = 7};
24 
ctor_init1(const MatrixType & m)25 template<typename MatrixType> void ctor_init1(const MatrixType& m)
26 {
27   // Check logic in PlainObjectBase::_init1
28   Index rows = m.rows();
29   Index cols = m.cols();
30 
31   MatrixType m0 = MatrixType::Random(rows,cols);
32 
33   VERIFY_EVALUATION_COUNT( MatrixType m1(m0), 1);
34   VERIFY_EVALUATION_COUNT( MatrixType m2(m0+m0), 1);
35   VERIFY_EVALUATION_COUNT( MatrixType m2(m0.block(0,0,rows,cols)) , 1);
36 
37   Wrapper<MatrixType> wrapper(m0);
38   VERIFY_EVALUATION_COUNT( MatrixType m3(wrapper) , 1);
39 }
40 
41 
EIGEN_DECLARE_TEST(constructor)42 EIGEN_DECLARE_TEST(constructor)
43 {
44   for(int i = 0; i < g_repeat; i++) {
45     CALL_SUBTEST_1( ctor_init1(Matrix<float, 1, 1>()) );
46     CALL_SUBTEST_1( ctor_init1(Matrix4d()) );
47     CALL_SUBTEST_1( ctor_init1(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
48     CALL_SUBTEST_1( ctor_init1(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
49   }
50   {
51     Matrix<Index,1,1> a(123);
52     VERIFY_IS_EQUAL(a[0], 123);
53   }
54   {
55     Matrix<Index,1,1> a(123.0);
56     VERIFY_IS_EQUAL(a[0], 123);
57   }
58   {
59     Matrix<float,1,1> a(123);
60     VERIFY_IS_EQUAL(a[0], 123.f);
61   }
62   {
63     Array<Index,1,1> a(123);
64     VERIFY_IS_EQUAL(a[0], 123);
65   }
66   {
67     Array<Index,1,1> a(123.0);
68     VERIFY_IS_EQUAL(a[0], 123);
69   }
70   {
71     Array<float,1,1> a(123);
72     VERIFY_IS_EQUAL(a[0], 123.f);
73   }
74   {
75     Array<Index,3,3> a(123);
76     VERIFY_IS_EQUAL(a(4), 123);
77   }
78   {
79     Array<Index,3,3> a(123.0);
80     VERIFY_IS_EQUAL(a(4), 123);
81   }
82   {
83     Array<float,3,3> a(123);
84     VERIFY_IS_EQUAL(a(4), 123.f);
85   }
86   {
87     MatrixXi m1(M,N);
88     VERIFY_IS_EQUAL(m1.rows(),M);
89     VERIFY_IS_EQUAL(m1.cols(),N);
90     ArrayXXi a1(M,N);
91     VERIFY_IS_EQUAL(a1.rows(),M);
92     VERIFY_IS_EQUAL(a1.cols(),N);
93     VectorXi v1(M);
94     VERIFY_IS_EQUAL(v1.size(),M);
95     ArrayXi a2(M);
96     VERIFY_IS_EQUAL(a2.size(),M);
97   }
98 }
99