coalesce scattered dupes of unit-test utilities

This commit is contained in:
Andrew Meadows 2015-08-12 14:13:28 -07:00
parent cae77cfd76
commit 6dfbf7f70a
9 changed files with 67 additions and 79 deletions

View file

@ -14,15 +14,49 @@
#include <glm/glm.hpp>
#include <glm/gtx/quaternion.hpp>
#include <QTextStream>
// 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

View file

@ -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<bool ()> {
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))

View file

@ -16,34 +16,8 @@
#include <NumericalConstants.h>
#include <SwingTwistConstraint.h>
// 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<bool ()> {
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)

View file

@ -9,22 +9,16 @@
// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
//
#include "AngularConstraintTests.h"
#include <iostream>
#include <AngularConstraint.h>
#include <NumericalConstants.h>
#include <StreamUtils.h>
#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)

View file

@ -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);

View file

@ -13,7 +13,12 @@
#include <qqueue.h>
#include <MovingMinMaxAvg.h>
#include <NumericalConstants.h>
#include <SharedUtil.h>
#include "../QTestExtensions.h"
QTEST_MAIN(MovingMinMaxAvgTests)

View file

@ -14,15 +14,6 @@
#include <QtTest/QtTest>
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:

View file

@ -10,10 +10,13 @@
#include "TransformTests.h"
#include <algorithm>
#include <glm/glm.hpp>
#include <SharedLogging.h>
#include <Transform.h>
#include "SharedLogging.h"
#include <../QTestExtensions.h>
#include "../QTestExtensions.h"
using namespace glm;

View file

@ -12,33 +12,6 @@
#define hifi_TransformTests_h
#include <QtTest/QtTest>
#include <glm/glm.hpp>
#include <algorithm>
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