12#include <QtTest/qttestglobal.h>
13#include <QtTest/qtestcase.h>
14#include <QtTest/qtestdata.h>
15#include <QtTest/qtesttostring.h>
16#include <QtTest/qbenchmark.h>
18#if defined(TESTCASE_LOWDPI)
19#include <QtCore/qcoreapplication.h>
23#include <initializer_list>
32inline bool qCompare(QString
const &t1, QLatin1StringView
const &t2,
const char *actual,
33 const char *expected,
const char *file,
int line)
35 return qCompare(t1, QString(t2), actual, expected, file, line);
38inline bool qCompare(QLatin1StringView
const &t1, QString
const &t2,
const char *actual,
39 const char *expected,
const char *file,
int line)
41 return qCompare(QString(t1), t2, actual, expected, file, line);
45template <
typename ActualIterator,
typename ExpectedIterator>
47 ExpectedIterator expectedBegin, ExpectedIterator expectedEnd,
48 const char *actual,
const char *expected,
49 const char *file,
int line)
54 const qsizetype actualSize = actualEnd - actualIt;
55 const qsizetype expectedSize = expectedEnd - expectedBegin;
56 bool isOk = actualSize == expectedSize;
59 std::snprintf(msg,
sizeof(msg),
"Compared lists have different sizes.\n"
60 " Actual (%s) size: %lld\n"
61 " Expected (%s) size: %lld",
62 actual, qlonglong(actualSize),
63 expected, qlonglong(expectedSize));
66 for (
auto expectedIt = expectedBegin; isOk && expectedIt < expectedEnd; ++actualIt, ++expectedIt) {
67 if (!(*actualIt == *expectedIt)) {
68 const qsizetype i = qsizetype(expectedIt - expectedBegin);
69 char *val1 = toString(*actualIt);
70 char *val2 = toString(*expectedIt);
72 std::snprintf(msg,
sizeof(msg),
"Compared lists differ at index %lld.\n"
75 qlonglong(i), actual, val1 ? val1 :
"<null>",
76 expected, val2 ? val2 :
"<null>");
83 return compare_helper(isOk, msg, actual, expected, file, line);
88#if defined(TESTCASE_LOWDPI)
91 qputenv(
"QT_ENABLE_HIGHDPI_SCALING",
"0");
99inline bool qCompare(QList<T>
const &t1, QList<T>
const &t2,
const char *actual,
const char *expected,
100 const char *file,
int line)
102 return _q_compareSequence(t1.cbegin(), t1.cend(), t2.cbegin(), t2.cend(),
103 actual, expected, file, line);
106template <
typename T,
int N>
108 const char *actual,
const char *expected,
109 const char *file,
int line)
111 return _q_compareSequence(t1.cbegin(), t1.cend(), t2.cbegin(), t2.cend(),
112 actual, expected, file, line);
116template <
typename T,
int N>
117bool qCompare(QList<T>
const &t1,
const T (& t2)[N],
118 const char *actual,
const char *expected,
119 const char *file,
int line)
121 return _q_compareSequence(t1.cbegin(), t1.cend(), t2, t2 + N,
122 actual, expected, file, line);
126inline bool qCompare(QFlags<T>
const &t1, T
const &t2,
const char *actual,
const char *expected,
127 const char *file,
int line)
129 using Int =
typename QFlags<T>::Int;
130 return qCompare(Int(t1), Int(t2), actual, expected, file, line);
134inline bool qCompare(QFlags<T>
const &t1,
int const &t2,
const char *actual,
const char *expected,
135 const char *file,
int line)
137 using Int =
typename QFlags<T>::Int;
138 return qCompare(Int(t1), Int(t2), actual, expected, file, line);
142inline bool qCompare(qint64
const &t1, qint32
const &t2,
const char *actual,
143 const char *expected,
const char *file,
int line)
145 return qCompare(t1,
static_cast<qint64>(t2), actual, expected, file, line);
149inline bool qCompare(qint64
const &t1, quint32
const &t2,
const char *actual,
150 const char *expected,
const char *file,
int line)
152 return qCompare(t1,
static_cast<qint64>(t2), actual, expected, file, line);
156inline bool qCompare(quint64
const &t1, quint32
const &t2,
const char *actual,
157 const char *expected,
const char *file,
int line)
159 return qCompare(t1,
static_cast<quint64>(t2), actual, expected, file, line);
163inline bool qCompare(qint32
const &t1, qint64
const &t2,
const char *actual,
164 const char *expected,
const char *file,
int line)
166 return qCompare(
static_cast<qint64>(t1), t2, actual, expected, file, line);
170inline bool qCompare(quint32
const &t1, qint64
const &t2,
const char *actual,
171 const char *expected,
const char *file,
int line)
173 return qCompare(
static_cast<qint64>(t1), t2, actual, expected, file, line);
177inline bool qCompare(quint32
const &t1, quint64
const &t2,
const char *actual,
178 const char *expected,
const char *file,
int line)
180 return qCompare(
static_cast<quint64>(t1), t2, actual, expected, file, line);
184template <
typename T,
typename =
void>
193 if constexpr (HasInitMain<T>::value)
202#ifdef QT_TESTCASE_BUILDDIR
203# define QTEST_SET_MAIN_SOURCE_PATH QTest::setMainSourcePath(__FILE__, QT_TESTCASE_BUILDDIR);
205# define QTEST_SET_MAIN_SOURCE_PATH QTest::setMainSourcePath(__FILE__);
209#if QT_CONFIG(testlib_selfcover) && defined(__COVERAGESCANNER__)
210struct QtCoverageScanner
212 QtCoverageScanner(
const char *name)
214 __coveragescanner_clear();
215 __coveragescanner_testname(name);
219 __coveragescanner_save();
220 __coveragescanner_testname(
"");
223#define TESTLIB_SELFCOVERAGE_START(name) QtCoverageScanner _qtCoverageScanner(name);
225#define TESTLIB_SELFCOVERAGE_START(name)
228#if !defined(QTEST_BATCH_TESTS)
232#define QTEST_MAIN_WRAPPER(TestObject, ...) int
233 main(int argc, char *argv[]) \
234{
236 QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)<TestObject>();
240 return QTest::qExec(&tc, argc, argv); \
241}
246#if !defined(BATCHED_TEST_NAME)
247#define BATCHED_TEST_NAME "other"
249#define QTEST_MAIN_WRAPPER(TestObject, ...) void
251 qRegister##TestObject() \
252{
253 auto runTest = [](int argc, char** argv) -> int {
254 TESTLIB_SELFCOVERAGE_START(TestObject)
255 QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)<TestObject>();
258 QTEST_SET_MAIN_SOURCE_PATH
259 return QTest::qExec(&tc, argc, argv);
261 QTest::qRegisterTestCase(QStringLiteral(BATCHED_TEST_NAME), runTest); \
262}Q_CONSTRUCTOR_FUNCTION
264 (qRegister##TestObject)
270#include <QtTest/qtestsystem.h>
272#if defined(QT_NETWORK_LIB)
273# include <QtTest/qtest_network.h>
277#define QTEST_QAPP_SETUP(klaz)
278 klaz app(argc, argv);
279 app.setAttribute(Qt::AA_Use96Dpi, true);
281#if defined(QT_WIDGETS_LIB)
282# include <QtTest/qtest_widgets.h>
283# ifdef QT_KEYPAD_NAVIGATION
284# define QTEST_DISABLE_KEYPAD_NAVIGATION QApplication::setNavigationMode(Qt::NavigationModeNone);
286# define QTEST_DISABLE_KEYPAD_NAVIGATION
289# define QTEST_MAIN_SETUP() QTEST_QAPP_SETUP(QApplication) QTEST_DISABLE_KEYPAD_NAVIGATION
290#elif defined(QT_GUI_LIB)
291# include <QtTest/qtest_gui.h>
293# define QTEST_MAIN_SETUP() QTEST_QAPP_SETUP(QGuiApplication)
303#define QTEST_GUILESS_MAIN(TestObject)
bool qCompare(QList< T > const &t1, const T(&t2)[N], const char *actual, const char *expected, const char *file, int line)
bool qCompare(QList< T > const &t1, std::initializer_list< T > t2, const char *actual, const char *expected, const char *file, int line)
bool _q_compareSequence(ActualIterator actualIt, ActualIterator actualEnd, ExpectedIterator expectedBegin, ExpectedIterator expectedEnd, const char *actual, const char *expected, const char *file, int line)
bool qCompare(QFlags< T > const &t1, T const &t2, const char *actual, const char *expected, const char *file, int line)
bool qCompare(QString const &t1, QLatin1StringView const &t2, const char *actual, const char *expected, const char *file, int line)
bool qCompare(QList< T > const &t1, QList< T > const &t2, const char *actual, const char *expected, const char *file, int line)
#define TESTLIB_SELFCOVERAGE_START(name)
#define QTEST_SET_MAIN_SOURCE_PATH
#define QTEST_MAIN_WRAPPER(TestObject,...)
#define QTEST_MAIN_SETUP()
#define QTEST_QAPP_SETUP(klaz)