From 4b7cf7ff82a5bfa252dd2e00b449073272482d65 Mon Sep 17 00:00:00 2001 From: Gael Guennebaud Date: Thu, 17 Jan 2019 17:35:32 +0100 Subject: Extend reshaped unit tests and remove useless const_cast --- test/reshape.cpp | 48 ++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 38 insertions(+), 10 deletions(-) (limited to 'test/reshape.cpp') diff --git a/test/reshape.cpp b/test/reshape.cpp index 14a02bb3b..7b16742a2 100644 --- a/test/reshape.cpp +++ b/test/reshape.cpp @@ -49,10 +49,25 @@ void check_auto_reshape4x4(MatType m) VERIFY(is_same_eq(m.template reshaped(AutoSize, fix< 1> ), m.template reshaped(v16, fix< 1>))); } +template +void check_direct_access_reshape4x4(MatType , internal::FixedInt) {} + +template +void check_direct_access_reshape4x4(MatType m, internal::FixedInt<0>) { + VERIFY_IS_EQUAL(m.reshaped( 1, 16).data(), m.data()); + VERIFY_IS_EQUAL(m.reshaped( 1, 16).innerStride(), 1); + + VERIFY_IS_EQUAL(m.reshaped( 2, 8).data(), m.data()); + VERIFY_IS_EQUAL(m.reshaped( 2, 8).innerStride(), 1); + VERIFY_IS_EQUAL(m.reshaped( 2, 8).outerStride(), 2); +} + // just test a 4x4 matrix, enumerate all combination manually template void reshape4x4(MatType m) { + typedef typename MatType::Scalar Scalar; + internal::VariableAndFixedInt v1( 1); internal::VariableAndFixedInt v2( 2); internal::VariableAndFixedInt v4( 4); @@ -124,12 +139,7 @@ void reshape4x4(MatType m) check_auto_reshape4x4 (m.transpose()); check_auto_reshape4x4(m.transpose()); - VERIFY_IS_EQUAL(m.reshaped( 1, 16).data(), m.data()); - VERIFY_IS_EQUAL(m.reshaped( 1, 16).innerStride(), 1); - - VERIFY_IS_EQUAL(m.reshaped( 2, 8).data(), m.data()); - VERIFY_IS_EQUAL(m.reshaped( 2, 8).innerStride(), 1); - VERIFY_IS_EQUAL(m.reshaped( 2, 8).outerStride(), 2); + check_direct_access_reshape4x4(m,fix); if((MatType::Flags&RowMajorBit)==0) { @@ -150,8 +160,8 @@ void reshape4x4(MatType m) VERIFY_IS_EQUAL( m28r1, m28r2); VERIFY(is_same_eq(m.reshaped(v16,fix<1>), m.reshaped())); - VERIFY_IS_EQUAL(m.reshaped(16,1), m.reshaped()); - VERIFY_IS_EQUAL(m.reshaped(1,16), m.reshaped().transpose()); + VERIFY_IS_EQUAL(m.reshaped(16,1).eval(), m.reshaped().eval()); + VERIFY_IS_EQUAL(m.reshaped(1,16).eval(), m.reshaped().transpose().eval()); VERIFY_IS_EQUAL(m.reshaped().reshaped(2,8), m.reshaped(2,8)); VERIFY_IS_EQUAL(m.reshaped().reshaped(4,4), m.reshaped(4,4)); VERIFY_IS_EQUAL(m.reshaped().reshaped(8,2), m.reshaped(8,2)); @@ -163,12 +173,30 @@ void reshape4x4(MatType m) VERIFY(is_same_eq(m.reshaped(AutoSize,fix<1>), m.reshaped())); VERIFY_IS_EQUAL(m.template reshaped(fix<1>,AutoSize), m.transpose().reshaped().transpose()); + + // check assignment + { + Matrix m1x(m.size()); m1x.setRandom(); + VERIFY_IS_APPROX(m.reshaped() = m1x, m1x); + VERIFY_IS_APPROX(m, m1x.reshaped(4,4)); + + Matrix m28(2,8); m28.setRandom(); + VERIFY_IS_APPROX(m.reshaped(2,8) = m28, m28); + VERIFY_IS_APPROX(m, m28.reshaped(4,4)); + VERIFY_IS_APPROX(m.template reshaped(2,8) = m28, m28); + + Matrix m24(2,4); m24.setRandom(); + VERIFY_IS_APPROX(m(seq(0,last,2),all).reshaped(2,4) = m24, m24); + + // check constness: + m.reshaped(2,8).nestedExpression() = m; + } } EIGEN_DECLARE_TEST(reshape) { - typedef Matrix RowMatrixXi; - typedef Matrix RowMatrix4i; + typedef Matrix RowMatrixXi; + typedef Matrix RowMatrix4i; MatrixXi mx = MatrixXi::Random(4, 4); Matrix4i m4 = Matrix4i::Random(4, 4); RowMatrixXi rmx = RowMatrixXi::Random(4, 4); -- cgit v1.2.3