From bbea562a6c4bb43d8ee13f9d7509ee6a273be55b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 30 Jan 2013 17:36:18 +0100 Subject: [PATCH] Math: properly testing all constexpr methods in RectangularMatrix. Fixed data() method, it doesn't need to use reinterpret_cast. --- src/Math/RectangularMatrix.h | 6 ++-- src/Math/Test/RectangularMatrixTest.cpp | 42 +++++++++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/Math/RectangularMatrix.h b/src/Math/RectangularMatrix.h index b7a2b846e..56a6acc6b 100644 --- a/src/Math/RectangularMatrix.h +++ b/src/Math/RectangularMatrix.h @@ -23,8 +23,6 @@ namespace Magnum { namespace Math { -/** @todo Properly test all constexpr */ - /** @brief Rectangular matrix @tparam cols Column count @@ -125,8 +123,8 @@ template class RectangularMatrix { * * @see operator[] */ - inline T* data() { return reinterpret_cast(_data); } - inline constexpr const T* data() const { return reinterpret_cast(_data); } /**< @overload */ + inline T* data() { return _data[0].data(); } + inline constexpr const T* data() const { return _data[0].data(); } /**< @overload */ /** * @brief %Matrix column diff --git a/src/Math/Test/RectangularMatrixTest.cpp b/src/Math/Test/RectangularMatrixTest.cpp index d8b9adf19..95aefcc8b 100644 --- a/src/Math/Test/RectangularMatrixTest.cpp +++ b/src/Math/Test/RectangularMatrixTest.cpp @@ -32,6 +32,8 @@ class RectangularMatrixTest: public Corrade::TestSuite::Tester { void constructFromDiagonal(); void data(); + void constExpressions(); + void compare(); void negative(); @@ -70,6 +72,8 @@ RectangularMatrixTest::RectangularMatrixTest() { &RectangularMatrixTest::constructFromDiagonal, &RectangularMatrixTest::data, + &RectangularMatrixTest::constExpressions, + &RectangularMatrixTest::compare, &RectangularMatrixTest::negative, @@ -174,6 +178,44 @@ void RectangularMatrixTest::data() { CORRADE_COMPARE(m, expected); } +void RectangularMatrixTest::constExpressions() { + /* Default constructor */ + constexpr Matrix3x4 a; + CORRADE_COMPARE(a, Matrix3x4(Vector4(0.0f, 0.0f, 0.0f, 0.0f), + Vector4(0.0f, 0.0f, 0.0f, 0.0f), + Vector4(0.0f, 0.0f, 0.0f, 0.0f))); + + /* Value constructor */ + constexpr Matrix3x4 b(Vector4(3.0f, 5.0f, 8.0f, 4.0f), + Vector4(4.5f, 4.0f, 7.0f, 3.0f), + Vector4(7.0f, -1.7f, 8.0f, 0.0f)); + CORRADE_COMPARE(b, Matrix3x4(Vector4(3.0f, 5.0f, 8.0f, 4.0f), + Vector4(4.5f, 4.0f, 7.0f, 3.0f), + Vector4(7.0f, -1.7f, 8.0f, 0.0f))); + + /* Conversion constructor */ + typedef RectangularMatrix<3, 4, std::int32_t> Matrix3x4i; + typedef Vector<4, std::int32_t> Vector4i; + constexpr Matrix3x4i c(b); + CORRADE_COMPARE(c, Matrix3x4i(Vector4i(3, 5, 8, 4), + Vector4i(4, 4, 7, 3), + Vector4i(7, -1, 8, 0))); + + /* Copy constructor */ + constexpr Matrix3x4 d(b); + CORRADE_COMPARE(d, Matrix3x4(Vector4(3.0f, 5.0f, 8.0f, 4.0f), + Vector4(4.5f, 4.0f, 7.0f, 3.0f), + Vector4(7.0f, -1.7f, 8.0f, 0.0f))); + + /* Data access, pointer chasings, i.e. *(b.data()[1]), are not possible */ + constexpr Vector4 e = b[2]; + constexpr float f = b[1][2]; + constexpr float g = *b.data(); + CORRADE_COMPARE(e, Vector4(7.0f, -1.7f, 8.0f, 0.0f)); + CORRADE_COMPARE(f, 7.0f); + CORRADE_COMPARE(g, 3.0f); +} + void RectangularMatrixTest::compare() { Matrix2 a(Vector2(1.0f, -3.0f), Vector2(5.0f, -10.0f));