Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qtestcase.h
Go to the documentation of this file.
1// Copyright (C) 2021 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QTESTCASE_H
5#define QTESTCASE_H
6
7#include <QtTest/qttestglobal.h>
8#include <QtTest/qtesttostring.h>
9
10#include <QtCore/qstring.h>
11#include <QtCore/qnamespace.h>
12#include <QtCore/qmetatype.h>
13#include <QtCore/qmetaobject.h>
14#include <QtCore/qsharedpointer.h>
15#include <QtCore/qtemporarydir.h>
16#include <QtCore/qthread.h>
17
18#include <chrono>
19#ifdef __cpp_concepts
20#include <concepts>
21#endif
22#include <QtCore/qxpfunctional.h>
23#include <QtCore/qxptype_traits.h>
24#include <QtCore/q20utility.h>
25
26#include <string.h>
27
28#ifndef QT_NO_EXCEPTIONS
29# include <exception>
30#endif // QT_NO_EXCEPTIONS
31
32QT_BEGIN_NAMESPACE
33
34#ifndef QT_NO_EXCEPTIONS
35
36#ifdef QTEST_THROW_ON_FAIL
37# define QTEST_FAIL_ACTION QTest::Internal::throwOnFail()
38#else
39# define QTEST_FAIL_ACTION do { QTest::Internal::maybeThrowOnFail(); return; } while (false)
40#endif
41
42#ifdef QTEST_THROW_ON_SKIP
43# define QTEST_SKIP_ACTION QTest::Internal::throwOnSkip()
44#else
45# define QTEST_SKIP_ACTION do { QTest::Internal::maybeThrowOnSkip(); return; } while (false)
46#endif
47
48#else
49# if defined(QTEST_THROW_ON_FAIL) || defined(QTEST_THROW_ON_SKIP)
50# error QTEST_THROW_ON_FAIL/SKIP require exception support enabled.
51# endif
52#endif // QT_NO_EXCEPTIONS
53
55# define QTEST_FAIL_ACTION return
56#endif
57
59# define QTEST_SKIP_ACTION return
60#endif
61
62class qfloat16;
63class QRegularExpression;
64
65#define QVERIFY(statement) do
66 {
67 if (!QTest::qVerify(static_cast<bool>(statement), #statement, "", __FILE__, __LINE__))
69}while (false)
70
71#define QFAIL(message) do
72 {
73 QTest::qFail(static_cast<const char *>(message), __FILE__, __LINE__);
75}while (false)
76
77#define QVERIFY2(statement, description) do
78 {
79 if (statement) {
80 if (!QTest::qVerify(true, #statement, static_cast<const char *>(description), __FILE__, __LINE__))
82 } else {
83 if (!QTest::qVerify(false, #statement, static_cast<const char *>(description), __FILE__, __LINE__))
85 }\
86}while (false)
87
88#define QCOMPARE(actual, expected) do
89 {
90 if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))
92}while (false)
93
94#define QCOMPARE_OP_IMPL(lhs, rhs, op, opId) do
95 {
96 if (!QTest::qCompareOp<QTest::ComparisonOperation::opId>(lhs, rhs, #lhs, #rhs, __FILE__, __LINE__))
98}while (false)
99
100#define QCOMPARE_EQ(computed, baseline) QCOMPARE_OP_IMPL(computed, baseline, ==, Equal)
101#define QCOMPARE_NE(computed, baseline) QCOMPARE_OP_IMPL(computed, baseline, !=, NotEqual)
102#define QCOMPARE_LT(computed, baseline) QCOMPARE_OP_IMPL(computed, baseline, <, LessThan)
103#define QCOMPARE_LE(computed, baseline) QCOMPARE_OP_IMPL(computed, baseline, <=, LessThanOrEqual)
104#define QCOMPARE_GT(computed, baseline) QCOMPARE_OP_IMPL(computed, baseline, >, GreaterThan)
105#define QCOMPARE_GE(computed, baseline) QCOMPARE_OP_IMPL(computed, baseline, >=, GreaterThanOrEqual)
106
107#ifndef QT_NO_EXCEPTIONS
108
109# define QVERIFY_THROWS_NO_EXCEPTION(...)
110 do {
111 QT_TRY {
112 __VA_ARGS__;
113 /* success */
114 } QT_CATCH (...) {
115 QTest::qCaught(nullptr, __FILE__, __LINE__);
117 }
118 } while (false)
119 /* end */
120
121#if QT_DEPRECATED_SINCE(6, 3)
122namespace QTest {
123QT_DEPRECATED_VERSION_X_6_3("Don't use QVERIFY_EXCEPTION_THROWN(expr, type) anymore, "
124 "use QVERIFY_THROWS_EXCEPTION(type, expr...) instead")
125inline void useVerifyThrowsException() {}
126} // namespace QTest
127# define QVERIFY_EXCEPTION_THROWN(expression, exceptiontype)
128 QVERIFY_THROWS_EXCEPTION(exceptiontype, QTest::useVerifyThrowsException(); expression)
129#endif
130
131# define QVERIFY_THROWS_EXCEPTION(exceptiontype, ...)
132 do {
133 bool qverify_throws_exception_did_not_throw = false;
134 QT_TRY {
135 __VA_ARGS__;
136 QTest::qFail("Expected exception of type " #exceptiontype " to be thrown"
137 " but no exception caught", __FILE__, __LINE__);
138 qverify_throws_exception_did_not_throw = true;
139 } QT_CATCH (const exceptiontype &) {
140 /* success */
141 } QT_CATCH (...) {
142 QTest::qCaught(#exceptiontype, __FILE__, __LINE__);
144 }
145 if (qverify_throws_exception_did_not_throw)
147 } while (false)
148
149#else // QT_NO_EXCEPTIONS
150
151/*
152 * These macros check whether the expression passed throws exceptions, but we can't
153 * catch them to check because Qt has been compiled without exception support. We can't
154 * skip the expression because it may have side effects and must be executed.
155 * So, users must use Qt with exception support enabled if they use exceptions
156 * in their code.
157 */
158# define QVERIFY_THROWS_EXCEPTION(...)
159 static_assert(false, "Support for exceptions is disabled")
160# define QVERIFY_THROWS_NO_EXCEPTION(...)
161 static_assert(false, "Support for exceptions is disabled")
162
163#endif // !QT_NO_EXCEPTIONS
164
165/* Ideally we would adapt qWaitFor(), or a variant on it, to implement roughly
166 * what the following provides as QTRY_LOOP_IMPL(); however, for now, the
167 * reporting of how much to increase the timeout to (if within a factor of two)
168 * on failure and the check for (QTest::runningTest() &&
169 * QTest::currentTestResolved()) go beyond qWaitFor(). (We no longer care about
170 * the bug in MSVC < 2017 that precluded using qWaitFor() in the implementation
171 * here, see QTBUG-59096.)
172 */
173
174// NB: not do {...} while (0) wrapped, as qt_test_i is accessed after it
175#define QTRY_LOOP_IMPL(expr, timeoutValue, step)
176 if (!(expr)) {
177 QTest::qWait(0);
178 }
179 std::chrono::milliseconds timeoutValueMs(timeoutValue);
180 std::chrono::milliseconds stepMs(step);
181 auto qt_test_i = std::chrono::milliseconds(0);
182 for (; qt_test_i < timeoutValueMs && !(QTest::runningTest() && QTest::currentTestResolved())
183 && !(expr); qt_test_i += stepMs) {
184 QTest::qWait(stepMs);
185 }
186// Ends in a for-block, so doesn't want a following semicolon.
187
188#define QTRY_TIMEOUT_DEBUG_IMPL(expr, timeoutValue, step)
189 if (!(QTest::runningTest() && QTest::currentTestResolved()) && !(expr)) {
190 QTRY_LOOP_IMPL(expr, 2 * (timeoutValue), step)
191 if ((expr)) {
192 QFAIL(qPrintable(QTest::Internal::formatTryTimeoutDebugMessage(
193 u8"" #expr, timeoutValue, timeoutValue + qt_test_i)));
194 }
195 }
196
197#define QTRY_IMPL(expr, timeoutAsGiven)
198 const auto qt_test_timeoutAsMs = [&] {
199 /* make 5s work w/o user action: */
200 using namespace std::chrono_literals;
201 return std::chrono::milliseconds{timeoutAsGiven};
202 }();
203 const auto qt_test_step = qt_test_timeoutAsMs < std::chrono::milliseconds(350)
204 ? qt_test_timeoutAsMs / 7 + std::chrono::milliseconds(1)
205 : std::chrono::milliseconds(50);
206 { QTRY_LOOP_IMPL(expr, qt_test_timeoutAsMs, qt_test_step) }
207 QTRY_TIMEOUT_DEBUG_IMPL(expr, qt_test_timeoutAsMs, qt_test_step)
208// Ends with an if-block, so doesn't want a following semicolon.
209
210// Will try to wait for the expression to become true while allowing event processing
211#define QTRY_VERIFY_WITH_TIMEOUT(expr, timeout) do
212 {
213 QTRY_IMPL(expr, timeout)
214 QVERIFY(expr); \
215}while (false)
216
217#define QTRY_VERIFY(expr) QTRY_VERIFY_WITH_TIMEOUT(
218 expr, QTest::defaultTryTimeout.load(std::memory_order_relaxed))
219
220// Will try to wait for the expression to become true while allowing event processing
221#define QTRY_VERIFY2_WITH_TIMEOUT(expr, messageExpression, timeout) do
222 {
223 QTRY_IMPL(expr, timeout)
224 QVERIFY2(expr, messageExpression); \
225}while (false)
226
227#define QTRY_VERIFY2(expr, messageExpression)
228 QTRY_VERIFY2_WITH_TIMEOUT(expr, messageExpression,
229 QTest::defaultTryTimeout.load(std::memory_order_relaxed))
230
231// Will try to wait for the comparison to become successful while allowing event processing
232#define QTRY_COMPARE_WITH_TIMEOUT(expr, expected, timeout) do
233 {
234 QTRY_IMPL((expr) == (expected), timeout)
235 QCOMPARE(expr, expected); \
236}while (false)
237
238#define QTRY_COMPARE(expr, expected)
239 QTRY_COMPARE_WITH_TIMEOUT(expr, expected,
240 QTest::defaultTryTimeout.load(std::memory_order_relaxed))
241
242#define QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, op, opId, timeout) do
243 {
244 using Q_Cmp = QTest::Internal::Compare<QTest::ComparisonOperation::opId>;
245 QTRY_IMPL(Q_Cmp::compare((computed), (baseline)), timeout)
246 QCOMPARE_OP_IMPL(computed, baseline, op, opId); \
247}while (false)
248
249#define QTRY_COMPARE_EQ_WITH_TIMEOUT(computed, baseline, timeout)
250 QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, ==, Equal, timeout)
251
252#define QTRY_COMPARE_EQ(computed, baseline)
253 QTRY_COMPARE_EQ_WITH_TIMEOUT(computed, baseline,
254 QTest::defaultTryTimeout.load(std::memory_order_relaxed))
255
256#define QTRY_COMPARE_NE_WITH_TIMEOUT(computed, baseline, timeout)
257 QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, !=, NotEqual, timeout)
258
259#define QTRY_COMPARE_NE(computed, baseline)
260 QTRY_COMPARE_NE_WITH_TIMEOUT(computed, baseline,
261 QTest::defaultTryTimeout.load(std::memory_order_relaxed))
262
263#define QTRY_COMPARE_LT_WITH_TIMEOUT(computed, baseline, timeout)
264 QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, <, LessThan, timeout)
265
266#define QTRY_COMPARE_LT(computed, baseline)
267 QTRY_COMPARE_LT_WITH_TIMEOUT(computed, baseline,
268 QTest::defaultTryTimeout.load(std::memory_order_relaxed))
269
270#define QTRY_COMPARE_LE_WITH_TIMEOUT(computed, baseline, timeout)
271 QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, <=, LessThanOrEqual, timeout)
272
273#define QTRY_COMPARE_LE(computed, baseline)
274 QTRY_COMPARE_LE_WITH_TIMEOUT(computed, baseline,
275 QTest::defaultTryTimeout.load(std::memory_order_relaxed))
276
277#define QTRY_COMPARE_GT_WITH_TIMEOUT(computed, baseline, timeout)
278 QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, >, GreaterThan, timeout)
279
280#define QTRY_COMPARE_GT(computed, baseline)
281 QTRY_COMPARE_GT_WITH_TIMEOUT(computed, baseline,
282 QTest::defaultTryTimeout.load(std::memory_order_relaxed))
283
284#define QTRY_COMPARE_GE_WITH_TIMEOUT(computed, baseline, timeout)
285 QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, >=, GreaterThanOrEqual, timeout)
286
287#define QTRY_COMPARE_GE(computed, baseline)
288 QTRY_COMPARE_GE_WITH_TIMEOUT(computed, baseline,
289 QTest::defaultTryTimeout.load(std::memory_order_relaxed))
290
291#define QSKIP_INTERNAL(statement) do
292 {
293 QTest::qSkip(static_cast<const char *>(statement), __FILE__, __LINE__);
295}while (false)
296
297#define QSKIP(statement, ...) QSKIP_INTERNAL(statement)
298
299#define QEXPECT_FAIL(dataIndex, comment, mode)do
300 {
301 if (!QTest::qExpectFail(dataIndex, static_cast<const char *>(comment), QTest::mode, __FILE__, __LINE__))
303}while (false)
304
305#define QFETCH(Type, name)
306 Type name = *static_cast<Type *>(QTest::qData(#name, ::qMetaTypeId<typename std::remove_cv<Type >::type>()))
307
308#define QFETCH_GLOBAL(Type, name)
309 Type name = *static_cast<Type *>(QTest::qGlobalData(#name, ::qMetaTypeId<typename std::remove_cv<Type >::type>()))
310
311#define QTEST(actual, testElement)do
312 {
313 if (!QTest::qTest(actual, testElement, #actual, #testElement, __FILE__, __LINE__))
315}while (false)
316
317#ifdef __cpp_lib_three_way_comparison
318#define QCOMPARE_3WAY(lhs, rhs, order) do
319 {
320 if (!QTest::qCompare3Way(lhs, rhs, order, #lhs, #rhs, #order, __FILE__, __LINE__))
321 QTEST_FAIL_ACTION; \
322}while (false)
323#else
324#define QCOMPARE_3WAY(...)
325 static_assert(false, "QCOMPARE_3WAY test requires C++20 operator<=>()")
326#endif // __cpp_lib_three_way_comparison
327
328#ifdef QT_TESTCASE_BUILDDIR
329
330#ifndef QT_TESTCASE_SOURCEDIR
331#define QT_TESTCASE_SOURCEDIR nullptr
332#endif
333
334# define QFINDTESTDATA(basepath)
335 QTest::qFindTestData(basepath, __FILE__, __LINE__, QT_TESTCASE_BUILDDIR, QT_TESTCASE_SOURCEDIR)
336#else
337# define QFINDTESTDATA(basepath)
338 QTest::qFindTestData(basepath, __FILE__, __LINE__)
339#endif
340
341# define QEXTRACTTESTDATA(resourcePath)
342 QTest::qExtractTestData(resourcePath)
343
344class QObject;
345class QTestData;
346
347namespace QTest
348{
349 namespace Internal {
350
355
356 Q_DECL_COLD_FUNCTION
360 Q_TESTLIB_EXPORT Q_DECL_COLD_FUNCTION
361 const char *formatPropertyTestHelperFailure(char *msg, size_t maxMsgLen,
362 const char *actual, const char *expected,
363 const char *actualExpr,
364 const char *expectedExpr);
365
366 template <ComparisonOperation> struct Compare;
367 template <> struct Compare<ComparisonOperation::Equal>
368 {
369 template <typename T1, typename T2> static bool compare(T1 &&lhs, T2 &&rhs)
370 { return std::forward<T1>(lhs) == std::forward<T2>(rhs); }
371 };
373 {
374 template <typename T1, typename T2> static bool compare(T1 &&lhs, T2 &&rhs)
375 { return std::forward<T1>(lhs) != std::forward<T2>(rhs); }
376 };
378 {
379 template <typename T1, typename T2> static bool compare(T1 &&lhs, T2 &&rhs)
380 { return std::forward<T1>(lhs) < std::forward<T2>(rhs); }
381 };
383 {
384 template <typename T1, typename T2> static bool compare(T1 &&lhs, T2 &&rhs)
385 { return std::forward<T1>(lhs) <= std::forward<T2>(rhs); }
386 };
388 {
389 template <typename T1, typename T2> static bool compare(T1 &&lhs, T2 &&rhs)
390 { return std::forward<T1>(lhs) > std::forward<T2>(rhs); }
391 };
393 {
394 template <typename T1, typename T2> static bool compare(T1 &&lhs, T2 &&rhs)
395 { return std::forward<T1>(lhs) >= std::forward<T2>(rhs); }
396 };
397
398 template <typename T1> const char *genericToString(const void *arg)
399 {
400 using QTest::toString;
401 return toString(*static_cast<const T1 *>(arg));
402 }
403
404 template <> inline const char *genericToString<char *>(const void *arg)
405 {
406 using QTest::toString;
407 return toString(static_cast<const char *>(arg));
408 }
409
410 template <> inline const char *genericToString<std::nullptr_t>(const void *)
411 {
412 return QTest::toString(nullptr);
413 }
414
415 template <typename T> const char *pointerToString(const void *arg)
416 {
417 using QTest::toString;
418 return toString(static_cast<const T *>(arg));
419 }
420
421 // Exported so Qt Quick Test can also use it for generating backtraces upon crashes.
423
424 } // namespace Internal
425
426 Q_TESTLIB_EXPORT void qInit(QObject *testObject, int argc = 0, char **argv = nullptr);
429
430 Q_TESTLIB_EXPORT int qExec(QObject *testObject, int argc = 0, char **argv = nullptr);
431 Q_TESTLIB_EXPORT int qExec(QObject *testObject, const QStringList &arguments);
432
433#if QT_CONFIG(batch_test_support) || defined(Q_QDOC)
434 using TestEntryFunction = std::function<int(int, char **)>;
436#endif // QT_CONFIG(batch_test_support)
437
438 Q_TESTLIB_EXPORT void setMainSourcePath(const char *file, const char *builddir = nullptr);
439 Q_TESTLIB_EXPORT void setThrowOnFail(bool enable) noexcept;
440 Q_TESTLIB_EXPORT void setThrowOnSkip(bool enable) noexcept;
441
448
455
462
469
470 Q_TESTLIB_EXPORT bool qVerify(bool statement, const char *statementStr, const char *description,
471 const char *file, int line);
472 Q_DECL_COLD_FUNCTION
473 Q_TESTLIB_EXPORT void qFail(const char *message, const char *file, int line);
474 Q_TESTLIB_EXPORT void qSkip(const char *message, const char *file, int line);
475 Q_TESTLIB_EXPORT bool qExpectFail(const char *dataIndex, const char *comment, TestFailMode mode,
476 const char *file, int line);
477 Q_DECL_COLD_FUNCTION
478 Q_TESTLIB_EXPORT void qCaught(const char *expected, const char *what, const char *file, int line);
479 Q_DECL_COLD_FUNCTION
480 Q_TESTLIB_EXPORT void qCaught(const char *expected, const char *file, int line);
481#if QT_DEPRECATED_SINCE(6, 3)
482 QT_DEPRECATED_VERSION_X_6_3("Use qWarning() instead")
483 Q_TESTLIB_EXPORT void qWarn(const char *message, const char *file = nullptr, int line = 0);
484#endif
485 Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const char *message);
486#if QT_CONFIG(regularexpression)
488#endif
490 Q_TESTLIB_EXPORT void failOnWarning(const char *message);
491#if QT_CONFIG(regularexpression)
493#endif
494
495#if QT_CONFIG(temporaryfile)
497#endif
498 Q_TESTLIB_EXPORT QString qFindTestData(const char* basepath, const char* file = nullptr, int line = 0, const char* builddir = nullptr, const char* sourcedir = nullptr);
499 Q_TESTLIB_EXPORT QString qFindTestData(const QString& basepath, const char* file = nullptr, int line = 0, const char* builddir = nullptr, const char *sourcedir = nullptr);
500
501 Q_TESTLIB_EXPORT void *qData(const char *tagName, int typeId);
502 Q_TESTLIB_EXPORT void *qGlobalData(const char *tagName, int typeId);
503 Q_TESTLIB_EXPORT void *qElementData(const char *elementName, int metaTypeId);
504 Q_TESTLIB_EXPORT QObject *testObject();
505
506 Q_TESTLIB_EXPORT const char *currentAppName();
507
509 Q_TESTLIB_EXPORT const char *currentDataTag();
513 Q_TESTLIB_EXPORT bool runningTest(); // Internal, for use by macros and QTestEventLoop.
514
516 Q_TESTLIB_EXPORT char keyToAscii(Qt::Key key);
517
518#if QT_DEPRECATED_SINCE(6, 4)
519 QT_DEPRECATED_VERSION_X_6_4("use an overload that takes a formatter callback, "
520 "or an overload that takes only failure message, if you "
521 "do not need to stringify the values")
522 Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
523 char *actualVal, char *expectedVal,
524 const char *actual, const char *expected,
525 const char *file, int line);
526#endif // QT_DEPRECATED_SINCE(6, 4)
527#if QT_DEPRECATED_SINCE(6, 8)
528 QT_DEPRECATED_VERSION_X_6_8("use an overload that takes a formatter callback, "
529 "or an overload that takes only failure message, if you "
530 "do not need to stringify the values")
531 Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
532 qxp::function_ref<const char*()> actualVal,
533 qxp::function_ref<const char*()> expectedVal,
534 const char *actual, const char *expected,
535 const char *file, int line);
536#endif // QT_DEPRECATED_SINCE(6, 8)
537 Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
538 const void *actualPtr, const void *expectedPtr,
539 const char *(*actualFormatter)(const void *),
540 const char *(*expectedFormatter)(const void *),
541 const char *actual, const char *expected,
542 const char *file, int line);
543 Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
544 const char *actual, const char *expected,
545 const char *file, int line);
546
547 Q_TESTLIB_EXPORT bool compare_3way_helper(bool success, const char *failureMsg,
548 const void *lhsPtr, const void *rhsPtr,
549 const char *(*lhsFormatter)(const void*),
550 const char *(*rhsFormatter)(const void*),
551 const char *lhsStr, const char *rhsStr,
552 const char *(*actualOrderFormatter)(const void *),
553 const char *(*expectedOrderFormatter)(const void *),
554 const void *actualOrderPtr,
555 const void *expectedOrderPtr,
556 const char *expectedExpression,
557 const char *file, int line);
558
559 Q_TESTLIB_EXPORT void addColumnInternal(int id, const char *name);
560
561 template <typename T>
562 inline void addColumn(const char *name, T * = nullptr)
563 {
564 using QIsSameTConstChar = std::is_same<T, const char*>;
565 static_assert(!QIsSameTConstChar::value, "const char* is not allowed as a test data format.");
566 addColumnInternal(qMetaTypeId<T>(), name);
567 }
568 Q_TESTLIB_EXPORT QTestData &newRow(const char *dataTag);
569 Q_TESTLIB_EXPORT QTestData &addRow(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(1, 2);
570
571 Q_TESTLIB_EXPORT bool qCompare(qfloat16 const &t1, qfloat16 const &t2,
572 const char *actual, const char *expected, const char *file, int line);
573
574 Q_TESTLIB_EXPORT bool qCompare(float const &t1, float const &t2,
575 const char *actual, const char *expected, const char *file, int line);
576
577 Q_TESTLIB_EXPORT bool qCompare(double const &t1, double const &t2,
578 const char *actual, const char *expected, const char *file, int line);
579
580 Q_TESTLIB_EXPORT bool qCompare(int t1, int t2, const char *actual, const char *expected,
581 const char *file, int line);
582
583#if QT_POINTER_SIZE == 8
584 Q_TESTLIB_EXPORT bool qCompare(qsizetype t1, qsizetype t2, const char *actual, const char *expected,
585 const char *file, int line);
586#endif
587
588 Q_TESTLIB_EXPORT bool qCompare(unsigned t1, unsigned t2, const char *actual, const char *expected,
589 const char *file, int line);
590
591 Q_TESTLIB_EXPORT bool qCompare(QStringView t1, QStringView t2,
592 const char *actual, const char *expected,
593 const char *file, int line);
594 Q_TESTLIB_EXPORT bool qCompare(QStringView t1, const QLatin1StringView &t2,
595 const char *actual, const char *expected,
596 const char *file, int line);
597 Q_TESTLIB_EXPORT bool qCompare(const QLatin1StringView &t1, QStringView t2,
598 const char *actual, const char *expected,
599 const char *file, int line);
600 inline bool qCompare(const QString &t1, const QString &t2,
601 const char *actual, const char *expected,
602 const char *file, int line)
603 {
604 return qCompare(QStringView(t1), QStringView(t2), actual, expected, file, line);
605 }
606 inline bool qCompare(const QString &t1, const QLatin1StringView &t2,
607 const char *actual, const char *expected,
608 const char *file, int line)
609 {
610 return qCompare(QStringView(t1), t2, actual, expected, file, line);
611 }
612 inline bool qCompare(const QLatin1StringView &t1, const QString &t2,
613 const char *actual, const char *expected,
614 const char *file, int line)
615 {
616 return qCompare(t1, QStringView(t2), actual, expected, file, line);
617 }
618
619 inline bool compare_ptr_helper(const volatile void *t1, const volatile void *t2, const char *actual,
620 const char *expected, const char *file, int line)
621 {
622 auto formatter = Internal::pointerToString<void>;
623 return compare_helper(t1 == t2, "Compared pointers are not the same",
624 const_cast<const void *>(t1), const_cast<const void *>(t2),
625 formatter, formatter, actual, expected, file, line);
626 }
627
628 inline bool compare_ptr_helper(const volatile QObject *t1, const volatile QObject *t2, const char *actual,
629 const char *expected, const char *file, int line)
630 {
631 auto formatter = Internal::pointerToString<QObject>;
632 return compare_helper(t1 == t2, "Compared QObject pointers are not the same",
633 const_cast<const QObject *>(t1), const_cast<const QObject *>(t2),
634 formatter, formatter, actual, expected, file, line);
635 }
636
637 inline bool compare_ptr_helper(const volatile QObject *t1, std::nullptr_t, const char *actual,
638 const char *expected, const char *file, int line)
639 {
640 auto lhsFormatter = Internal::pointerToString<QObject>;
641 auto rhsFormatter = Internal::genericToString<std::nullptr_t>;
642 return compare_helper(t1 == nullptr, "Compared QObject pointers are not the same",
643 const_cast<const QObject *>(t1), nullptr,
644 lhsFormatter, rhsFormatter, actual, expected, file, line);
645 }
646
647 inline bool compare_ptr_helper(std::nullptr_t, const volatile QObject *t2, const char *actual,
648 const char *expected, const char *file, int line)
649 {
650 auto lhsFormatter = Internal::genericToString<std::nullptr_t>;
651 auto rhsFormatter = Internal::pointerToString<QObject>;
652 return compare_helper(nullptr == t2, "Compared QObject pointers are not the same",
653 nullptr, const_cast<const QObject *>(t2),
654 lhsFormatter, rhsFormatter, actual, expected, file, line);
655 }
656
657 inline bool compare_ptr_helper(const volatile void *t1, std::nullptr_t, const char *actual,
658 const char *expected, const char *file, int line)
659 {
660 auto lhsFormatter = Internal::pointerToString<void>;
661 auto rhsFormatter = Internal::genericToString<std::nullptr_t>;
662 return compare_helper(t1 == nullptr, "Compared pointers are not the same",
663 const_cast<const void *>(t1), nullptr,
664 lhsFormatter, rhsFormatter, actual, expected, file, line);
665 }
666
667 inline bool compare_ptr_helper(std::nullptr_t, const volatile void *t2, const char *actual,
668 const char *expected, const char *file, int line)
669 {
670 auto lhsFormatter = Internal::genericToString<std::nullptr_t>;
671 auto rhsFormatter = Internal::pointerToString<void>;
672 return compare_helper(nullptr == t2, "Compared pointers are not the same",
673 nullptr, const_cast<const void *>(t2),
674 lhsFormatter, rhsFormatter, actual, expected, file, line);
675 }
676
677 template <typename T1, typename T2 = T1>
678 inline bool qCompare(const T1 &t1, const T2 &t2, const char *actual, const char *expected,
679 const char *file, int line)
680 {
681 using Internal::genericToString;
682 if constexpr (QtPrivate::is_standard_or_extended_integer_type_v<T1>
683 && QtPrivate::is_standard_or_extended_integer_type_v<T2>) {
684 return compare_helper(q20::cmp_equal(t1, t2), "Compared values are not the same",
685 std::addressof(t1), std::addressof(t2),
686 genericToString<T1>, genericToString<T2>,
687 actual, expected, file, line);
688 } else {
689 return compare_helper(t1 == t2, "Compared values are not the same",
690 std::addressof(t1), std::addressof(t2),
691 genericToString<T1>, genericToString<T2>,
692 actual, expected, file, line);
693 }
694 }
695
696 inline bool qCompare(double const &t1, float const &t2, const char *actual,
697 const char *expected, const char *file, int line)
698 {
699 return qCompare(qreal(t1), qreal(t2), actual, expected, file, line);
700 }
701
702 inline bool qCompare(float const &t1, double const &t2, const char *actual,
703 const char *expected, const char *file, int line)
704 {
705 return qCompare(qreal(t1), qreal(t2), actual, expected, file, line);
706 }
707
708 template <typename T>
709 inline bool qCompare(const T *t1, const T *t2, const char *actual, const char *expected,
710 const char *file, int line)
711 {
712 return compare_ptr_helper(t1, t2, actual, expected, file, line);
713 }
714 template <typename T>
715 inline bool qCompare(T *t1, T *t2, const char *actual, const char *expected,
716 const char *file, int line)
717 {
718 return compare_ptr_helper(t1, t2, actual, expected, file, line);
719 }
720
721 template <typename T>
722 inline bool qCompare(T *t1, std::nullptr_t, const char *actual, const char *expected,
723 const char *file, int line)
724 {
725 return compare_ptr_helper(t1, nullptr, actual, expected, file, line);
726 }
727 template <typename T>
728 inline bool qCompare(std::nullptr_t, T *t2, const char *actual, const char *expected,
729 const char *file, int line)
730 {
731 return compare_ptr_helper(nullptr, t2, actual, expected, file, line);
732 }
733
734 template <typename T1, typename T2>
735 inline bool qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected,
736 const char *file, int line)
737 {
738 return compare_ptr_helper(t1, static_cast<const T1 *>(t2), actual, expected, file, line);
739 }
740 template <typename T1, typename T2>
741 inline bool qCompare(T1 *t1, T2 *t2, const char *actual, const char *expected,
742 const char *file, int line)
743 {
744 return compare_ptr_helper(const_cast<const T1 *>(t1),
745 static_cast<const T1 *>(const_cast<const T2 *>(t2)), actual, expected, file, line);
746 }
747 inline bool qCompare(const char *t1, const char *t2, const char *actual,
748 const char *expected, const char *file, int line)
749 {
750 return compare_string_helper(t1, t2, actual, expected, file, line);
751 }
752 inline bool qCompare(char *t1, char *t2, const char *actual, const char *expected,
753 const char *file, int line)
754 {
755 return compare_string_helper(t1, t2, actual, expected, file, line);
756 }
757
758 /* The next two overloads are for MSVC that shows problems with implicit
759 conversions
760 */
761 inline bool qCompare(char *t1, const char *t2, const char *actual,
762 const char *expected, const char *file, int line)
763 {
764 return compare_string_helper(t1, t2, actual, expected, file, line);
765 }
766 inline bool qCompare(const char *t1, char *t2, const char *actual,
767 const char *expected, const char *file, int line)
768 {
769 return compare_string_helper(t1, t2, actual, expected, file, line);
770 }
771
772 template <class T>
773 inline bool qTest(const T& actual, const char *elementName, const char *actualStr,
774 const char *expected, const char *file, int line)
775 {
776 return qCompare(actual, *static_cast<const T *>(QTest::qElementData(elementName,
777 qMetaTypeId<T>())), actualStr, expected, file, line);
778 }
779
780#if QT_DEPRECATED_SINCE(6, 8)
781 QT_DEPRECATED_VERSION_X_6_8("use the overload without qxp::function_ref")
782 Q_TESTLIB_EXPORT bool reportResult(bool success, qxp::function_ref<const char*()> lhs,
783 qxp::function_ref<const char*()> rhs,
784 const char *lhsExpr, const char *rhsExpr,
785 ComparisonOperation op, const char *file, int line);
786#endif // QT_DEPRECATED_SINCE(6, 8)
787
788 Q_TESTLIB_EXPORT bool reportResult(bool success, const void *lhs, const void *rhs,
789 const char *(*lhsFormatter)(const void*),
790 const char *(*rhsFormatter)(const void*),
791 const char *lhsExpr, const char *rhsExpr,
792 ComparisonOperation op, const char *file, int line);
793
794 template <ComparisonOperation op, typename T1, typename T2 = T1>
795 inline bool qCompareOp(T1 &&lhs, T2 &&rhs, const char *lhsExpr, const char *rhsExpr,
796 const char *file, int line)
797 {
798 using D1 = std::decay_t<T1>;
799 using D2 = std::decay_t<T2>;
800 using Internal::genericToString;
801 using Comparator = Internal::Compare<op>;
802
803 // force decaying of T1 and T2
804 auto doReport = [&](bool result, const D1 &lhs_, const D2 &rhs_) {
805 return reportResult(result, std::addressof(lhs_), std::addressof(rhs_),
806 genericToString<D1>, genericToString<D2>,
807 lhsExpr, rhsExpr, op, file, line);
808 };
809
810 /* assumes that op does not actually move from lhs and rhs */
811 bool result = Comparator::compare(std::forward<T1>(lhs), std::forward<T2>(rhs));
812 return doReport(result, std::forward<T1>(lhs), std::forward<T2>(rhs));
813 }
814
815#if defined(__cpp_lib_three_way_comparison)
816 template <typename OrderingType, typename LHS, typename RHS = LHS>
817 inline bool qCompare3Way(LHS &&lhs, RHS &&rhs, OrderingType order,
818 const char *lhsExpression,
819 const char *rhsExpression,
820 const char *resultExpression,
821 const char *file, int line)
822 {
823#if defined(__cpp_concepts)
824 static_assert(requires { lhs <=> rhs; },
825 "The three-way comparison operator (<=>) is not implemented");
826#endif // __cpp_concepts
828 "Please provide, as the order parameter, a value "
829 "of one of the Qt::{partial,weak,strong}_ordering or "
830 "std::{partial,weak,strong}_ordering types.");
831
832 const auto comparisonResult = std::forward<LHS>(lhs) <=> std::forward<RHS>(rhs);
833 static_assert(std::is_same_v<decltype(QtOrderingPrivate::to_Qt(comparisonResult)),
834 decltype(QtOrderingPrivate::to_Qt(order))>,
835 "The expected and actual ordering types should be the same "
836 "strength for proper comparison.");
837
839 using DLHS = q20::remove_cvref_t<LHS>;
840 using DRHS = q20::remove_cvref_t<RHS>;
842
844 "The result of operator<=>() is not what was expected",
853 }
854#else
855 template <typename OrderingType, typename LHS, typename RHS = LHS>
856 void qCompare3Way(LHS &&lhs, RHS &&rhs, OrderingType order,
857 const char *lhsExpression,
858 const char *rhsExpression,
859 const char *resultExpression,
860 const char *file, int line) = delete;
861#endif // __cpp_lib_three_way_comparison
862
863}
864
865#define QWARN(msg) QTest::qWarn(static_cast<const char *>(msg), __FILE__, __LINE__)
866
867QT_END_NAMESPACE
868
869#endif
\keyword 16-bit Floating Point Support\inmodule QtCore \inheaderfile QFloat16
Definition qfloat16.h:48
const char * pointerToString(const void *arg)
Definition qtestcase.h:415
Q_TESTLIB_EXPORT void maybeThrowOnFail()
Q_TESTLIB_EXPORT void throwOnFail()
Q_TESTLIB_EXPORT void maybeThrowOnSkip()
Q_TESTLIB_EXPORT void throwOnSkip()
const char * genericToString(const void *arg)
Definition qtestcase.h:398
const char * genericToString< char * >(const void *arg)
Definition qtestcase.h:404
Q_TESTLIB_EXPORT Q_DECL_COLD_FUNCTION const char * formatPropertyTestHelperFailure(char *msg, size_t maxMsgLen, const char *actual, const char *expected, const char *actualExpr, const char *expectedExpr)
void qCompare3Way(LHS &&lhs, RHS &&rhs, OrderingType order, const char *lhsExpression, const char *rhsExpression, const char *resultExpression, const char *file, int line)=delete
Q_TESTLIB_EXPORT int qExec(QObject *testObject, const QStringList &arguments)
This is an overloaded member function, provided for convenience. It differs from the above function o...
bool qCompare(std::nullptr_t, T *t2, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:728
Q_TESTLIB_EXPORT int qRun()
Q_TESTLIB_EXPORT bool currentTestResolved()
bool reportResult(bool success, const void *lhs, const void *rhs, const char *(*lhsFormatter)(const void *), const char *(*rhsFormatter)(const void *), const char *lhsExpr, const char *rhsExpr, ComparisonOperation op, const char *file, int line)
Q_TESTLIB_EXPORT bool qCompare(unsigned t1, unsigned t2, const char *actual, const char *expected, const char *file, int line)
bool qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:735
bool qCompareOp(T1 &&lhs, T2 &&rhs, const char *lhsExpr, const char *rhsExpr, const char *file, int line)
Definition qtestcase.h:795
Q_TESTLIB_EXPORT bool compare_3way_helper(bool success, const char *failureMsg, const void *lhsPtr, const void *rhsPtr, const char *(*lhsFormatter)(const void *), const char *(*rhsFormatter)(const void *), const char *lhsStr, const char *rhsStr, const char *(*actualOrderFormatter)(const void *), const char *(*expectedOrderFormatter)(const void *), const void *actualOrderPtr, const void *expectedOrderPtr, const char *expectedExpression, const char *file, int line)
Q_TESTLIB_EXPORT bool qVerify(bool statement, const char *statementStr, const char *description, const char *file, int line)
bool qCompare(const T *t1, const T *t2, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:709
bool qCompare(T1 *t1, T2 *t2, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:741
Q_TESTLIB_EXPORT QTestData Q_TESTLIB_EXPORT bool qCompare(qfloat16 const &t1, qfloat16 const &t2, const char *actual, const char *expected, const char *file, int line)
Q_TESTLIB_EXPORT const char * currentTestFunction()
Returns the name of the test function that is currently executed.
Q_TESTLIB_EXPORT char keyToAscii(Qt::Key key)
Q_TESTLIB_EXPORT bool runningTest()
bool qCompare(double const &t1, float const &t2, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:696
Q_TESTLIB_EXPORT void setMainSourcePath(const char *file, const char *builddir=nullptr)
bool compare_ptr_helper(const volatile QObject *t1, const volatile QObject *t2, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:628
Q_TESTLIB_EXPORT void * qElementData(const char *elementName, int metaTypeId)
bool qCompare(char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:761
Q_TESTLIB_EXPORT bool qCompare(int t1, int t2, const char *actual, const char *expected, const char *file, int line)
bool qCompare(const char *t1, char *t2, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:766
void setThrowOnFail(bool enable) noexcept
bool qCompare(const T1 &t1, const T2 &t2, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:678
Q_TESTLIB_EXPORT bool qExpectFail(const char *dataIndex, const char *comment, TestFailMode mode, const char *file, int line)
Q_TESTLIB_EXPORT void * qData(const char *tagName, int typeId)
Q_TESTLIB_EXPORT bool qCompare(double const &t1, double const &t2, const char *actual, const char *expected, const char *file, int line)
Q_TESTLIB_EXPORT bool currentTestFailed()
Returns true if the current test function has failed, otherwise false.
Q_TESTLIB_EXPORT bool qCompare(float const &t1, float const &t2, const char *actual, const char *expected, const char *file, int line)
Q_TESTLIB_EXPORT void qInit(QObject *testObject, int argc=0, char **argv=nullptr)
void setThrowOnSkip(bool enable) noexcept
bool qCompare(T *t1, std::nullptr_t, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:722
bool compare_ptr_helper(std::nullptr_t, const volatile void *t2, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:667
bool qTest(const T &actual, const char *elementName, const char *actualStr, const char *expected, const char *file, int line)
Definition qtestcase.h:773
Q_TESTLIB_EXPORT bool qCompare(const QLatin1StringView &t1, QStringView t2, const char *actual, const char *expected, const char *file, int line)
Q_TESTLIB_EXPORT const char * currentGlobalDataTag()
Returns the name of the current global test data.
Q_TESTLIB_EXPORT void failOnWarning()
Q_DECL_COLD_FUNCTION Q_TESTLIB_EXPORT void qCaught(const char *expected, const char *what, const char *file, int line)
bool compare_ptr_helper(const volatile QObject *t1, std::nullptr_t, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:637
Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const char *message)
Ignores messages created by qDebug(), qInfo() or qWarning().
bool compare_ptr_helper(const volatile void *t1, const volatile void *t2, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:619
Q_TESTLIB_EXPORT void failOnWarning(const char *message)
bool compare_ptr_helper(const volatile void *t1, std::nullptr_t, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:657
Q_TESTLIB_EXPORT void qSkip(const char *message, const char *file, int line)
bool qCompare(QString const &t1, QLatin1StringView const &t2, const char *actual, const char *expected, const char *file, int line)
Definition qtest.h:32
Q_DECL_COLD_FUNCTION Q_TESTLIB_EXPORT void qCaught(const char *expected, const char *file, int line)
Q_TESTLIB_EXPORT const char * currentDataTag()
Returns the name of the current test data.
bool qCompare(T *t1, T *t2, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:715
Q_TESTLIB_EXPORT int qExec(QObject *testObject, int argc=0, char **argv=nullptr)
Executes tests declared in testObject.
Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg, const void *actualPtr, const void *expectedPtr, const char *(*actualFormatter)(const void *), const char *(*expectedFormatter)(const void *), const char *actual, const char *expected, const char *file, int line)
Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg, const char *actual, const char *expected, const char *file, int line)
bool qCompare(char *t1, char *t2, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:752
Q_TESTLIB_EXPORT void qCleanup()
Q_TESTLIB_EXPORT void addColumnInternal(int id, const char *name)
Q_DECL_COLD_FUNCTION Q_TESTLIB_EXPORT void qFail(const char *message, const char *file, int line)
bool qCompare(float const &t1, double const &t2, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:702
Q_TESTLIB_EXPORT void * qGlobalData(const char *tagName, int typeId)
Q_TESTLIB_EXPORT bool qCompare(QStringView t1, const QLatin1StringView &t2, const char *actual, const char *expected, const char *file, int line)
bool qCompare(const char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:747
bool compare_ptr_helper(std::nullptr_t, const volatile QObject *t2, const char *actual, const char *expected, const char *file, int line)
Definition qtestcase.h:647
Q_TESTLIB_EXPORT const char * currentAppName()
Returns the name of the binary that is currently executed.
void addColumn(const char *name, T *=nullptr)
Adds a column with type {T} to the current test data.
Definition qtestcase.h:562
#define QTRY_COMPARE_LE_WITH_TIMEOUT(computed, baseline, timeout)
Definition qtestcase.h:270
#define QTRY_TIMEOUT_DEBUG_IMPL(expr, timeoutValue, step)
Definition qtestcase.h:188
#define QSKIP_INTERNAL(statement)
Definition qtestcase.h:291
#define QTRY_VERIFY_WITH_TIMEOUT(expr, timeout)
Definition qtestcase.h:211
#define QTRY_LOOP_IMPL(expr, timeoutValue, step)
Definition qtestcase.h:175
#define QTRY_COMPARE_GE_WITH_TIMEOUT(computed, baseline, timeout)
Definition qtestcase.h:284
#define QTRY_COMPARE_NE_WITH_TIMEOUT(computed, baseline, timeout)
Definition qtestcase.h:256
#define QTRY_COMPARE_WITH_TIMEOUT(expr, expected, timeout)
Definition qtestcase.h:232
#define QTRY_COMPARE_LT_WITH_TIMEOUT(computed, baseline, timeout)
Definition qtestcase.h:263
#define QTRY_COMPARE_GT_WITH_TIMEOUT(computed, baseline, timeout)
Definition qtestcase.h:277
#define QTEST_FAIL_ACTION
Definition qtestcase.h:39
#define QFAIL(message)
Definition qtestcase.h:71
#define QTRY_VERIFY2_WITH_TIMEOUT(expr, messageExpression, timeout)
Definition qtestcase.h:221
#define QCOMPARE_OP_IMPL(lhs, rhs, op, opId)
Definition qtestcase.h:94
#define QCOMPARE(actual, expected)
Definition qtestcase.h:88
#define QVERIFY(statement)
Definition qtestcase.h:65
#define QTRY_COMPARE_EQ_WITH_TIMEOUT(computed, baseline, timeout)
Definition qtestcase.h:249
#define QVERIFY2(statement, description)
Definition qtestcase.h:77
#define QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, op, opId, timeout)
Definition qtestcase.h:242
#define QTEST_SKIP_ACTION
Definition qtestcase.h:45
#define QTRY_IMPL(expr, timeoutAsGiven)
Definition qtestcase.h:197