diff options
Diffstat (limited to 'tests/auto/corelib')
17 files changed, 920 insertions, 89 deletions
diff --git a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp index 32344f1e26..1b7d410beb 100644 --- a/tests/auto/corelib/io/qdebug/tst_qdebug.cpp +++ b/tests/auto/corelib/io/qdebug/tst_qdebug.cpp @@ -49,6 +49,7 @@ private slots: void debugWithBool() const; void debugSpaceHandling() const; void debugNoQuotes() const; + void verbosity() const; void stateSaver() const; void veryLongWarningMessage() const; void qDebugQChar() const; @@ -192,7 +193,11 @@ public: QDebug operator<< (QDebug s, const MyLine& line) { const QDebugStateSaver saver(s); - s.nospace() << "MyLine(" << line.p1 << ", " << line.p2 << ")"; + s.nospace(); + s << "MyLine(" << line.p1 << ", "<< line.p2; + if (s.verbosity() > 2) + s << ", Manhattan length=" << (qAbs(line.p2.v1 - line.p1.v1) + qAbs(line.p2.v2 - line.p1.v2)); + s << ')'; return s; } @@ -255,6 +260,33 @@ void tst_QDebug::debugNoQuotes() const QCOMPARE(s_msg, QString::fromLatin1("'H' \"Hello\" \"Hello\" H Hello Hello")); } +void tst_QDebug::verbosity() const +{ + MyLine line(MyPoint(10, 11), MyPoint (12, 13)); + QString output; + QDebug d(&output); + d.nospace(); + d << line << '\n'; + const int oldVerbosity = d.verbosity(); + d.setVerbosity(0); + QCOMPARE(d.verbosity(), 0); + d.setVerbosity(7); + QCOMPARE(d.verbosity(), 7); + const int newVerbosity = oldVerbosity + 2; + d.setVerbosity(newVerbosity); + QCOMPARE(d.verbosity(), newVerbosity); + d << line << '\n'; + d.setVerbosity(oldVerbosity ); + QCOMPARE(d.verbosity(), oldVerbosity ); + d << line; + const QStringList lines = output.split(QLatin1Char('\n')); + QCOMPARE(lines.size(), 3); + // Verbose should be longer + QVERIFY2(lines.at(1).size() > lines.at(0).size(), qPrintable(lines.join(QLatin1Char(',')))); + // Switching back to brief produces same output + QCOMPARE(lines.at(0).size(), lines.at(2).size()); +} + void tst_QDebug::stateSaver() const { MessageHandlerSetter mhs(myMessageHandler); diff --git a/tests/auto/corelib/io/qfileselector/platforms/+haiku/test b/tests/auto/corelib/io/qfileselector/platforms/+haiku/test new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/auto/corelib/io/qfileselector/platforms/+haiku/test diff --git a/tests/auto/corelib/io/qfileselector/platforms/+haiku/test2 b/tests/auto/corelib/io/qfileselector/platforms/+haiku/test2 new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/auto/corelib/io/qfileselector/platforms/+haiku/test2 diff --git a/tests/auto/corelib/io/qfileselector/platforms/+unix/+haiku/test b/tests/auto/corelib/io/qfileselector/platforms/+unix/+haiku/test new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/auto/corelib/io/qfileselector/platforms/+unix/+haiku/test diff --git a/tests/auto/corelib/io/qfileselector/qfileselector.qrc b/tests/auto/corelib/io/qfileselector/qfileselector.qrc index 661647f933..6e2699774d 100644 --- a/tests/auto/corelib/io/qfileselector/qfileselector.qrc +++ b/tests/auto/corelib/io/qfileselector/qfileselector.qrc @@ -19,6 +19,7 @@ <file>platforms/+unix/+darwin/+mac/+osx/test</file> <file>platforms/+unix/+darwin/+mac/test</file> <file>platforms/+unix/+darwin/test</file> + <file>platforms/+unix/+haiku/test</file> <file>platforms/+unix/+linux/test</file> <file>platforms/+unix/test</file> <file>platforms/+windows/+wince/test</file> @@ -30,6 +31,7 @@ <file>platforms/+osx/test</file> <file>platforms/+darwin/test</file> <file>platforms/+mac/test</file> + <file>platforms/+haiku/test</file> <file>platforms/+linux/test</file> <file>platforms/+wince/test</file> @@ -39,6 +41,7 @@ <file>platforms/+blackberry/test2</file> <file>platforms/+ios/test2</file> <file>platforms/+osx/test2</file> + <file>platforms/+haiku/test2</file> <file>platforms/+linux/test2</file> <file>platforms/+wince/test2</file> <file>platforms/+winnt/test2</file> diff --git a/tests/auto/corelib/io/qfileselector/tst_qfileselector.cpp b/tests/auto/corelib/io/qfileselector/tst_qfileselector.cpp index b3767b4887..87381f4c4e 100644 --- a/tests/auto/corelib/io/qfileselector/tst_qfileselector.cpp +++ b/tests/auto/corelib/io/qfileselector/tst_qfileselector.cpp @@ -89,7 +89,7 @@ void tst_QFileSelector::basicTest_data() QString expectedPlatform2File(""); //Only the last selector QString expectedPlatform3File; // Only the first selector (the family) #if defined(Q_OS_UNIX) && !defined(Q_OS_ANDROID) && !defined(Q_OS_BLACKBERRY) && \ - !defined(Q_OS_DARWIN) && !defined(Q_OS_LINUX) + !defined(Q_OS_DARWIN) && !defined(Q_OS_LINUX) && !defined(Q_OS_HAIKU) /* We are only aware of specific unixes, and do not have test files for any of the others. However those unixes can get a selector added from the result of a uname call, so this will lead to a case where we don't have that file so we can't expect the concatenation of platform diff --git a/tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp b/tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp index db0136dd20..769a96ac64 100644 --- a/tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp +++ b/tests/auto/corelib/io/qurlquery/tst_qurlquery.cpp @@ -177,6 +177,7 @@ void tst_QUrlQuery::constructing() QVERIFY(copy.isEmpty()); QVERIFY(!copy.isDetached()); QVERIFY(copy == empty); + QCOMPARE(qHash(copy), qHash(empty)); QVERIFY(!(copy != empty)); copy = empty; @@ -184,6 +185,7 @@ void tst_QUrlQuery::constructing() copy = QUrlQuery(); QVERIFY(copy == empty); + QCOMPARE(qHash(copy), qHash(empty)); } { QUrlQuery copy(emptyQuery()); @@ -298,6 +300,7 @@ void tst_QUrlQuery::addRemove() QVERIFY(query == original); QVERIFY(!(query != original)); + QCOMPARE(qHash(query), qHash(original)); } { diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index a192ccde59..6da8f55e61 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -3357,9 +3357,6 @@ void tst_QVariant::numericalConvert_data() void tst_QVariant::numericalConvert() { -#if defined(Q_OS_LINUX) && defined(Q_CC_GNU) && !defined(__x86_64__) - QSKIP("Known to fail due to a GCC bug on at least Ubuntu 10.04 32-bit - check QTBUG-8959"); -#endif QFETCH(QVariant, v); QFETCH(bool, isInteger); double num = isInteger ? 5 : 5.3; diff --git a/tests/auto/corelib/plugin/qpluginloader/tst_qpluginloader.cpp b/tests/auto/corelib/plugin/qpluginloader/tst_qpluginloader.cpp index bdbb291d7f..cd77b188cc 100644 --- a/tests/auto/corelib/plugin/qpluginloader/tst_qpluginloader.cpp +++ b/tests/auto/corelib/plugin/qpluginloader/tst_qpluginloader.cpp @@ -120,6 +120,7 @@ private slots: void loadGarbage(); #endif void relativePath(); + void absolutePath(); void reloadPlugin(); void preloadedPlugin_data(); void preloadedPlugin(); @@ -406,6 +407,20 @@ void tst_QPluginLoader::relativePath() QVERIFY(loader.unload()); } +void tst_QPluginLoader::absolutePath() +{ + // Windows binaries run from release and debug subdirs, so we can't rely on the current dir. + const QString binDir = QFINDTESTDATA("bin"); + QVERIFY(!binDir.isEmpty()); + QVERIFY(QDir::isAbsolutePath(binDir)); + QPluginLoader loader(binDir + "/theplugin"); + loader.load(); // not recommended, instance() should do the job. + PluginInterface *instance = qobject_cast<PluginInterface*>(loader.instance()); + QVERIFY(instance); + QCOMPARE(instance->pluginName(), QLatin1String("Plugin ok")); + QVERIFY(loader.unload()); +} + void tst_QPluginLoader::reloadPlugin() { QPluginLoader loader; diff --git a/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp b/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp index 9bce948140..99e5c4c85d 100644..100755 --- a/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp +++ b/tests/auto/corelib/tools/qalgorithms/tst_qalgorithms.cpp @@ -31,6 +31,7 @@ ** ****************************************************************************/ +#include "../../../../../src/corelib/tools/qalgorithms.h" #include <QtTest/QtTest> #include <iostream> @@ -80,6 +81,24 @@ private slots: void popCount32() { popCount_impl<quint32>(); } void popCount64() { popCount_impl<quint64>(); } + void countTrailing08_data() { countTrailing_data_impl(sizeof(quint8 )); } + void countTrailing16_data() { countTrailing_data_impl(sizeof(quint16)); } + void countTrailing32_data() { countTrailing_data_impl(sizeof(quint32)); } + void countTrailing64_data() { countTrailing_data_impl(sizeof(quint64)); } + void countTrailing08() { countTrailing_impl<quint8 >(); } + void countTrailing16() { countTrailing_impl<quint16>(); } + void countTrailing32() { countTrailing_impl<quint32>(); } + void countTrailing64() { countTrailing_impl<quint64>(); } + + void countLeading08_data() { countLeading_data_impl(sizeof(quint8 )); } + void countLeading16_data() { countLeading_data_impl(sizeof(quint16)); } + void countLeading32_data() { countLeading_data_impl(sizeof(quint32)); } + void countLeading64_data() { countLeading_data_impl(sizeof(quint64)); } + void countLeading08() { countLeading_impl<quint8 >(); } + void countLeading16() { countLeading_impl<quint16>(); } + void countLeading32() { countLeading_impl<quint32>(); } + void countLeading64() { countLeading_impl<quint64>(); } + private: #if Q_TEST_PERFORMANCE void performance(); @@ -87,6 +106,14 @@ private: void popCount_data_impl(size_t sizeof_T_Int); template <typename T_Int> void popCount_impl(); + + void countTrailing_data_impl(size_t sizeof_T_Int); + template <typename T_Int> + void countTrailing_impl(); + + void countLeading_data_impl(size_t sizeof_T_Int); + template <typename T_Int> + void countLeading_impl(); }; class TestInt @@ -1084,6 +1111,86 @@ void tst_QAlgorithms::popCount_impl() QCOMPARE(qPopulationCount(value), expected); } +void tst_QAlgorithms::countTrailing_data_impl(size_t sizeof_T_Int) +{ + using namespace QTest; + addColumn<quint64>("input"); + addColumn<uint>("expected"); + + int nibs = sizeof_T_Int*2; + + newRow(("0x"+QByteArray::number(0,16).rightJustified(nibs,'0')).constData()) << Q_UINT64_C(0) << uint(sizeof_T_Int*8); + for (uint i = 0; i < sizeof_T_Int*8; ++i) { + const quint64 input = Q_UINT64_C(1) << i; + newRow(("0x"+QByteArray::number(input,16).rightJustified(nibs,'0')).constData()) << input << i; + } + + quint64 type_mask; + if (sizeof_T_Int>=8) + type_mask = ~Q_UINT64_C(0); + else + type_mask = (Q_UINT64_C(1) << (sizeof_T_Int*8))-1; + + // and some random ones: + for (uint i = 0; i < sizeof_T_Int*8; ++i) { + for (uint j = 0; j < sizeof_T_Int*3; ++j) { // 3 is arbitrary + const quint64 r = quint64(qrand()) << 32 | quint32(qrand()); + const quint64 b = Q_UINT64_C(1) << i; + const quint64 mask = ((~(b-1)) ^ b) & type_mask; + const quint64 input = (r&mask) | b; + newRow(("0x"+QByteArray::number(input,16).rightJustified(nibs,'0')).constData()) << input << i; + } + } +} + +template <typename T_Int> +void tst_QAlgorithms::countTrailing_impl() +{ + QFETCH(quint64, input); + QFETCH(uint, expected); + + const T_Int value = static_cast<T_Int>(input); + + QCOMPARE(qCountTrailingZeroBits(value), expected); +} + +void tst_QAlgorithms::countLeading_data_impl(size_t sizeof_T_Int) +{ + using namespace QTest; + addColumn<quint64>("input"); + addColumn<uint>("expected"); + + int nibs = sizeof_T_Int*2; + + newRow(("0x"+QByteArray::number(0,16).rightJustified(nibs,'0')).constData()) << Q_UINT64_C(0) << uint(sizeof_T_Int*8); + for (uint i = 0; i < sizeof_T_Int*8; ++i) { + const quint64 input = Q_UINT64_C(1) << i; + newRow(("0x"+QByteArray::number(input,16).rightJustified(nibs,'0')).constData()) << input << uint(sizeof_T_Int*8-i-1); + } + + // and some random ones: + for (uint i = 0; i < sizeof_T_Int*8; ++i) { + for (uint j = 0; j < sizeof_T_Int*3; ++j) { // 3 is arbitrary + const quint64 r = quint64(qrand()) << 32 | quint32(qrand()); + const quint64 b = Q_UINT64_C(1) << i; + const quint64 mask = b-1; + const quint64 input = (r&mask) | b; + newRow(("0x"+QByteArray::number(input,16).rightJustified(nibs,'0')).constData()) << input << uint(sizeof_T_Int*8-i-1); + } + } +} + +template <typename T_Int> +void tst_QAlgorithms::countLeading_impl() +{ + QFETCH(quint64, input); + QFETCH(uint, expected); + + const T_Int value = static_cast<T_Int>(input); + + QCOMPARE(qCountLeadingZeroBits(value), expected); +} + QTEST_APPLESS_MAIN(tst_QAlgorithms) #include "tst_qalgorithms.moc" diff --git a/tests/auto/corelib/tools/qcommandlineparser/testhelper/qcommandlineparser_test_helper.cpp b/tests/auto/corelib/tools/qcommandlineparser/testhelper/qcommandlineparser_test_helper.cpp index 7b1b7ce963..6e09ebb09b 100644 --- a/tests/auto/corelib/tools/qcommandlineparser/testhelper/qcommandlineparser_test_helper.cpp +++ b/tests/auto/corelib/tools/qcommandlineparser/testhelper/qcommandlineparser_test_helper.cpp @@ -73,6 +73,12 @@ int main(int argc, char *argv[]) "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz")); parser.addOption(newlineOption); + // A hidden option + QCommandLineOption hiddenOption(QStringList() << QStringLiteral("hidden")); + hiddenOption.setDescription(QStringLiteral("THIS SHOULD NEVER APPEAR")); + hiddenOption.setHidden(true); + parser.addOption(hiddenOption); + // This program supports different options depending on the "command" (first argument). // Call parse() to find out the positional arguments. parser.parse(QCoreApplication::arguments()); diff --git a/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp b/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp index 6ff46ed20b..9c4ded69de 100644 --- a/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp +++ b/tests/auto/corelib/tools/qcommandlineparser/tst_qcommandlineparser.cpp @@ -35,6 +35,7 @@ #include <QtCore/QCommandLineParser> Q_DECLARE_METATYPE(char**) +Q_DECLARE_METATYPE(QCommandLineParser::OptionsAfterPositionalArgumentsMode) class tst_QCommandLineParser : public QObject { @@ -51,6 +52,8 @@ private slots: void testPositionalArguments(); void testBooleanOption_data(); void testBooleanOption(); + void testOptionsAndPositional_data(); + void testOptionsAndPositional(); void testMultipleNames_data(); void testMultipleNames(); void testSingleValueOption_data(); @@ -141,6 +144,40 @@ void tst_QCommandLineParser::testBooleanOption() QVERIFY(!parser.isSet("c")); } +void tst_QCommandLineParser::testOptionsAndPositional_data() +{ + QTest::addColumn<QStringList>("args"); + QTest::addColumn<QStringList>("expectedOptionNames"); + QTest::addColumn<bool>("expectedIsSet"); + QTest::addColumn<QStringList>("expectedPositionalArguments"); + QTest::addColumn<QCommandLineParser::OptionsAfterPositionalArgumentsMode>("parsingMode"); + + const QStringList arg = QStringList() << "arg"; + QTest::newRow("before_positional_default") << (QStringList() << "tst_qcommandlineparser" << "-b" << "arg") << (QStringList() << "b") << true << arg << QCommandLineParser::ParseAsOptions; + QTest::newRow("after_positional_default") << (QStringList() << "tst_qcommandlineparser" << "arg" << "-b") << (QStringList() << "b") << true << arg << QCommandLineParser::ParseAsOptions; + QTest::newRow("before_positional_parseAsArg") << (QStringList() << "tst_qcommandlineparser" << "-b" << "arg") << (QStringList() << "b") << true << arg << QCommandLineParser::ParseAsPositionalArguments; + QTest::newRow("after_positional_parseAsArg") << (QStringList() << "tst_qcommandlineparser" << "arg" << "-b") << (QStringList()) << false << (QStringList() << "arg" << "-b") << QCommandLineParser::ParseAsPositionalArguments; +} + +void tst_QCommandLineParser::testOptionsAndPositional() +{ + QFETCH(QStringList, args); + QFETCH(QStringList, expectedOptionNames); + QFETCH(bool, expectedIsSet); + QFETCH(QStringList, expectedPositionalArguments); + QFETCH(QCommandLineParser::OptionsAfterPositionalArgumentsMode, parsingMode); + + QCoreApplication app(empty_argc, empty_argv); + QCommandLineParser parser; + parser.setOptionsAfterPositionalArgumentsMode(parsingMode); + QVERIFY(parser.addOption(QCommandLineOption(QStringLiteral("b"), QStringLiteral("a boolean option")))); + QVERIFY(parser.parse(args)); + QCOMPARE(parser.optionNames(), expectedOptionNames); + QCOMPARE(parser.isSet("b"), expectedIsSet); + QCOMPARE(parser.values("b"), QStringList()); + QCOMPARE(parser.positionalArguments(), expectedPositionalArguments); +} + void tst_QCommandLineParser::testMultipleNames_data() { QTest::addColumn<QStringList>("args"); diff --git a/tests/auto/corelib/tools/qlist/tst_qlist.cpp b/tests/auto/corelib/tools/qlist/tst_qlist.cpp index 1207986dde..dd9371fbe1 100644 --- a/tests/auto/corelib/tools/qlist/tst_qlist.cpp +++ b/tests/auto/corelib/tools/qlist/tst_qlist.cpp @@ -76,6 +76,13 @@ struct Movable { return i == other.i; } + bool operator<(const Movable &other) const + { + check(state, Constructed); + check(other.state, Constructed); + return i < other.i; + } + Movable &operator=(const Movable &other) { check(state, Constructed); @@ -144,6 +151,13 @@ struct Optimal return i == other.i; } + bool operator<(const Optimal &other) const + { + check(state, Constructed); + check(other.state, Constructed); + return i < other.i; + } + Optimal &operator=(const Optimal &other) { check(state, Constructed); @@ -220,6 +234,12 @@ struct Complex return value == other.value; } + bool operator<(Complex const &other) const + { + check(); other.check(); + return value < other.value; + } + void check() const { QVERIFY(this == checkSum); @@ -329,6 +349,9 @@ private slots: void replaceOptimal() const; void replaceMovable() const; void replaceComplex() const; + void reverseIteratorsOptimal() const; + void reverseIteratorsMovable() const; + void reverseIteratorsComplex() const; void startsWithOptimal() const; void startsWithMovable() const; void startsWithComplex() const; @@ -376,6 +399,9 @@ private slots: void eraseValidIteratorsOnSharedList() const; void insertWithValidIteratorsOnSharedList() const; + void qhashOptimal() const { qhash<Optimal>(); } + void qhashMovable() const { qhash<Movable>(); } + void qhashComplex() const { qhash<Complex>(); } void reserve() const; private: template<typename T> void length() const; @@ -392,10 +418,12 @@ private: template<typename T> void endsWith() const; template<typename T> void lastIndexOf() const; template<typename T> void move() const; + template<typename T> void qhash() const; template<typename T> void removeAll() const; template<typename T> void removeAt() const; template<typename T> void removeOne() const; template<typename T> void replace() const; + template<typename T> void reverseIterators() const; template<typename T> void startsWith() const; template<typename T> void swap() const; template<typename T> void takeAt() const; @@ -1220,6 +1248,43 @@ void tst_QList::replaceComplex() const } template<typename T> +void tst_QList::reverseIterators() const +{ + QList<T> v; + v << T_CAT << T_DOG << T_BLAH << T_BAZ; + QList<T> vr = v; + std::reverse(vr.begin(), vr.end()); + const QList<T> &cvr = vr; + QVERIFY(std::equal(v.begin(), v.end(), vr.rbegin())); + QVERIFY(std::equal(v.begin(), v.end(), vr.crbegin())); + QVERIFY(std::equal(v.begin(), v.end(), cvr.rbegin())); + QVERIFY(std::equal(vr.rbegin(), vr.rend(), v.begin())); + QVERIFY(std::equal(vr.crbegin(), vr.crend(), v.begin())); + QVERIFY(std::equal(cvr.rbegin(), cvr.rend(), v.begin())); +} + +void tst_QList::reverseIteratorsOptimal() const +{ + const int liveCount = Optimal::getLiveCount(); + reverseIterators<Optimal>(); + QCOMPARE(liveCount, Optimal::getLiveCount()); +} + +void tst_QList::reverseIteratorsMovable() const +{ + const int liveCount = Movable::getLiveCount(); + reverseIterators<Movable>(); + QCOMPARE(liveCount, Movable::getLiveCount()); +} + +void tst_QList::reverseIteratorsComplex() const +{ + const int liveCount = Complex::getLiveCount(); + reverseIterators<Complex>(); + QCOMPARE(liveCount, Complex::getLiveCount()); +} + +template<typename T> void tst_QList::startsWith() const { QList<T> list; @@ -1576,6 +1641,19 @@ void tst_QList::testOperators() const // [] QCOMPARE(list[0], T_FOO); QCOMPARE(list[list.size() - 1], T_CAT); + + // <, >, <=, >= + QVERIFY(!(list < listtwo)); + QVERIFY(!(list > listtwo)); + QVERIFY( list <= listtwo); + QVERIFY( list >= listtwo); + listtwo.push_back(T_CAT); + QVERIFY( list < listtwo); + QVERIFY(!(list > listtwo)); + QVERIFY( list <= listtwo); + QVERIFY(!(list >= listtwo)); + QVERIFY(listtwo > list); + QVERIFY(listtwo >= list); } void tst_QList::testOperatorsOptimal() const @@ -1834,6 +1912,16 @@ void tst_QList::insertWithValidIteratorsOnSharedList() const QCOMPARE(a.at(1), 15); } +template <typename T> +void tst_QList::qhash() const +{ + QList<T> l1, l2; + QCOMPARE(qHash(l1), qHash(l2)); + l1 << T_BAR; + l2 << T_BAR; + QCOMPARE(qHash(l1), qHash(l2)); +} + void tst_QList::reserve() const { // Note: diff --git a/tests/auto/corelib/tools/qringbuffer/tst_qringbuffer.cpp b/tests/auto/corelib/tools/qringbuffer/tst_qringbuffer.cpp index 8c29064457..044fdbdee5 100644 --- a/tests/auto/corelib/tools/qringbuffer/tst_qringbuffer.cpp +++ b/tests/auto/corelib/tools/qringbuffer/tst_qringbuffer.cpp @@ -52,6 +52,7 @@ private slots: void ungetChar(); void indexOf(); void appendAndRead(); + void peek(); void readLine(); }; @@ -60,7 +61,7 @@ void tst_QRingBuffer::sizeWhenReserved() QRingBuffer ringBuffer; ringBuffer.reserve(5); - QCOMPARE(ringBuffer.size(), 5); + QCOMPARE(ringBuffer.size(), Q_INT64_C(5)); } void tst_QRingBuffer::sizeWhenReservedAndChopped() @@ -69,14 +70,14 @@ void tst_QRingBuffer::sizeWhenReservedAndChopped() ringBuffer.reserve(31337); ringBuffer.chop(31337); - QCOMPARE(ringBuffer.size(), 0); + QCOMPARE(ringBuffer.size(), Q_INT64_C(0)); } void tst_QRingBuffer::sizeWhenEmpty() { QRingBuffer ringBuffer; - QCOMPARE(ringBuffer.size(), 0); + QCOMPARE(ringBuffer.size(), Q_INT64_C(0)); } void tst_QRingBuffer::readPointerAtPositionReadTooMuch() @@ -101,22 +102,22 @@ void tst_QRingBuffer::readPointerAtPositionWithHead() qint64 length; const char* buf2 = ringBuffer.readPointerAtPosition(0, length); - QCOMPARE(length, qint64(2)); + QCOMPARE(length, Q_INT64_C(2)); QVERIFY(*buf2 == '2'); QVERIFY(*(buf2+1) == '3'); // advance 2 more, ringBuffer should be empty then ringBuffer.free(2); buf2 = ringBuffer.readPointerAtPosition(0, length); - QCOMPARE(length, qint64(0)); + QCOMPARE(length, Q_INT64_C(0)); QVERIFY(buf2 == 0); // check buffer with 2 blocks memcpy(ringBuffer.reserve(4), "0123", 4); ringBuffer.append(QByteArray("45678", 5)); ringBuffer.free(3); - buf2 = ringBuffer.readPointerAtPosition(1, length); - QCOMPARE(length, qint64(5)); + buf2 = ringBuffer.readPointerAtPosition(Q_INT64_C(1), length); + QCOMPARE(length, Q_INT64_C(5)); } void tst_QRingBuffer::readPointerAtPositionEmptyRead() @@ -149,14 +150,14 @@ void tst_QRingBuffer::readPointerAtPositionWriteRead() // write in chunks of 50 bytes // this ensures there will be multiple QByteArrays inside the QRingBuffer // since QRingBuffer is then only using individual arrays of around 4000 bytes - qint64 thisWrite = qMin(remaining, qint64(50)); + qint64 thisWrite = qMin(remaining, Q_INT64_C(50)); char *pos = ringBuffer.reserve(thisWrite); inData.read(pos, thisWrite); remaining -= thisWrite; } // was data put into it? QVERIFY(ringBuffer.size() > 0); - QCOMPARE(qint64(ringBuffer.size()), inData.size()); + QCOMPARE(ringBuffer.size(), inData.size()); //read from the QRingBuffer in loop, put back into another QBuffer QBuffer outData; @@ -187,12 +188,12 @@ void tst_QRingBuffer::free() ringBuffer.append(QByteArray("01234", 5)); ringBuffer.free(1); - QCOMPARE(ringBuffer.size(), 4095 + 2048 + 5); + QCOMPARE(ringBuffer.size(), Q_INT64_C(4095 + 2048 + 5)); ringBuffer.free(4096); - QCOMPARE(ringBuffer.size(), 2047 + 5); + QCOMPARE(ringBuffer.size(), Q_INT64_C(2047 + 5)); ringBuffer.free(48); ringBuffer.free(2000); - QCOMPARE(ringBuffer.size(), 4); + QCOMPARE(ringBuffer.size(), Q_INT64_C(4)); QVERIFY(memcmp(ringBuffer.readPointer(), "1234", 4) == 0); } @@ -211,8 +212,8 @@ void tst_QRingBuffer::reserveAndRead() for (int i = 1; i < 256; ++i) { QByteArray ba; ba.resize(i); - int thisRead = ringBuffer.read(ba.data(), i); - QCOMPARE(thisRead, i); + qint64 thisRead = ringBuffer.read(ba.data(), i); + QCOMPARE(thisRead, qint64(i)); QVERIFY(ba.count(char(i)) == i); } QVERIFY(ringBuffer.size() == 0); @@ -227,12 +228,12 @@ void tst_QRingBuffer::chop() ringBuffer.reserve(4096); ringBuffer.chop(1); - QCOMPARE(ringBuffer.size(), 5 + 2048 + 4095); + QCOMPARE(ringBuffer.size(), Q_INT64_C(5 + 2048 + 4095)); ringBuffer.chop(4096); - QCOMPARE(ringBuffer.size(), 5 + 2047); + QCOMPARE(ringBuffer.size(), Q_INT64_C(5 + 2047)); ringBuffer.chop(48); ringBuffer.chop(2000); - QCOMPARE(ringBuffer.size(), 4); + QCOMPARE(ringBuffer.size(), Q_INT64_C(4)); QVERIFY(memcmp(ringBuffer.readPointer(), "0123", 4) == 0); } @@ -248,7 +249,7 @@ void tst_QRingBuffer::ungetChar() ringBuffer.getChar(); ringBuffer.ungetChar(char(c)); // unget first char } - QCOMPARE(ringBuffer.size(), 1); + QCOMPARE(ringBuffer.size(), Q_INT64_C(1)); } void tst_QRingBuffer::indexOf() @@ -258,8 +259,8 @@ void tst_QRingBuffer::indexOf() ringBuffer.putChar(char(i)); for (int i = 1; i < 256; ++i) { - int index = ringBuffer.indexOf(char(i)); - QCOMPARE(i - 1, index); + qint64 index = ringBuffer.indexOf(char(i)); + QCOMPARE(qint64(i - 1), index); QCOMPARE(index, ringBuffer.indexOf(char(i), i)); QVERIFY(ringBuffer.indexOf(char(i), i - 1) == -1); // test for absent char } @@ -280,6 +281,31 @@ void tst_QRingBuffer::appendAndRead() QVERIFY(ringBuffer.read() == ba3); } +void tst_QRingBuffer::peek() +{ + QRingBuffer ringBuffer; + QByteArray testBuffer; + // fill buffer with an arithmetic progression + for (int i = 1; i < 256; ++i) { + char *ringPos = ringBuffer.reserve(i); + QVERIFY(ringPos); + memset(ringPos, i, i); + testBuffer.append(ringPos, i); + } + + // check stored data + QByteArray resultBuffer; + int peekPosition = testBuffer.size(); + for (int i = 1; i < 256; ++i) { + QByteArray ba(i, 0); + peekPosition -= i; + qint64 thisPeek = ringBuffer.peek(ba.data(), i, peekPosition); + QCOMPARE(thisPeek, qint64(i)); + resultBuffer.prepend(ba); + } + QCOMPARE(testBuffer, resultBuffer); +} + void tst_QRingBuffer::readLine() { QRingBuffer ringBuffer; @@ -298,7 +324,7 @@ void tst_QRingBuffer::readLine() // check first empty string reading stringBuf[0] = char(0xFF); - QCOMPARE(ringBuffer.readLine(stringBuf, int(sizeof(stringBuf)) - 2), ba2.size()); + QCOMPARE(ringBuffer.readLine(stringBuf, int(sizeof(stringBuf)) - 2), qint64(ba2.size())); QVERIFY(stringBuf[0] == ba2[0]); QVERIFY(ringBuffer.readLine(stringBuf, int(sizeof(stringBuf)) - 2) == (ba3.size() + ba4.size() diff --git a/tests/auto/corelib/tools/qstring/tst_qstring.cpp b/tests/auto/corelib/tools/qstring/tst_qstring.cpp index d2f7a6ee50..d4b3ba7f15 100644 --- a/tests/auto/corelib/tools/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/tools/qstring/tst_qstring.cpp @@ -60,11 +60,241 @@ #include <qhash.h> #include <string> +#include <algorithm> #define CREATE_REF(string) \ const QString padded = QString::fromLatin1(" %1 ").arg(string); \ QStringRef ref = padded.midRef(1, padded.size() - 2); +namespace { + +// this wraps an argument to a QString function, as well as how to apply +// the argument to a given QString member function. +template <typename T> +class Arg; + +template <typename T> +class Reversed {}; // marker for Arg<QChar> to apply the operation in reverse order (for prepend()) + +class ArgBase +{ +protected: + QString pinned; + explicit ArgBase(const char *str) + : pinned(QString::fromLatin1(str)) {} +}; + +template <> +class Arg<QChar> : protected ArgBase +{ +public: + explicit Arg(const char *str) : ArgBase(str) {} + + template <typename MemFun> + void apply0(QString &s, MemFun mf) const + { Q_FOREACH (QChar ch, this->pinned) (s.*mf)(ch); } + + template <typename MemFun, typename A1> + void apply1(QString &s, MemFun mf, A1 a1) const + { Q_FOREACH (QChar ch, this->pinned) (s.*mf)(a1, ch); } +}; + +template <> +class Arg<Reversed<QChar> > : private Arg<QChar> +{ +public: + explicit Arg(const char *str) : Arg<QChar>(str) + { + std::reverse(this->pinned.begin(), this->pinned.end()); + } + + using Arg<QChar>::apply0; + using Arg<QChar>::apply1; +}; + +template <> +class Arg<QString> : ArgBase +{ +public: + explicit Arg(const char *str) : ArgBase(str) {} + + template <typename MemFun> + void apply0(QString &s, MemFun mf) const + { (s.*mf)(this->pinned); } + + template <typename MemFun, typename A1> + void apply1(QString &s, MemFun mf, A1 a1) const + { (s.*mf)(a1, this->pinned); } +}; + +template <> +class Arg<QStringRef> : ArgBase +{ + QStringRef ref() const + { return this->pinned.isNull() ? QStringRef() : this->pinned.midRef(0) ; } +public: + explicit Arg(const char *str) : ArgBase(str) {} + + template <typename MemFun> + void apply0(QString &s, MemFun mf) const + { (s.*mf)(ref()); } + + template <typename MemFun, typename A1> + void apply1(QString &s, MemFun mf, A1 a1) const + { (s.*mf)(a1, ref()); } +}; + +template <> +class Arg<QPair<const QChar *, int> > : ArgBase +{ +public: + explicit Arg(const char *str) : ArgBase(str) {} + + template <typename MemFun> + void apply0(QString &s, MemFun mf) const + { (s.*mf)(this->pinned.constData(), this->pinned.length()); } + + template <typename MemFun, typename A1> + void apply1(QString &s, MemFun mf, A1 a1) const + { (s.*mf)(a1, this->pinned.constData(), this->pinned.length()); } +}; + +template <> +class Arg<QLatin1String> +{ + QLatin1String l1; +public: + explicit Arg(const char *str) : l1(str) {} + + template <typename MemFun> + void apply0(QString &s, MemFun mf) const + { (s.*mf)(l1); } + + template <typename MemFun, typename A1> + void apply1(QString &s, MemFun mf, A1 a1) const + { (s.*mf)(a1, l1); } +}; + +template <> +class Arg<char> +{ +protected: + const char *str; +public: + explicit Arg(const char *str) : str(str) {} + + template <typename MemFun> + void apply0(QString &s, MemFun mf) const + { + if (str) { + for (const char *it = str; *it; ++it) + (s.*mf)(*it); + } + } + + template <typename MemFun, typename A1> + void apply1(QString &s, MemFun mf, A1 a1) const + { + if (str) { + for (const char *it = str; *it; ++it) + (s.*mf)(a1, *it); + } + } +}; + +template <> +class Arg<Reversed<char> > : private Arg<char> +{ + static const char *dupAndReverse(const char *s) + { + char *s2 = qstrdup(s); + std::reverse(s2, s2 + qstrlen(s2)); + return s2; + } +public: + explicit Arg(const char *str) : Arg<char>(dupAndReverse(str)) {} + ~Arg() { delete[] str; } + + using Arg<char>::apply0; + using Arg<char>::apply1; +}; + +template <> +class Arg<const char*> +{ + const char *str; +public: + explicit Arg(const char *str) : str(str) {} + + template <typename MemFun> + void apply0(QString &s, MemFun mf) const + { (s.*mf)(str); } + + template <typename MemFun, typename A1> + void apply1(QString &s, MemFun mf, A1 a1) const + { (s.*mf)(a1, str); } +}; + +template <> +class Arg<QByteArray> +{ + QByteArray ba; +public: + explicit Arg(const char *str) : ba(str) {} + + template <typename MemFun> + void apply0(QString &s, MemFun mf) const + { (s.*mf)(ba); } + + template <typename MemFun, typename A1> + void apply1(QString &s, MemFun mf, A1 a1) const + { (s.*mf)(a1, ba); } +}; + +// const char* is not allowed as columns in data-driven tests (causes static_assert failure), +// so wrap it in a container (default ctor is a QMetaType/QVariant requirement): +class CharStarContainer +{ + const char *str; +public: + explicit Q_DECL_CONSTEXPR CharStarContainer(const char *s = Q_NULLPTR) : str(s) {} + Q_DECL_CONSTEXPR operator const char *() const { return str; } +}; + +} // unnamed namespace + +Q_DECLARE_METATYPE(CharStarContainer) + +// implementation helpers for append_impl/prepend_impl etc +template <typename ArgType, typename MemFun> +static void do_apply0(MemFun mf) +{ + QFETCH(QString, s); + QFETCH(CharStarContainer, arg); + QFETCH(QString, expected); + + Arg<ArgType>(arg).apply0(s, mf); + + QCOMPARE(s, expected); + QCOMPARE(s.isEmpty(), expected.isEmpty()); + QCOMPARE(s.isNull(), expected.isNull()); +} + +template <typename ArgType, typename A1, typename MemFun> +static void do_apply1(MemFun mf) +{ + QFETCH(QString, s); + QFETCH(CharStarContainer, arg); + QFETCH(A1, a1); + QFETCH(QString, expected); + + Arg<ArgType>(arg).apply1(s, mf, a1); + + QCOMPARE(s, expected); + QCOMPARE(s.isEmpty(), expected.isEmpty()); + QCOMPARE(s.isNull(), expected.isNull()); +} + class tst_QString : public QObject { Q_OBJECT @@ -73,6 +303,27 @@ class tst_QString : public QObject void split_regexp(const QString &string, const QString &pattern, QStringList result); template<typename List> void split(const QString &string, const QString &separator, QStringList result); + + template <typename ArgType, typename MemFun> + void append_impl() const { do_apply0<ArgType>(MemFun(&QString::append)); } + template <typename ArgType> + void append_impl() const { append_impl<ArgType, QString &(QString::*)(const ArgType&)>(); } + void append_data(bool emptyIsNoop = false); + template <typename ArgType, typename MemFun> + void operator_pluseq_impl() const { do_apply0<ArgType>(MemFun(&QString::operator+=)); } + template <typename ArgType> + void operator_pluseq_impl() const { operator_pluseq_impl<ArgType, QString &(QString::*)(const ArgType&)>(); } + void operator_pluseq_data(bool emptyIsNoop = false); + template <typename ArgType, typename MemFun> + void prepend_impl() const { do_apply0<ArgType>(MemFun(&QString::prepend)); } + template <typename ArgType> + void prepend_impl() const { prepend_impl<ArgType, QString &(QString::*)(const ArgType&)>(); } + void prepend_data(bool emptyIsNoop = false); + template <typename ArgType, typename MemFun> + void insert_impl() const { do_apply1<ArgType, int>(MemFun(&QString::insert)); } + template <typename ArgType> + void insert_impl() const { insert_impl<ArgType, QString &(QString::*)(int, const ArgType&)>(); } + void insert_data(bool emptyIsNoop = false); public: tst_QString(); public slots: @@ -121,19 +372,86 @@ private slots: void remove_regexp_data(); void remove_regexp(); void swap(); - void prepend(); - void prepend_bytearray_data(); - void prepend_bytearray(); - void append(); - void append_bytearray_data(); - void append_bytearray(); - void operator_pluseq_bytearray_data(); - void operator_pluseq_bytearray(); + + void prepend_qstring() { prepend_impl<QString>(); } + void prepend_qstring_data() { prepend_data(true); } + void prepend_qstringref() { prepend_impl<QStringRef>(); } + void prepend_qstringref_data() { prepend_data(true); } + void prepend_qlatin1string() { prepend_impl<QLatin1String, QString &(QString::*)(QLatin1String)>(); } + void prepend_qlatin1string_data() { prepend_data(true); } + void prepend_qcharstar_int() { prepend_impl<QPair<const QChar *, int>, QString &(QString::*)(const QChar *, int)>(); } + void prepend_qcharstar_int_data() { prepend_data(true); } + void prepend_qchar() { prepend_impl<Reversed<QChar>, QString &(QString::*)(QChar)>(); } + void prepend_qchar_data() { prepend_data(true); } + void prepend_qbytearray() { prepend_impl<QByteArray>(); } + void prepend_qbytearray_data() { prepend_data(true); } + void prepend_char() { prepend_impl<Reversed<char>, QString &(QString::*)(QChar)>(); } + void prepend_char_data() { prepend_data(true); } + void prepend_charstar() { prepend_impl<const char *, QString &(QString::*)(const char *)>(); } + void prepend_charstar_data() { prepend_data(true); } + void prepend_bytearray_special_cases_data(); + void prepend_bytearray_special_cases(); + + void append_qstring() { append_impl<QString>(); } + void append_qstring_data() { append_data(); } + void append_qstringref() { append_impl<QStringRef>(); } + void append_qstringref_data() { append_data(); } + void append_qlatin1string() { append_impl<QLatin1String, QString &(QString::*)(QLatin1String)>(); } + void append_qlatin1string_data() { append_data(); } + void append_qcharstar_int() { append_impl<QPair<const QChar *, int>, QString&(QString::*)(const QChar *, int)>(); } + void append_qcharstar_int_data() { append_data(true); } + void append_qchar() { append_impl<QChar, QString &(QString::*)(QChar)>(); } + void append_qchar_data() { append_data(true); } + void append_qbytearray() { append_impl<QByteArray>(); } + void append_qbytearray_data() { append_data(); } + void append_char() { append_impl<char, QString &(QString::*)(QChar)>(); } + void append_char_data() { append_data(true); } + void append_charstar() { append_impl<const char *, QString &(QString::*)(const char *)>(); } + void append_charstar_data() { append_data(); } + void append_special_cases(); + void append_bytearray_special_cases_data(); + void append_bytearray_special_cases(); + + void operator_pluseq_qstring() { operator_pluseq_impl<QString>(); } + void operator_pluseq_qstring_data() { operator_pluseq_data(); } + void operator_pluseq_qstringref() { operator_pluseq_impl<QStringRef>(); } + void operator_pluseq_qstringref_data() { operator_pluseq_data(); } + void operator_pluseq_qlatin1string() { operator_pluseq_impl<QLatin1String, QString &(QString::*)(QLatin1String)>(); } + void operator_pluseq_qlatin1string_data() { operator_pluseq_data(); } + void operator_pluseq_qchar() { operator_pluseq_impl<QChar, QString &(QString::*)(QChar)>(); } + void operator_pluseq_qchar_data() { operator_pluseq_data(true); } + void operator_pluseq_qbytearray() { operator_pluseq_impl<QByteArray>(); } + void operator_pluseq_qbytearray_data() { operator_pluseq_data(); } + void operator_pluseq_char() { operator_pluseq_impl<char, QString &(QString::*)(char)>(); } + void operator_pluseq_char_data() { operator_pluseq_data(true); } + void operator_pluseq_charstar() { operator_pluseq_impl<const char *, QString &(QString::*)(const char *)>(); } + void operator_pluseq_charstar_data() { operator_pluseq_data(); } + void operator_pluseq_bytearray_special_cases_data(); + void operator_pluseq_bytearray_special_cases(); + void operator_eqeq_bytearray_data(); void operator_eqeq_bytearray(); void operator_eqeq_nullstring(); void operator_smaller(); - void insert(); + + void insert_qstring() { insert_impl<QString>(); } + void insert_qstring_data() { insert_data(true); } + void insert_qstringref() { insert_impl<QStringRef>(); } + void insert_qstringref_data() { insert_data(true); } + void insert_qlatin1string() { insert_impl<QLatin1String, QString &(QString::*)(int, QLatin1String)>(); } + void insert_qlatin1string_data() { insert_data(true); } + void insert_qcharstar_int() { insert_impl<QPair<const QChar *, int>, QString &(QString::*)(int, const QChar*, int) >(); } + void insert_qcharstar_int_data() { insert_data(true); } + void insert_qchar() { insert_impl<Reversed<QChar>, QString &(QString::*)(int, QChar)>(); } + void insert_qchar_data() { insert_data(true); } + void insert_qbytearray() { insert_impl<QByteArray>(); } + void insert_qbytearray_data() { insert_data(true); } + void insert_char() { insert_impl<Reversed<char>, QString &(QString::*)(int, QChar)>(); } + void insert_char_data() { insert_data(true); } + void insert_charstar() { insert_impl<const char *, QString &(QString::*)(int, const char*) >(); } + void insert_charstar_data() { insert_data(true); } + void insert_special_cases(); + void simplified_data(); void simplified(); void trimmed(); @@ -183,6 +501,7 @@ private slots: void fromLocal8Bit(); void local8Bit_data(); void local8Bit(); + void nullFromLocal8Bit(); void fromLatin1Roundtrip_data(); void fromLatin1Roundtrip(); void toLatin1Roundtrip_data(); @@ -827,7 +1146,7 @@ void tst_QString::constructorQByteArray_data() ba1[5] = 'e'; ba1[6] = 'f'; - QTest::newRow( "2" ) << ba1 << QString("abc"); + QTest::newRow( "2" ) << ba1 << QStringLiteral("abc\0def"); QTest::newRow( "3" ) << QByteArray::fromRawData("abcd", 3) << QString("abc"); QTest::newRow( "4" ) << QByteArray("\xc3\xa9") << QString("\xc3\xa9"); @@ -848,12 +1167,6 @@ void tst_QString::constructorQByteArray() QCOMPARE( strBA, expected ); // test operator= too - if (src.constData()[src.length()] == '\0') { - str1.clear(); - str1 = src.constData(); - QCOMPARE( str1, expected ); - } - strBA.clear(); strBA = src; QCOMPARE( strBA, expected ); @@ -2066,7 +2379,46 @@ void tst_QString::simplified() QCOMPARE(qMove(full).simplified(), simple); } -void tst_QString::insert() +void tst_QString::insert_data(bool emptyIsNoop) +{ + QTest::addColumn<QString>("s"); + QTest::addColumn<CharStarContainer>("arg"); + QTest::addColumn<int>("a1"); + QTest::addColumn<QString>("expected"); + + const CharStarContainer nullC; + const CharStarContainer emptyC(""); + const CharStarContainer aC("a"); + const CharStarContainer bC("b"); + //const CharStarContainer abC("ab"); + const CharStarContainer baC("ba"); + + const QString null; + const QString empty(""); + const QString a("a"); + const QString b("b"); + const QString ab("ab"); + const QString ba("ba"); + + QTest::newRow("null.insert(0, null)") << null << nullC << 0 << null; + QTest::newRow("null.insert(0, empty)") << null << emptyC << 0 << (emptyIsNoop ? null : empty); + QTest::newRow("null.insert(0, a)") << null << aC << 0 << a; + QTest::newRow("empty.insert(0, null)") << empty << nullC << 0 << empty; + QTest::newRow("empty.insert(0, empty)") << empty << emptyC << 0 << empty; + QTest::newRow("empty.insert(0, a)") << empty << aC << 0 << a; + QTest::newRow("a.insert(0, null)") << a << nullC << 0 << a; + QTest::newRow("a.insert(0, empty)") << a << emptyC << 0 << a; + QTest::newRow("a.insert(0, b)") << a << bC << 0 << ba; + QTest::newRow("a.insert(0, ba)") << a << baC << 0 << (ba + a); + QTest::newRow("a.insert(1, null)") << a << nullC << 1 << a; + QTest::newRow("a.insert(1, empty)") << a << emptyC << 1 << a; + QTest::newRow("a.insert(1, b)") << a << bC << 1 << ab; + QTest::newRow("a.insert(1, ba)") << a << baC << 1 << (a + ba); + QTest::newRow("ba.insert(1, a)") << ba << aC << 1 << (ba + a); + QTest::newRow("ba.insert(2, b)") << ba << bC << 2 << (ba + b); +} + +void tst_QString::insert_special_cases() { QString a; @@ -2097,14 +2449,37 @@ void tst_QString::insert() QCOMPARE(a.insert(0, QLatin1String("a")), QString("aMontreal")); } -void tst_QString::append() +void tst_QString::append_data(bool emptyIsNoop) { - { - QString a; - a = "<>ABCABCABCABC"; - QCOMPARE(a.append(">"),QString("<>ABCABCABCABC>")); - } + QTest::addColumn<QString>("s"); + QTest::addColumn<CharStarContainer>("arg"); + QTest::addColumn<QString>("expected"); + + const CharStarContainer nullC; + const CharStarContainer emptyC(""); + const CharStarContainer aC("a"); + const CharStarContainer bC("b"); + //const CharStarContainer abC("ab"); + const QString null; + const QString empty(""); + const QString a("a"); + //const QString b("b"); + const QString ab("ab"); + + QTest::newRow("null + null") << null << nullC << null; + QTest::newRow("null + empty") << null << emptyC << (emptyIsNoop ? null : empty); + QTest::newRow("null + a") << null << aC << a; + QTest::newRow("empty + null") << empty << nullC << empty; + QTest::newRow("empty + empty") << empty << emptyC << empty; + QTest::newRow("empty + a") << empty << aC << a; + QTest::newRow("a + null") << a << nullC << a; + QTest::newRow("a + empty") << a << emptyC << a; + QTest::newRow("a + b") << a << bC << ab; +} + +void tst_QString::append_special_cases() +{ { QString a; static const QChar unicode[] = { 'H', 'e', 'l', 'l', 'o', ',', ' ', 'W', 'o', 'r', 'l', 'd', '!' }; @@ -2124,7 +2499,7 @@ void tst_QString::append() } } -void tst_QString::append_bytearray_data() +void tst_QString::append_bytearray_special_cases_data() { QTest::addColumn<QString>("str" ); QTest::addColumn<QByteArray>("ba" ); @@ -2158,7 +2533,7 @@ void tst_QString::append_bytearray_data() QTest::newRow( "nonAsciiByteArray2") << QString() << QByteArray("\xc3\xa9") << QString::fromUtf8("\xc3\xa9"); } -void tst_QString::append_bytearray() +void tst_QString::append_bytearray_special_cases() { { QFETCH( QString, str ); @@ -2176,22 +2551,19 @@ void tst_QString::append_bytearray() QTEST( str, "res" ); } +} - QFETCH( QByteArray, ba ); - if (ba.constData()[ba.length()] == '\0') { - QFETCH( QString, str ); - - str.append(ba.constData()); - QTEST( str, "res" ); - } +void tst_QString::operator_pluseq_data(bool emptyIsNoop) +{ + append_data(emptyIsNoop); } -void tst_QString::operator_pluseq_bytearray_data() +void tst_QString::operator_pluseq_bytearray_special_cases_data() { - append_bytearray_data(); + append_bytearray_special_cases_data(); } -void tst_QString::operator_pluseq_bytearray() +void tst_QString::operator_pluseq_bytearray_special_cases() { { QFETCH( QString, str ); @@ -2209,14 +2581,6 @@ void tst_QString::operator_pluseq_bytearray() QTEST( str, "res" ); } - - QFETCH( QByteArray, ba ); - if (ba.constData()[ba.length()] == '\0') { - QFETCH( QString, str ); - - str += ba.constData(); - QTEST( str, "res" ); - } } void tst_QString::operator_eqeq_bytearray_data() @@ -2231,11 +2595,6 @@ void tst_QString::operator_eqeq_bytearray() QVERIFY(expected == src); QVERIFY(!(expected != src)); - - if (src.constData()[src.length()] == '\0') { - QVERIFY(expected == src.constData()); - QVERIFY(!(expected != src.constData())); - } } void tst_QString::swap() @@ -2248,14 +2607,37 @@ void tst_QString::swap() QCOMPARE(s2,QLatin1String("s1")); } -void tst_QString::prepend() +void tst_QString::prepend_data(bool emptyIsNoop) { - QString a; - a = "<>ABCABCABCABC>"; - QCOMPARE(a.prepend("-"),(QString)"-<>ABCABCABCABC>"); + QTest::addColumn<QString>("s"); + QTest::addColumn<CharStarContainer>("arg"); + QTest::addColumn<QString>("expected"); + + const CharStarContainer nullC; + const CharStarContainer emptyC(""); + const CharStarContainer aC("a"); + const CharStarContainer bC("b"); + const CharStarContainer baC("ba"); + + const QString null; + const QString empty(""); + const QString a("a"); + //const QString b("b"); + const QString ba("ba"); + + QTest::newRow("null.prepend(null)") << null << nullC << null; + QTest::newRow("null.prepend(empty)") << null << emptyC << (emptyIsNoop ? null : empty); + QTest::newRow("null.prepend(a)") << null << aC << a; + QTest::newRow("empty.prepend(null)") << empty << nullC << empty; + QTest::newRow("empty.prepend(empty)") << empty << emptyC << empty; + QTest::newRow("empty.prepend(a)") << empty << aC << a; + QTest::newRow("a.prepend(null)") << a << nullC << a; + QTest::newRow("a.prepend(empty)") << a << emptyC << a; + QTest::newRow("a.prepend(b)") << a << bC << ba; + QTest::newRow("a.prepend(ba)") << a << baC << (ba + a); } -void tst_QString::prepend_bytearray_data() +void tst_QString::prepend_bytearray_special_cases_data() { QTest::addColumn<QString>("str" ); QTest::addColumn<QByteArray>("ba" ); @@ -2270,7 +2652,7 @@ void tst_QString::prepend_bytearray_data() // byte array with only a 0 ba.resize( 1 ); ba[0] = 0; - QTest::newRow( "emptyString" ) << QString("foobar ") << ba << QString("foobar "); + QTest::newRow( "emptyString" ) << QString("foobar ") << ba << QStringLiteral("\0foobar "); // empty byte array ba.resize( 0 ); @@ -2281,7 +2663,7 @@ void tst_QString::prepend_bytearray_data() QTest::newRow( "nonAsciiByteArray2") << QString() << QByteArray("\xc3\xa9") << QString::fromUtf8("\xc3\xa9"); } -void tst_QString::prepend_bytearray() +void tst_QString::prepend_bytearray_special_cases() { { QFETCH( QString, str ); @@ -2300,14 +2682,6 @@ void tst_QString::prepend_bytearray() QTEST( str, "res" ); } - - QFETCH( QByteArray, ba ); - if (ba.constData()[ba.length()] == '\0') { - QFETCH( QString, str ); - - str.prepend(ba.constData()); - QTEST( str, "res" ); - } } void tst_QString::replace_uint_uint() @@ -3697,6 +4071,12 @@ void tst_QString::nullFromUtf8() a = QString::fromUtf8(""); QVERIFY(!a.isNull()); QVERIFY(a.isEmpty()); + a = QString::fromUtf8(QByteArray()); + QVERIFY(a.isNull()); + QVERIFY(a.isEmpty()); + a = QString::fromUtf8(QByteArray("")); + QVERIFY(!a.isNull()); + QVERIFY(a.isEmpty()); } void tst_QString::fromLocal8Bit_data() @@ -3779,6 +4159,23 @@ void tst_QString::local8Bit() QCOMPARE(local8Bit.toLocal8Bit(), QByteArray(result)); } +void tst_QString::nullFromLocal8Bit() +{ + QString a; + a = QString::fromLocal8Bit(0); + QVERIFY(a.isNull()); + QVERIFY(a.isEmpty()); + a = QString::fromLocal8Bit(""); + QVERIFY(!a.isNull()); + QVERIFY(a.isEmpty()); + a = QString::fromLocal8Bit(QByteArray()); + QVERIFY(a.isNull()); + QVERIFY(a.isEmpty()); + a = QString::fromLocal8Bit(QByteArray("")); + QVERIFY(!a.isNull()); + QVERIFY(a.isEmpty()); +} + void tst_QString::stringRef_local8Bit_data() { local8Bit_data(); @@ -3945,6 +4342,12 @@ void tst_QString::fromLatin1() a = QString::fromLatin1( "" ); QVERIFY( !a.isNull() ); QVERIFY( a.isEmpty() ); + a = QString::fromLatin1(QByteArray()); + QVERIFY(a.isNull()); + QVERIFY(a.isEmpty()); + a = QString::fromLatin1(QByteArray("")); + QVERIFY(!a.isNull()); + QVERIFY(a.isEmpty()); a = QString::fromLatin1(0, 0); QVERIFY(a.isNull()); diff --git a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp index 5d12cd9804..719daad3b6 100644 --- a/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp +++ b/tests/auto/corelib/tools/qvarlengtharray/tst_qvarlengtharray.cpp @@ -47,10 +47,12 @@ private slots: void appendCausingRealloc(); void resize(); void realloc(); + void reverseIterators(); void count(); void first(); void last(); void squeeze(); + void operators(); void indexOf(); void lastIndexOf(); void contains(); @@ -562,6 +564,21 @@ void tst_QVarLengthArray::realloc() QVERIFY(reallocTestProceed); } +void tst_QVarLengthArray::reverseIterators() +{ + QVarLengthArray<int> v; + v << 1 << 2 << 3 << 4; + QVarLengthArray<int> vr = v; + std::reverse(vr.begin(), vr.end()); + const QVarLengthArray<int> &cvr = vr; + QVERIFY(std::equal(v.begin(), v.end(), vr.rbegin())); + QVERIFY(std::equal(v.begin(), v.end(), vr.crbegin())); + QVERIFY(std::equal(v.begin(), v.end(), cvr.rbegin())); + QVERIFY(std::equal(vr.rbegin(), vr.rend(), v.begin())); + QVERIFY(std::equal(vr.crbegin(), vr.crend(), v.begin())); + QVERIFY(std::equal(cvr.rbegin(), cvr.rend(), v.begin())); +} + void tst_QVarLengthArray::count() { // tests size(), count() and length(), since they're the same thing @@ -690,6 +707,49 @@ void tst_QVarLengthArray::squeeze() QCOMPARE(list.capacity(), sizeOnHeap); } +void tst_QVarLengthArray::operators() +{ + QVarLengthArray<QString> myvla; + myvla << "A" << "B" << "C"; + QVarLengthArray<QString> myvlatwo; + myvlatwo << "D" << "E" << "F"; + QVarLengthArray<QString> combined; + combined << "A" << "B" << "C" << "D" << "E" << "F"; + + // != + QVERIFY(myvla != myvlatwo); + + // +=: not provided, emulate + //myvla += myvlatwo; + Q_FOREACH (const QString &s, myvlatwo) + myvla.push_back(s); + QCOMPARE(myvla, combined); + + // == + QVERIFY(myvla == combined); + + // <, >, <=, >= + QVERIFY(!(myvla < combined)); + QVERIFY(!(myvla > combined)); + QVERIFY( myvla <= combined); + QVERIFY( myvla >= combined); + combined.push_back("G"); + QVERIFY( myvla < combined); + QVERIFY(!(myvla > combined)); + QVERIFY( myvla <= combined); + QVERIFY(!(myvla >= combined)); + QVERIFY(combined > myvla); + QVERIFY(combined >= myvla); + + // [] + QCOMPARE(myvla[0], QLatin1String("A")); + QCOMPARE(myvla[1], QLatin1String("B")); + QCOMPARE(myvla[2], QLatin1String("C")); + QCOMPARE(myvla[3], QLatin1String("D")); + QCOMPARE(myvla[4], QLatin1String("E")); + QCOMPARE(myvla[5], QLatin1String("F")); +} + void tst_QVarLengthArray::indexOf() { QVarLengthArray<QString> myvec; diff --git a/tests/auto/corelib/tools/qvector/tst_qvector.cpp b/tests/auto/corelib/tools/qvector/tst_qvector.cpp index 69da6e450e..3f95fa18f6 100644 --- a/tests/auto/corelib/tools/qvector/tst_qvector.cpp +++ b/tests/auto/corelib/tools/qvector/tst_qvector.cpp @@ -86,6 +86,8 @@ private: } }; +inline uint qHash(const Movable &key, uint seed = 0) { return qHash(key.i, seed); } + QAtomicInt Movable::counter = 0; QT_BEGIN_NAMESPACE Q_DECLARE_TYPEINFO(Movable, Q_MOVABLE_TYPE); @@ -123,6 +125,13 @@ struct Custom { return i == other.i; } + bool operator<(const Custom &other) const + { + check(&other); + check(this); + return i < other.i; + } + Custom &operator=(const Custom &other) { check(&other); @@ -148,6 +157,8 @@ private: }; QAtomicInt Custom::counter = 0; +inline uint qHash(const Custom &key, uint seed = 0) { return qHash(key.i, seed); } + Q_DECLARE_METATYPE(Custom); // tests depends on the fact that: @@ -230,6 +241,9 @@ private slots: void prependInt() const; void prependMovable() const; void prependCustom() const; + void qhashInt() const { qhash<int>(); } + void qhashMovable() const { qhash<Movable>(); } + void qhashCustom() const { qhash<Custom>(); } void removeInt() const; void removeMovable() const; void removeCustom() const; @@ -241,6 +255,7 @@ private slots: void resizeComplex_data() const; void resizeComplex() const; void resizeCtorAndDtor() const; + void reverseIterators() const; void sizeInt() const; void sizeMovable() const; void sizeCustom() const; @@ -294,6 +309,7 @@ private: template<typename T> void fill() const; template<typename T> void fromList() const; template<typename T> void insert() const; + template<typename T> void qhash() const; template<typename T> void prepend() const; template<typename T> void remove() const; template<typename T> void size() const; @@ -1437,6 +1453,16 @@ void tst_QVector::mid() const QCOMPARE(list.mid(4), QVector<QString>() << "buck" << "hello" << "kitty"); } +template <typename T> +void tst_QVector::qhash() const +{ + QVector<T> l1, l2; + QCOMPARE(qHash(l1), qHash(l2)); + l1 << SimpleValue<T>::at(0); + l2 << SimpleValue<T>::at(0); + QCOMPARE(qHash(l1), qHash(l2)); +} + template<typename T> void tst_QVector::prepend() const { @@ -1907,6 +1933,21 @@ void tst_QVector::resizeCtorAndDtor() const QCOMPARE(Custom::counter.loadAcquire(), items); } +void tst_QVector::reverseIterators() const +{ + QVector<int> v; + v << 1 << 2 << 3 << 4; + QVector<int> vr = v; + std::reverse(vr.begin(), vr.end()); + const QVector<int> &cvr = vr; + QVERIFY(std::equal(v.begin(), v.end(), vr.rbegin())); + QVERIFY(std::equal(v.begin(), v.end(), vr.crbegin())); + QVERIFY(std::equal(v.begin(), v.end(), cvr.rbegin())); + QVERIFY(std::equal(vr.rbegin(), vr.rend(), v.begin())); + QVERIFY(std::equal(vr.crbegin(), vr.crend(), v.begin())); + QVERIFY(std::equal(cvr.rbegin(), cvr.rend(), v.begin())); +} + template<typename T> void tst_QVector::size() const { @@ -2076,6 +2117,19 @@ void tst_QVector::testOperators() const // == QVERIFY(myvec == combined); + // <, >, <=, >= + QVERIFY(!(myvec < combined)); + QVERIFY(!(myvec > combined)); + QVERIFY( myvec <= combined); + QVERIFY( myvec >= combined); + combined.push_back("G"); + QVERIFY( myvec < combined); + QVERIFY(!(myvec > combined)); + QVERIFY( myvec <= combined); + QVERIFY(!(myvec >= combined)); + QVERIFY(combined > myvec); + QVERIFY(combined >= myvec); + // [] QCOMPARE(myvec[0], QLatin1String("A")); QCOMPARE(myvec[1], QLatin1String("B")); |