From 9ade2cae479ad904e26c52d5689bc5522798c5c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Thu, 1 Sep 2016 12:01:50 +0200 Subject: [PATCH] Math: added TypeTraits::name(), using that in the test. --- src/Magnum/Math/Test/TypeTraitsTest.cpp | 63 +++++++++++++++++-------- src/Magnum/Math/TypeTraits.h | 31 +++++++++++- 2 files changed, 72 insertions(+), 22 deletions(-) diff --git a/src/Magnum/Math/Test/TypeTraitsTest.cpp b/src/Magnum/Math/Test/TypeTraitsTest.cpp index 82d22fcbd..b3767e999 100644 --- a/src/Magnum/Math/Test/TypeTraitsTest.cpp +++ b/src/Magnum/Math/Test/TypeTraitsTest.cpp @@ -33,6 +33,8 @@ namespace Magnum { namespace Math { namespace Test { struct TypeTraitsTest: Corrade::TestSuite::Tester { explicit TypeTraitsTest(); + void name(); + template void equalsIntegral(); template void equalsFloatingPoint0(); template void equalsFloatingPoint1(); @@ -42,53 +44,74 @@ struct TypeTraitsTest: Corrade::TestSuite::Tester { }; TypeTraitsTest::TypeTraitsTest() { - addTests({&TypeTraitsTest::equalsIntegral, - &TypeTraitsTest::equalsIntegral, - &TypeTraitsTest::equalsIntegral, - &TypeTraitsTest::equalsIntegral, - &TypeTraitsTest::equalsIntegral, - &TypeTraitsTest::equalsIntegral, - #ifndef CORRADE_TARGET_EMSCRIPTEN - &TypeTraitsTest::equalsIntegral, - &TypeTraitsTest::equalsIntegral, - #endif - &TypeTraitsTest::equalsFloatingPoint0, - &TypeTraitsTest::equalsFloatingPoint0, - &TypeTraitsTest::equalsFloatingPoint1, - &TypeTraitsTest::equalsFloatingPoint1, - &TypeTraitsTest::equalsFloatingPointLarge, - &TypeTraitsTest::equalsFloatingPointLarge, - &TypeTraitsTest::equalsFloatingPointInfinity, - &TypeTraitsTest::equalsFloatingPointInfinity, - &TypeTraitsTest::equalsFloatingPointNaN, - &TypeTraitsTest::equalsFloatingPointNaN}); + addTests({ + &TypeTraitsTest::name, + + &TypeTraitsTest::equalsIntegral, + &TypeTraitsTest::equalsIntegral, + &TypeTraitsTest::equalsIntegral, + &TypeTraitsTest::equalsIntegral, + &TypeTraitsTest::equalsIntegral, + &TypeTraitsTest::equalsIntegral, + #ifndef CORRADE_TARGET_EMSCRIPTEN + &TypeTraitsTest::equalsIntegral, + &TypeTraitsTest::equalsIntegral, + #endif + + &TypeTraitsTest::equalsFloatingPoint0, + &TypeTraitsTest::equalsFloatingPoint0, + &TypeTraitsTest::equalsFloatingPoint1, + &TypeTraitsTest::equalsFloatingPoint1, + &TypeTraitsTest::equalsFloatingPointLarge, + &TypeTraitsTest::equalsFloatingPointLarge, + &TypeTraitsTest::equalsFloatingPointInfinity, + &TypeTraitsTest::equalsFloatingPointInfinity, + &TypeTraitsTest::equalsFloatingPointNaN, + &TypeTraitsTest::equalsFloatingPointNaN}); +} + +void TypeTraitsTest::name() { + CORRADE_COMPARE(TypeTraits::name(), "UnsignedShort"); + CORRADE_COMPARE(TypeTraits::name(), "Float"); } template void TypeTraitsTest::equalsIntegral() { + setTestCaseName(std::string{"equalsIntegral<"} + TypeTraits::name() + ">"); + CORRADE_VERIFY(!TypeTraits::equals(1, 1+TypeTraits::epsilon())); } template void TypeTraitsTest::equalsFloatingPoint0() { + setTestCaseName(std::string{"equalsFloatingPoint0<"} + TypeTraits::name() + ">"); + CORRADE_VERIFY(TypeTraits::equals(T(0)+TypeTraits::epsilon()/T(2), T(0))); CORRADE_VERIFY(!TypeTraits::equals(T(0)+TypeTraits::epsilon()*T(2), T(0))); } template void TypeTraitsTest::equalsFloatingPoint1() { + setTestCaseName(std::string{"equalsFloatingPoint1<"} + TypeTraits::name() + ">"); + CORRADE_VERIFY(TypeTraits::equals(T(1)+TypeTraits::epsilon()/T(2), T(1))); CORRADE_VERIFY(!TypeTraits::equals(T(1)+TypeTraits::epsilon()*T(3), T(1))); } template void TypeTraitsTest::equalsFloatingPointLarge() { + setTestCaseName(std::string{"equalsFloatingPointLarge<"} + TypeTraits::name() + ">"); + CORRADE_VERIFY(TypeTraits::equals(T(25)+TypeTraits::epsilon()*T(2), T(25))); CORRADE_VERIFY(!TypeTraits::equals(T(25)+TypeTraits::epsilon()*T(75), T(25))); } template void TypeTraitsTest::equalsFloatingPointInfinity() { + setTestCaseName(std::string{"equalsFloatingPointInfinity<"} + TypeTraits::name() + ">"); + CORRADE_VERIFY(TypeTraits::equals(Constants::inf(), Constants::inf())); } template void TypeTraitsTest::equalsFloatingPointNaN() { + setTestCaseName(std::string{"equalsFloatingPointNaN<"} + TypeTraits::name() + ">"); + CORRADE_VERIFY(!TypeTraits::equals(Constants::nan(), Constants::nan())); } diff --git a/src/Magnum/Math/TypeTraits.h b/src/Magnum/Math/TypeTraits.h index b15869ea6..1632b7c89 100644 --- a/src/Magnum/Math/TypeTraits.h +++ b/src/Magnum/Math/TypeTraits.h @@ -87,6 +87,14 @@ template struct TypeTraits: Implementation::TypeTraitsDefault { */ typedef U FloatingPointType; + /** + * @brief Type name + * + * Returns a string representation of type name, such as `"UnsignedInt"` + * for @ref UnsignedInt. + */ + constexpr static const char* name(); + /** * @brief Epsilon value for fuzzy compare * @@ -108,7 +116,26 @@ template struct TypeTraits: Implementation::TypeTraitsDefault { /* Integral scalar types */ namespace Implementation { - template struct TypeTraitsIntegral: TypeTraitsDefault { + template struct TypeTraitsName; + #define _c(type) template<> struct TypeTraitsName { \ + constexpr static const char* name() { return #type; } \ + }; + _c(UnsignedByte) + _c(Byte) + _c(UnsignedShort) + _c(Short) + _c(UnsignedInt) + _c(Int) + #ifndef MAGNUM_TARGET_WEBGL + _c(UnsignedLong) + _c(Long) + #endif + _c(Float) + _c(Double) + _c(long double) + #undef _c + + template struct TypeTraitsIntegral: TypeTraitsDefault, TypeTraitsName { constexpr static T epsilon() { return T(1); } }; } @@ -144,7 +171,7 @@ template<> struct TypeTraits: Implementation::TypeTraitsIntegral { /* Floating-point scalar types */ namespace Implementation { -template struct TypeTraitsFloatingPoint { +template struct TypeTraitsFloatingPoint: TypeTraitsName { TypeTraitsFloatingPoint() = delete; static bool equals(T a, T b);