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