From 1becbae4ec3cd2167e8551059a0163fafbfc7be1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Tue, 20 Oct 2015 20:28:46 +0200 Subject: [PATCH] Math: test Dual subclass implementation macro. Not sure why I did not do this already. --- src/Magnum/Math/Test/DualTest.cpp | 66 ++++++++++++++++++++++++++++++- 1 file changed, 65 insertions(+), 1 deletion(-) diff --git a/src/Magnum/Math/Test/DualTest.cpp b/src/Magnum/Math/Test/DualTest.cpp index 30fe43cb4..071fd7ce8 100644 --- a/src/Magnum/Math/Test/DualTest.cpp +++ b/src/Magnum/Math/Test/DualTest.cpp @@ -49,11 +49,15 @@ struct DualTest: Corrade::TestSuite::Tester { void conjugated(); void sqrt(); + void subclassTypes(); + void subclass(); + void debug(); }; typedef Math::Dual Dual; -typedef Math::Dual> DualVector2; +typedef Math::Vector2 Vector2; +typedef Math::Dual DualVector2; DualTest::DualTest() { addTests({&DualTest::construct, @@ -71,6 +75,9 @@ DualTest::DualTest() { &DualTest::conjugated, &DualTest::sqrt, + &DualTest::subclassTypes, + &DualTest::subclass, + &DualTest::debug}); } @@ -156,6 +163,63 @@ void DualTest::sqrt() { CORRADE_COMPARE(Math::sqrt(Dual(16.0f, 2.0f)), Dual(4.0f, 0.25f)); } +namespace { + +template class BasicDualVec2: public Math::Dual> { + public: + /* MSVC 2015 can't handle {} here */ + template constexpr BasicDualVec2(U&&... args): Math::Dual>(args...) {} + + MAGNUM_DUAL_SUBCLASS_IMPLEMENTATION(BasicDualVec2, Math::Vector2) +}; + +typedef BasicDualVec2 DualVec2; + +} + +void DualTest::subclassTypes() { + const DualVec2 a; + CORRADE_VERIFY((std::is_same::value)); + CORRADE_VERIFY((std::is_same::value)); + CORRADE_VERIFY((std::is_same::value)); + CORRADE_VERIFY((std::is_same::value)); + CORRADE_VERIFY((std::is_same::value)); + + DualVec2 b; + CORRADE_VERIFY((std::is_same::value)); + CORRADE_VERIFY((std::is_same::value)); + + const Dual c; + CORRADE_VERIFY((std::is_same::value)); + //CORRADE_VERIFY((std::is_same::value)); does not compile yet + CORRADE_VERIFY((std::is_same::value)); + //CORRADE_VERIFY((std::is_same::value)); does not compile yet +} + +void DualTest::subclass() { + const DualVec2 a{Vector2{1.5f, 2.0f}, Vector2{-4.0f, 1.3f}}; + const DualVec2 b{Vector2{3.0f, -1.2f}, Vector2{ 0.2f, -1.0f}}; + const DualVec2 c{Vector2{4.5f, 0.8f}, Vector2{-3.8f, 0.3f}}; + const DualVec2 d{Vector2{4.5f, -2.4f}, Vector2{-11.7f, -3.56f}}; + + CORRADE_COMPARE(-a, (DualVec2{Vector2{-1.5f, -2.0f}, Vector2{4.0f, -1.3f}})); + CORRADE_COMPARE(a + b, c); + CORRADE_COMPARE(c - b, a); + CORRADE_COMPARE(a*b, d); + CORRADE_COMPARE(d/b, a); + + /* No need to test in-place operators as the other ones are implemented + using them */ + + const Dual e{-2.0f, 0.5f}; + const DualVec2 f{Vector2{-3.0f, -4.0f}, Vector2{8.75f, -1.6f}}; + const DualVec2 g{Vector2{-2.0f/1.5f, -1.0f}, Vector2{-7.25f/2.25f, 3.6f/4.0f}}; + CORRADE_COMPARE(a*e, f); + //CORRADE_COMPARE(e*a, f); does not compile yet + CORRADE_COMPARE(f/e, a); + //CORRADE_COMPARE(e/a, g); does not compile yet +} + void DualTest::debug() { std::ostringstream o;