diff --git a/tests/GLMTestUtils.h b/tests/GLMTestUtils.h index 6559e9f3c9..7dbc4a840f 100644 --- a/tests/GLMTestUtils.h +++ b/tests/GLMTestUtils.h @@ -14,15 +14,49 @@ #include #include +#include // Implements functionality in QTestExtensions.h for glm types +// Computes the error value between two quaternions (using glm::dot) +float getErrorDifference(const glm::quat& a, const glm::quat& b) { + return fabsf(glm::dot(a, b)) - 1.0f; +} + inline float getErrorDifference(const glm::vec3& a, const glm::vec3& b) { return glm::distance(a, b); } +inline float getErrorDifference(const glm::mat4& a, const glm::mat4& b) { + float maxDiff = 0; + for (int i = 0; i < 4; ++i) { + for (int j = 0; j < 4; ++j) { + float diff = fabs(a[i][j] - b[i][j]); + maxDiff = std::max(diff, maxDiff); + } + } + return maxDiff; +} + inline QTextStream& operator<<(QTextStream& stream, const glm::vec3& v) { return stream << "glm::vec3 { " << v.x << ", " << v.y << ", " << v.z << " }"; } +QTextStream& operator<<(QTextStream& stream, const glm::quat& q) { + return stream << "glm::quat { " << q.x << ", " << q.y << ", " << q.z << ", " << q.w << " }"; +} + +inline QTextStream& operator<< (QTextStream& stream, const glm::mat4& matrix) { + stream << "[\n\t\t"; + stream.setFieldWidth(15); + for (int r = 0; r < 4; ++r) { + for (int c = 0; c < 4; ++c) { + stream << matrix[c][r]; + } + stream << "\n\t\t"; + } + stream.setFieldWidth(0); + stream << "]\n\t"; // hacky as hell, but this should work... + return stream; +} #endif diff --git a/tests/QTestExtensions.h b/tests/QTestExtensions.h index d1918ebed8..16e51b41ee 100644 --- a/tests/QTestExtensions.h +++ b/tests/QTestExtensions.h @@ -37,6 +37,10 @@ // +float getErrorDifference(const float& a, const float& b) { + return fabsf(a - b); +} + // Generates a QCOMPARE-style failure message that can be passed to QTest::qFail. // // Formatting looks like this: @@ -281,3 +285,20 @@ bool compareData (const char* data, const char* expectedData, size_t length) { #define COMPARE_DATA(actual, expected, length) \ QCOMPARE_WITH_EXPR((ByteData ( actual, length )), (ByteData ( expected, length )), compareData(actual, expected, length)) + + +// Produces a relative error test for float usable QCOMPARE_WITH_LAMBDA. +inline auto errorTest (float actual, float expected, float acceptableRelativeError) +-> std::function { + return [actual, expected, acceptableRelativeError] () { + if (fabsf(expected) <= acceptableRelativeError) { + return fabsf(actual - expected) < fabsf(acceptableRelativeError); + } + return fabsf((actual - expected) / expected) < fabsf(acceptableRelativeError); + }; +} + +#define QCOMPARE_WITH_RELATIVE_ERROR(actual, expected, relativeError) \ + QCOMPARE_WITH_LAMBDA(actual, expected, errorTest(actual, expected, relativeError)) + + diff --git a/tests/animation/src/RotationConstraintTests.cpp b/tests/animation/src/RotationConstraintTests.cpp index 2cdb44ee8f..4aac875f2a 100644 --- a/tests/animation/src/RotationConstraintTests.cpp +++ b/tests/animation/src/RotationConstraintTests.cpp @@ -16,34 +16,8 @@ #include #include -// HACK -- these helper functions need to be defined BEFORE including magic inside QTestExtensions.h -// TODO: fix QTestExtensions so we don't need to do this in every test. - -// Computes the error value between two quaternions (using glm::dot) -float getErrorDifference(const glm::quat& a, const glm::quat& b) { - return fabsf(glm::dot(a, b)) - 1.0f; -} - -QTextStream& operator<<(QTextStream& stream, const glm::quat& q) { - return stream << "glm::quat { " << q.x << ", " << q.y << ", " << q.z << ", " << q.w << " }"; -} - -// Produces a relative error test for float usable QCOMPARE_WITH_LAMBDA. -inline auto errorTest (float actual, float expected, float acceptableRelativeError) --> std::function { - return [actual, expected, acceptableRelativeError] () { - if (fabsf(expected) <= acceptableRelativeError) { - return fabsf(actual - expected) < fabsf(acceptableRelativeError); - } - return fabsf((actual - expected) / expected) < fabsf(acceptableRelativeError); - }; -} - #include "../QTestExtensions.h" -#define QCOMPARE_WITH_RELATIVE_ERROR(actual, expected, relativeError) \ - QCOMPARE_WITH_LAMBDA(actual, expected, errorTest(actual, expected, relativeError)) - QTEST_MAIN(RotationConstraintTests) diff --git a/tests/shared/src/AngularConstraintTests.cpp b/tests/shared/src/AngularConstraintTests.cpp index 4dcf3d7296..a711bac709 100644 --- a/tests/shared/src/AngularConstraintTests.cpp +++ b/tests/shared/src/AngularConstraintTests.cpp @@ -9,22 +9,16 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +#include "AngularConstraintTests.h" + #include #include #include #include -#include "AngularConstraintTests.h" #include "../QTestExtensions.h" -// Computes the error value between two quaternions (using glm::dot) -float getErrorDifference(const glm::quat& a, const glm::quat& b) { - return fabsf(glm::dot(a, b) - 1.0f); -} -QTextStream& operator<<(QTextStream& stream, const glm::quat& q) { - return stream << "glm::quat { " << q.x << ", " << q.y << ", " << q.z << ", " << q.w << " }"; -} QTEST_MAIN(AngularConstraintTests) diff --git a/tests/shared/src/GeometryUtilTests.cpp b/tests/shared/src/GeometryUtilTests.cpp index 44a540b478..eaf1e7cd8a 100644 --- a/tests/shared/src/GeometryUtilTests.cpp +++ b/tests/shared/src/GeometryUtilTests.cpp @@ -22,13 +22,6 @@ QTEST_MAIN(GeometryUtilTests) -float getErrorDifference(const float& a, const float& b) { - return fabsf(a - b); -} - -float getErrorDifference(const glm::vec3& a, const glm::vec3& b) { - return glm::distance(a, b); -} void GeometryUtilTests::testLocalRayRectangleIntersection() { glm::vec3 xAxis(1.0f, 0.0f, 0.0f); diff --git a/tests/shared/src/MovingMinMaxAvgTests.cpp b/tests/shared/src/MovingMinMaxAvgTests.cpp index 48c3c59058..f3ba3239ee 100644 --- a/tests/shared/src/MovingMinMaxAvgTests.cpp +++ b/tests/shared/src/MovingMinMaxAvgTests.cpp @@ -13,7 +13,12 @@ #include +#include #include +#include + +#include "../QTestExtensions.h" + QTEST_MAIN(MovingMinMaxAvgTests) diff --git a/tests/shared/src/MovingMinMaxAvgTests.h b/tests/shared/src/MovingMinMaxAvgTests.h index c64b087c15..1922bd207a 100644 --- a/tests/shared/src/MovingMinMaxAvgTests.h +++ b/tests/shared/src/MovingMinMaxAvgTests.h @@ -14,15 +14,6 @@ #include -inline float getErrorDifference(float a, float b) { - return fabsf(a - b); -} - -#include "../QTestExtensions.h" - -#include "MovingMinMaxAvg.h" -#include "SharedUtil.h" - class MovingMinMaxAvgTests : public QObject { private slots: diff --git a/tests/shared/src/TransformTests.cpp b/tests/shared/src/TransformTests.cpp index be22914b9d..b936d45555 100644 --- a/tests/shared/src/TransformTests.cpp +++ b/tests/shared/src/TransformTests.cpp @@ -10,10 +10,13 @@ #include "TransformTests.h" +#include +#include + +#include #include -#include "SharedLogging.h" -#include <../QTestExtensions.h> +#include "../QTestExtensions.h" using namespace glm; diff --git a/tests/shared/src/TransformTests.h b/tests/shared/src/TransformTests.h index a4d9b2a6c0..06cdbcd3cb 100644 --- a/tests/shared/src/TransformTests.h +++ b/tests/shared/src/TransformTests.h @@ -12,33 +12,6 @@ #define hifi_TransformTests_h #include -#include -#include - -inline float getErrorDifference(const glm::mat4& a, const glm::mat4& b) { - float maxDiff = 0; - for (int i = 0; i < 4; ++i) { - for (int j = 0; j < 4; ++j) { - float diff = fabs(a[i][j] - b[i][j]); - maxDiff = std::max(diff, maxDiff); - } - } - return maxDiff; -} - -inline QTextStream& operator<< (QTextStream& stream, const glm::mat4& matrix) { - stream << "[\n\t\t"; - stream.setFieldWidth(15); - for (int r = 0; r < 4; ++r) { - for (int c = 0; c < 4; ++c) { - stream << matrix[c][r]; - } - stream << "\n\t\t"; - } - stream.setFieldWidth(0); - stream << "]\n\t"; // hacky as hell, but this should work... - return stream; -} class TransformTests : public QObject { Q_OBJECT