-
Notifications
You must be signed in to change notification settings - Fork 439
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP] Math: add constexpr to Vector{,2,3,4}
- Loading branch information
Showing
8 changed files
with
322 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
/* | ||
This file is part of Magnum. | ||
Copyright © 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018, 2019, | ||
2020, 2021, 2022 Vladimír Vondruš <[email protected]> | ||
Permission is hereby granted, free of charge, to any person obtaining a | ||
copy of this software and associated documentation files (the "Software"), | ||
to deal in the Software without restriction, including without limitation | ||
the rights to use, copy, modify, merge, publish, distribute, sublicense, | ||
and/or sell copies of the Software, and to permit persons to whom the | ||
Software is furnished to do so, subject to the following conditions: | ||
The above copyright notice and this permission notice shall be included | ||
in all copies or substantial portions of the Software. | ||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
DEALINGS IN THE SOFTWARE. | ||
*/ | ||
|
||
#include <Corrade/TestSuite/Tester.h> | ||
#include <Magnum/Magnum.h> | ||
#include <Magnum/Math/Vector.h> | ||
#include <Magnum/Math/Vector2.h> | ||
|
||
namespace Magnum { namespace Math { namespace Test { namespace { | ||
|
||
struct Constexpr14Test: Corrade::TestSuite::Tester { | ||
explicit Constexpr14Test(); | ||
#ifndef CORRADE_NO_CONSTEVAL | ||
template<class Vec, typename T> void simple(); | ||
template<class Vec, typename T> void integer(); | ||
template<class Vec, typename T> void scalar(); | ||
template<class Vec, typename T> static constexpr int simpleBody(); | ||
template<class Vec, typename T> static constexpr int integerBody(); | ||
template<class Vec, typename T> static constexpr int scalarBody(); | ||
#else | ||
void no_consteval_on_this_compiler() { CORRADE_COMPARE(0, 0); } | ||
#endif | ||
}; | ||
|
||
Constexpr14Test::Constexpr14Test() { | ||
addTests<Constexpr14Test>({ | ||
#ifndef CORRADE_NO_CONSTEVAL | ||
&Constexpr14Test::simple<Vector2i, int>, | ||
&Constexpr14Test::simple<Vector2ui, unsigned>, | ||
&Constexpr14Test::simple<Vector<2, int>, int>, | ||
&Constexpr14Test::simple<Vector<2, Short>, Short>, | ||
&Constexpr14Test::simple<Vector<2, unsigned>, unsigned>, | ||
&Constexpr14Test::simple<Vector<2, float>, float>, | ||
&Constexpr14Test::simple<Vector<2, double>, double>, | ||
&Constexpr14Test::integer<Vector<2, int>, int>, | ||
&Constexpr14Test::integer<Vector2i, int>, | ||
&Constexpr14Test::integer<Vector2ui, int>, | ||
&Constexpr14Test::integer<Vector<2, int>, int>, | ||
&Constexpr14Test::integer<Vector<2, Short>, Short>, | ||
&Constexpr14Test::integer<Vector<2, unsigned>, unsigned> | ||
#else | ||
&Constexpr14Test::no_consteval_on_this_compiler | ||
#endif | ||
}); | ||
} | ||
|
||
#ifndef CORRADE_NO_CONSTEVAL | ||
template<class Vec, typename T> | ||
void Constexpr14Test::simple() { | ||
setTestCaseTemplateName(TypeTraits<T>::name()); | ||
constexpr int ret = simpleBody<Vec, T>(); | ||
static_assert(ret == 0, ""); | ||
CORRADE_COMPARE(0, ret); | ||
} | ||
|
||
#if CORRADE_CXX_STANDARD >= 201603 | ||
#define ASSERT(...) if constexpr(__VA_ARGS__) (void)0; else throw "failed" | ||
#else | ||
#define ASSERT(...) if (!(__VA_ARGS__)) return __LINE__ | ||
#endif | ||
|
||
|
||
template<class Vec, typename T> | ||
constexpr int Constexpr14Test::simpleBody() { | ||
constexpr Vec a{T(1), T(2)}, b{T(2), T(3)}; | ||
ASSERT(a[0] == T(1) && a[1] == T(2)); | ||
ASSERT(a + b == Vec(T(3), T(5))); | ||
ASSERT(a - b == Vec(T(-1), T(-1))); | ||
ASSERT(a * b == Vec(T(2), T(6))); | ||
ASSERT(b / a == (std::is_integral<T>::value ? (Vec(T(2), T(1))) : (Vec(T(2), T(1.5))))); | ||
ASSERT(b.product() == T(6)); | ||
ASSERT(b.sum() == T(5)); | ||
return 0; | ||
} | ||
|
||
template<class Vec, typename T> | ||
void Constexpr14Test::integer() | ||
{ | ||
setTestCaseTemplateName(TypeTraits<T>::name()); | ||
constexpr int ret = integerBody<Vec, T>(); | ||
static_assert(ret == 0, ""); | ||
CORRADE_COMPARE(0, ret); | ||
} | ||
|
||
template<class Vec, typename T> | ||
constexpr int Constexpr14Test::integerBody() | ||
{ | ||
constexpr Vec a{T(1), T(2)}, b{T(3), T(4)}; | ||
ASSERT(a[0] == T(1) && a[1] == T(2)); | ||
ASSERT(b % a == Vec(T(0), T(0))); | ||
ASSERT(a % b == Vec(T(1), T(2))); | ||
ASSERT((~a)[0] == ~a[0]); | ||
ASSERT((~b)[0] == ~b[0]); | ||
return 0; | ||
} | ||
#endif | ||
|
||
}}}} | ||
|
||
CORRADE_TEST_MAIN(Magnum::Math::Test::Constexpr14Test) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.