cxx11_tensor_move.cpp (1734B)
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 10 #include "main.h" 11 12 #include <Eigen/CXX11/Tensor> 13 #include <utility> 14 15 using Eigen::Tensor; 16 using Eigen::RowMajor; 17 18 static void calc_indices(int i, int& x, int& y, int& z) 19 { 20 x = i / 4; 21 y = (i % 4) / 2; 22 z = i % 2; 23 } 24 25 static void test_move() 26 { 27 int x; 28 int y; 29 int z; 30 31 Tensor<int,3> tensor1(2, 2, 2); 32 Tensor<int,3,RowMajor> tensor2(2, 2, 2); 33 34 for (int i = 0; i < 8; i++) 35 { 36 calc_indices(i, x, y, z); 37 tensor1(x,y,z) = i; 38 tensor2(x,y,z) = 2 * i; 39 } 40 41 // Invokes the move constructor. 42 Tensor<int,3> moved_tensor1 = std::move(tensor1); 43 Tensor<int,3,RowMajor> moved_tensor2 = std::move(tensor2); 44 45 VERIFY_IS_EQUAL(tensor1.size(), 0); 46 VERIFY_IS_EQUAL(tensor2.size(), 0); 47 48 for (int i = 0; i < 8; i++) 49 { 50 calc_indices(i, x, y, z); 51 VERIFY_IS_EQUAL(moved_tensor1(x,y,z), i); 52 VERIFY_IS_EQUAL(moved_tensor2(x,y,z), 2 * i); 53 } 54 55 Tensor<int,3> moved_tensor3(2,2,2); 56 Tensor<int,3,RowMajor> moved_tensor4(2,2,2); 57 58 moved_tensor3.setZero(); 59 moved_tensor4.setZero(); 60 61 // Invokes the move assignment operator. 62 moved_tensor3 = std::move(moved_tensor1); 63 moved_tensor4 = std::move(moved_tensor2); 64 65 for (int i = 0; i < 8; i++) 66 { 67 calc_indices(i, x, y, z); 68 VERIFY_IS_EQUAL(moved_tensor3(x,y,z), i); 69 VERIFY_IS_EQUAL(moved_tensor4(x,y,z), 2 * i); 70 } 71 } 72 73 EIGEN_DECLARE_TEST(cxx11_tensor_move) 74 { 75 CALL_SUBTEST(test_move()); 76 }