1 // This file is part of Eigen, a lightweight C++ template library 2 // for linear algebra. 3 // 4 // Copyright (C) 2017 Gael Guennebaud <gael.guennebaud (at) 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 15 template<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 23 template<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 40 void test_constructor() 41 { 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 } 84 } 85