From a8d264fa9c56e42f77e2129d4e504f5c854821c2 Mon Sep 17 00:00:00 2001 From: Eugene Zhulenev Date: Tue, 3 Sep 2019 11:38:39 -0700 Subject: Add test for const TensorMap underlying data mutation --- unsupported/test/cxx11_tensor_map.cpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'unsupported/test/cxx11_tensor_map.cpp') diff --git a/unsupported/test/cxx11_tensor_map.cpp b/unsupported/test/cxx11_tensor_map.cpp index dc8532f5c..4d4f68911 100644 --- a/unsupported/test/cxx11_tensor_map.cpp +++ b/unsupported/test/cxx11_tensor_map.cpp @@ -288,6 +288,30 @@ static void test_0d_const_tensor() VERIFY_IS_EQUAL(scalar4(), 13); } +static void test_0d_const_tensor_map() +{ + Tensor scalar1; + Tensor scalar2; + + const TensorMap > scalar3(scalar1.data()); + const TensorMap > scalar4(scalar2.data()); + + // Although TensorMap is constant, we still can write to the underlying + // storage, because we map over non-constant Tensor. + scalar3() = 7; + scalar4() = 13; + + VERIFY_IS_EQUAL(scalar1(), 7); + VERIFY_IS_EQUAL(scalar2(), 13); + + // Pointer to the underlying storage is also non-const. + scalar3.data()[0] = 8; + scalar4.data()[0] = 14; + + VERIFY_IS_EQUAL(scalar1(), 8); + VERIFY_IS_EQUAL(scalar2(), 14); +} + EIGEN_DECLARE_TEST(cxx11_tensor_map) { CALL_SUBTEST(test_0d()); @@ -299,4 +323,5 @@ EIGEN_DECLARE_TEST(cxx11_tensor_map) CALL_SUBTEST(test_casting()); CALL_SUBTEST(test_0d_const_tensor()); + CALL_SUBTEST(test_0d_const_tensor_map()); } -- cgit v1.2.3