From 1aba6e8fe1d70c56ad70bf8f232d06f8356e81dc Mon Sep 17 00:00:00 2001 From: Brad Davis Date: Thu, 17 May 2018 15:34:57 -0700 Subject: [PATCH] Fix unit test compile issues --- tests/QTestExtensions.h | 96 ++++++++++++++--------------- tests/animation/src/AnimTests.cpp | 14 ++--- tests/shared/src/TransformTests.cpp | 10 +-- 3 files changed, 58 insertions(+), 62 deletions(-) diff --git a/tests/QTestExtensions.h b/tests/QTestExtensions.h index ac037d831b..c9235eeedb 100644 --- a/tests/QTestExtensions.h +++ b/tests/QTestExtensions.h @@ -16,6 +16,7 @@ #include #include #include +#include #include "GLMTestUtils.h" // Implements several extensions to QtTest. @@ -37,7 +38,6 @@ // from scratch using QTest::qFail, for example). // - float getErrorDifference(const float& a, const float& b) { return fabsf(a - b); } @@ -69,7 +69,7 @@ QString QTest_generateCompareFailureMessage ( QString pad2 = QString(")").rightJustified(pad2_, ' '); QString msg; - QTextStream stream (&msg); + QTextStream stream(&msg); stream << failMessage << "\n\t" "Actual: (" << actual_expr << pad1 << ": " << actual << "\n\t" "Expected: (" << expected_expr << pad2 << ": " << expected << "\n\t"; @@ -100,7 +100,7 @@ QString QTest_generateCompareFailureMessage ( QString pad2 = QString("): ").rightJustified(pad2_, ' '); QString msg; - QTextStream stream (&msg); + QTextStream stream(&msg); stream << failMessage << "\n\t" "Actual: (" << actual_expr << pad1 << actual << "\n\t" "Expected: (" << expected_expr << pad2 << expected; @@ -134,10 +134,10 @@ void QTest_failWithCustomMessage ( // QFAIL_WITH_MESSAGE("Message " << thing << ";"); // } // -#define QFAIL_WITH_MESSAGE(...) \ -do { \ - QTest_failWithCustomMessage([&](QTextStream& stream) { stream << __VA_ARGS__; }, __LINE__, __FILE__); \ - return; \ +#define QFAIL_WITH_MESSAGE(...) \ +do { \ + QTest_failWithCustomMessage([&](QTextStream& stream) { stream << __VA_ARGS__; }, __LINE__, __FILE__); \ + return; \ } while(0) // Calls qFail using QTest_generateCompareFailureMessage. @@ -181,7 +181,7 @@ bool QTest_compareWithAbsError( actual, expected, actual_expr, expected_expr, line, file, [&] (QTextStream& stream) -> QTextStream& { return stream << "Err tolerance: " << getErrorDifference((actual), (expected)) << " > " << epsilon; - }); + }); return false; } return true; @@ -200,10 +200,10 @@ bool QTest_compareWithAbsError( // return stream << "glm::vec3 { " << v.x << ", " << v.y << ", " << v.z << " }" // } // -#define QCOMPARE_WITH_ABS_ERROR(actual, expected, epsilon) \ -do { \ - if (!QTest_compareWithAbsError((actual), (expected), #actual, #expected, __LINE__, __FILE__, epsilon)) \ - return; \ +#define QCOMPARE_WITH_ABS_ERROR(actual, expected, epsilon) \ +do { \ + if (!QTest_compareWithAbsError((actual), (expected), #actual, #expected, __LINE__, __FILE__, epsilon)) \ + return; \ } while(0) // Implements QCOMPARE using an explicit, externally defined test function. @@ -212,12 +212,12 @@ do { \ // // testFunc(const T & actual, const T & expected) -> bool: true (test succeeds) | false (test fails) // -#define QCOMPARE_WITH_FUNCTION(actual, expected, testFunc) \ -do { \ - if (!(testFunc((actual), (expected)))) { \ +#define QCOMPARE_WITH_FUNCTION(actual, expected, testFunc) \ +do { \ + if (!(testFunc((actual), (expected)))) { \ QTest_failWithMessage("Compared values are not the same", (actual), (expected), #actual, #expected, __LINE__, __FILE__); \ - return; \ - } \ + return; \ + } \ } while (0) // Implements QCOMPARE using an explicit, externally defined test function. @@ -230,41 +230,40 @@ do { \ // }); // (fails if foo is not as fooish as expectedFoo) // -#define QCOMPARE_WITH_LAMBDA(actual, expected, testClosure) \ -do { \ - if (!(testClosure())) { \ +#define QCOMPARE_WITH_LAMBDA(actual, expected, testClosure) \ +do { \ + if (!(testClosure())) { \ QTest_failWithMessage("Compared values are not the same", (actual), (expected), #actual, #expected, __LINE__, __FILE__); \ - return; \ - } \ + return; \ + } \ } while (0) // Same as QCOMPARE_WITH_FUNCTION, but with a custom fail message -#define QCOMPARE_WITH_FUNCTION_AND_MESSAGE(actual, expected, testfunc, failMessage) \ -do { \ - if (!(testFunc((actual), (expected)))) { \ - QTest_failWithMessage((failMessage), (actual), (expected), #actual, #expected, __LINE__, __FILE__); \ - return; \ - } \ +#define QCOMPARE_WITH_FUNCTION_AND_MESSAGE(actual, expected, testfunc, failMessage) \ +do { \ + if (!(testFunc((actual), (expected)))) { \ + QTest_failWithMessage((failMessage), (actual), (expected), #actual, #expected, __LINE__, __FILE__); \ + return; \ + } \ } while (0) // Same as QCOMPARE_WITH_FUNCTION, but with a custom fail message -#define QCOMPARE_WITH_LAMBDA_AND_MESSAGE(actual, expected, testClosure, failMessage) \ -do { \ - if (!(testClosure())) { \ - QTest_failWithMessage((failMessage), (actual), (expected), #actual, #expected, __LINE__, __FILE__); \ - return; \ - } \ +#define QCOMPARE_WITH_LAMBDA_AND_MESSAGE(actual, expected, testClosure, failMessage) \ +do { \ + if (!(testClosure())) { \ + QTest_failWithMessage((failMessage), (actual), (expected), #actual, #expected, __LINE__, __FILE__); \ + return; \ + } \ } while (0) #endif -#define QCOMPARE_WITH_EXPR(actual, expected, testExpr) \ - do { \ - if (!(testExpr)) { \ +#define QCOMPARE_WITH_EXPR(actual, expected, testExpr) \ + do { \ + if (!(testExpr)) { \ QTest_failWithMessage("Compared values are not the same", (actual), (expected), #actual, #expected, __LINE__, __FILE__); \ - } \ - } while(0) - + } \ + } while (0) struct ByteData { ByteData (const char* data, size_t length) @@ -273,25 +272,24 @@ struct ByteData { size_t length; }; -QTextStream & operator << (QTextStream& stream, const ByteData & wrapper) { +QTextStream& operator<<(QTextStream& stream, const ByteData& wrapper) { // Print bytes as hex stream << QByteArray::fromRawData(wrapper.data, (int)wrapper.length).toHex(); return stream; } -bool compareData (const char* data, const char* expectedData, size_t length) { +bool compareData(const char* data, const char* expectedData, size_t length) { return memcmp(data, expectedData, length) == 0; } #define COMPARE_DATA(actual, expected, length) \ - QCOMPARE_WITH_EXPR((ByteData ( actual, length )), (ByteData ( expected, length )), compareData(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] () { + return [actual, expected, acceptableRelativeError]() { if (fabsf(expected) <= acceptableRelativeError) { return fabsf(actual - expected) < fabsf(acceptableRelativeError); } @@ -302,18 +300,16 @@ inline auto errorTest (float actual, float expected, float acceptableRelativeErr #define QCOMPARE_WITH_RELATIVE_ERROR(actual, expected, relativeError) \ QCOMPARE_WITH_LAMBDA(actual, expected, errorTest(actual, expected, relativeError)) - - inline QString getTestResource(const QString& relativePath) { static QDir dir; static std::once_flag once; - std::call_once(once, []{ + std::call_once(once, [] { QFileInfo fileInfo(__FILE__); auto parentDir = fileInfo.absoluteDir(); auto rootDir = parentDir.absoluteFilePath(".."); - dir = QDir::cleanPath(rootDir); + dir = QDir::cleanPath(rootDir); }); - + return QDir::cleanPath(dir.absoluteFilePath(relativePath)); } diff --git a/tests/animation/src/AnimTests.cpp b/tests/animation/src/AnimTests.cpp index 432129594a..d758cc7a27 100644 --- a/tests/animation/src/AnimTests.cpp +++ b/tests/animation/src/AnimTests.cpp @@ -24,7 +24,7 @@ QTEST_MAIN(AnimTests) -const float EPSILON = 0.001f; +const float TEST_EPSILON = 0.001f; void AnimTests::initTestCase() { DependencyManager::registerInheritance(); @@ -86,12 +86,12 @@ void AnimTests::testClipEvaulate() { AnimNode::Triggers triggers; clip.evaluate(vars, context, framesToSec(10.0f), triggers); - QCOMPARE_WITH_ABS_ERROR(clip._frame, 12.0f, EPSILON); + QCOMPARE_WITH_ABS_ERROR(clip._frame, 12.0f, TEST_EPSILON); // does it loop? triggers.clear(); clip.evaluate(vars, context, framesToSec(12.0f), triggers); - QCOMPARE_WITH_ABS_ERROR(clip._frame, 3.0f, EPSILON); // Note: frame 3 and not 4, because extra frame between start and end. + QCOMPARE_WITH_ABS_ERROR(clip._frame, 3.0f, TEST_EPSILON); // Note: frame 3 and not 4, because extra frame between start and end. // did we receive a loop trigger? QVERIFY(std::find(triggers.begin(), triggers.end(), "myClipNodeOnLoop") != triggers.end()); @@ -100,7 +100,7 @@ void AnimTests::testClipEvaulate() { triggers.clear(); clip.setLoopFlagVar("FalseVar"); clip.evaluate(vars, context, framesToSec(20.0f), triggers); - QCOMPARE_WITH_ABS_ERROR(clip._frame, 22.0f, EPSILON); + QCOMPARE_WITH_ABS_ERROR(clip._frame, 22.0f, TEST_EPSILON); // did we receive a done trigger? QVERIFY(std::find(triggers.begin(), triggers.end(), "myClipNodeOnDone") != triggers.end()); @@ -402,7 +402,7 @@ void AnimTests::testAnimPose() { glm::vec3(10.0f, 5.0f, -7.5f) }; - const float EPSILON = 0.001f; + const float TEST_EPSILON = 0.001f; for (auto& scale : scaleVec) { for (auto& rot : rotVec) { @@ -417,7 +417,7 @@ void AnimTests::testAnimPose() { AnimPose pose(scale, rot, trans); glm::mat4 poseMat = pose; - QCOMPARE_WITH_ABS_ERROR(rawMat, poseMat, EPSILON); + QCOMPARE_WITH_ABS_ERROR(rawMat, poseMat, TEST_EPSILON); } } } @@ -437,7 +437,7 @@ void AnimTests::testAnimPose() { // now build a new matrix from those parts. glm::mat4 poseMat = pose; - QCOMPARE_WITH_ABS_ERROR(rawMat, poseMat, EPSILON); + QCOMPARE_WITH_ABS_ERROR(rawMat, poseMat, TEST_EPSILON); } } } diff --git a/tests/shared/src/TransformTests.cpp b/tests/shared/src/TransformTests.cpp index ca55bbf873..085d02fd0f 100644 --- a/tests/shared/src/TransformTests.cpp +++ b/tests/shared/src/TransformTests.cpp @@ -29,7 +29,7 @@ const quat rot90 = glm::angleAxis((float)M_PI / 2.0f, yAxis); QTEST_MAIN(TransformTests) -const float EPSILON = 0.001f; +const float TEST_EPSILON = 0.001f; void TransformTests::getMatrix() { @@ -55,7 +55,7 @@ void TransformTests::getMatrix() { mat4 result_b; xform.getMatrix(result_b); - QCOMPARE_WITH_ABS_ERROR(result_a, result_b, EPSILON); + QCOMPARE_WITH_ABS_ERROR(result_a, result_b, TEST_EPSILON); } void TransformTests::getInverseMatrix() { @@ -92,7 +92,7 @@ void TransformTests::getInverseMatrix() { auto yb = transformPoint(result_b, yAxis); auto zb = transformPoint(result_b, zAxis); - QCOMPARE_WITH_ABS_ERROR(xa, xb, EPSILON); - QCOMPARE_WITH_ABS_ERROR(ya, yb, EPSILON); - QCOMPARE_WITH_ABS_ERROR(za, zb, EPSILON); + QCOMPARE_WITH_ABS_ERROR(xa, xb, TEST_EPSILON); + QCOMPARE_WITH_ABS_ERROR(ya, yb, TEST_EPSILON); + QCOMPARE_WITH_ABS_ERROR(za, zb, TEST_EPSILON); }