Skip to content

Commit

Permalink
Math: more Vector constexpr 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 5f1e214
Show file tree
Hide file tree
Showing 12 changed files with 352 additions and 223 deletions.
13 changes: 13 additions & 0 deletions src/Magnum/Math/Math.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,17 @@ namespace Implementation {

}}

// TODO remove this once corrade#152 gets merged
#if defined __cpp_constexpr && __cpp_constexpr >= 201304 || \
defined CORRADE_TARGET_MSVC && _MSC_VER >= 1910 && CORRADE_CXX_STANDARD >= 201402L
#define MAGNUM_CONSTEXPR14 constexpr
#else
#define MAGNUM_CONSTEXPR14
#endif
#if defined(__clang__) && __clang_major__ >= 9 || defined(__GNUG__) && __GNUG__ >= 9 || defined(_MSC_VER) && _MSC_VER >= 1931
#define MAGNUM_CONSTEVAL (__builtin_is_constant_evaluated())
#elif CORRADE_CXX_STANDARD >= 202002L
#define MAGNUM_CONSTEVAL (std::is_constant_evaluated())
#endif

#endif
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 Vector2Test Vector3Test 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
14 changes: 14 additions & 0 deletions src/Magnum/Math/Test/Cpp14VectorTest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#ifndef Magnum_Math_Test_Cpp14Vector
#define Magnum_Math_Test_Cpp14Vector

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

#endif
34 changes: 29 additions & 5 deletions src/Magnum/Math/Test/Vector2Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "Magnum/Math/StrictWeakOrdering.h"
#include "Magnum/Math/Swizzle.h"

#include "Cpp14VectorTest.h"

struct Vec2 {
float x, y;
};
Expand All @@ -57,6 +59,7 @@ namespace Test { namespace {
struct Vector2Test: Corrade::TestSuite::Tester {
explicit Vector2Test();

#ifndef SKIP_TESTING
void construct();
void constructDefault();
void constructNoInit();
Expand All @@ -76,14 +79,24 @@ struct Vector2Test: Corrade::TestSuite::Tester {

void swizzleType();
void debug();
#else
void skipTesting();
#endif
};

typedef Math::Vector3<Int> Vector3i;
typedef Math::Vector2<Float> Vector2;
typedef Math::Vector2<Int> Vector2i;

Vector2Test::Vector2Test() {
addTests({&Vector2Test::construct,
#ifndef TESTING_CONSTEXPR
setTestName("MathVector2Test");
#else
setTestName("Cpp14MathVector2Test");
#endif
addTests({
#ifndef SKIP_TESTING
&Vector2Test::construct,
&Vector2Test::constructDefault,
&Vector2Test::constructNoInit,
&Vector2Test::constructOneValue,
Expand All @@ -101,9 +114,15 @@ Vector2Test::Vector2Test() {
&Vector2Test::strictWeakOrdering,

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

#ifndef SKIP_TESTING

void Vector2Test::construct() {
constexpr Vector2 a = {1.5f, 2.5f};
CORRADE_COMPARE(a, (Vector<2, Float>(1.5f, 2.5f)));
Expand Down Expand Up @@ -197,7 +216,7 @@ void Vector2Test::convert() {
}

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

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

void Vector2Test::cross() {
Vector2i a(1, -1);
Vector2i b(4, 3);
CE Vector2i a(1, -1);
CE 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 Expand Up @@ -268,6 +287,11 @@ void Vector2Test::debug() {
Debug(&o) << Vector2(0.5f, 15.0f);
CORRADE_COMPARE(o.str(), "Vector(0.5, 15)\n");
}
#else
void Vector2Test::skipTesting() {
CORRADE_SKIP("Relaxed constexpr not supported by the compiler.");
}
#endif

}}}}

Expand Down
37 changes: 29 additions & 8 deletions src/Magnum/Math/Test/Vector3Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
DEALINGS IN THE SOFTWARE.
*/

#include "Cpp14VectorTest.h"

#include <sstream>
#include <Corrade/TestSuite/Tester.h>
#include <Corrade/Utility/DebugStl.h>
Expand Down Expand Up @@ -57,6 +59,7 @@ namespace Test { namespace {
struct Vector3Test: Corrade::TestSuite::Tester {
explicit Vector3Test();

#ifndef SKIP_TESTING
void construct();
void constructDefault();
void constructNoInit();
Expand All @@ -76,14 +79,24 @@ struct Vector3Test: Corrade::TestSuite::Tester {

void swizzleType();
void debug();
#else
void skipTesting();
#endif
};

typedef Math::Vector3<Float> Vector3;
typedef Math::Vector3<Int> Vector3i;
typedef Math::Vector2<Float> Vector2;

Vector3Test::Vector3Test() {
addTests({&Vector3Test::construct,
#ifndef TESTING_CONSTEXPR
setTestName("MathVector3Test");
#else
setTestName("Cpp14MathVector3Test");
#endif
addTests({
#ifndef SKIP_TESTING
&Vector3Test::construct,
&Vector3Test::constructDefault,
&Vector3Test::constructNoInit,
&Vector3Test::constructOneValue,
Expand All @@ -101,9 +114,13 @@ Vector3Test::Vector3Test() {
&Vector3Test::strictWeakOrdering,

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

#ifndef SKIP_TESTING
void Vector3Test::construct() {
constexpr Vector3 a = {1.0f, 2.5f, -3.0f};
CORRADE_COMPARE(a, (Vector<3, Float>(1.0f, 2.5f, -3.0f)));
Expand Down Expand Up @@ -206,7 +223,7 @@ void Vector3Test::convert() {
}

void Vector3Test::access() {
Vector3 vec(1.0f, -2.0f, 5.0f);
CE 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 +247,8 @@ void Vector3Test::access() {
}

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

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

void Vector3Test::twoComponent() {
Vector3 a(1.0f, 2.0f, 3.0f);
CE 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 Expand Up @@ -292,7 +309,11 @@ void Vector3Test::debug() {
Debug(&o) << Vector3(0.5f, 15.0f, 1.0f);
CORRADE_COMPARE(o.str(), "Vector(0.5, 15, 1)\n");
}

#else
void Vector3Test::skipTesting() {
CORRADE_SKIP("Relaxed constexpr not supported by the compiler.");
}
#endif
}}}}

CORRADE_TEST_MAIN(Magnum::Math::Test::Vector3Test)
55 changes: 39 additions & 16 deletions src/Magnum/Math/Test/Vector4Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "Magnum/Math/StrictWeakOrdering.h"
#include "Magnum/Math/Swizzle.h"

#include "Cpp14VectorTest.h"

struct Vec4 {
float x, y, z, w;
};
Expand All @@ -57,6 +59,7 @@ namespace Test { namespace {
struct Vector4Test: Corrade::TestSuite::Tester {
explicit Vector4Test();

#ifndef SKIP_TESTING
void construct();
void constructPad();
void constructDefault();
Expand All @@ -78,6 +81,9 @@ struct Vector4Test: Corrade::TestSuite::Tester {

void swizzleType();
void debug();
#else
void skipTesting();
#endif
};

typedef Math::Vector4<Float> Vector4;
Expand All @@ -86,7 +92,14 @@ typedef Math::Vector3<Float> Vector3;
typedef Math::Vector2<Float> Vector2;

Vector4Test::Vector4Test() {
addTests({&Vector4Test::construct,
#ifndef TESTING_CONSTEXPR
setTestName("MathVector4Test");
#else
setTestName("Cpp14MathVector4Test");
#endif
addTests({
#ifndef SKIP_TESTING
&Vector4Test::construct,
&Vector4Test::constructPad,
&Vector4Test::constructDefault,
&Vector4Test::constructNoInit,
Expand All @@ -106,9 +119,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 +246,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 +276,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 +288,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 +299,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 +314,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 +328,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 +353,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 Vector4Test::skipTesting() {
CORRADE_SKIP("Relaxed constexpr not supported by the compiler.");
}
#endif

}}}}

Expand Down
Loading

0 comments on commit 5f1e214

Please sign in to comment.