constructor.cpp 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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@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. #define TEST_ENABLE_TEMPORARY_TRACKING
  10. #include "main.h"
  11. template<typename MatrixType> struct Wrapper
  12. {
  13. MatrixType m_mat;
  14. inline Wrapper(const MatrixType &x) : m_mat(x) {}
  15. inline operator const MatrixType& () const { return m_mat; }
  16. inline operator MatrixType& () { return m_mat; }
  17. };
  18. enum my_sizes { M = 12, N = 7};
  19. template<typename MatrixType> void ctor_init1(const MatrixType& m)
  20. {
  21. // Check logic in PlainObjectBase::_init1
  22. Index rows = m.rows();
  23. Index cols = m.cols();
  24. MatrixType m0 = MatrixType::Random(rows,cols);
  25. VERIFY_EVALUATION_COUNT( MatrixType m1(m0), 1);
  26. VERIFY_EVALUATION_COUNT( MatrixType m2(m0+m0), 1);
  27. VERIFY_EVALUATION_COUNT( MatrixType m2(m0.block(0,0,rows,cols)) , 1);
  28. Wrapper<MatrixType> wrapper(m0);
  29. VERIFY_EVALUATION_COUNT( MatrixType m3(wrapper) , 1);
  30. }
  31. void test_constructor()
  32. {
  33. for(int i = 0; i < g_repeat; i++) {
  34. CALL_SUBTEST_1( ctor_init1(Matrix<float, 1, 1>()) );
  35. CALL_SUBTEST_1( ctor_init1(Matrix4d()) );
  36. CALL_SUBTEST_1( ctor_init1(MatrixXcf(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  37. CALL_SUBTEST_1( ctor_init1(MatrixXi(internal::random<int>(1,EIGEN_TEST_MAX_SIZE), internal::random<int>(1,EIGEN_TEST_MAX_SIZE))) );
  38. }
  39. {
  40. Matrix<Index,1,1> a(123);
  41. VERIFY_IS_EQUAL(a[0], 123);
  42. }
  43. {
  44. Matrix<Index,1,1> a(123.0);
  45. VERIFY_IS_EQUAL(a[0], 123);
  46. }
  47. {
  48. Matrix<float,1,1> a(123);
  49. VERIFY_IS_EQUAL(a[0], 123.f);
  50. }
  51. {
  52. Array<Index,1,1> a(123);
  53. VERIFY_IS_EQUAL(a[0], 123);
  54. }
  55. {
  56. Array<Index,1,1> a(123.0);
  57. VERIFY_IS_EQUAL(a[0], 123);
  58. }
  59. {
  60. Array<float,1,1> a(123);
  61. VERIFY_IS_EQUAL(a[0], 123.f);
  62. }
  63. {
  64. Array<Index,3,3> a(123);
  65. VERIFY_IS_EQUAL(a(4), 123);
  66. }
  67. {
  68. Array<Index,3,3> a(123.0);
  69. VERIFY_IS_EQUAL(a(4), 123);
  70. }
  71. {
  72. Array<float,3,3> a(123);
  73. VERIFY_IS_EQUAL(a(4), 123.f);
  74. }
  75. {
  76. MatrixXi m1(M,N);
  77. VERIFY_IS_EQUAL(m1.rows(),M);
  78. VERIFY_IS_EQUAL(m1.cols(),N);
  79. ArrayXXi a1(M,N);
  80. VERIFY_IS_EQUAL(a1.rows(),M);
  81. VERIFY_IS_EQUAL(a1.cols(),N);
  82. VectorXi v1(M);
  83. VERIFY_IS_EQUAL(v1.size(),M);
  84. ArrayXi a2(M);
  85. VERIFY_IS_EQUAL(a2.size(),M);
  86. }
  87. }