Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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#include <QtCore/qxpfunctional.h>
18
19#include <string.h>
20
21#ifndef QT_NO_EXCEPTIONS
22# include <exception>
23#endif // QT_NO_EXCEPTIONS
24
26
27#ifndef QT_NO_EXCEPTIONS
28
29#ifdef QTEST_THROW_ON_FAILURE
30# define QTEST_FAIL_ACTION QTest::Internal::throwOnFail()
31#else
32# define QTEST_FAIL_ACTION do { QTest::Internal::maybeThrowOnFail(); return; } while (false)
33#endif
34
35#ifdef QTEST_THROW_ON_SKIP
36# define QTEST_SKIP_ACTION QTest::Internal::throwOnSkip()
37#else
38# define QTEST_SKIP_ACTION do { QTest::Internal::maybeThrowOnSkip(); return; } while (false)
39#endif
40
41#else
42# if defined(QTEST_THROW_ON_FAILURE) || defined(QTEST_THROW_ON_SKIP)
43# error QTEST_THROW_ON_FAILURE/SKIP require exception support enabled.
44# endif
45#endif // QT_NO_EXCEPTIONS
46
47#ifndef QTEST_FAIL_ACTION
48# define QTEST_FAIL_ACTION return
49#endif
50
51#ifndef QTEST_SKIP_ACTION
52# define QTEST_SKIP_ACTION return
53#endif
54
55class qfloat16;
57
58#define QVERIFY(statement) \
59do {\
60 if (!QTest::qVerify(static_cast<bool>(statement), #statement, "", __FILE__, __LINE__))\
61 QTEST_FAIL_ACTION; \
62} while (false)
63
64#define QFAIL(message) \
65do {\
66 QTest::qFail(static_cast<const char *>(message), __FILE__, __LINE__);\
67 QTEST_FAIL_ACTION; \
68} while (false)
69
70#define QVERIFY2(statement, description) \
71do {\
72 if (statement) {\
73 if (!QTest::qVerify(true, #statement, static_cast<const char *>(description), __FILE__, __LINE__))\
74 QTEST_FAIL_ACTION; \
75 } else {\
76 if (!QTest::qVerify(false, #statement, static_cast<const char *>(description), __FILE__, __LINE__))\
77 QTEST_FAIL_ACTION; \
78 }\
79} while (false)
80
81#define QCOMPARE(actual, expected) \
82do {\
83 if (!QTest::qCompare(actual, expected, #actual, #expected, __FILE__, __LINE__))\
84 QTEST_FAIL_ACTION; \
85} while (false)
86
87#define QCOMPARE_OP_IMPL(lhs, rhs, op, opId) \
88do { \
89 if (!QTest::qCompareOp<QTest::ComparisonOperation::opId>(lhs, rhs, #lhs, #rhs, __FILE__, __LINE__)) \
90 QTEST_FAIL_ACTION; \
91} while (false)
92
93#define QCOMPARE_EQ(computed, baseline) QCOMPARE_OP_IMPL(computed, baseline, ==, Equal)
94#define QCOMPARE_NE(computed, baseline) QCOMPARE_OP_IMPL(computed, baseline, !=, NotEqual)
95#define QCOMPARE_LT(computed, baseline) QCOMPARE_OP_IMPL(computed, baseline, <, LessThan)
96#define QCOMPARE_LE(computed, baseline) QCOMPARE_OP_IMPL(computed, baseline, <=, LessThanOrEqual)
97#define QCOMPARE_GT(computed, baseline) QCOMPARE_OP_IMPL(computed, baseline, >, GreaterThan)
98#define QCOMPARE_GE(computed, baseline) QCOMPARE_OP_IMPL(computed, baseline, >=, GreaterThanOrEqual)
99
100#ifndef QT_NO_EXCEPTIONS
101
102# define QVERIFY_THROWS_NO_EXCEPTION(...) \
103 do { \
104 QT_TRY { \
105 __VA_ARGS__; \
106 /* success */ \
107 } QT_CATCH (...) { \
108 QTest::qCaught(nullptr, __FILE__, __LINE__); \
109 QTEST_FAIL_ACTION; \
110 } \
111 } while (false) \
112 /* end */
113
114#if QT_DEPRECATED_SINCE(6, 3)
115namespace QTest {
116QT_DEPRECATED_VERSION_X_6_3("Don't use QVERIFY_EXCEPTION_THROWN(expr, type) anymore, "
117 "use QVERIFY_THROWS_EXCEPTION(type, expr...) instead")
118inline void useVerifyThrowsException() {}
119} // namespace QTest
120# define QVERIFY_EXCEPTION_THROWN(expression, exceptiontype) \
121 QVERIFY_THROWS_EXCEPTION(exceptiontype, QTest::useVerifyThrowsException(); expression)
122#endif
123
124# define QVERIFY_THROWS_EXCEPTION(exceptiontype, ...) \
125 do {\
126 bool qverify_throws_exception_did_not_throw = false; \
127 QT_TRY {\
128 __VA_ARGS__; \
129 QTest::qFail("Expected exception of type " #exceptiontype " to be thrown" \
130 " but no exception caught", __FILE__, __LINE__); \
131 qverify_throws_exception_did_not_throw = true; \
132 } QT_CATCH (const exceptiontype &) { \
133 /* success */ \
134 } QT_CATCH (...) {\
135 QTest::qCaught(#exceptiontype, __FILE__, __LINE__); \
136 QTEST_FAIL_ACTION; \
137 }\
138 if (qverify_throws_exception_did_not_throw) \
139 QTEST_FAIL_ACTION; \
140 } while (false)
141
142#else // QT_NO_EXCEPTIONS
143
144/*
145 * These macros check whether the expression passed throws exceptions, but we can't
146 * catch them to check because Qt has been compiled without exception support. We can't
147 * skip the expression because it may have side effects and must be executed.
148 * So, users must use Qt with exception support enabled if they use exceptions
149 * in their code.
150 */
151# define QVERIFY_THROWS_EXCEPTION(...) \
152 static_assert(false, "Support for exceptions is disabled")
153# define QVERIFY_THROWS_NO_EXCEPTION(...) \
154 static_assert(false, "Support for exceptions is disabled")
155
156#endif // !QT_NO_EXCEPTIONS
157
158/* Ideally we would adapt qWaitFor(), or a variant on it, to implement roughly
159 * what the following provides as QTRY_LOOP_IMPL(); however, for now, the
160 * reporting of how much to increase the timeout to (if within a factor of two)
161 * on failure and the check for (QTest::runningTest() &&
162 * QTest::currentTestResolved()) go beyond qWaitFor(). (We no longer care about
163 * the bug in MSVC < 2017 that precluded using qWaitFor() in the implementation
164 * here, see QTBUG-59096.)
165 */
166
167// NB: not do {...} while (0) wrapped, as qt_test_i is accessed after it
168#define QTRY_LOOP_IMPL(expr, timeoutValue, step) \
169 if (!(expr)) { \
170 QTest::qWait(0); \
171 } \
172 int qt_test_i = 0; \
173 for (; qt_test_i < timeoutValue && !(QTest::runningTest() && QTest::currentTestResolved()) \
174 && !(expr); qt_test_i += step) { \
175 QTest::qWait(step); \
176 }
177// Ends in a for-block, so doesn't want a following semicolon.
178
179#define QTRY_TIMEOUT_DEBUG_IMPL(expr, timeoutValue, step) \
180 if (!(QTest::runningTest() && QTest::currentTestResolved()) && !(expr)) { \
181 QTRY_LOOP_IMPL(expr, 2 * (timeoutValue), step) \
182 if ((expr)) { \
183 QFAIL(qPrintable(QTest::Internal::formatTryTimeoutDebugMessage(\
184 u8"" #expr, timeoutValue, timeoutValue + qt_test_i))); \
185 } \
186 }
187
188#define QTRY_IMPL(expr, timeoutAsGiven)\
189 const auto qt_test_timeoutAsMs = [&] { \
190 /* make 5s work w/o user action: */ \
191 using namespace std::chrono_literals; \
192 return std::chrono::milliseconds{timeoutAsGiven}; \
193 }(); \
194 const int qt_test_step = qt_test_timeoutAsMs.count() < 350 ? qt_test_timeoutAsMs.count() / 7 + 1 : 50; \
195 const int qt_test_timeoutValue = qt_test_timeoutAsMs.count(); \
196 { QTRY_LOOP_IMPL(expr, qt_test_timeoutValue, qt_test_step) } \
197 QTRY_TIMEOUT_DEBUG_IMPL(expr, qt_test_timeoutValue, qt_test_step)
198// Ends with an if-block, so doesn't want a following semicolon.
199
200// Will try to wait for the expression to become true while allowing event processing
201#define QTRY_VERIFY_WITH_TIMEOUT(expr, timeout) \
202do { \
203 QTRY_IMPL(expr, timeout) \
204 QVERIFY(expr); \
205} while (false)
206
207#define QTRY_VERIFY(expr) QTRY_VERIFY_WITH_TIMEOUT(expr, 5s)
208
209// Will try to wait for the expression to become true while allowing event processing
210#define QTRY_VERIFY2_WITH_TIMEOUT(expr, messageExpression, timeout) \
211do { \
212 QTRY_IMPL(expr, timeout) \
213 QVERIFY2(expr, messageExpression); \
214} while (false)
215
216#define QTRY_VERIFY2(expr, messageExpression) QTRY_VERIFY2_WITH_TIMEOUT(expr, messageExpression, 5s)
217
218// Will try to wait for the comparison to become successful while allowing event processing
219#define QTRY_COMPARE_WITH_TIMEOUT(expr, expected, timeout) \
220do { \
221 QTRY_IMPL((expr) == (expected), timeout) \
222 QCOMPARE(expr, expected); \
223} while (false)
224
225#define QTRY_COMPARE(expr, expected) QTRY_COMPARE_WITH_TIMEOUT(expr, expected, 5s)
226
227#define QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, op, opId, timeout) \
228do { \
229 using Q_Cmp = QTest::Internal::Compare<QTest::ComparisonOperation::opId>; \
230 QTRY_IMPL(Q_Cmp::compare((computed), (baseline)), timeout) \
231 QCOMPARE_OP_IMPL(computed, baseline, op, opId); \
232} while (false)
233
234#define QTRY_COMPARE_EQ_WITH_TIMEOUT(computed, baseline, timeout) \
235 QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, ==, Equal, timeout)
236
237#define QTRY_COMPARE_EQ(computed, baseline) QTRY_COMPARE_EQ_WITH_TIMEOUT(computed, baseline, 5s)
238
239#define QTRY_COMPARE_NE_WITH_TIMEOUT(computed, baseline, timeout) \
240 QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, !=, NotEqual, timeout)
241
242#define QTRY_COMPARE_NE(computed, baseline) QTRY_COMPARE_NE_WITH_TIMEOUT(computed, baseline, 5s)
243
244#define QTRY_COMPARE_LT_WITH_TIMEOUT(computed, baseline, timeout) \
245 QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, <, LessThan, timeout)
246
247#define QTRY_COMPARE_LT(computed, baseline) QTRY_COMPARE_LT_WITH_TIMEOUT(computed, baseline, 5s)
248
249#define QTRY_COMPARE_LE_WITH_TIMEOUT(computed, baseline, timeout) \
250 QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, <=, LessThanOrEqual, timeout)
251
252#define QTRY_COMPARE_LE(computed, baseline) QTRY_COMPARE_LE_WITH_TIMEOUT(computed, baseline, 5s)
253
254#define QTRY_COMPARE_GT_WITH_TIMEOUT(computed, baseline, timeout) \
255 QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, >, GreaterThan, timeout)
256
257#define QTRY_COMPARE_GT(computed, baseline) QTRY_COMPARE_GT_WITH_TIMEOUT(computed, baseline, 5s)
258
259#define QTRY_COMPARE_GE_WITH_TIMEOUT(computed, baseline, timeout) \
260 QTRY_COMPARE_OP_WITH_TIMEOUT_IMPL(computed, baseline, >=, GreaterThanOrEqual, timeout)
261
262#define QTRY_COMPARE_GE(computed, baseline) QTRY_COMPARE_GE_WITH_TIMEOUT(computed, baseline, 5s)
263
264#define QSKIP_INTERNAL(statement) \
265do {\
266 QTest::qSkip(static_cast<const char *>(statement), __FILE__, __LINE__);\
267 QTEST_SKIP_ACTION; \
268} while (false)
269
270#define QSKIP(statement, ...) QSKIP_INTERNAL(statement)
271
272#define QEXPECT_FAIL(dataIndex, comment, mode)\
273do {\
274 if (!QTest::qExpectFail(dataIndex, static_cast<const char *>(comment), QTest::mode, __FILE__, __LINE__))\
275 QTEST_FAIL_ACTION; \
276} while (false)
277
278#define QFETCH(Type, name)\
279 Type name = *static_cast<Type *>(QTest::qData(#name, ::qMetaTypeId<typename std::remove_cv<Type >::type>()))
280
281#define QFETCH_GLOBAL(Type, name)\
282 Type name = *static_cast<Type *>(QTest::qGlobalData(#name, ::qMetaTypeId<typename std::remove_cv<Type >::type>()))
283
284#define QTEST(actual, testElement)\
285do {\
286 if (!QTest::qTest(actual, testElement, #actual, #testElement, __FILE__, __LINE__))\
287 QTEST_FAIL_ACTION; \
288} while (false)
289
290#ifdef QT_TESTCASE_BUILDDIR
291
292#ifndef QT_TESTCASE_SOURCEDIR
293#define QT_TESTCASE_SOURCEDIR nullptr
294#endif
295
296# define QFINDTESTDATA(basepath)\
297 QTest::qFindTestData(basepath, __FILE__, __LINE__, QT_TESTCASE_BUILDDIR, QT_TESTCASE_SOURCEDIR)
298#else
299# define QFINDTESTDATA(basepath)\
300 QTest::qFindTestData(basepath, __FILE__, __LINE__)
301#endif
302
303# define QEXTRACTTESTDATA(resourcePath) \
304 QTest::qExtractTestData(resourcePath)
305
306class QObject;
307class QTestData;
308
309namespace QTest
310{
311 namespace Internal {
312
313 [[noreturn]] Q_TESTLIB_EXPORT void throwOnFail();
314 [[noreturn]] Q_TESTLIB_EXPORT void throwOnSkip();
315 Q_TESTLIB_EXPORT void maybeThrowOnFail();
316 Q_TESTLIB_EXPORT void maybeThrowOnSkip();
317
318 Q_TESTLIB_EXPORT QString formatTryTimeoutDebugMessage(q_no_char8_t::QUtf8StringView expr, int timeout, int actual);
319
320 template <ComparisonOperation> struct Compare;
321 template <> struct Compare<ComparisonOperation::Equal>
322 {
323 template <typename T1, typename T2> static bool compare(T1 &&lhs, T2 &&rhs)
324 { return std::forward<T1>(lhs) == std::forward<T2>(rhs); }
325 };
327 {
328 template <typename T1, typename T2> static bool compare(T1 &&lhs, T2 &&rhs)
329 { return std::forward<T1>(lhs) != std::forward<T2>(rhs); }
330 };
332 {
333 template <typename T1, typename T2> static bool compare(T1 &&lhs, T2 &&rhs)
334 { return std::forward<T1>(lhs) < std::forward<T2>(rhs); }
335 };
337 {
338 template <typename T1, typename T2> static bool compare(T1 &&lhs, T2 &&rhs)
339 { return std::forward<T1>(lhs) <= std::forward<T2>(rhs); }
340 };
342 {
343 template <typename T1, typename T2> static bool compare(T1 &&lhs, T2 &&rhs)
344 { return std::forward<T1>(lhs) > std::forward<T2>(rhs); }
345 };
347 {
348 template <typename T1, typename T2> static bool compare(T1 &&lhs, T2 &&rhs)
349 { return std::forward<T1>(lhs) >= std::forward<T2>(rhs); }
350 };
351
352 template <typename T1> const char *genericToString(const void *arg)
353 {
354 using QTest::toString;
355 return toString(*static_cast<const T1 *>(arg));
356 }
357
358 template <> inline const char *genericToString<char *>(const void *arg)
359 {
360 using QTest::toString;
361 return toString(static_cast<const char *>(arg));
362 }
363
364 template <typename T> const char *pointerToString(const void *arg)
365 {
366 using QTest::toString;
367 return toString(static_cast<const T *>(arg));
368 }
369
370 // Exported so Qt Quick Test can also use it for generating backtraces upon crashes.
371 Q_TESTLIB_EXPORT extern bool noCrashHandler;
372
373 } // namespace Internal
374
375 Q_TESTLIB_EXPORT void qInit(QObject *testObject, int argc = 0, char **argv = nullptr);
376 Q_TESTLIB_EXPORT int qRun();
377 Q_TESTLIB_EXPORT void qCleanup();
378
379 Q_TESTLIB_EXPORT int qExec(QObject *testObject, int argc = 0, char **argv = nullptr);
380 Q_TESTLIB_EXPORT int qExec(QObject *testObject, const QStringList &arguments);
381
382#if QT_CONFIG(batch_test_support) || defined(Q_QDOC)
383 using TestEntryFunction = int (*)(int, char **);
384 Q_TESTLIB_EXPORT void qRegisterTestCase(const QString &name, TestEntryFunction entryFunction);
385#endif // QT_CONFIG(batch_test_support)
386
387 Q_TESTLIB_EXPORT void setMainSourcePath(const char *file, const char *builddir = nullptr);
388 Q_TESTLIB_EXPORT void setThrowOnFail(bool enable) noexcept;
389 Q_TESTLIB_EXPORT void setThrowOnSkip(bool enable) noexcept;
390
392 Q_DISABLE_COPY_MOVE(ThrowOnFailEnabler)
393 public:
396 };
397
399 Q_DISABLE_COPY_MOVE(ThrowOnSkipEnabler)
400 public:
403 };
404
406 Q_DISABLE_COPY_MOVE(ThrowOnFailDisabler)
407 public:
410 };
411
413 Q_DISABLE_COPY_MOVE(ThrowOnSkipDisabler)
414 public:
417 };
418
419 Q_TESTLIB_EXPORT bool qVerify(bool statement, const char *statementStr, const char *description,
420 const char *file, int line);
422 Q_TESTLIB_EXPORT void qFail(const char *message, const char *file, int line);
423 Q_TESTLIB_EXPORT void qSkip(const char *message, const char *file, int line);
424 Q_TESTLIB_EXPORT bool qExpectFail(const char *dataIndex, const char *comment, TestFailMode mode,
425 const char *file, int line);
427 Q_TESTLIB_EXPORT void qCaught(const char *expected, const char *what, const char *file, int line);
429 Q_TESTLIB_EXPORT void qCaught(const char *expected, const char *file, int line);
430#if QT_DEPRECATED_SINCE(6, 3)
431 QT_DEPRECATED_VERSION_X_6_3("Use qWarning() instead")
432 Q_TESTLIB_EXPORT void qWarn(const char *message, const char *file = nullptr, int line = 0);
433#endif
434 Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const char *message);
435#if QT_CONFIG(regularexpression)
436 Q_TESTLIB_EXPORT void ignoreMessage(QtMsgType type, const QRegularExpression &messagePattern);
437#endif
438 Q_TESTLIB_EXPORT void failOnWarning();
439 Q_TESTLIB_EXPORT void failOnWarning(const char *message);
440#if QT_CONFIG(regularexpression)
441 Q_TESTLIB_EXPORT void failOnWarning(const QRegularExpression &messagePattern);
442#endif
443
444#if QT_CONFIG(temporaryfile)
445 Q_TESTLIB_EXPORT QSharedPointer<QTemporaryDir> qExtractTestData(const QString &dirName);
446#endif
447 Q_TESTLIB_EXPORT QString qFindTestData(const char* basepath, const char* file = nullptr, int line = 0, const char* builddir = nullptr, const char* sourcedir = nullptr);
448 Q_TESTLIB_EXPORT QString qFindTestData(const QString& basepath, const char* file = nullptr, int line = 0, const char* builddir = nullptr, const char *sourcedir = nullptr);
449
450 Q_TESTLIB_EXPORT void *qData(const char *tagName, int typeId);
451 Q_TESTLIB_EXPORT void *qGlobalData(const char *tagName, int typeId);
452 Q_TESTLIB_EXPORT void *qElementData(const char *elementName, int metaTypeId);
453 Q_TESTLIB_EXPORT QObject *testObject();
454
455 Q_TESTLIB_EXPORT const char *currentAppName();
456
457 Q_TESTLIB_EXPORT const char *currentTestFunction();
458 Q_TESTLIB_EXPORT const char *currentDataTag();
459 Q_TESTLIB_EXPORT bool currentTestFailed();
460 Q_TESTLIB_EXPORT bool currentTestResolved();
461 Q_TESTLIB_EXPORT bool runningTest(); // Internal, for use by macros and QTestEventLoop.
462
463 Q_TESTLIB_EXPORT Qt::Key asciiToKey(char ascii);
464 Q_TESTLIB_EXPORT char keyToAscii(Qt::Key key);
465
466#if QT_DEPRECATED_SINCE(6, 4)
467 QT_DEPRECATED_VERSION_X_6_4("use an overload that takes a formatter callback, "
468 "or an overload that takes only failure message, if you "
469 "do not need to stringify the values")
470 Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
471 char *actualVal, char *expectedVal,
472 const char *actual, const char *expected,
473 const char *file, int line);
474#endif // QT_DEPRECATED_SINCE(6, 4)
475#if QT_DEPRECATED_SINCE(6, 8)
476 QT_DEPRECATED_VERSION_X_6_8("use an overload that takes a formatter callback, "
477 "or an overload that takes only failure message, if you "
478 "do not need to stringify the values")
479 Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
480 qxp::function_ref<const char*()> actualVal,
481 qxp::function_ref<const char*()> expectedVal,
482 const char *actual, const char *expected,
483 const char *file, int line);
484#endif // QT_DEPRECATED_SINCE(6, 8)
485 Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
486 const void *actualPtr, const void *expectedPtr,
487 const char *(*actualFormatter)(const void *),
488 const char *(*expectedFormatter)(const void *),
489 const char *actual, const char *expected,
490 const char *file, int line);
491 Q_TESTLIB_EXPORT bool compare_helper(bool success, const char *failureMsg,
492 const char *actual, const char *expected,
493 const char *file, int line);
494
495 Q_TESTLIB_EXPORT void addColumnInternal(int id, const char *name);
496
497 template <typename T>
498 inline void addColumn(const char *name, T * = nullptr)
499 {
500 using QIsSameTConstChar = std::is_same<T, const char*>;
501 static_assert(!QIsSameTConstChar::value, "const char* is not allowed as a test data format.");
502 addColumnInternal(qMetaTypeId<T>(), name);
503 }
504 Q_TESTLIB_EXPORT QTestData &newRow(const char *dataTag);
505 Q_TESTLIB_EXPORT QTestData &addRow(const char *format, ...) Q_ATTRIBUTE_FORMAT_PRINTF(1, 2);
506
507 Q_TESTLIB_EXPORT bool qCompare(qfloat16 const &t1, qfloat16 const &t2,
508 const char *actual, const char *expected, const char *file, int line);
509
510 Q_TESTLIB_EXPORT bool qCompare(float const &t1, float const &t2,
511 const char *actual, const char *expected, const char *file, int line);
512
513 Q_TESTLIB_EXPORT bool qCompare(double const &t1, double const &t2,
514 const char *actual, const char *expected, const char *file, int line);
515
516 Q_TESTLIB_EXPORT bool qCompare(int t1, int t2, const char *actual, const char *expected,
517 const char *file, int line);
518
519#if QT_POINTER_SIZE == 8
520 Q_TESTLIB_EXPORT bool qCompare(qsizetype t1, qsizetype t2, const char *actual, const char *expected,
521 const char *file, int line);
522#endif
523
524 Q_TESTLIB_EXPORT bool qCompare(unsigned t1, unsigned t2, const char *actual, const char *expected,
525 const char *file, int line);
526
527 Q_TESTLIB_EXPORT bool qCompare(QStringView t1, QStringView t2,
528 const char *actual, const char *expected,
529 const char *file, int line);
530 Q_TESTLIB_EXPORT bool qCompare(QStringView t1, const QLatin1StringView &t2,
531 const char *actual, const char *expected,
532 const char *file, int line);
533 Q_TESTLIB_EXPORT bool qCompare(const QLatin1StringView &t1, QStringView t2,
534 const char *actual, const char *expected,
535 const char *file, int line);
536 inline bool qCompare(const QString &t1, const QString &t2,
537 const char *actual, const char *expected,
538 const char *file, int line)
539 {
540 return qCompare(QStringView(t1), QStringView(t2), actual, expected, file, line);
541 }
542 inline bool qCompare(const QString &t1, const QLatin1StringView &t2,
543 const char *actual, const char *expected,
544 const char *file, int line)
545 {
546 return qCompare(QStringView(t1), t2, actual, expected, file, line);
547 }
548 inline bool qCompare(const QLatin1StringView &t1, const QString &t2,
549 const char *actual, const char *expected,
550 const char *file, int line)
551 {
552 return qCompare(t1, QStringView(t2), actual, expected, file, line);
553 }
554
555 inline bool compare_ptr_helper(const volatile void *t1, const volatile void *t2, const char *actual,
556 const char *expected, const char *file, int line)
557 {
558 auto formatter = Internal::pointerToString<void>;
559 return compare_helper(t1 == t2, "Compared pointers are not the same",
560 const_cast<const void *>(t1), const_cast<const void *>(t2),
561 formatter, formatter, actual, expected, file, line);
562 }
563
564 inline bool compare_ptr_helper(const volatile QObject *t1, const volatile QObject *t2, const char *actual,
565 const char *expected, const char *file, int line)
566 {
567 auto formatter = Internal::pointerToString<QObject>;
568 return compare_helper(t1 == t2, "Compared QObject pointers are not the same",
569 const_cast<const QObject *>(t1), const_cast<const QObject *>(t2),
570 formatter, formatter, actual, expected, file, line);
571 }
572
573 inline bool compare_ptr_helper(const volatile QObject *t1, std::nullptr_t, const char *actual,
574 const char *expected, const char *file, int line)
575 {
576 auto lhsFormatter = Internal::pointerToString<QObject>;
577 auto rhsFormatter = Internal::genericToString<std::nullptr_t>;
578 return compare_helper(t1 == nullptr, "Compared QObject pointers are not the same",
579 const_cast<const QObject *>(t1), nullptr,
580 lhsFormatter, rhsFormatter, actual, expected, file, line);
581 }
582
583 inline bool compare_ptr_helper(std::nullptr_t, const volatile QObject *t2, const char *actual,
584 const char *expected, const char *file, int line)
585 {
586 auto lhsFormatter = Internal::genericToString<std::nullptr_t>;
587 auto rhsFormatter = Internal::pointerToString<QObject>;
588 return compare_helper(nullptr == t2, "Compared QObject pointers are not the same",
589 nullptr, const_cast<const QObject *>(t2),
590 lhsFormatter, rhsFormatter, actual, expected, file, line);
591 }
592
593 inline bool compare_ptr_helper(const volatile void *t1, std::nullptr_t, const char *actual,
594 const char *expected, const char *file, int line)
595 {
596 auto lhsFormatter = Internal::pointerToString<void>;
597 auto rhsFormatter = Internal::genericToString<std::nullptr_t>;
598 return compare_helper(t1 == nullptr, "Compared pointers are not the same",
599 const_cast<const void *>(t1), nullptr,
600 lhsFormatter, rhsFormatter, actual, expected, file, line);
601 }
602
603 inline bool compare_ptr_helper(std::nullptr_t, const volatile void *t2, const char *actual,
604 const char *expected, const char *file, int line)
605 {
606 auto lhsFormatter = Internal::genericToString<std::nullptr_t>;
607 auto rhsFormatter = Internal::pointerToString<void>;
608 return compare_helper(nullptr == t2, "Compared pointers are not the same",
609 nullptr, const_cast<const void *>(t2),
610 lhsFormatter, rhsFormatter, actual, expected, file, line);
611 }
612
613 template <typename T1, typename T2 = T1>
614 inline bool qCompare(const T1 &t1, const T2 &t2, const char *actual, const char *expected,
615 const char *file, int line)
616 {
617 using D1 = std::decay_t<T1>;
618 using D2 = std::decay_t<T2>;
620 return compare_helper(t1 == t2, "Compared values are not the same",
621 std::addressof(t1), std::addressof(t2),
622 genericToString<D1>, genericToString<D2>,
623 actual, expected, file, line);
624 }
625
626 inline bool qCompare(double const &t1, float const &t2, const char *actual,
627 const char *expected, const char *file, int line)
628 {
629 return qCompare(qreal(t1), qreal(t2), actual, expected, file, line);
630 }
631
632 inline bool qCompare(float const &t1, double const &t2, const char *actual,
633 const char *expected, const char *file, int line)
634 {
635 return qCompare(qreal(t1), qreal(t2), actual, expected, file, line);
636 }
637
638 template <typename T>
639 inline bool qCompare(const T *t1, const T *t2, const char *actual, const char *expected,
640 const char *file, int line)
641 {
642 return compare_ptr_helper(t1, t2, actual, expected, file, line);
643 }
644 template <typename T>
645 inline bool qCompare(T *t1, T *t2, const char *actual, const char *expected,
646 const char *file, int line)
647 {
648 return compare_ptr_helper(t1, t2, actual, expected, file, line);
649 }
650
651 template <typename T>
652 inline bool qCompare(T *t1, std::nullptr_t, const char *actual, const char *expected,
653 const char *file, int line)
654 {
655 return compare_ptr_helper(t1, nullptr, actual, expected, file, line);
656 }
657 template <typename T>
658 inline bool qCompare(std::nullptr_t, T *t2, const char *actual, const char *expected,
659 const char *file, int line)
660 {
661 return compare_ptr_helper(nullptr, t2, actual, expected, file, line);
662 }
663
664 template <typename T1, typename T2>
665 inline bool qCompare(const T1 *t1, const T2 *t2, const char *actual, const char *expected,
666 const char *file, int line)
667 {
668 return compare_ptr_helper(t1, static_cast<const T1 *>(t2), actual, expected, file, line);
669 }
670 template <typename T1, typename T2>
671 inline bool qCompare(T1 *t1, T2 *t2, const char *actual, const char *expected,
672 const char *file, int line)
673 {
674 return compare_ptr_helper(const_cast<const T1 *>(t1),
675 static_cast<const T1 *>(const_cast<const T2 *>(t2)), actual, expected, file, line);
676 }
677 inline bool qCompare(const char *t1, const char *t2, const char *actual,
678 const char *expected, const char *file, int line)
679 {
680 return compare_string_helper(t1, t2, actual, expected, file, line);
681 }
682 inline bool qCompare(char *t1, char *t2, const char *actual, const char *expected,
683 const char *file, int line)
684 {
685 return compare_string_helper(t1, t2, actual, expected, file, line);
686 }
687
688 /* The next two overloads are for MSVC that shows problems with implicit
689 conversions
690 */
691 inline bool qCompare(char *t1, const char *t2, const char *actual,
692 const char *expected, const char *file, int line)
693 {
694 return compare_string_helper(t1, t2, actual, expected, file, line);
695 }
696 inline bool qCompare(const char *t1, char *t2, const char *actual,
697 const char *expected, const char *file, int line)
698 {
699 return compare_string_helper(t1, t2, actual, expected, file, line);
700 }
701
702 template <class T>
703 inline bool qTest(const T& actual, const char *elementName, const char *actualStr,
704 const char *expected, const char *file, int line)
705 {
706 return qCompare(actual, *static_cast<const T *>(QTest::qElementData(elementName,
707 qMetaTypeId<T>())), actualStr, expected, file, line);
708 }
709
710#if QT_DEPRECATED_SINCE(6, 8)
711 QT_DEPRECATED_VERSION_X_6_8("use the overload without qxp::function_ref")
712 Q_TESTLIB_EXPORT bool reportResult(bool success, qxp::function_ref<const char*()> lhs,
713 qxp::function_ref<const char*()> rhs,
714 const char *lhsExpr, const char *rhsExpr,
715 ComparisonOperation op, const char *file, int line);
716#endif // QT_DEPRECATED_SINCE(6, 8)
717
718 Q_TESTLIB_EXPORT bool reportResult(bool success, const void *lhs, const void *rhs,
719 const char *(*lhsFormatter)(const void*),
720 const char *(*rhsFormatter)(const void*),
721 const char *lhsExpr, const char *rhsExpr,
722 ComparisonOperation op, const char *file, int line);
723
724 template <ComparisonOperation op, typename T1, typename T2 = T1>
725 inline bool qCompareOp(T1 &&lhs, T2 &&rhs, const char *lhsExpr, const char *rhsExpr,
726 const char *file, int line)
727 {
728 using D1 = std::decay_t<T1>;
729 using D2 = std::decay_t<T2>;
731 using Comparator = Internal::Compare<op>;
732
733 /* assumes that op does not actually move from lhs and rhs */
734 bool result = Comparator::compare(std::forward<T1>(lhs), std::forward<T2>(rhs));
735 return reportResult(result, std::addressof(lhs), std::addressof(rhs),
736 genericToString<D1>, genericToString<D2>,
737 lhsExpr, rhsExpr, op, file, line);
738
739 }
740}
741
742
743#define QWARN(msg) QTest::qWarn(static_cast<const char *>(msg), __FILE__, __LINE__)
744
746
747#endif
\inmodule QtCore
Definition qobject.h:103
\inmodule QtCore \reentrant
\inmodule QtCore
\inmodule QtCore
Definition qstringview.h:78
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
~ThrowOnFailDisabler()
Destructor.
Definition qtestcase.h:409
ThrowOnFailDisabler()
Constructor.
Definition qtestcase.h:408
ThrowOnFailEnabler()
Constructor.
Definition qtestcase.h:394
~ThrowOnFailEnabler()
Destructor.
Definition qtestcase.h:395
ThrowOnSkipDisabler()
Constructor.
Definition qtestcase.h:415
~ThrowOnSkipDisabler()
Destructor.
Definition qtestcase.h:416
~ThrowOnSkipEnabler()
Destructor.
Definition qtestcase.h:402
ThrowOnSkipEnabler()
Constructor.
Definition qtestcase.h:401
\keyword 16-bit Floating Point Support\inmodule QtCore \inheaderfile QFloat16
Definition qfloat16.h:47
QJSValue expected
Definition qjsengine.cpp:12
QList< QVariant > arguments
Combined button and popup list for selecting options.
const char * toString(QSizePolicy::Policy p)
const char * pointerToString(const void *arg)
Definition qtestcase.h:364
Q_TESTLIB_EXPORT void maybeThrowOnFail()
Q_TESTLIB_EXPORT QString formatTryTimeoutDebugMessage(q_no_char8_t::QUtf8StringView expr, int timeout, int actual)
Q_TESTLIB_EXPORT void throwOnFail()
Q_TESTLIB_EXPORT void maybeThrowOnSkip()
Q_TESTLIB_EXPORT void throwOnSkip()
const char * genericToString(const void *arg)
Definition qtestcase.h:352
const char * genericToString< char * >(const void *arg)
Definition qtestcase.h:358
bool qTest(const T &actual, const char *elementName, const char *actualStr, const char *expected, const char *file, int line)
Definition qtestcase.h:703
Q_TESTLIB_EXPORT int qRun()
Q_TESTLIB_EXPORT bool currentTestResolved()
Q_TESTLIB_EXPORT 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 QTestData & newRow(const char *dataTag)
Appends a new row to the current test data.
bool qCompareOp(T1 &&lhs, T2 &&rhs, const char *lhsExpr, const char *rhsExpr, const char *file, int line)
Definition qtestcase.h:725
Q_TESTLIB_EXPORT bool qVerify(bool statement, const char *statementStr, const char *description, 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 QObject * testObject()
Q_TESTLIB_EXPORT char keyToAscii(Qt::Key key)
Q_TESTLIB_EXPORT bool runningTest()
Q_TESTLIB_EXPORT void setMainSourcePath(const char *file, const char *builddir=nullptr)
Q_TESTLIB_EXPORT void * qElementData(const char *elementName, int metaTypeId)
Q_TESTLIB_EXPORT Qt::Key asciiToKey(char ascii)
Definition qasciikey.cpp:16
void setThrowOnFail(bool enable) noexcept
Q_TESTLIB_EXPORT bool qExpectFail(const char *dataIndex, const char *comment, TestFailMode mode, const char *file, int line)
char * toString(const MyPoint &point)
Q_TESTLIB_EXPORT void * qData(const char *tagName, int typeId)
Q_TESTLIB_EXPORT bool currentTestFailed()
Returns true if the current test function has failed, otherwise false.
Q_TESTLIB_EXPORT void qInit(QObject *testObject, int argc=0, char **argv=nullptr)
void setThrowOnSkip(bool enable) noexcept
Q_TESTLIB_EXPORT bool compare_string_helper(const char *t1, const char *t2, const char *actual, const char *expected, const char *file, int line)
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)
Q_TESTLIB_EXPORT QString qFindTestData(const char *basepath, const char *file=nullptr, int line=0, const char *builddir=nullptr, const char *sourcedir=nullptr)
ComparisonOperation
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:555
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:31
void addColumn(const char *name, T *=nullptr)
Adds a column with type {T} to the current test data.
Definition qtestcase.h:498
Q_TESTLIB_EXPORT const char * currentDataTag()
Returns the name of the current test data.
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 QTestData & addRow(const char *format,...) Q_ATTRIBUTE_FORMAT_PRINTF(1
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)
Q_TESTLIB_EXPORT void * qGlobalData(const char *tagName, int typeId)
Q_TESTLIB_EXPORT const char * currentAppName()
Returns the name of the binary that is currently executed.
#define Q_ATTRIBUTE_FORMAT_PRINTF(A, B)
#define Q_DECL_COLD_FUNCTION
QtMsgType
Definition qlogging.h:29
GLenum mode
GLuint64 key
GLbitfield GLuint64 timeout
[4]
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
[4]
GLenum type
GLboolean enable
GLuint GLsizei const GLchar * message
GLuint name
GLint GLsizei GLsizei GLenum format
GLuint64EXT * result
[6]
SSL_CTX int void * arg
#define t2
#define QT_DEPRECATED_VERSION_X_6_3(text)
#define QT_DEPRECATED_VERSION_X_6_4(text)
#define QT_DEPRECATED_VERSION_X_6_8(text)
ptrdiff_t qsizetype
Definition qtypes.h:165
double qreal
Definition qtypes.h:187
QFile file
[0]