cxx11_tensor_move.cpp 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // This file is part of Eigen, a lightweight C++ template library
  2. // for linear algebra.
  3. //
  4. // Copyright (C) 2017 Viktor Csomor <viktor.csomor@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. #include "main.h"
  10. #include <Eigen/CXX11/Tensor>
  11. #include <utility>
  12. using Eigen::Tensor;
  13. using Eigen::RowMajor;
  14. static void calc_indices(int i, int& x, int& y, int& z)
  15. {
  16. x = i / 4;
  17. y = (i % 4) / 2;
  18. z = i % 2;
  19. }
  20. static void test_move()
  21. {
  22. int x;
  23. int y;
  24. int z;
  25. Tensor<int,3> tensor1(2, 2, 2);
  26. Tensor<int,3,RowMajor> tensor2(2, 2, 2);
  27. for (int i = 0; i < 8; i++)
  28. {
  29. calc_indices(i, x, y, z);
  30. tensor1(x,y,z) = i;
  31. tensor2(x,y,z) = 2 * i;
  32. }
  33. // Invokes the move constructor.
  34. Tensor<int,3> moved_tensor1 = std::move(tensor1);
  35. Tensor<int,3,RowMajor> moved_tensor2 = std::move(tensor2);
  36. VERIFY_IS_EQUAL(tensor1.size(), 0);
  37. VERIFY_IS_EQUAL(tensor2.size(), 0);
  38. for (int i = 0; i < 8; i++)
  39. {
  40. calc_indices(i, x, y, z);
  41. VERIFY_IS_EQUAL(moved_tensor1(x,y,z), i);
  42. VERIFY_IS_EQUAL(moved_tensor2(x,y,z), 2 * i);
  43. }
  44. Tensor<int,3> moved_tensor3(2,2,2);
  45. Tensor<int,3,RowMajor> moved_tensor4(2,2,2);
  46. moved_tensor3.setZero();
  47. moved_tensor4.setZero();
  48. // Invokes the move assignment operator.
  49. moved_tensor3 = std::move(moved_tensor1);
  50. moved_tensor4 = std::move(moved_tensor2);
  51. for (int i = 0; i < 8; i++)
  52. {
  53. calc_indices(i, x, y, z);
  54. VERIFY_IS_EQUAL(moved_tensor3(x,y,z), i);
  55. VERIFY_IS_EQUAL(moved_tensor4(x,y,z), 2 * i);
  56. }
  57. }
  58. EIGEN_DECLARE_TEST(cxx11_tensor_move)
  59. {
  60. CALL_SUBTEST(test_move());
  61. }