Skip to content

Commit

Permalink
Math: constexpr for more Vector ops under C++14 rules
Browse files Browse the repository at this point in the history
  • Loading branch information
sthalik committed Oct 29, 2022
1 parent 852fd16 commit 3f979cf
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 64 deletions.
7 changes: 7 additions & 0 deletions src/Magnum/Math/Test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ corrade_add_test(MathVectorTest VectorTest.cpp LIBRARIES MagnumMathTestLib)
corrade_add_test(MathVector2Test Vector2Test.cpp LIBRARIES MagnumMathTestLib)
corrade_add_test(MathVector3Test Vector3Test.cpp LIBRARIES MagnumMathTestLib)
corrade_add_test(MathVector4Test Vector4Test.cpp LIBRARIES MagnumMathTestLib)

foreach(_test VectorTest Vector4Test)
corrade_add_test(Cpp14Math${_test} ${_test}.cpp LIBRARIES MagnumMathTestLib)
set_target_properties(Cpp14Math${_test} PROPERTIES CORRADE_CXX_STANDARD 14)
target_compile_definitions(Cpp14Math${_test} PRIVATE TESTING_CONSTEXPR CORRADE_GRACEFUL_ASSERT)
endforeach()

corrade_add_test(MathColorTest ColorTest.cpp LIBRARIES MagnumMathTestLib)

corrade_add_test(MathRectangularMatrixTest RectangularMatrixTest.cpp LIBRARIES MagnumMathTestLib)
Expand Down
6 changes: 3 additions & 3 deletions src/Magnum/Math/Test/Vector2Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void Vector2Test::convert() {
}

void Vector2Test::access() {
Vector2 vec(1.0f, -2.0f);
MAGNUM_CONSTEXPR14 Vector2 vec(1.0f, -2.0f);
CORRADE_COMPARE(vec.x(), 1.0f);
CORRADE_COMPARE(vec.y(), -2.0f);

Expand All @@ -209,8 +209,8 @@ void Vector2Test::access() {
}

void Vector2Test::cross() {
Vector2i a(1, -1);
Vector2i b(4, 3);
MAGNUM_CONSTEXPR14 Vector2i a(1, -1);
MAGNUM_CONSTEXPR14 Vector2i b(4, 3);

CORRADE_COMPARE(Math::cross(a, b), 7);
CORRADE_COMPARE(Math::cross<Int>({a, 0}, {b, 0}), Vector3i(0, 0, Math::cross(a, b)));
Expand Down
8 changes: 4 additions & 4 deletions src/Magnum/Math/Test/Vector3Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ void Vector3Test::convert() {
}

void Vector3Test::access() {
Vector3 vec(1.0f, -2.0f, 5.0f);
MAGNUM_CONSTEXPR14 Vector3 vec(1.0f, -2.0f, 5.0f);
CORRADE_COMPARE(vec.x(), 1.0f);
CORRADE_COMPARE(vec.r(), 1.0f);
CORRADE_COMPARE(vec.y(), -2.0f);
Expand All @@ -230,8 +230,8 @@ void Vector3Test::access() {
}

void Vector3Test::cross() {
Vector3i a(1, -1, 1);
Vector3i b(4, 3, 7);
MAGNUM_CONSTEXPR14 Vector3i a(1, -1, 1);
MAGNUM_CONSTEXPR14 Vector3i b(4, 3, 7);

CORRADE_COMPARE(Math::cross(a, b), Vector3i(-10, -3, 7));
}
Expand All @@ -255,7 +255,7 @@ void Vector3Test::scales() {
}

void Vector3Test::twoComponent() {
Vector3 a(1.0f, 2.0f, 3.0f);
MAGNUM_CONSTEXPR14 Vector3 a(1.0f, 2.0f, 3.0f);
CORRADE_COMPARE(a.xy(), Vector2(1.0f, 2.0f));

constexpr Vector3 b(1.0f, 2.0f, 3.0f);
Expand Down
59 changes: 43 additions & 16 deletions src/Magnum/Math/Test/Vector4Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@
#include "Magnum/Math/StrictWeakOrdering.h"
#include "Magnum/Math/Swizzle.h"

#define CE
#ifdef TESTING_CONSTEXPR
#if __cpp_constexpr >= 201304
#undef CE
#define CE constexpr
#else
#define SKIP_TESTING
#endif
#endif

struct Vec4 {
float x, y, z, w;
};
Expand Down Expand Up @@ -86,7 +96,14 @@ typedef Math::Vector3<Float> Vector3;
typedef Math::Vector2<Float> Vector2;

Vector4Test::Vector4Test() {
addTests({&Vector4Test::construct,
#ifdef TESTING_CONSTEXPR
setTestName("MathVector4Test");
#else
setTestName("Cpp14MathVector4Test");
#endif
addTests({
#ifndef SKIP_TESTING
&Vector4Test::construct,
&Vector4Test::constructPad,
&Vector4Test::constructDefault,
&Vector4Test::constructNoInit,
Expand All @@ -106,9 +123,14 @@ Vector4Test::Vector4Test() {
&Vector4Test::strictWeakOrdering,

&Vector4Test::swizzleType,
&Vector4Test::debug});
&Vector4Test::debug
#else
&Vector4Test::skipTesting
#endif
});
}

#ifndef SKIP_TESTING
void Vector4Test::construct() {
constexpr Vector4 a = {1.0f, -2.5f, 3.0f, 4.1f};
CORRADE_COMPARE(a, (Vector<4, Float>(1.0f, -2.5f, 3.0f, 4.1f)));
Expand Down Expand Up @@ -228,7 +250,7 @@ void Vector4Test::convert() {
}

void Vector4Test::access() {
Vector4 vec(1.0f, -2.0f, 5.0f, 0.5f);
CE Vector4 vec(1.0f, -2.0f, 5.0f, 0.5f);
CORRADE_COMPARE(vec.x(), 1.0f);
CORRADE_COMPARE(vec.r(), 1.0f);
CORRADE_COMPARE(vec.y(), -2.0f);
Expand Down Expand Up @@ -258,7 +280,7 @@ void Vector4Test::access() {
}

void Vector4Test::threeComponent() {
Vector4 a(1.0f, 2.0f, 3.0f, 4.0f);
CE Vector4 a(1.0f, 2.0f, 3.0f, 4.0f);
CORRADE_COMPARE(a.xyz(), Vector3(1.0f, 2.0f, 3.0f));
CORRADE_COMPARE(a.rgb(), Vector3(1.0f, 2.0f, 3.0f));

Expand All @@ -270,7 +292,7 @@ void Vector4Test::threeComponent() {
}

void Vector4Test::twoComponent() {
Vector4 a(1.0f, 2.0f, 3.0f, 4.0f);
CE Vector4 a(1.0f, 2.0f, 3.0f, 4.0f);
CORRADE_COMPARE(a.xy(), Vector2(1.0f, 2.0f));

constexpr Vector4 b(1.0f, 2.0f, 3.0f, 4.0f);
Expand All @@ -281,9 +303,9 @@ void Vector4Test::twoComponent() {
}

void Vector4Test::planeEquationThreePoints() {
const Vector3 a{1.0f, 0.5f, 3.0f};
const Vector3 b{1.5f, 1.5f, 2.5f};
const Vector3 c{2.0f, 1.5f, 1.0f};
CE const Vector3 a{1.0f, 0.5f, 3.0f};
CE const Vector3 b{1.5f, 1.5f, 2.5f};
CE const Vector3 c{2.0f, 1.5f, 1.0f};
const Vector4 eq = Math::planeEquation(a, b, c);

CORRADE_COMPARE(Math::dot(a, eq.xyz()) + eq.w(), 0.0f);
Expand All @@ -296,12 +318,12 @@ void Vector4Test::planeEquationThreePoints() {
}

void Vector4Test::planeEquationNormalPoint() {
const Vector3 a{1.0f, 0.5f, 3.0f};
const Vector3 normal{-0.9045340f, 0.3015113f, -0.3015113f};
const Vector4 eq = Math::planeEquation(normal, a);
CE const Vector3 a{1.0f, 0.5f, 3.0f};
CE const Vector3 normal{-0.9045340f, 0.3015113f, -0.3015113f};
CE const Vector4 eq = Math::planeEquation(normal, a);

const Vector3 b{1.5f, 1.5f, 2.5f};
const Vector3 c{2.0f, 1.5f, 1.0f};
CE const Vector3 b{1.5f, 1.5f, 2.5f};
CE const Vector3 c{2.0f, 1.5f, 1.0f};
CORRADE_COMPARE(Math::dot(a, eq.xyz()) + eq.w(), 0.0f);
CORRADE_COMPARE(Math::dot(b, eq.xyz()) + eq.w(), 0.0f);
CORRADE_COMPARE(Math::dot(c, eq.xyz()) + eq.w(), 0.0f);
Expand All @@ -310,9 +332,9 @@ void Vector4Test::planeEquationNormalPoint() {

void Vector4Test::strictWeakOrdering() {
StrictWeakOrdering o;
const Vector4 v4a{1.0f, 2.0f, 3.0f, 4.0f};
const Vector4 v4b{2.0f, 3.0f, 4.0f, 5.0f};
const Vector4 v4c{1.0f, 2.0f, 3.0f, 5.0f};
CE const Vector4 v4a{1.0f, 2.0f, 3.0f, 4.0f};
CE const Vector4 v4b{2.0f, 3.0f, 4.0f, 5.0f};
CE const Vector4 v4c{1.0f, 2.0f, 3.0f, 5.0f};

CORRADE_VERIFY( o(v4a, v4b));
CORRADE_VERIFY(!o(v4b, v4a));
Expand All @@ -335,6 +357,11 @@ void Vector4Test::debug() {
Debug(&o) << Vector4(0.5f, 15.0f, 1.0f, 1.0f);
CORRADE_COMPARE(o.str(), "Vector(0.5, 15, 1, 1)\n");
}
#else
void VectorTest::skipTesting() {
CORRADE_SKIP("Relaxed constexpr not supported by the compiler.");
}
#endif

}}}}

Expand Down
Loading

0 comments on commit 3f979cf

Please sign in to comment.