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)
55 const qsizetype actualSize = actualEnd - actualIt;
56 const qsizetype expectedSize = expectedEnd - expectedBegin;
57 bool isOk = actualSize == expectedSize;
60 std::snprintf(msg,
sizeof(msg),
"Compared lists have different sizes.\n"
61 " Actual (%s) size: %lld\n"
62 " Expected (%s) size: %lld",
63 actual, qlonglong(actualSize),
64 expected, qlonglong(expectedSize));
67 for (
auto expectedIt = expectedBegin; isOk && expectedIt < expectedEnd; ++actualIt, ++expectedIt) {
68 if (!(*actualIt == *expectedIt)) {
69 const qsizetype i = qsizetype(expectedIt - expectedBegin);
70 char *val1 = toString(*actualIt);
71 char *val2 = toString(*expectedIt);
73 std::snprintf(msg,
sizeof(msg),
"Compared lists differ at index %lld.\n"
76 qlonglong(i), actual, val1 ? val1 :
"<null>",
77 expected, val2 ? val2 :
"<null>");
84 return compare_helper(isOk, msg, actual, expected, file, line);
89#if defined(TESTCASE_LOWDPI)
92 qputenv(
"QT_ENABLE_HIGHDPI_SCALING",
"0");
100inline bool qCompare(QList<T>
const &t1, QList<T>
const &t2,
const char *actual,
const char *expected,
101 const char *file,
int line)
103 return _q_compareSequence(t1.cbegin(), t1.cend(), t2.cbegin(), t2.cend(),
104 actual, expected, file, line);
107template <
typename T,
int N>
109 const char *actual,
const char *expected,
110 const char *file,
int line)
112 return _q_compareSequence(t1.cbegin(), t1.cend(), t2.cbegin(), t2.cend(),
113 actual, expected, file, line);
117template <
typename T,
int N>
118bool qCompare(QList<T>
const &t1,
const T (& t2)[N],
119 const char *actual,
const char *expected,
120 const char *file,
int line)
122 return _q_compareSequence(t1.cbegin(), t1.cend(), t2, t2 + N,
123 actual, expected, file, line);
127inline bool qCompare(QFlags<T>
const &t1, T
const &t2,
const char *actual,
const char *expected,
128 const char *file,
int line)
130 using Int =
typename QFlags<T>::Int;
131 return qCompare(Int(t1), Int(t2), actual, expected, file, line);
135inline bool qCompare(QFlags<T>
const &t1,
int const &t2,
const char *actual,
const char *expected,
136 const char *file,
int line)
138 using Int =
typename QFlags<T>::Int;
139 return qCompare(Int(t1), Int(t2), actual, expected, file, line);
143inline bool qCompare(qint64
const &t1, qint32
const &t2,
const char *actual,
144 const char *expected,
const char *file,
int line)
146 return qCompare(t1,
static_cast<qint64>(t2), actual, expected, file, line);
150inline bool qCompare(qint64
const &t1, quint32
const &t2,
const char *actual,
151 const char *expected,
const char *file,
int line)
153 return qCompare(t1,
static_cast<qint64>(t2), actual, expected, file, line);
157inline bool qCompare(quint64
const &t1, quint32
const &t2,
const char *actual,
158 const char *expected,
const char *file,
int line)
160 return qCompare(t1,
static_cast<quint64>(t2), actual, expected, file, line);
164inline bool qCompare(qint32
const &t1, qint64
const &t2,
const char *actual,
165 const char *expected,
const char *file,
int line)
167 return qCompare(
static_cast<qint64>(t1), t2, actual, expected, file, line);
171inline bool qCompare(quint32
const &t1, qint64
const &t2,
const char *actual,
172 const char *expected,
const char *file,
int line)
174 return qCompare(
static_cast<qint64>(t1), t2, actual, expected, file, line);
178inline bool qCompare(quint32
const &t1, quint64
const &t2,
const char *actual,
179 const char *expected,
const char *file,
int line)
181 return qCompare(
static_cast<quint64>(t1), t2, actual, expected, file, line);
189constexpr inline bool hasInitMain = qxp::is_detected_v<InitMainTest, T>;
194 if constexpr (hasInitMain<T>)
203#ifdef QT_TESTCASE_BUILDDIR
204# define QTEST_SET_MAIN_SOURCE_PATH QTest::setMainSourcePath(__FILE__, QT_TESTCASE_BUILDDIR);
206# define QTEST_SET_MAIN_SOURCE_PATH QTest::setMainSourcePath(__FILE__);
210#if QT_CONFIG(testlib_selfcover) && defined(__COVERAGESCANNER__)
211struct QtCoverageScanner
213 QtCoverageScanner(
const char *name)
215 __coveragescanner_clear();
216 __coveragescanner_testname(name);
220 __coveragescanner_save();
221 __coveragescanner_testname(
"");
224#define TESTLIB_SELFCOVERAGE_START(name) QtCoverageScanner _qtCoverageScanner(name);
226#define TESTLIB_SELFCOVERAGE_START(name)
229#if !defined(QTEST_BATCH_TESTS)
233#define QTEST_MAIN_WRAPPER(TestObject, ...) int
234 main(int argc, char *argv[]) \
235{
237 QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)<TestObject>();
241 return QTest::qExec(&tc, argc, argv); \
242}
247#if !defined(BATCHED_TEST_NAME)
248#define BATCHED_TEST_NAME "other"
250#define QTEST_MAIN_WRAPPER(TestObject, ...) void
252 qRegister##TestObject() \
253{
254 auto runTest = [](int argc, char** argv) -> int {
255 TESTLIB_SELFCOVERAGE_START(TestObject)
256 QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)<TestObject>();
259 QTEST_SET_MAIN_SOURCE_PATH
260 return QTest::qExec(&tc, argc, argv);
262 QTest::qRegisterTestCase(QStringLiteral(BATCHED_TEST_NAME), runTest); \
263}Q_CONSTRUCTOR_FUNCTION
265 (qRegister##TestObject)
271#include <QtTest/qtestsystem.h>
273#if defined(QT_NETWORK_LIB)
274# include <QtTest/qtest_network.h>
278#define QTEST_QAPP_SETUP(klaz)
279 klaz app(argc, argv);
280 app.setAttribute(Qt::AA_Use96Dpi, true);
282#if defined(QT_WIDGETS_LIB)
283# include <QtTest/qtest_widgets.h>
285# define QTEST_MAIN_SETUP() QTEST_QAPP_SETUP(QApplication)
286#elif defined(QT_GUI_LIB)
287# include <QtTest/qtest_gui.h>
289# define QTEST_MAIN_SETUP() QTEST_QAPP_SETUP(QGuiApplication)
299#define QTEST_GUILESS_MAIN(TestObject)
decltype(&T::initMain) InitMainTest
constexpr bool hasInitMain
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)