blob: e793dfca1d256cfa9a9fddff2eb7e1a4860b3a29 [file] [log] [blame]
Gael Guennebaud582b5e32017-02-17 14:10:57 +01001// This file is part of Eigen, a lightweight C++ template library
2// for linear algebra.
3//
4// Copyright (C) 2017 Gael Guennebaud <gael.guennebaud@inria.fr>
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
15template<typename MatrixType> struct Wrapper
16{
17 MatrixType m_mat;
18 inline Wrapper(const MatrixType &x) : m_mat(x) {}
19 inline operator const MatrixType& () const { return m_mat; }
20 inline operator MatrixType& () { return m_mat; }
21};
22
23template<typename MatrixType> void ctor_init1(const MatrixType& m)
24{
25 // Check logic in PlainObjectBase::_init1
26 Index rows = m.rows();
27 Index cols = m.cols();
28
29 MatrixType m0 = MatrixType::Random(rows,cols);
30
31 VERIFY_EVALUATION_COUNT( MatrixType m1(m0), 1);
32 VERIFY_EVALUATION_COUNT( MatrixType m2(m0+m0), 1);
33 VERIFY_EVALUATION_COUNT( MatrixType m2(m0.block(0,0,rows,cols)) , 1);
34
35 Wrapper<MatrixType> wrapper(m0);
36 VERIFY_EVALUATION_COUNT( MatrixType m3(wrapper) , 1);
37}
38
39
Gael Guennebaud82f0ce22018-07-17 14:46:15 +020040EIGEN_DECLARE_TEST(constructor)
Gael Guennebaud582b5e32017-02-17 14:10:57 +010041{
42 for(int i = 0; i < g_repeat; i++) {
43 CALL_SUBTEST_1( ctor_init1(Matrix<float, 1, 1>()) );
44 CALL_SUBTEST_1( ctor_init1(Matrix4d()) );
45 CALL_SUBTEST_1( ctor_init1(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
46 CALL_SUBTEST_1( ctor_init1(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
47 }
48 {
49 Matrix<Index,1,1> a(123);
50 VERIFY_IS_EQUAL(a[0], 123);
51 }
52 {
53 Matrix<Index,1,1> a(123.0);
54 VERIFY_IS_EQUAL(a[0], 123);
55 }
56 {
57 Matrix<float,1,1> a(123);
58 VERIFY_IS_EQUAL(a[0], 123.f);
59 }
60 {
61 Array<Index,1,1> a(123);
62 VERIFY_IS_EQUAL(a[0], 123);
63 }
64 {
65 Array<Index,1,1> a(123.0);
66 VERIFY_IS_EQUAL(a[0], 123);
67 }
68 {
69 Array<float,1,1> a(123);
70 VERIFY_IS_EQUAL(a[0], 123.f);
71 }
72 {
73 Array<Index,3,3> a(123);
74 VERIFY_IS_EQUAL(a(4), 123);
75 }
76 {
77 Array<Index,3,3> a(123.0);
78 VERIFY_IS_EQUAL(a(4), 123);
79 }
80 {
81 Array<float,3,3> a(123);
82 VERIFY_IS_EQUAL(a(4), 123.f);
83 }
Gael Guennebaudcf7e2e22019-03-17 21:59:30 +010084 {
85 enum { M = 12, N = 7};
86 MatrixXi m1(M,N);
87 VERIFY_IS_EQUAL(m1.rows(),M);
88 VERIFY_IS_EQUAL(m1.cols(),N);
89 ArrayXXi a1(M,N);
90 VERIFY_IS_EQUAL(a1.rows(),M);
91 VERIFY_IS_EQUAL(a1.cols(),N);
92 VectorXi v1(M);
93 VERIFY_IS_EQUAL(v1.size(),M);
94 ArrayXi a2(M);
95 VERIFY_IS_EQUAL(a2.size(),M);
96 }
Gael Guennebaud582b5e32017-02-17 14:10:57 +010097}