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);
188 using YesType =
char[1];
189 using NoType =
char[2];
191 template <
typename C>
static YesType& test(
decltype(&C::initMain) ) ;
192 template <
typename C>
static NoType& test(...);
195 enum {
value =
sizeof(test<T>(
nullptr)) ==
sizeof(YesType) };
214#ifdef QT_TESTCASE_BUILDDIR
215# define QTEST_SET_MAIN_SOURCE_PATH QTest::setMainSourcePath(__FILE__, QT_TESTCASE_BUILDDIR);
217# define QTEST_SET_MAIN_SOURCE_PATH QTest::setMainSourcePath(__FILE__);
221#if QT_CONFIG(testlib_selfcover) && defined(__COVERAGESCANNER__)
222struct QtCoverageScanner
224 QtCoverageScanner(
const char *name)
226 __coveragescanner_clear();
227 __coveragescanner_testname(name);
231 __coveragescanner_save();
232 __coveragescanner_testname(
"");
235#define TESTLIB_SELFCOVERAGE_START(name) QtCoverageScanner _qtCoverageScanner(name);
237#define TESTLIB_SELFCOVERAGE_START(name)
240#if !defined(QTEST_BATCH_TESTS)
244#define QTEST_MAIN_WRAPPER(TestObject, ...) int
245 main(int argc, char *argv[]) \
246{
248 QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)<TestObject>();
252 return QTest::qExec(&tc, argc, argv); \
253}
258#if !defined(BATCHED_TEST_NAME)
259#define BATCHED_TEST_NAME "other"
261#define QTEST_MAIN_WRAPPER(TestObject, ...) void
263 qRegister##TestObject() \
264{
265 auto runTest = [](int argc, char** argv) -> int {
266 TESTLIB_SELFCOVERAGE_START(TestObject)
267 QT_PREPEND_NAMESPACE(QTest::Internal::callInitMain)<TestObject>();
270 QTEST_SET_MAIN_SOURCE_PATH
271 return QTest::qExec(&tc, argc, argv);
273 QTest::qRegisterTestCase(QStringLiteral(BATCHED_TEST_NAME), runTest); \
274}Q_CONSTRUCTOR_FUNCTION
276 (qRegister##TestObject)
282#include <QtTest/qtestsystem.h>
284#if defined(QT_NETWORK_LIB)
285# include <QtTest/qtest_network.h>
289#define QTEST_QAPP_SETUP(klaz)
290 klaz app(argc, argv);
291 app.setAttribute(Qt::AA_Use96Dpi, true);
293#if defined(QT_WIDGETS_LIB)
294# include <QtTest/qtest_widgets.h>
295# ifdef QT_KEYPAD_NAVIGATION
296# define QTEST_DISABLE_KEYPAD_NAVIGATION QApplication::setNavigationMode(Qt::NavigationModeNone);
298# define QTEST_DISABLE_KEYPAD_NAVIGATION
301# define QTEST_MAIN_SETUP() QTEST_QAPP_SETUP(QApplication) QTEST_DISABLE_KEYPAD_NAVIGATION
302#elif defined(QT_GUI_LIB)
303# include <QtTest/qtest_gui.h>
305# define QTEST_MAIN_SETUP() QTEST_QAPP_SETUP(QGuiApplication)
315#define QTEST_GUILESS_MAIN(TestObject)
std::enable_if< HasInitMain< T >::value, void >::type callInitMain()
std::enable_if<!HasInitMain< T >::value, void >::type callInitMain()
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)