From 0d9a66183907a0dcd14a35e8cdc13cfcfed4785b Mon Sep 17 00:00:00 2001 From: Seiji Emery Date: Mon, 22 Jun 2015 15:55:34 -0700 Subject: [PATCH] physics test cleanup --- tests/physics/src/BulletTestUtils.h | 87 +++++++++++++++++++++ tests/physics/src/BulletUtilTests.h | 18 +---- tests/physics/src/CollisionInfoTests.h | 18 +---- tests/physics/src/GlmTestUtils.h | 24 ++++++ tests/physics/src/MeshMassPropertiesTests.h | 86 +++----------------- tests/physics/src/ShapeColliderTests.cpp | 9 +-- tests/physics/src/ShapeColliderTests.h | 7 ++ tests/physics/src/ShapeInfoTests.h | 7 +- 8 files changed, 147 insertions(+), 109 deletions(-) create mode 100644 tests/physics/src/BulletTestUtils.h create mode 100644 tests/physics/src/GlmTestUtils.h diff --git a/tests/physics/src/BulletTestUtils.h b/tests/physics/src/BulletTestUtils.h new file mode 100644 index 0000000000..570aaadf45 --- /dev/null +++ b/tests/physics/src/BulletTestUtils.h @@ -0,0 +1,87 @@ +// +// BulletTestUtils.h +// hifi +// +// Created by Seiji Emery on 6/22/15. +// +// + +#ifndef hifi_BulletTestUtils_h +#define hifi_BulletTestUtils_h + +#include + +// Implements functionality in QTestExtensions.hpp for glm types +// There are 3 functions in here (which need to be defined for all types that use them): +// +// - fuzzyCompare (const T &, const T &) -> V (used by QFUZZY_COMPARE) +// - operator << (QTextStream &, const T &) -> QTextStream & (used by all (additional) test macros) +// - errorTest (const T &, const T &, V) -> std::function +// (used by QCOMPARE_WITH_RELATIVE_ERROR via QCOMPARE_WITH_LAMBDA) +// (this is only used by btMatrix3x3 in MeshMassPropertiesTests.cpp, so it's only defined for the Mat3 type) + +// +// fuzzy compare (this is a distance function, basically) +// + +inline btScalar fuzzyCompare(const btScalar & a, const btScalar & b) { + return fabs(a - b); +} +inline btScalar fuzzyCompare(const btVector3 & a, const btVector3 & b) +{ + return (a - b).length(); +} +// Matrices are compared element-wise -- if the error value for any element > epsilon, then fail +inline btScalar fuzzyCompare (const btMatrix3x3 & a, const btMatrix3x3 & b) { + btScalar maxDiff = 0; + for (int i = 0; i < 3; ++i) { + for (int j = 0; j < 3; ++j) { + btScalar diff = fabs(a[i][j] - b[i][j]); + maxDiff = qMax(diff, maxDiff); + } + } + return maxDiff; +} + +// +// Printing (operator <<) +// + +// btMatrix3x3 stream printing (not advised to use this outside of the test macros, due to formatting) +inline QTextStream & operator << (QTextStream & stream, const btMatrix3x3 & matrix) { + stream << "[\n\t\t"; + for (int i = 0; i < 3; ++i) { + for (int j = 0; j < 3; ++j) { + stream << " " << matrix[i][j]; + } + stream << "\n\t\t"; + } + stream << "]\n\t"; // hacky as hell, but this should work... + return stream; +} +inline QTextStream & operator << (QTextStream & stream, const btVector3 & v) { + return stream << "btVector3 { " << v.x() << ", " << v.y() << ", " << v.z() << " }"; +} +// btScalar operator<< is already implemented (as it's just a typedef-ed float/double) + +// +// errorTest (actual, expected, acceptableRelativeError) -> callback closure +// + +// Produces a relative error test for btMatrix3x3 usable with QCOMPARE_WITH_LAMBDA +inline auto errorTest (const btMatrix3x3 & actual, const btMatrix3x3 & expected, const btScalar acceptableRelativeError) +-> std::function +{ + return [&actual, &expected, acceptableRelativeError] () { + for (int i = 0; i < 3; ++i) { + for (int j = 0; j < 3; ++j) { + auto err = (actual[i][j] - expected[i][j]) / expected[i][j]; + if (fabsf(err) > acceptableRelativeError) + return false; + } + } + return true; + }; +} + +#endif diff --git a/tests/physics/src/BulletUtilTests.h b/tests/physics/src/BulletUtilTests.h index 42606fb950..5d31e2af10 100644 --- a/tests/physics/src/BulletUtilTests.h +++ b/tests/physics/src/BulletUtilTests.h @@ -13,8 +13,10 @@ #define hifi_BulletUtilTests_h #include -#include -#include + +// Add additional qtest functionality (the include order is important!) +#include "GlmTestUtils.h" +#include "../QTestExtensions.hpp" class BulletUtilTests : public QObject { Q_OBJECT @@ -25,16 +27,4 @@ private slots: // void fooTest (); }; -// Define comparison + printing functions for the data types we need - -inline float fuzzyCompare (const glm::vec3 & a, const glm::vec3 & b) { - return glm::distance(a, b); -} -inline QTextStream & operator << (QTextStream & stream, const glm::vec3 & v) { - return stream << "glm::vec3 { " << v.x << ", " << v.y << ", " << v.z << " }"; -} - -// These hook into this (and must be defined first...) -#include "../QTestExtensions.hpp" - #endif // hifi_BulletUtilTests_h diff --git a/tests/physics/src/CollisionInfoTests.h b/tests/physics/src/CollisionInfoTests.h index a53386e726..6b89a30aee 100644 --- a/tests/physics/src/CollisionInfoTests.h +++ b/tests/physics/src/CollisionInfoTests.h @@ -12,10 +12,12 @@ #ifndef hifi_CollisionInfoTests_h #define hifi_CollisionInfoTests_h -#include -#include #include +// Add additional qtest functionality (the include order is important!) +#include "GlmTestUtils.h" +#include "../QTestExtensions.hpp" + class CollisionInfoTests : public QObject { Q_OBJECT @@ -24,16 +26,4 @@ private slots: // void translateThenRotate(); }; - -// Define comparison + printing functions for the data types we need -inline float fuzzyCompare (const glm::vec3 & a, const glm::vec3 & b) { - return glm::distance(a, b); -} -inline QTextStream & operator << (QTextStream & stream, const glm::vec3 & v) { - return stream << "glm::vec3 { " << v.x << ", " << v.y << ", " << v.z << " }"; -} - -// These hook into this (and must be defined first...) -#include "../QTestExtensions.hpp" - #endif // hifi_CollisionInfoTests_h diff --git a/tests/physics/src/GlmTestUtils.h b/tests/physics/src/GlmTestUtils.h new file mode 100644 index 0000000000..1bd2988146 --- /dev/null +++ b/tests/physics/src/GlmTestUtils.h @@ -0,0 +1,24 @@ +// +// GlmTestUtils.h +// hifi +// +// Created by Seiji Emery on 6/22/15. +// +// + +#ifndef hifi_GlmTestUtils_h +#define hifi_GlmTestUtils_h + +#include +#include + +// Implements functionality in QTestExtensions.hpp for glm types + +inline float fuzzyCompare(const glm::vec3 & a, const glm::vec3 & b) { + return glm::distance(a, b); +} +inline QTextStream & operator << (QTextStream & stream, const glm::vec3 & v) { + return stream << "glm::vec3 { " << v.x << ", " << v.y << ", " << v.z << " }"; +} + +#endif diff --git a/tests/physics/src/MeshMassPropertiesTests.h b/tests/physics/src/MeshMassPropertiesTests.h index 6ebe016535..489bee835a 100644 --- a/tests/physics/src/MeshMassPropertiesTests.h +++ b/tests/physics/src/MeshMassPropertiesTests.h @@ -12,13 +12,20 @@ #ifndef hifi_MeshMassPropertiesTests_h #define hifi_MeshMassPropertiesTests_h -#include -#include -#include - #include #include +// Add additional qtest functionality (the include order is important!) +#include "BulletTestUtils.h" +#include "GlmTestUtils.h" +#include "../QTestExtensions.hpp" + +// Relative error macro (see errorTest in BulletTestUtils.h) +#define QCOMPARE_WITH_RELATIVE_ERROR(actual, expected, relativeError) \ + QCOMPARE_WITH_LAMBDA(actual, expected, errorTest(actual, expected, relativeError)) + + +// Testcase class class MeshMassPropertiesTests : public QObject { Q_OBJECT @@ -30,75 +37,4 @@ private slots: void testBoxAsMesh(); }; -// Define comparison + printing functions for the data types we need - -inline float fuzzyCompare(const glm::vec3 & a, const glm::vec3 & b) { - return glm::distance(a, b); -} -inline QTextStream & operator << (QTextStream & stream, const glm::vec3 & v) { - return stream << "glm::vec3 { " << v.x << ", " << v.y << ", " << v.z << " }"; -} -inline btScalar fuzzyCompare(const btScalar & a, const btScalar & b) { - return fabs(a - b); -} - -// Matrices are compared element-wise -- if the error value for any element > epsilon, then fail -inline btScalar fuzzyCompare (const btMatrix3x3 & a, const btMatrix3x3 & b) { - btScalar totalDiff = 0; - btScalar maxDiff = 0; - for (int i = 0; i < 3; ++i) { - for (int j = 0; j < 3; ++j) { - btScalar diff = fabs(a[i][j] - b[i][j]); - totalDiff += diff; - maxDiff = qMax(diff, maxDiff); - } - } -// return totalDiff; - return maxDiff; -} - -inline QTextStream & operator << (QTextStream & stream, const btMatrix3x3 & matrix) { - stream << "[\n\t\t"; - for (int i = 0; i < 3; ++i) { - for (int j = 0; j < 3; ++j) { - stream << " " << matrix[i][j]; - } - stream << "\n\t\t"; - } - stream << "]\n\t"; // hacky as hell, but this should work... - return stream; -} - -inline btScalar fuzzyCompare(const btVector3 & a, const btVector3 & b) -{ - return (a - b).length(); -} -inline QTextStream & operator << (QTextStream & stream, const btVector3 & v) { - return stream << "btVector3 { " << v.x() << ", " << v.y() << ", " << v.z() << " }"; -} - -// Produces a relative error test for btMatrix3x3 usable with QCOMPARE_WITH_LAMBDA -inline auto errorTest (const btMatrix3x3 & actual, const btMatrix3x3 & expected, const btScalar acceptableRelativeError) --> std::function -{ - return [&actual, &expected, acceptableRelativeError] () { - for (int i = 0; i < 3; ++i) { - for (int j = 0; j < 3; ++j) { - auto err = (actual[i][j] - expected[i][j]) / expected[i][j]; - if (fabsf(err) > acceptableRelativeError) - return false; - } - } - return true; - }; -} - -#define QCOMPARE_WITH_RELATIVE_ERROR(actual, expected, relativeError) \ - QCOMPARE_WITH_LAMBDA(actual, expected, errorTest(actual, expected, relativeError)) - - -// These hook into this (and must be defined first...) -#include "../QTestExtensions.hpp" - - #endif // hifi_MeshMassPropertiesTests_h diff --git a/tests/physics/src/ShapeColliderTests.cpp b/tests/physics/src/ShapeColliderTests.cpp index 4a896ca8a4..1b22470594 100644 --- a/tests/physics/src/ShapeColliderTests.cpp +++ b/tests/physics/src/ShapeColliderTests.cpp @@ -10,7 +10,6 @@ // //#include -#include "PhysicsTestUtil.h" #include #include @@ -29,10 +28,10 @@ #include "ShapeColliderTests.h" -//const glm::vec3 origin(0.0f); -//static const glm::vec3 xAxis(1.0f, 0.0f, 0.0f); -//static const glm::vec3 yAxis(0.0f, 1.0f, 0.0f); -//static const glm::vec3 zAxis(0.0f, 0.0f, 1.0f); +const glm::vec3 origin(0.0f); +static const glm::vec3 xAxis(1.0f, 0.0f, 0.0f); +static const glm::vec3 yAxis(0.0f, 1.0f, 0.0f); +static const glm::vec3 zAxis(0.0f, 0.0f, 1.0f); QTEST_MAIN(ShapeColliderTests) diff --git a/tests/physics/src/ShapeColliderTests.h b/tests/physics/src/ShapeColliderTests.h index 73e2b972a9..c26c4311d1 100644 --- a/tests/physics/src/ShapeColliderTests.h +++ b/tests/physics/src/ShapeColliderTests.h @@ -13,6 +13,13 @@ #define hifi_ShapeColliderTests_h #include +#include + +// Add additional qtest functionality (the include order is important!) +#include "BulletTestUtils.h" +#include "GlmTestUtils.h" +#include "../QTestExtensions.hpp" + class ShapeColliderTests : public QObject { Q_OBJECT diff --git a/tests/physics/src/ShapeInfoTests.h b/tests/physics/src/ShapeInfoTests.h index bb2bff4f51..baef255ff3 100644 --- a/tests/physics/src/ShapeInfoTests.h +++ b/tests/physics/src/ShapeInfoTests.h @@ -14,6 +14,10 @@ #include +// Add additional qtest functionality (the include order is important!) +#include "BulletTestUtils.h" +#include "../QTestExtensions.hpp" + class ShapeInfoTests : public QObject { Q_OBJECT private slots: @@ -22,7 +26,8 @@ private slots: void testSphereShape(); void testCylinderShape(); void testCapsuleShape(); -// void runAllTests(); }; +#include "../QTestExtensions.hpp" + #endif // hifi_ShapeInfoTests_h