diff options
Diffstat (limited to 'src/testlib/qtestcase.cpp')
-rw-r--r-- | src/testlib/qtestcase.cpp | 189 |
1 files changed, 114 insertions, 75 deletions
diff --git a/src/testlib/qtestcase.cpp b/src/testlib/qtestcase.cpp index 0c935a1f1c..1f69429053 100644 --- a/src/testlib/qtestcase.cpp +++ b/src/testlib/qtestcase.cpp @@ -52,6 +52,7 @@ #include <QtCore/qfileinfo.h> #include <QtCore/qdir.h> #include <QtCore/qdebug.h> +#include <QtCore/qfloat16.h> #include <QtCore/qlibraryinfo.h> #include <QtCore/private/qtools_p.h> #include <QtCore/qdiriterator.h> @@ -249,7 +250,7 @@ static void stackTrace() static bool installCoverageTool(const char * appname, const char * testname) { -#ifdef __COVERAGESCANNER__ +#if defined(__COVERAGESCANNER__) && !QT_CONFIG(testlib_selfcover) if (!qEnvironmentVariableIsEmpty("QT_TESTCOCOON_ACTIVE")) return false; // Set environment variable QT_TESTCOCOON_ACTIVE to prevent an eventual subtest from @@ -286,74 +287,75 @@ namespace QTestPrivate namespace QTest { - class WatchDog; +class WatchDog; - static QObject *currentTestObject = 0; - static QString mainSourcePath; +static QObject *currentTestObject = 0; +static QString mainSourcePath; #if defined(Q_OS_MACOS) - bool macNeedsActivate = false; - IOPMAssertionID powerID; +bool macNeedsActivate = false; +IOPMAssertionID powerID; #endif - class TestMethods { - Q_DISABLE_COPY(TestMethods) - public: - typedef std::vector<QMetaMethod> MetaMethods; +class TestMethods { +public: + Q_DISABLE_COPY_MOVE(TestMethods) - explicit TestMethods(const QObject *o, const MetaMethods &m = MetaMethods()); + typedef std::vector<QMetaMethod> MetaMethods; - void invokeTests(QObject *testObject) const; + explicit TestMethods(const QObject *o, const MetaMethods &m = MetaMethods()); - static QMetaMethod findMethod(const QObject *obj, const char *signature); + void invokeTests(QObject *testObject) const; - private: - bool invokeTest(int index, const char *data, WatchDog *watchDog) const; - void invokeTestOnData(int index) const; + static QMetaMethod findMethod(const QObject *obj, const char *signature); - QMetaMethod m_initTestCaseMethod; // might not exist, check isValid(). - QMetaMethod m_initTestCaseDataMethod; - QMetaMethod m_cleanupTestCaseMethod; - QMetaMethod m_initMethod; - QMetaMethod m_cleanupMethod; +private: + bool invokeTest(int index, const char *data, WatchDog *watchDog) const; + void invokeTestOnData(int index) const; - MetaMethods m_methods; - }; + QMetaMethod m_initTestCaseMethod; // might not exist, check isValid(). + QMetaMethod m_initTestCaseDataMethod; + QMetaMethod m_cleanupTestCaseMethod; + QMetaMethod m_initMethod; + QMetaMethod m_cleanupMethod; - TestMethods::TestMethods(const QObject *o, const MetaMethods &m) - : m_initTestCaseMethod(TestMethods::findMethod(o, "initTestCase()")) - , m_initTestCaseDataMethod(TestMethods::findMethod(o, "initTestCase_data()")) - , m_cleanupTestCaseMethod(TestMethods::findMethod(o, "cleanupTestCase()")) - , m_initMethod(TestMethods::findMethod(o, "init()")) - , m_cleanupMethod(TestMethods::findMethod(o, "cleanup()")) - , m_methods(m) - { - if (m.empty()) { - const QMetaObject *metaObject = o->metaObject(); - const int count = metaObject->methodCount(); - m_methods.reserve(count); - for (int i = 0; i < count; ++i) { - const QMetaMethod me = metaObject->method(i); - if (isValidSlot(me)) - m_methods.push_back(me); - } + MetaMethods m_methods; +}; + +TestMethods::TestMethods(const QObject *o, const MetaMethods &m) + : m_initTestCaseMethod(TestMethods::findMethod(o, "initTestCase()")) + , m_initTestCaseDataMethod(TestMethods::findMethod(o, "initTestCase_data()")) + , m_cleanupTestCaseMethod(TestMethods::findMethod(o, "cleanupTestCase()")) + , m_initMethod(TestMethods::findMethod(o, "init()")) + , m_cleanupMethod(TestMethods::findMethod(o, "cleanup()")) + , m_methods(m) +{ + if (m.empty()) { + const QMetaObject *metaObject = o->metaObject(); + const int count = metaObject->methodCount(); + m_methods.reserve(count); + for (int i = 0; i < count; ++i) { + const QMetaMethod me = metaObject->method(i); + if (isValidSlot(me)) + m_methods.push_back(me); } } +} - QMetaMethod TestMethods::findMethod(const QObject *obj, const char *signature) - { - const QMetaObject *metaObject = obj->metaObject(); - const int funcIndex = metaObject->indexOfMethod(signature); - return funcIndex >= 0 ? metaObject->method(funcIndex) : QMetaMethod(); - } +QMetaMethod TestMethods::findMethod(const QObject *obj, const char *signature) +{ + const QMetaObject *metaObject = obj->metaObject(); + const int funcIndex = metaObject->indexOfMethod(signature); + return funcIndex >= 0 ? metaObject->method(funcIndex) : QMetaMethod(); +} - static int keyDelay = -1; - static int mouseDelay = -1; - static int eventDelay = -1; +static int keyDelay = -1; +static int mouseDelay = -1; +static int eventDelay = -1; #if QT_CONFIG(thread) - static int timeout = -1; +static int timeout = -1; #endif - static bool noCrashHandler = false; +static bool noCrashHandler = false; /*! \internal Invoke a method of the object without generating warning if the method does not exist @@ -500,7 +502,7 @@ static void qPrintDataTags(FILE *stream) } } -static int qToInt(char *str) +static int qToInt(const char *str) { char *pEnd; int l = (int)strtol(str, &pEnd, 10); @@ -511,7 +513,7 @@ static int qToInt(char *str) return l; } -Q_TESTLIB_EXPORT void qtest_qParseArgs(int argc, char *argv[], bool qml) +Q_TESTLIB_EXPORT void qtest_qParseArgs(int argc, const char *const argv[], bool qml) { int logFormat = -1; // Not set const char *logFilename = 0; @@ -815,9 +817,9 @@ Q_TESTLIB_EXPORT void qtest_qParseArgs(int argc, char *argv[], bool qml) // we load the QML files. So just store the data for now. int colon = -1; int offset; - for (offset = 0; *(argv[i]+offset); ++offset) { - if (*(argv[i]+offset) == ':') { - if (*(argv[i]+offset+1) == ':') { + for (offset = 0; argv[i][offset]; ++offset) { + if (argv[i][offset] == ':') { + if (argv[i][offset + 1] == ':') { // "::" is used as a test name separator. // e.g. "ClickTests::test_click:row1". ++offset; @@ -864,6 +866,11 @@ Q_TESTLIB_EXPORT void qtest_qParseArgs(int argc, char *argv[], bool qml) QTestLog::addLogger(QTestLog::Plain, logFilename); } +// Temporary, backwards compatibility, until qtdeclarative's use of it is converted +Q_TESTLIB_EXPORT void qtest_qParseArgs(int argc, char *argv[], bool qml) { + qtest_qParseArgs(argc, const_cast<const char *const *>(argv), qml); +} + QBenchmarkResult qMedian(const QVector<QBenchmarkResult> &container) { const int count = container.count(); @@ -1214,7 +1221,9 @@ char *formatString(const char *prefix, const char *suffix, size_t numArguments, Returns a pointer to a string that is the string \a ba represented as a space-separated sequence of hex characters. If the input is considered too long, it is truncated. A trucation is indicated in - the returned string as an ellipsis at the end. + the returned string as an ellipsis at the end. The caller has + ownership of the returned pointer and must ensure it is later passed + to operator delete[]. \a length is the length of the string \a ba. */ @@ -1616,7 +1625,7 @@ FatalSignalHandler::~FatalSignalHandler() // Helper class for resolving symbol names by dynamically loading "dbghelp.dll". class DebugSymbolResolver { - Q_DISABLE_COPY(DebugSymbolResolver) + Q_DISABLE_COPY_MOVE(DebugSymbolResolver) public: struct Symbol { Symbol() : name(nullptr), address(0) {} @@ -1832,8 +1841,6 @@ void QTest::qInit(QObject *testObject, int argc, char **argv) #endif QTestPrivate::parseBlackList(); - QTestPrivate::parseGpuBlackList(); - QTestResult::reset(); QTEST_ASSERT(testObject); @@ -2183,13 +2190,12 @@ QString QTest::qFindTestData(const QString& base, const char *file, int line, co if (found.isEmpty()) { const char *testObjectName = QTestResult::currentTestObjectName(); if (testObjectName) { - QString testsPath = QLibraryInfo::location(QLibraryInfo::TestsPath); - QString candidate = QString::fromLatin1("%1/%2/%3") + const QString testsPath = QLibraryInfo::location(QLibraryInfo::TestsPath); + const QString candidate = QString::fromLatin1("%1/%2/%3") .arg(testsPath, QFile::decodeName(testObjectName).toLower(), base); if (QFileInfo::exists(candidate)) { found = candidate; - } - else if (QTestLog::verboseLevel() >= 2) { + } else if (QTestLog::verboseLevel() >= 2) { QTestLog::info(qPrintable( QString::fromLatin1("testdata %1 not found in tests install path [%2]; " "checking next location") @@ -2211,11 +2217,10 @@ QString QTest::qFindTestData(const QString& base, const char *file, int line, co } const QString canonicalPath = srcdir.canonicalFilePath(); - QString candidate = QString::fromLatin1("%1/%2").arg(canonicalPath, base); + const QString candidate = QString::fromLatin1("%1/%2").arg(canonicalPath, base); if (!canonicalPath.isEmpty() && QFileInfo::exists(candidate)) { found = candidate; - } - else if (QTestLog::verboseLevel() >= 2) { + } else if (QTestLog::verboseLevel() >= 2) { QTestLog::info(qPrintable( QString::fromLatin1("testdata %1 not found relative to source path [%2]") .arg(base, QDir::toNativeSeparators(candidate))), @@ -2225,31 +2230,48 @@ QString QTest::qFindTestData(const QString& base, const char *file, int line, co // 4. Try resources if (found.isEmpty()) { - QString candidate = QString::fromLatin1(":/%1").arg(base); - if (QFileInfo::exists(candidate)) + const QString candidate = QString::fromLatin1(":/%1").arg(base); + if (QFileInfo::exists(candidate)) { found = candidate; + } else if (QTestLog::verboseLevel() >= 2) { + QTestLog::info(qPrintable( + QString::fromLatin1("testdata %1 not found in resources [%2]") + .arg(base, QDir::toNativeSeparators(candidate))), + file, line); + } } // 5. Try current directory if (found.isEmpty()) { const QString candidate = QDir::currentPath() + QLatin1Char('/') + base; - if (QFileInfo::exists(candidate)) + if (QFileInfo::exists(candidate)) { found = candidate; + } else if (QTestLog::verboseLevel() >= 2) { + QTestLog::info(qPrintable( + QString::fromLatin1("testdata %1 not found in current directory [%2]") + .arg(base, QDir::toNativeSeparators(candidate))), + file, line); + } } // 6. Try main source directory if (found.isEmpty()) { - QString candidate = QTest::mainSourcePath % QLatin1Char('/') % base; - if (QFileInfo::exists(candidate)) + const QString candidate = QTest::mainSourcePath % QLatin1Char('/') % base; + if (QFileInfo::exists(candidate)) { found = candidate; + } else if (QTestLog::verboseLevel() >= 2) { + QTestLog::info(qPrintable( + QString::fromLatin1("testdata %1 not found in main source directory [%2]") + .arg(base, QDir::toNativeSeparators(candidate))), + file, line); + } } if (found.isEmpty()) { QTest::qWarn(qPrintable( QString::fromLatin1("testdata %1 could not be located!").arg(base)), file, line); - } - else if (QTestLog::verboseLevel() >= 1) { + } else if (QTestLog::verboseLevel() >= 1) { QTestLog::info(qPrintable( QString::fromLatin1("testdata %1 was located at %2").arg(base, QDir::toNativeSeparators(found))), file, line); @@ -2323,7 +2345,7 @@ void QTest::addColumnInternal(int id, const char *name) */ QTestData &QTest::newRow(const char *dataTag) { - QTEST_ASSERT_X(dataTag, "QTest::newRow()", "Data tag can not be null"); + QTEST_ASSERT_X(dataTag, "QTest::newRow()", "Data tag cannot be null"); QTestTable *tbl = QTestTable::currentTestTable(); QTEST_ASSERT_X(tbl, "QTest::newRow()", "Cannot add testdata outside of a _data slot."); QTEST_ASSERT_X(tbl->elementCount(), "QTest::newRow()", "Must add columns before attempting to add rows."); @@ -2497,6 +2519,16 @@ bool QTest::compare_helper(bool success, const char *failureMsg, return QTestResult::compare(success, failureMsg, val1, val2, actual, expected, file, line); } +/*! \fn bool QTest::qCompare(const qfloat16 &t1, const qfloat16 &t2, const char *actual, const char *expected, const char *file, int line) + \internal + */ +bool QTest::qCompare(qfloat16 const &t1, qfloat16 const &t2, const char *actual, const char *expected, + const char *file, int line) +{ + return compare_helper(qFuzzyCompare(t1, t2), "Compared qfloat16s are not the same (fuzzy compare)", + toString(t1), toString(t2), actual, expected, file, line); +} + /*! \fn bool QTest::qCompare(const float &t1, const float &t2, const char *actual, const char *expected, const char *file, int line) \internal */ @@ -2619,6 +2651,13 @@ template <> Q_TESTLIB_EXPORT char *QTest::toString<TYPE>(const TYPE &t) \ TO_STRING_FLOAT(float, %g) TO_STRING_FLOAT(double, %.12lg) +template <> Q_TESTLIB_EXPORT char *QTest::toString<qfloat16>(const qfloat16 &t) +{ + char *msg = new char[16]; + qsnprintf(msg, 16, "%.3g", static_cast<float>(t)); + return msg; +} + template <> Q_TESTLIB_EXPORT char *QTest::toString<char>(const char &t) { unsigned char c = static_cast<unsigned char>(t); |