File indexing completed on 2026-05-05 08:49:06
0001
0002
0003
0004 #ifndef QTESTCASE_H
0005 #define QTESTCASE_H
0006
0007 #include <QtTest/qttestglobal.h>
0008 #include <QtTest/qtesttostring.h>
0009
0010 #include <QtCore/qstring.h>
0011 #include <QtCore/qnamespace.h>
0012 #include <QtCore/qmetatype.h>
0013 #include <QtCore/qmetaobject.h>
0014 #include <QtCore/qsharedpointer.h>
0015 #include <QtCore/qtemporarydir.h>
0016 #include <QtCore/qthread.h>
0017
0018 #include <chrono>
0019 #ifdef __cpp_concepts
0020 #include <concepts>
0021 #endif
0022 #include <QtCore/qxpfunctional.h>
0023 #include <QtCore/qxptype_traits.h>
0024 #include <QtCore/q20utility.h>
0025
0026 #include <string.h>
0027
0028 #ifndef QT_NO_EXCEPTIONS
0029 # include <exception>
0030 #endif
0031
0032 QT_BEGIN_NAMESPACE
0033
0034 #ifndef QT_NO_EXCEPTIONS
0035
0036 #ifdef QTEST_THROW_ON_FAIL
0037 # define QTEST_FAIL_ACTION QTest::Internal::throwOnFail()
0038 #else
0039 # define QTEST_FAIL_ACTION do { QTest::Internal::maybeThrowOnFail(); return; } while (false)
0040 #endif
0041
0042 #ifdef QTEST_THROW_ON_SKIP
0043 # define QTEST_SKIP_ACTION QTest::Internal::throwOnSkip()
0044 #else
0045 # define QTEST_SKIP_ACTION do { QTest::Internal::maybeThrowOnSkip(); return; } while (false)
0046 #endif
0047
0048 #else
0049 # if defined(QTEST_THROW_ON_FAIL) || defined(QTEST_THROW_ON_SKIP)
0050 # error QTEST_THROW_ON_FAIL/SKIP require exception support enabled.
0051 # endif
0052 #endif
0053
0054 #ifndef QTEST_FAIL_ACTION
0055 # define QTEST_FAIL_ACTION return
0056 #endif
0057
0058 #ifndef QTEST_SKIP_ACTION
0059 # define QTEST_SKIP_ACTION return
0060 #endif
0061
0062 class qfloat16;
0063 class QRegularExpression;
0064
0065 #define QVERIFY(statement) \
0066 do {\
0067 if (!QTest::qVerify(static_cast<bool>(statement), #statement, "", __FILE__, __LINE__))\
0068 QTEST_FAIL_ACTION; \
0069 } while (false)
0070
0071 #define QFAIL(message) \
0072 do {\
0073 QTest::qFail(static_cast<const char *>(message), __FILE__, __LINE__);\
0074 QTEST_FAIL_ACTION; \
0075 } while (false)
0076
0077 #define QVERIFY2(statement, description) \
0078 do {\
0079 if (statement) {\
0080 if (!QTest::qVerify(true, #statement, static_cast<const char *>(description), __FILE__, __LINE__))\
0081 QTEST_FAIL_ACTION; \
0082 } else {\
0083 if (!QTest::qVerify(false, #statement, static_cast<const char *>(description), __FILE__, __LINE__))\
0084 QTEST_FAIL_ACTION; \
0085 }\
0086 } while (false)
0087
0088 #define QCOMPARE(actual, expected) \
0089 do {\
0090 if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\
0091 QTEST_FAIL_ACTION; \
0092 } while (false)
0093
0094 #define QCOMPARE_OP_IMPL(lhs, rhs, op, opId) \
0095 do { \
0096 if (!QTest::qCompareOp<QTest::ComparisonOperation::opId>(lhs, rhs, #lhs, #rhs, __FILE__, __LINE__)) \
0097 QTEST_FAIL_ACTION; \
0098 } while (false)
0099
0100 #define QCOMPARE_EQ(computed, baseline) QCOMPARE_OP_IMPL(computed, baseline, ==, Equal)
0101 #define QCOMPARE_NE(computed, baseline) QCOMPARE_OP_IMPL(computed, baseline, !=, NotEqual)
0102 #define QCOMPARE_LT(computed, baseline) QCOMPARE_OP_IMPL(computed, baseline, <, LessThan)
0103 #define QCOMPARE_LE(computed, baseline) QCOMPARE_OP_IMPL(computed, baseline, <=, LessThanOrEqual)
0104 #define QCOMPARE_GT(computed, baseline) QCOMPARE_OP_IMPL(computed, baseline, >, GreaterThan)
0105 #define QCOMPARE_GE(computed, baseline) QCOMPARE_OP_IMPL(computed, baseline, >=, GreaterThanOrEqual)
0106
0107 #ifndef QT_NO_EXCEPTIONS
0108
0109 # define QVERIFY_THROWS_NO_EXCEPTION(...) \
0110 do { \
0111 QT_TRY { \
0112 __VA_ARGS__; \
0113 \
0114 } QT_CATCH (...) { \
0115 QTest::qCaught(nullptr, __FILE__, __LINE__); \
0116 QTEST_FAIL_ACTION; \
0117 } \
0118 } while (false) \
0119
0120
0121 #if QT_DEPRECATED_SINCE(6, 3)
0122 namespace QTest {
0123 QT_DEPRECATED_VERSION_X_6_3("Don't use QVERIFY_EXCEPTION_THROWN(expr, type) anymore, "
0124 "use QVERIFY_THROWS_EXCEPTION(type, expr...) instead")
0125 inline void useVerifyThrowsException() {}
0126 }
0127 # define QVERIFY_EXCEPTION_THROWN(expression, exceptiontype) \
0128 QVERIFY_THROWS_EXCEPTION(exceptiontype, QTest::useVerifyThrowsException(); expression)
0129 #endif
0130
0131 # define QVERIFY_THROWS_EXCEPTION(exceptiontype, ...) \
0132 do {\
0133 bool qverify_throws_exception_did_not_throw = false; \
0134 QT_TRY {\
0135 __VA_ARGS__; \
0136 QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \
0137 " but no exception caught", __FILE__, __LINE__); \
0138 qverify_throws_exception_did_not_throw = true; \
0139 } QT_CATCH (const exceptiontype &) { \
0140 \
0141 } QT_CATCH (...) {\
0142 QTest::qCaught(#exceptiontype, __FILE__, __LINE__); \
0143 QTEST_FAIL_ACTION; \
0144 }\
0145 if (qverify_throws_exception_did_not_throw) \
0146 QTEST_FAIL_ACTION; \
0147 } while (false)
0148
0149 #else
0150
0151
0152
0153
0154
0155
0156
0157
0158 # define QVERIFY_THROWS_EXCEPTION(...) \
0159 static_assert(false, "Support for exceptions is disabled")
0160 # define QVERIFY_THROWS_NO_EXCEPTION(...) \
0161 static_assert(false, "Support for exceptions is disabled")
0162
0163 #endif
0164
0165
0166
0167
0168
0169
0170
0171
0172
0173
0174
0175 #define QTRY_LOOP_IMPL(expr, timeoutValue, step) \
0176 if (!(expr)) { \
0177 QTest::qWait(0); \
0178 } \
0179 std::chrono::milliseconds timeoutValueMs(timeoutValue); \
0180 std::chrono::milliseconds stepMs(step); \
0181 auto qt_test_i = std::chrono::milliseconds(0); \
0182 for (; qt_test_i < timeoutValueMs && !(QTest::runningTest() && QTest::currentTestResolved()) \
0183 && !(expr); qt_test_i += stepMs) { \
0184 QTest::qWait(stepMs); \
0185 }
0186
0187
0188 #define QTRY_TIMEOUT_DEBUG_IMPL(expr, timeoutValue, step) \
0189 if (!(QTest::runningTest() && QTest::currentTestResolved()) && !(expr)) { \
0190 QTRY_LOOP_IMPL(expr, 2 * (timeoutValue), step) \
0191 if ((expr)) { \
0192 QFAIL(qPrintable(QTest::Internal::formatTryTimeoutDebugMessage(\
0193 u8"" #expr, timeoutValue, timeoutValue + qt_test_i))); \
0194 } \
0195 }
0196
0197 #define QTRY_IMPL(expr, timeoutAsGiven)\
0198 const auto qt_test_timeoutAsMs = [&] { \
0199 \
0200 using namespace std::chrono_literals; \
0201 return std::chrono::milliseconds{timeoutAsGiven}; \
0202 }(); \
0203 const auto qt_test_step = qt_test_timeoutAsMs < std::chrono::milliseconds(350) \
0204 ? qt_test_timeoutAsMs / 7 + std::chrono::milliseconds(1) \
0205 : std::chrono::milliseconds(50); \
0206 { QTRY_LOOP_IMPL(expr, qt_test_timeoutAsMs, qt_test_step) } \
0207 QTRY_TIMEOUT_DEBUG_IMPL(expr, qt_test_timeoutAsMs, qt_test_step)
0208
0209
0210
0211 #define QTRY_VERIFY_WITH_TIMEOUT(expr, timeout) \
0212 do { \
0213 QTRY_IMPL(expr, timeout) \
0214 QVERIFY(expr); \
0215 } while (false)
0216
0217 #define QTRY_VERIFY(expr) QTRY_VERIFY_WITH_TIMEOUT(expr, QTest::Internal::defaultTryTimeout)
0218
0219
0220 #define QTRY_VERIFY2_WITH_TIMEOUT(expr, messageExpression, timeout) \
0221 do { \
0222 QTRY_IMPL(expr, timeout) \
0223 QVERIFY2(expr, messageExpression); \
0224 } while (false)
0225
0226 #define QTRY_VERIFY2(expr, messageExpression) \
0227 QTRY_VERIFY2_WITH_TIMEOUT(expr, messageExpression, QTest::Internal::defaultTryTimeout)
0228
0229
0230 #define QTRY_COMPARE_WITH_TIMEOUT(expr, expected, timeout) \
0231 do { \
0232 QTRY_IMPL((expr) == (expected), timeout) \
0233 QCOMPARE(expr, expected); \
0234 } while (false)
0235
0236 #define QTRY_COMPARE(expr, expected) \
0237 QTRY_COMPARE_WITH_TIMEOUT(expr, expected, QTest::Internal::defaultTryTimeout)
0238
0239 #define QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, op, opId, timeout) \
0240 do { \
0241 using Q_Cmp = QTest::Internal::Compare<QTest::ComparisonOperation::opId>; \
0242 QTRY_IMPL(Q_Cmp::compare((computed), (baseline)), timeout) \
0243 QCOMPARE_OP_IMPL(computed, baseline, op, opId); \
0244 } while (false)
0245
0246 #define QTRY_COMPARE_EQ_WITH_TIMEOUT(computed, baseline, timeout) \
0247 QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, ==, Equal, timeout)
0248
0249 #define QTRY_COMPARE_EQ(computed, baseline) \
0250 QTRY_COMPARE_EQ_WITH_TIMEOUT(computed, baseline, QTest::Internal::defaultTryTimeout)
0251
0252 #define QTRY_COMPARE_NE_WITH_TIMEOUT(computed, baseline, timeout) \
0253 QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, !=, NotEqual, timeout)
0254
0255 #define QTRY_COMPARE_NE(computed, baseline) \
0256 QTRY_COMPARE_NE_WITH_TIMEOUT(computed, baseline, QTest::Internal::defaultTryTimeout)
0257
0258 #define QTRY_COMPARE_LT_WITH_TIMEOUT(computed, baseline, timeout) \
0259 QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, <, LessThan, timeout)
0260
0261 #define QTRY_COMPARE_LT(computed, baseline) \
0262 QTRY_COMPARE_LT_WITH_TIMEOUT(computed, baseline, QTest::Internal::defaultTryTimeout)
0263
0264 #define QTRY_COMPARE_LE_WITH_TIMEOUT(computed, baseline, timeout) \
0265 QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, <=, LessThanOrEqual, timeout)
0266
0267 #define QTRY_COMPARE_LE(computed, baseline) \
0268 QTRY_COMPARE_LE_WITH_TIMEOUT(computed, baseline, QTest::Internal::defaultTryTimeout)
0269
0270 #define QTRY_COMPARE_GT_WITH_TIMEOUT(computed, baseline, timeout) \
0271 QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, >, GreaterThan, timeout)
0272
0273 #define QTRY_COMPARE_GT(computed, baseline) \
0274 QTRY_COMPARE_GT_WITH_TIMEOUT(computed, baseline, QTest::Internal::defaultTryTimeout)
0275
0276 #define QTRY_COMPARE_GE_WITH_TIMEOUT(computed, baseline, timeout) \
0277 QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, >=, GreaterThanOrEqual, timeout)
0278
0279 #define QTRY_COMPARE_GE(computed, baseline) \
0280 QTRY_COMPARE_GE_WITH_TIMEOUT(computed, baseline, QTest::Internal::defaultTryTimeout)
0281
0282 #define QSKIP_INTERNAL(statement) \
0283 do {\
0284 QTest::qSkip(static_cast<const char *>(statement), __FILE__, __LINE__);\
0285 QTEST_SKIP_ACTION; \
0286 } while (false)
0287
0288 #define QSKIP(statement, ...) QSKIP_INTERNAL(statement)
0289
0290 #define QEXPECT_FAIL(dataIndex, comment, mode)\
0291 do {\
0292 if (!QTest::qExpectFail(dataIndex, static_cast<const char *>(comment), QTest::mode, __FILE__, __LINE__))\
0293 QTEST_FAIL_ACTION; \
0294 } while (false)
0295
0296 #define QFETCH(Type, name)\
0297 Type name = *static_cast<Type *>(QTest::qData(#name, ::qMetaTypeId<typename std::remove_cv<Type >::type>()))
0298
0299 #define QFETCH_GLOBAL(Type, name)\
0300 Type name = *static_cast<Type *>(QTest::qGlobalData(#name, ::qMetaTypeId<typename std::remove_cv<Type >::type>()))
0301
0302 #define QTEST(actual, testElement)\
0303 do {\
0304 if (!QTest::qTest(actual, testElement, #actual, #testElement, __FILE__, __LINE__))\
0305 QTEST_FAIL_ACTION; \
0306 } while (false)
0307
0308 #ifdef __cpp_lib_three_way_comparison
0309 #define QCOMPARE_3WAY(lhs, rhs, order) \
0310 do { \
0311 if (!QTest::qCompare3Way(lhs, rhs, order, #lhs, #rhs, #order, __FILE__, __LINE__)) \
0312 QTEST_FAIL_ACTION; \
0313 } while (false)
0314 #else
0315 #define QCOMPARE_3WAY(...) \
0316 static_assert(false, "QCOMPARE_3WAY test requires C++20 operator<=>()")
0317 #endif
0318
0319 #ifdef QT_TESTCASE_BUILDDIR
0320
0321 #ifndef QT_TESTCASE_SOURCEDIR
0322 #define QT_TESTCASE_SOURCEDIR nullptr
0323 #endif
0324
0325 # define QFINDTESTDATA(basepath)\
0326 QTest::qFindTestData(basepath, __FILE__, __LINE__, QT_TESTCASE_BUILDDIR, QT_TESTCASE_SOURCEDIR)
0327 #else
0328 # define QFINDTESTDATA(basepath)\
0329 QTest::qFindTestData(basepath, __FILE__, __LINE__)
0330 #endif
0331
0332 # define QEXTRACTTESTDATA(resourcePath) \
0333 QTest::qExtractTestData(resourcePath)
0334
0335 class QObject;
0336 class QTestData;
0337
0338 namespace QTest
0339 {
0340 namespace Internal {
0341
0342 [[noreturn]] Q_TESTLIB_EXPORT void throwOnFail();
0343 [[noreturn]] Q_TESTLIB_EXPORT void throwOnSkip();
0344 Q_TESTLIB_EXPORT void maybeThrowOnFail();
0345 Q_TESTLIB_EXPORT void maybeThrowOnSkip();
0346
0347 Q_DECL_COLD_FUNCTION
0348 Q_TESTLIB_EXPORT QString formatTryTimeoutDebugMessage(q_no_char8_t::QUtf8StringView expr,
0349 std::chrono::milliseconds timeout,
0350 std::chrono::milliseconds actual);
0351 Q_TESTLIB_EXPORT Q_DECL_COLD_FUNCTION
0352 const char *formatPropertyTestHelperFailure(char *msg, size_t maxMsgLen,
0353 const char *actual, const char *expected,
0354 const char *actualExpr,
0355 const char *expectedExpr);
0356
0357 template <ComparisonOperation> struct Compare;
0358 template <> struct Compare<ComparisonOperation::Equal>
0359 {
0360 template <typename T1, typename T2> static bool compare(T1 &&lhs, T2 &&rhs)
0361 { return std::forward<T1>(lhs) == std::forward<T2>(rhs); }
0362 };
0363 template <> struct Compare<ComparisonOperation::NotEqual>
0364 {
0365 template <typename T1, typename T2> static bool compare(T1 &&lhs, T2 &&rhs)
0366 { return std::forward<T1>(lhs) != std::forward<T2>(rhs); }
0367 };
0368 template <> struct Compare<ComparisonOperation::LessThan>
0369 {
0370 template <typename T1, typename T2> static bool compare(T1 &&lhs, T2 &&rhs)
0371 { return std::forward<T1>(lhs) < std::forward<T2>(rhs); }
0372 };
0373 template <> struct Compare<ComparisonOperation::LessThanOrEqual>
0374 {
0375 template <typename T1, typename T2> static bool compare(T1 &&lhs, T2 &&rhs)
0376 { return std::forward<T1>(lhs) <= std::forward<T2>(rhs); }
0377 };
0378 template <> struct Compare<ComparisonOperation::GreaterThan>
0379 {
0380 template <typename T1, typename T2> static bool compare(T1 &&lhs, T2 &&rhs)
0381 { return std::forward<T1>(lhs) > std::forward<T2>(rhs); }
0382 };
0383 template <> struct Compare<ComparisonOperation::GreaterThanOrEqual>
0384 {
0385 template <typename T1, typename T2> static bool compare(T1 &&lhs, T2 &&rhs)
0386 { return std::forward<T1>(lhs) >= std::forward<T2>(rhs); }
0387 };
0388
0389 template <typename T1> const char *genericToString(const void *arg)
0390 {
0391 using QTest::toString;
0392 return toString(*static_cast<const T1 *>(arg));
0393 }
0394
0395 template <> inline const char *genericToString<char *>(const void *arg)
0396 {
0397 using QTest::toString;
0398 return toString(static_cast<const char *>(arg));
0399 }
0400
0401 template <> inline const char *genericToString<std::nullptr_t>(const void *)
0402 {
0403 return QTest::toString(nullptr);
0404 }
0405
0406 template <typename T> const char *pointerToString(const void *arg)
0407 {
0408 using QTest::toString;
0409 return toString(static_cast<const T *>(arg));
0410 }
0411
0412
0413 Q_TESTLIB_EXPORT extern bool noCrashHandler;
0414
0415 }
0416
0417 Q_TESTLIB_EXPORT void qInit(QObject *testObject, int argc = 0, char **argv = nullptr);
0418 Q_TESTLIB_EXPORT int qRun();
0419 Q_TESTLIB_EXPORT void qCleanup();
0420
0421 Q_TESTLIB_EXPORT int qExec(QObject *testObject, int argc = 0, char **argv = nullptr);
0422 Q_TESTLIB_EXPORT int qExec(QObject *testObject, const QStringList &arguments);
0423
0424 #if QT_CONFIG(batch_test_support) || defined(Q_QDOC)
0425 using TestEntryFunction = int (*)(int, char **);
0426 Q_TESTLIB_EXPORT void qRegisterTestCase(const QString &name, TestEntryFunction entryFunction);
0427 #endif
0428
0429 Q_TESTLIB_EXPORT void setMainSourcePath(const char *file, const char *builddir = nullptr);
0430 Q_TESTLIB_EXPORT void setThrowOnFail(bool enable) noexcept;
0431 Q_TESTLIB_EXPORT void setThrowOnSkip(bool enable) noexcept;
0432
0433 class ThrowOnFailEnabler {
0434 Q_DISABLE_COPY_MOVE(ThrowOnFailEnabler)
0435 public:
0436 ThrowOnFailEnabler() { setThrowOnFail(true); }
0437 ~ThrowOnFailEnabler() { setThrowOnFail(false); }
0438 };
0439
0440 class ThrowOnSkipEnabler {
0441 Q_DISABLE_COPY_MOVE(ThrowOnSkipEnabler)
0442 public:
0443 ThrowOnSkipEnabler() { setThrowOnSkip(true); }
0444 ~ThrowOnSkipEnabler() { setThrowOnSkip(false); }
0445 };
0446
0447 class ThrowOnFailDisabler {
0448 Q_DISABLE_COPY_MOVE(ThrowOnFailDisabler)
0449 public:
0450 ThrowOnFailDisabler() { setThrowOnFail(false); }
0451 ~ThrowOnFailDisabler() { setThrowOnFail(true); }
0452 };
0453
0454 class ThrowOnSkipDisabler {
0455 Q_DISABLE_COPY_MOVE(ThrowOnSkipDisabler)
0456 public:
0457 ThrowOnSkipDisabler() { setThrowOnSkip(false); }
0458 ~ThrowOnSkipDisabler() { setThrowOnSkip(true); }
0459 };
0460
0461 Q_TESTLIB_EXPORT bool qVerify(bool statement, const char *statementStr, const char *description,
0462 const char *file, int line);
0463 Q_DECL_COLD_FUNCTION
0464 Q_TESTLIB_EXPORT void qFail(const char *message, const char *file, int line);
0465 Q_TESTLIB_EXPORT void qSkip(const char *message, const char *file, int line);
0466 Q_TESTLIB_EXPORT bool qExpectFail(const char *dataIndex, const char *comment, TestFailMode mode,
0467 const char *file, int line);
0468 Q_DECL_COLD_FUNCTION
0469 Q_TESTLIB_EXPORT void qCaught(const char *expected, const char *what, const char *file, int line);
0470 Q_DECL_COLD_FUNCTION
0471 Q_TESTLIB_EXPORT void qCaught(const char *expected, const char *file, int line);
0472 #if QT_DEPRECATED_SINCE(6, 3)
0473 QT_DEPRECATED_VERSION_X_6_3("Use qWarning() instead")
0474 Q_TESTLIB_EXPORT void qWarn(const char *message, const char *file = nullptr, int line = 0);
0475 #endif
0476 Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const char *message);
0477 #if QT_CONFIG(regularexpression)
0478 Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const QRegularExpression &messagePattern);
0479 #endif
0480 Q_TESTLIB_EXPORT void failOnWarning();
0481 Q_TESTLIB_EXPORT void failOnWarning(const char *message);
0482 #if QT_CONFIG(regularexpression)
0483 Q_TESTLIB_EXPORT void failOnWarning(const QRegularExpression &messagePattern);
0484 #endif
0485
0486 #if QT_CONFIG(temporaryfile)
0487 Q_TESTLIB_EXPORT QSharedPointer<QTemporaryDir> qExtractTestData(const QString &dirName);
0488 #endif
0489 Q_TESTLIB_EXPORT QString qFindTestData(const char* basepath, const char* file = nullptr, int line = 0, const char* builddir = nullptr, const char* sourcedir = nullptr);
0490 Q_TESTLIB_EXPORT QString qFindTestData(const QString& basepath, const char* file = nullptr, int line = 0, const char* builddir = nullptr, const char *sourcedir = nullptr);
0491
0492 Q_TESTLIB_EXPORT void *qData(const char *tagName, int typeId);
0493 Q_TESTLIB_EXPORT void *qGlobalData(const char *tagName, int typeId);
0494 Q_TESTLIB_EXPORT void *qElementData(const char *elementName, int metaTypeId);
0495 Q_TESTLIB_EXPORT QObject *testObject();
0496
0497 Q_TESTLIB_EXPORT const char *currentAppName();
0498
0499 Q_TESTLIB_EXPORT const char *currentTestFunction();
0500 Q_TESTLIB_EXPORT const char *currentDataTag();
0501 Q_TESTLIB_EXPORT bool currentTestFailed();
0502 Q_TESTLIB_EXPORT bool currentTestResolved();
0503 Q_TESTLIB_EXPORT bool runningTest();
0504
0505 Q_TESTLIB_EXPORT Qt::Key asciiToKey(char ascii);
0506 Q_TESTLIB_EXPORT char keyToAscii(Qt::Key key);
0507
0508 #if QT_DEPRECATED_SINCE(6, 4)
0509 QT_DEPRECATED_VERSION_X_6_4("use an overload that takes a formatter callback, "
0510 "or an overload that takes only failure message, if you "
0511 "do not need to stringify the values")
0512 Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
0513 char *actualVal, char *expectedVal,
0514 const char *actual, const char *expected,
0515 const char *file, int line);
0516 #endif
0517 #if QT_DEPRECATED_SINCE(6, 8)
0518 QT_DEPRECATED_VERSION_X_6_8("use an overload that takes a formatter callback, "
0519 "or an overload that takes only failure message, if you "
0520 "do not need to stringify the values")
0521 Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
0522 qxp::function_ref<const char*()> actualVal,
0523 qxp::function_ref<const char*()> expectedVal,
0524 const char *actual, const char *expected,
0525 const char *file, int line);
0526 #endif
0527 Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
0528 const void *actualPtr, const void *expectedPtr,
0529 const char *(*actualFormatter)(const void *),
0530 const char *(*expectedFormatter)(const void *),
0531 const char *actual, const char *expected,
0532 const char *file, int line);
0533 Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
0534 const char *actual, const char *expected,
0535 const char *file, int line);
0536
0537 Q_TESTLIB_EXPORT bool compare_3way_helper(bool success, const char *failureMsg,
0538 const void *lhsPtr, const void *rhsPtr,
0539 const char *(*lhsFormatter)(const void*),
0540 const char *(*rhsFormatter)(const void*),
0541 const char *lhsStr, const char *rhsStr,
0542 const char *(*actualOrderFormatter)(const void *),
0543 const char *(*expectedOrderFormatter)(const void *),
0544 const void *actualOrderPtr,
0545 const void *expectedOrderPtr,
0546 const char *expectedExpression,
0547 const char *file, int line);
0548
0549 Q_TESTLIB_EXPORT void addColumnInternal(int id, const char *name);
0550
0551 template <typename T>
0552 inline void addColumn(const char *name, T * = nullptr)
0553 {
0554 using QIsSameTConstChar = std::is_same<T, const char*>;
0555 static_assert(!QIsSameTConstChar::value, "const char* is not allowed as a test data format.");
0556 addColumnInternal(qMetaTypeId<T>(), name);
0557 }
0558 Q_TESTLIB_EXPORT QTestData &newRow(const char *dataTag);
0559 Q_TESTLIB_EXPORT QTestData &addRow(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(1, 2);
0560
0561 Q_TESTLIB_EXPORT bool qCompare(qfloat16 const &t1, qfloat16 const &t2,
0562 const char *actual, const char *expected, const char *file, int line);
0563
0564 Q_TESTLIB_EXPORT bool qCompare(float const &t1, float const &t2,
0565 const char *actual, const char *expected, const char *file, int line);
0566
0567 Q_TESTLIB_EXPORT bool qCompare(double const &t1, double const &t2,
0568 const char *actual, const char *expected, const char *file, int line);
0569
0570 Q_TESTLIB_EXPORT bool qCompare(int t1, int t2, const char *actual, const char *expected,
0571 const char *file, int line);
0572
0573 #if QT_POINTER_SIZE == 8
0574 Q_TESTLIB_EXPORT bool qCompare(qsizetype t1, qsizetype t2, const char *actual, const char *expected,
0575 const char *file, int line);
0576 #endif
0577
0578 Q_TESTLIB_EXPORT bool qCompare(unsigned t1, unsigned t2, const char *actual, const char *expected,
0579 const char *file, int line);
0580
0581 Q_TESTLIB_EXPORT bool qCompare(QStringView t1, QStringView t2,
0582 const char *actual, const char *expected,
0583 const char *file, int line);
0584 Q_TESTLIB_EXPORT bool qCompare(QStringView t1, const QLatin1StringView &t2,
0585 const char *actual, const char *expected,
0586 const char *file, int line);
0587 Q_TESTLIB_EXPORT bool qCompare(const QLatin1StringView &t1, QStringView t2,
0588 const char *actual, const char *expected,
0589 const char *file, int line);
0590 inline bool qCompare(const QString &t1, const QString &t2,
0591 const char *actual, const char *expected,
0592 const char *file, int line)
0593 {
0594 return qCompare(QStringView(t1), QStringView(t2), actual, expected, file, line);
0595 }
0596 inline bool qCompare(const QString &t1, const QLatin1StringView &t2,
0597 const char *actual, const char *expected,
0598 const char *file, int line)
0599 {
0600 return qCompare(QStringView(t1), t2, actual, expected, file, line);
0601 }
0602 inline bool qCompare(const QLatin1StringView &t1, const QString &t2,
0603 const char *actual, const char *expected,
0604 const char *file, int line)
0605 {
0606 return qCompare(t1, QStringView(t2), actual, expected, file, line);
0607 }
0608
0609 inline bool compare_ptr_helper(const volatile void *t1, const volatile void *t2, const char *actual,
0610 const char *expected, const char *file, int line)
0611 {
0612 auto formatter = Internal::pointerToString<void>;
0613 return compare_helper(t1 == t2, "Compared pointers are not the same",
0614 const_cast<const void *>(t1), const_cast<const void *>(t2),
0615 formatter, formatter, actual, expected, file, line);
0616 }
0617
0618 inline bool compare_ptr_helper(const volatile QObject *t1, const volatile QObject *t2, const char *actual,
0619 const char *expected, const char *file, int line)
0620 {
0621 auto formatter = Internal::pointerToString<QObject>;
0622 return compare_helper(t1 == t2, "Compared QObject pointers are not the same",
0623 const_cast<const QObject *>(t1), const_cast<const QObject *>(t2),
0624 formatter, formatter, actual, expected, file, line);
0625 }
0626
0627 inline bool compare_ptr_helper(const volatile QObject *t1, std::nullptr_t, const char *actual,
0628 const char *expected, const char *file, int line)
0629 {
0630 auto lhsFormatter = Internal::pointerToString<QObject>;
0631 auto rhsFormatter = Internal::genericToString<std::nullptr_t>;
0632 return compare_helper(t1 == nullptr, "Compared QObject pointers are not the same",
0633 const_cast<const QObject *>(t1), nullptr,
0634 lhsFormatter, rhsFormatter, actual, expected, file, line);
0635 }
0636
0637 inline bool compare_ptr_helper(std::nullptr_t, const volatile QObject *t2, const char *actual,
0638 const char *expected, const char *file, int line)
0639 {
0640 auto lhsFormatter = Internal::genericToString<std::nullptr_t>;
0641 auto rhsFormatter = Internal::pointerToString<QObject>;
0642 return compare_helper(nullptr == t2, "Compared QObject pointers are not the same",
0643 nullptr, const_cast<const QObject *>(t2),
0644 lhsFormatter, rhsFormatter, actual, expected, file, line);
0645 }
0646
0647 inline bool compare_ptr_helper(const volatile void *t1, std::nullptr_t, const char *actual,
0648 const char *expected, const char *file, int line)
0649 {
0650 auto lhsFormatter = Internal::pointerToString<void>;
0651 auto rhsFormatter = Internal::genericToString<std::nullptr_t>;
0652 return compare_helper(t1 == nullptr, "Compared pointers are not the same",
0653 const_cast<const void *>(t1), nullptr,
0654 lhsFormatter, rhsFormatter, actual, expected, file, line);
0655 }
0656
0657 inline bool compare_ptr_helper(std::nullptr_t, const volatile void *t2, const char *actual,
0658 const char *expected, const char *file, int line)
0659 {
0660 auto lhsFormatter = Internal::genericToString<std::nullptr_t>;
0661 auto rhsFormatter = Internal::pointerToString<void>;
0662 return compare_helper(nullptr == t2, "Compared pointers are not the same",
0663 nullptr, const_cast<const void *>(t2),
0664 lhsFormatter, rhsFormatter, actual, expected, file, line);
0665 }
0666
0667 template <typename T1, typename T2 = T1>
0668 inline bool qCompare(const T1 &t1, const T2 &t2, const char *actual, const char *expected,
0669 const char *file, int line)
0670 {
0671 using Internal::genericToString;
0672 if constexpr (QtPrivate::is_standard_or_extended_integer_type_v<T1>
0673 && QtPrivate::is_standard_or_extended_integer_type_v<T2>) {
0674 return compare_helper(q20::cmp_equal(t1, t2), "Compared values are not the same",
0675 std::addressof(t1), std::addressof(t2),
0676 genericToString<T1>, genericToString<T2>,
0677 actual, expected, file, line);
0678 } else {
0679 return compare_helper(t1 == t2, "Compared values are not the same",
0680 std::addressof(t1), std::addressof(t2),
0681 genericToString<T1>, genericToString<T2>,
0682 actual, expected, file, line);
0683 }
0684 }
0685
0686 inline bool qCompare(double const &t1, float const &t2, const char *actual,
0687 const char *expected, const char *file, int line)
0688 {
0689 return qCompare(qreal(t1), qreal(t2), actual, expected, file, line);
0690 }
0691
0692 inline bool qCompare(float const &t1, double const &t2, const char *actual,
0693 const char *expected, const char *file, int line)
0694 {
0695 return qCompare(qreal(t1), qreal(t2), actual, expected, file, line);
0696 }
0697
0698 template <typename T>
0699 inline bool qCompare(const T *t1, const T *t2, const char *actual, const char *expected,
0700 const char *file, int line)
0701 {
0702 return compare_ptr_helper(t1, t2, actual, expected, file, line);
0703 }
0704 template <typename T>
0705 inline bool qCompare(T *t1, T *t2, const char *actual, const char *expected,
0706 const char *file, int line)
0707 {
0708 return compare_ptr_helper(t1, t2, actual, expected, file, line);
0709 }
0710
0711 template <typename T>
0712 inline bool qCompare(T *t1, std::nullptr_t, const char *actual, const char *expected,
0713 const char *file, int line)
0714 {
0715 return compare_ptr_helper(t1, nullptr, actual, expected, file, line);
0716 }
0717 template <typename T>
0718 inline bool qCompare(std::nullptr_t, T *t2, const char *actual, const char *expected,
0719 const char *file, int line)
0720 {
0721 return compare_ptr_helper(nullptr, t2, actual, expected, file, line);
0722 }
0723
0724 template <typename T1, typename T2>
0725 inline bool qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected,
0726 const char *file, int line)
0727 {
0728 return compare_ptr_helper(t1, static_cast<const T1 *>(t2), actual, expected, file, line);
0729 }
0730 template <typename T1, typename T2>
0731 inline bool qCompare(T1 *t1, T2 *t2, const char *actual, const char *expected,
0732 const char *file, int line)
0733 {
0734 return compare_ptr_helper(const_cast<const T1 *>(t1),
0735 static_cast<const T1 *>(const_cast<const T2 *>(t2)), actual, expected, file, line);
0736 }
0737 inline bool qCompare(const char *t1, const char *t2, const char *actual,
0738 const char *expected, const char *file, int line)
0739 {
0740 return compare_string_helper(t1, t2, actual, expected, file, line);
0741 }
0742 inline bool qCompare(char *t1, char *t2, const char *actual, const char *expected,
0743 const char *file, int line)
0744 {
0745 return compare_string_helper(t1, t2, actual, expected, file, line);
0746 }
0747
0748
0749
0750
0751 inline bool qCompare(char *t1, const char *t2, const char *actual,
0752 const char *expected, const char *file, int line)
0753 {
0754 return compare_string_helper(t1, t2, actual, expected, file, line);
0755 }
0756 inline bool qCompare(const char *t1, char *t2, const char *actual,
0757 const char *expected, const char *file, int line)
0758 {
0759 return compare_string_helper(t1, t2, actual, expected, file, line);
0760 }
0761
0762 template <class T>
0763 inline bool qTest(const T& actual, const char *elementName, const char *actualStr,
0764 const char *expected, const char *file, int line)
0765 {
0766 return qCompare(actual, *static_cast<const T *>(QTest::qElementData(elementName,
0767 qMetaTypeId<T>())), actualStr, expected, file, line);
0768 }
0769
0770 #if QT_DEPRECATED_SINCE(6, 8)
0771 QT_DEPRECATED_VERSION_X_6_8("use the overload without qxp::function_ref")
0772 Q_TESTLIB_EXPORT bool reportResult(bool success, qxp::function_ref<const char*()> lhs,
0773 qxp::function_ref<const char*()> rhs,
0774 const char *lhsExpr, const char *rhsExpr,
0775 ComparisonOperation op, const char *file, int line);
0776 #endif
0777
0778 Q_TESTLIB_EXPORT bool reportResult(bool success, const void *lhs, const void *rhs,
0779 const char *(*lhsFormatter)(const void*),
0780 const char *(*rhsFormatter)(const void*),
0781 const char *lhsExpr, const char *rhsExpr,
0782 ComparisonOperation op, const char *file, int line);
0783
0784 template <ComparisonOperation op, typename T1, typename T2 = T1>
0785 inline bool qCompareOp(T1 &&lhs, T2 &&rhs, const char *lhsExpr, const char *rhsExpr,
0786 const char *file, int line)
0787 {
0788 using D1 = std::decay_t<T1>;
0789 using D2 = std::decay_t<T2>;
0790 using Internal::genericToString;
0791 using Comparator = Internal::Compare<op>;
0792
0793
0794 auto doReport = [&](bool result, const D1 &lhs_, const D2 &rhs_) {
0795 return reportResult(result, std::addressof(lhs_), std::addressof(rhs_),
0796 genericToString<D1>, genericToString<D2>,
0797 lhsExpr, rhsExpr, op, file, line);
0798 };
0799
0800
0801 bool result = Comparator::compare(std::forward<T1>(lhs), std::forward<T2>(rhs));
0802 return doReport(result, std::forward<T1>(lhs), std::forward<T2>(rhs));
0803 }
0804
0805 #if defined(__cpp_lib_three_way_comparison)
0806 template <typename OrderingType, typename LHS, typename RHS = LHS>
0807 inline bool qCompare3Way(LHS &&lhs, RHS &&rhs, OrderingType order,
0808 const char *lhsExpression,
0809 const char *rhsExpression,
0810 const char *resultExpression,
0811 const char *file, int line)
0812 {
0813 #if defined(__cpp_concepts)
0814 static_assert(requires { lhs <=> rhs; },
0815 "The three-way comparison operator (<=>) is not implemented");
0816 #endif
0817 static_assert(QtOrderingPrivate::is_ordering_type_v<OrderingType>,
0818 "Please provide, as the order parameter, a value "
0819 "of one of the Qt::{partial,weak,strong}_ordering or "
0820 "std::{partial,weak,strong}_ordering types.");
0821
0822 const auto comparisonResult = std::forward<LHS>(lhs) <=> std::forward<RHS>(rhs);
0823 static_assert(std::is_same_v<decltype(QtOrderingPrivate::to_Qt(comparisonResult)),
0824 decltype(QtOrderingPrivate::to_Qt(order))>,
0825 "The expected and actual ordering types should be the same "
0826 "strength for proper comparison.");
0827
0828 const OrderingType actualOrder = comparisonResult;
0829 using DLHS = q20::remove_cvref_t<LHS>;
0830 using DRHS = q20::remove_cvref_t<RHS>;
0831 using Internal::genericToString;
0832
0833 return compare_3way_helper(actualOrder == order,
0834 "The result of operator<=>() is not what was expected",
0835 std::addressof(lhs), std::addressof(rhs),
0836 genericToString<DLHS>, genericToString<DRHS>,
0837 lhsExpression, rhsExpression,
0838 genericToString<decltype(comparisonResult)>,
0839 genericToString<OrderingType>,
0840 std::addressof(comparisonResult),
0841 std::addressof(order),
0842 resultExpression, file, line);
0843 }
0844 #else
0845 template <typename OrderingType, typename LHS, typename RHS = LHS>
0846 void qCompare3Way(LHS &&lhs, RHS &&rhs, OrderingType order,
0847 const char *lhsExpression,
0848 const char *rhsExpression,
0849 const char *resultExpression,
0850 const char *file, int line) = delete;
0851 #endif
0852
0853 }
0854
0855 #define QWARN(msg) QTest::qWarn(static_cast<const char *>(msg), __FILE__, __LINE__)
0856
0857 QT_END_NAMESPACE
0858
0859 #endif