rvalue_types.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2013 Hauke Heibel <hauke.heibel@gmail.com>
  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 EIGEN_RUNTIME_NO_MALLOC
  10. #include "main.h"
  11. #include <Eigen/Core>
  12. using internal::UIntPtr;
  13. #if EIGEN_HAS_RVALUE_REFERENCES
  14. template <typename MatrixType>
  15. void rvalue_copyassign(const MatrixType& m)
  16. {
  17. typedef typename internal::traits<MatrixType>::Scalar Scalar;
  18. // create a temporary which we are about to destroy by moving
  19. MatrixType tmp = m;
  20. UIntPtr src_address = reinterpret_cast<UIntPtr>(tmp.data());
  21. Eigen::internal::set_is_malloc_allowed(false); // moving from an rvalue reference shall never allocate
  22. // move the temporary to n
  23. MatrixType n = std::move(tmp);
  24. UIntPtr dst_address = reinterpret_cast<UIntPtr>(n.data());
  25. if (MatrixType::RowsAtCompileTime==Dynamic|| MatrixType::ColsAtCompileTime==Dynamic)
  26. {
  27. // verify that we actually moved the guts
  28. VERIFY_IS_EQUAL(src_address, dst_address);
  29. VERIFY_IS_EQUAL(tmp.size(), 0);
  30. VERIFY_IS_EQUAL(reinterpret_cast<UIntPtr>(tmp.data()), UIntPtr(0));
  31. }
  32. // verify that the content did not change
  33. Scalar abs_diff = (m-n).array().abs().sum();
  34. VERIFY_IS_EQUAL(abs_diff, Scalar(0));
  35. Eigen::internal::set_is_malloc_allowed(true);
  36. }
  37. template<typename TranspositionsType>
  38. void rvalue_transpositions(Index rows)
  39. {
  40. typedef typename TranspositionsType::IndicesType PermutationVectorType;
  41. PermutationVectorType vec;
  42. randomPermutationVector(vec, rows);
  43. TranspositionsType t0(vec);
  44. Eigen::internal::set_is_malloc_allowed(false); // moving from an rvalue reference shall never allocate
  45. UIntPtr t0_address = reinterpret_cast<UIntPtr>(t0.indices().data());
  46. // Move constructors:
  47. TranspositionsType t1 = std::move(t0);
  48. UIntPtr t1_address = reinterpret_cast<UIntPtr>(t1.indices().data());
  49. VERIFY_IS_EQUAL(t0_address, t1_address);
  50. // t0 must be de-allocated:
  51. VERIFY_IS_EQUAL(t0.size(), 0);
  52. VERIFY_IS_EQUAL(reinterpret_cast<UIntPtr>(t0.indices().data()), UIntPtr(0));
  53. // Move assignment:
  54. t0 = std::move(t1);
  55. t0_address = reinterpret_cast<UIntPtr>(t0.indices().data());
  56. VERIFY_IS_EQUAL(t0_address, t1_address);
  57. // t1 must be de-allocated:
  58. VERIFY_IS_EQUAL(t1.size(), 0);
  59. VERIFY_IS_EQUAL(reinterpret_cast<UIntPtr>(t1.indices().data()), UIntPtr(0));
  60. Eigen::internal::set_is_malloc_allowed(true);
  61. }
  62. #else
  63. template <typename MatrixType>
  64. void rvalue_copyassign(const MatrixType&) {}
  65. template<typename TranspositionsType>
  66. void rvalue_transpositions(Index) {}
  67. #endif
  68. void test_rvalue_types()
  69. {
  70. for(int i = 0; i < g_repeat; i++) {
  71. CALL_SUBTEST_1(rvalue_copyassign( MatrixXf::Random(50,50).eval() ));
  72. CALL_SUBTEST_1(rvalue_copyassign( ArrayXXf::Random(50,50).eval() ));
  73. CALL_SUBTEST_1(rvalue_copyassign( Matrix<float,1,Dynamic>::Random(50).eval() ));
  74. CALL_SUBTEST_1(rvalue_copyassign( Array<float,1,Dynamic>::Random(50).eval() ));
  75. CALL_SUBTEST_1(rvalue_copyassign( Matrix<float,Dynamic,1>::Random(50).eval() ));
  76. CALL_SUBTEST_1(rvalue_copyassign( Array<float,Dynamic,1>::Random(50).eval() ));
  77. CALL_SUBTEST_2(rvalue_copyassign( Array<float,2,1>::Random().eval() ));
  78. CALL_SUBTEST_2(rvalue_copyassign( Array<float,3,1>::Random().eval() ));
  79. CALL_SUBTEST_2(rvalue_copyassign( Array<float,4,1>::Random().eval() ));
  80. CALL_SUBTEST_2(rvalue_copyassign( Array<float,2,2>::Random().eval() ));
  81. CALL_SUBTEST_2(rvalue_copyassign( Array<float,3,3>::Random().eval() ));
  82. CALL_SUBTEST_2(rvalue_copyassign( Array<float,4,4>::Random().eval() ));
  83. CALL_SUBTEST_3((rvalue_transpositions<PermutationMatrix<Dynamic, Dynamic, int> >(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))));
  84. CALL_SUBTEST_3((rvalue_transpositions<PermutationMatrix<Dynamic, Dynamic, Index> >(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))));
  85. CALL_SUBTEST_4((rvalue_transpositions<Transpositions<Dynamic, Dynamic, int> >(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))));
  86. CALL_SUBTEST_4((rvalue_transpositions<Transpositions<Dynamic, Dynamic, Index> >(internal::random<int>(1,EIGEN_TEST_MAX_SIZE))));
  87. }
  88. }