diff options
Diffstat (limited to 'tests/auto/corelib')
16 files changed, 774 insertions, 161 deletions
diff --git a/tests/auto/corelib/global/qflags/qflags.pro b/tests/auto/corelib/global/qflags/qflags.pro index 9e80d5634b..3f78bc045b 100644 --- a/tests/auto/corelib/global/qflags/qflags.pro +++ b/tests/auto/corelib/global/qflags/qflags.pro @@ -3,3 +3,4 @@ TARGET = tst_qflags QT = core testlib SOURCES = tst_qflags.cpp DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 +contains(QT_CONFIG, c++11): CONFIG += c++11 c++14 diff --git a/tests/auto/corelib/global/qflags/tst_qflags.cpp b/tests/auto/corelib/global/qflags/tst_qflags.cpp index 4c74ac166b..15fe93298d 100644 --- a/tests/auto/corelib/global/qflags/tst_qflags.cpp +++ b/tests/auto/corelib/global/qflags/tst_qflags.cpp @@ -94,6 +94,16 @@ void tst_QFlags::testFlagMultiBits() const template <unsigned int N, typename T> bool verifyConstExpr(T n) { return n == N; } +Q_DECL_RELAXED_CONSTEXPR Qt::MouseButtons testRelaxedConstExpr() +{ + Qt::MouseButtons value; + value = Qt::LeftButton | Qt::RightButton; + value |= Qt::MiddleButton; + value &= ~Qt::LeftButton; + value ^= Qt::RightButton; + return value; +} + void tst_QFlags::constExpr() { #ifdef Q_COMPILER_CONSTEXPR @@ -115,6 +125,10 @@ void tst_QFlags::constExpr() QVERIFY(verifyConstExpr<Qt::MouseButtons(Qt::RightButton) | 0xff>(0xff)); QVERIFY(!verifyConstExpr<Qt::RightButton>(!Qt::MouseButtons(Qt::LeftButton))); + +#if defined(__cpp_constexpr) && __cpp_constexpr-0 >= 201304 + QVERIFY(verifyConstExpr<testRelaxedConstExpr()>(Qt::MiddleButton)); +#endif #endif } diff --git a/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp b/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp index c0b4ff654a..3304a09061 100644 --- a/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp +++ b/tests/auto/corelib/global/qgetputenv/tst_qgetputenv.cpp @@ -41,14 +41,22 @@ class tst_QGetPutEnv : public QObject Q_OBJECT private slots: void getSetCheck(); + void intValue_data(); + void intValue(); }; void tst_QGetPutEnv::getSetCheck() { const char varName[] = "should_not_exist"; + bool ok; + QVERIFY(!qEnvironmentVariableIsSet(varName)); QVERIFY(qEnvironmentVariableIsEmpty(varName)); + ok = true; + QCOMPARE(qEnvironmentVariableIntValue(varName), 0); + QCOMPARE(qEnvironmentVariableIntValue(varName, &ok), 0); + QVERIFY(!ok); QByteArray result = qgetenv(varName); QCOMPARE(result, QByteArray()); @@ -57,12 +65,20 @@ void tst_QGetPutEnv::getSetCheck() QVERIFY(qEnvironmentVariableIsSet(varName)); QVERIFY(qEnvironmentVariableIsEmpty(varName)); + ok = true; + QCOMPARE(qEnvironmentVariableIntValue(varName), 0); + QCOMPARE(qEnvironmentVariableIntValue(varName, &ok), 0); + QVERIFY(!ok); #endif QVERIFY(qputenv(varName, QByteArray("supervalue"))); QVERIFY(qEnvironmentVariableIsSet(varName)); QVERIFY(!qEnvironmentVariableIsEmpty(varName)); + ok = true; + QCOMPARE(qEnvironmentVariableIntValue(varName), 0); + QCOMPARE(qEnvironmentVariableIntValue(varName, &ok), 0); + QVERIFY(!ok); result = qgetenv(varName); QVERIFY(result == "supervalue"); @@ -72,9 +88,61 @@ void tst_QGetPutEnv::getSetCheck() QVERIFY(qunsetenv(varName)); QVERIFY(!qEnvironmentVariableIsSet(varName)); QVERIFY(qEnvironmentVariableIsEmpty(varName)); + ok = true; + QCOMPARE(qEnvironmentVariableIntValue(varName), 0); + QCOMPARE(qEnvironmentVariableIntValue(varName, &ok), 0); + QVERIFY(!ok); result = qgetenv(varName); QCOMPARE(result, QByteArray()); } +void tst_QGetPutEnv::intValue_data() +{ + QTest::addColumn<QByteArray>("value"); + QTest::addColumn<int>("expected"); + QTest::addColumn<bool>("ok"); + + // most non-success cases already tested in getSetCheck() + +#define ROW(x, i, b) \ + QTest::newRow(#x) << QByteArray(#x) << (i) << (b) + ROW(auto, 0, false); + ROW(0, 0, true); + ROW(1, 1, true); + ROW(010, 8, true); + ROW(0x10, 16, true); + ROW(-1, -1, true); + ROW(-010, -8, true); + // ROW(0xffffffff, -1, true); // could be expected, but not how QByteArray::toInt() works + ROW(0xffffffff, 0, false); + const int bases[] = {10, 8, 16}; + for (size_t i = 0; i < sizeof bases / sizeof *bases; ++i) { + QTest::newRow(qPrintable(QString().sprintf("INT_MAX, base %d", bases[i]))) + << QByteArray::number(INT_MAX) << INT_MAX << true; + QTest::newRow(qPrintable(QString().sprintf("INT_MAX+1, base %d", bases[i]))) + << QByteArray::number(qlonglong(INT_MAX) + 1) << 0 << false; + QTest::newRow(qPrintable(QString().sprintf("INT_MIN, base %d", bases[i]))) + << QByteArray::number(INT_MIN) << INT_MIN << true; + QTest::newRow(qPrintable(QString().sprintf("INT_MIN-1, base %d", bases[i]))) + << QByteArray::number(qlonglong(INT_MIN) - 1) << 0 << false; + }; +} + +void tst_QGetPutEnv::intValue() +{ + const char varName[] = "should_not_exist"; + + QFETCH(QByteArray, value); + QFETCH(int, expected); + QFETCH(bool, ok); + + bool actualOk = !ok; + + QVERIFY(qputenv(varName, value)); + QCOMPARE(qEnvironmentVariableIntValue(varName), expected); + QCOMPARE(qEnvironmentVariableIntValue(varName, &actualOk), expected); + QCOMPARE(actualOk, ok); +} + QTEST_MAIN(tst_QGetPutEnv) #include "tst_qgetputenv.moc" diff --git a/tests/auto/corelib/json/tst_qtjson.cpp b/tests/auto/corelib/json/tst_qtjson.cpp index 56a5a28c50..997ceaf7b9 100644 --- a/tests/auto/corelib/json/tst_qtjson.cpp +++ b/tests/auto/corelib/json/tst_qtjson.cpp @@ -90,7 +90,9 @@ private Q_SLOTS: void fromVariant(); void fromVariantMap(); + void fromVariantHash(); void toVariantMap(); + void toVariantHash(); void toVariantList(); void toJson(); @@ -1165,6 +1167,17 @@ void tst_QtJson::fromVariantMap() QCOMPARE(array.at(3).toString(), QLatin1String("foo")); } +void tst_QtJson::fromVariantHash() +{ + QVariantHash map; + map.insert(QLatin1String("key1"), QLatin1String("value1")); + map.insert(QLatin1String("key2"), QLatin1String("value2")); + QJsonObject object = QJsonObject::fromVariantHash(map); + QCOMPARE(object.size(), 2); + QCOMPARE(object.value(QLatin1String("key1")), QJsonValue(QLatin1String("value1"))); + QCOMPARE(object.value(QLatin1String("key2")), QJsonValue(QLatin1String("value2"))); +} + void tst_QtJson::toVariantMap() { QCOMPARE(QMetaType::Type(QJsonValue(QJsonObject()).toVariant().type()), QMetaType::QVariantMap); // QTBUG-32524 @@ -1196,6 +1209,35 @@ void tst_QtJson::toVariantMap() QCOMPARE(list.at(3), QVariant()); } +void tst_QtJson::toVariantHash() +{ + QJsonObject object; + QVariantHash hash = object.toVariantHash(); + QVERIFY(hash.isEmpty()); + + object.insert("Key", QString("Value")); + object.insert("null", QJsonValue()); + QJsonArray array; + array.append(true); + array.append(999.); + array.append(QLatin1String("string")); + array.append(QJsonValue()); + object.insert("Array", array); + + hash = object.toVariantHash(); + + QCOMPARE(hash.size(), 3); + QCOMPARE(hash.value("Key"), QVariant(QString("Value"))); + QCOMPARE(hash.value("null"), QVariant()); + QCOMPARE(hash.value("Array").type(), QVariant::List); + QVariantList list = hash.value("Array").toList(); + QCOMPARE(list.size(), 4); + QCOMPARE(list.at(0), QVariant(true)); + QCOMPARE(list.at(1), QVariant(999.)); + QCOMPARE(list.at(2), QVariant(QLatin1String("string"))); + QCOMPARE(list.at(3), QVariant()); +} + void tst_QtJson::toVariantList() { QCOMPARE(QMetaType::Type(QJsonValue(QJsonArray()).toVariant().type()), QMetaType::QVariantList); // QTBUG-32524 diff --git a/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp b/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp index 1d39280afb..18de761e2a 100644 --- a/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp +++ b/tests/auto/corelib/kernel/qmetamethod/tst_qmetamethod.cpp @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Olivier Goffart <ogoffart@woboq.com> ** Contact: http://www.qt-project.org/legal ** ** This file is part of the test suite of the Qt Toolkit. @@ -50,6 +51,8 @@ private slots: void comparisonOperators(); void fromSignal(); + + void gadget(); }; struct CustomType { }; @@ -727,5 +730,51 @@ void tst_QMetaMethod::fromSignal() #undef FROMSIGNAL_HELPER } +class MyGadget { + Q_GADGET +public: + QString m_value; + Q_INVOKABLE void setValue(const QString &value) { m_value = value; } + Q_INVOKABLE QString getValue() { return m_value; } +}; + +void tst_QMetaMethod::gadget() +{ + int idx; + + idx = MyGadget::staticMetaObject.indexOfMethod("setValue(QString)"); + QVERIFY(idx >= 0); + QMetaMethod setValueMethod = MyGadget::staticMetaObject.method(idx); + QVERIFY(setValueMethod.isValid()); + + idx = MyGadget::staticMetaObject.indexOfMethod("getValue()"); + QVERIFY(idx >= 0); + QMetaMethod getValueMethod = MyGadget::staticMetaObject.method(idx); + QVERIFY(getValueMethod.isValid()); + + { + MyGadget gadget; + QString string; + + QVERIFY(getValueMethod.invokeOnGadget(&gadget, Q_RETURN_ARG(QString, string))); + QCOMPARE(string, gadget.m_value); + + QVERIFY(setValueMethod.invokeOnGadget(&gadget, Q_ARG(QString, QLatin1String("hello")))); + QCOMPARE(gadget.m_value, QLatin1String("hello")); + + QVERIFY(getValueMethod.invokeOnGadget(&gadget, Q_RETURN_ARG(QString, string))); + QCOMPARE(string, gadget.m_value); + } + + { + // Call with null should not crash + MyGadget *gadget = Q_NULLPTR; + QString string; + QVERIFY(!setValueMethod.invokeOnGadget(gadget, Q_ARG(QString, QLatin1String("hi")))); + QVERIFY(!getValueMethod.invokeOnGadget(gadget, Q_RETURN_ARG(QString, string))); + } +} + + QTEST_MAIN(tst_QMetaMethod) #include "tst_qmetamethod.moc" diff --git a/tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp b/tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp index 5cac80191c..941abf9039 100644 --- a/tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp +++ b/tests/auto/corelib/kernel/qmetaproperty/tst_qmetaproperty.cpp @@ -1,6 +1,7 @@ /**************************************************************************** ** ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Olivier Goffart <ogoffart@woboq.com> ** Contact: http://www.qt-project.org/legal ** ** This file is part of the test suite of the Qt Toolkit. @@ -50,6 +51,7 @@ private slots: void hasStdCppSet(); void isConstant(); void isFinal(); + void gadget(); public: enum EnumType { EnumType1 }; @@ -103,5 +105,32 @@ void tst_QMetaProperty::isFinal() QVERIFY(!prop.isFinal()); } +class MyGadget { + Q_GADGET + Q_PROPERTY(QString value READ getValue WRITE setValue RESET resetValue) +public: + QString m_value; + void setValue(const QString &value) { m_value = value; } + QString getValue() { return m_value; } + void resetValue() { m_value = QLatin1Literal("reset"); } +}; + +void tst_QMetaProperty::gadget() +{ + const QMetaObject *mo = &MyGadget::staticMetaObject; + QMetaProperty valueProp = mo->property(mo->indexOfProperty("value")); + QVERIFY(valueProp.isValid()); + { + MyGadget g; + QString hello = QLatin1Literal("hello"); + QVERIFY(valueProp.writeOnGadget(&g, hello)); + QCOMPARE(g.m_value, QLatin1String("hello")); + QCOMPARE(valueProp.readOnGadget(&g), QVariant(hello)); + QVERIFY(valueProp.resetOnGadget(&g)); + QCOMPARE(valueProp.readOnGadget(&g), QVariant(QLatin1String("reset"))); + } +} + + QTEST_MAIN(tst_QMetaProperty) #include "tst_qmetaproperty.moc" diff --git a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp index a35896283a..e3ef2b6714 100644 --- a/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp +++ b/tests/auto/corelib/kernel/qmetatype/tst_qmetatype.cpp @@ -79,6 +79,10 @@ private slots: void normalizedTypes(); void typeName_data(); void typeName(); + void type_data(); + void type(); + void type_fromSubString_data(); + void type_fromSubString(); void create_data(); void create(); void createCopy_data(); @@ -109,6 +113,7 @@ private slots: void saveAndLoadBuiltin_data(); void saveAndLoadBuiltin(); void saveAndLoadCustom(); + void metaObject_data(); void metaObject(); void constexprMetaTypeIds(); void constRefs(); @@ -385,6 +390,61 @@ void tst_QMetaType::typeName() QCOMPARE(name.toLatin1(), QMetaObject::normalizedType(name.toLatin1().constData())); } +void tst_QMetaType::type_data() +{ + QTest::addColumn<QMetaType::Type>("aType"); + QTest::addColumn<QByteArray>("aTypeName"); + +#define TST_QMETATYPE_TYPE_DATA(MetaTypeName, MetaTypeId, RealType)\ + QTest::newRow(#RealType) << QMetaType::MetaTypeName << QByteArray( #RealType ); +#define TST_QMETATYPE_TYPE_DATA_ALIAS(MetaTypeName, MetaTypeId, AliasType, RealTypeString)\ + QTest::newRow(RealTypeString) << QMetaType::MetaTypeName << QByteArray( #AliasType ); + + QTest::newRow("empty") << QMetaType::UnknownType << QByteArray(); + + QT_FOR_EACH_STATIC_TYPE(TST_QMETATYPE_TYPE_DATA) + QT_FOR_EACH_STATIC_ALIAS_TYPE(TST_QMETATYPE_TYPE_DATA_ALIAS) + +#undef TST_QMETATYPE_TYPE_DATA +#undef TST_METATYPE_TYPE_DATA_ALIAS +} + +void tst_QMetaType::type() +{ + QFETCH(QMetaType::Type, aType); + QFETCH(QByteArray, aTypeName); + + // QMetaType::type(QByteArray) + QCOMPARE(QMetaType::type(aTypeName), int(aType)); + // QMetaType::type(const char *) + QCOMPARE(QMetaType::type(aTypeName.constData()), int(aType)); +} + +void tst_QMetaType::type_fromSubString_data() +{ + QTest::addColumn<int>("offset"); + QTest::addColumn<int>("size"); + QTest::addColumn<int>("expectedType"); + + // The test string is defined in the test function below + QTest::newRow("int") << 0 << 3 << int(QMetaType::Int); + QTest::newRow("boo") << 3 << 3 << 0; + QTest::newRow("bool") << 3 << 4 << int(QMetaType::Bool); + QTest::newRow("intbool") << 0 << 7 << 0; + QTest::newRow("QMetaType::Type") << 7 << 15 << ::qMetaTypeId<QMetaType::Type>(); + QTest::newRow("double") << 22 << 6 << int(QMetaType::Double); +} + +void tst_QMetaType::type_fromSubString() +{ + static const char *types = "intboolQMetaType::Typedoublexxx"; + QFETCH(int, offset); + QFETCH(int, size); + QFETCH(int, expectedType); + QByteArray ba = QByteArray::fromRawData(types + offset, size); + QCOMPARE(QMetaType::type(ba), expectedType); +} + #define FOR_EACH_PRIMITIVE_METATYPE(F) \ QT_FOR_EACH_STATIC_PRIMITIVE_TYPE(F) \ QT_FOR_EACH_STATIC_CORE_POINTER(F) \ @@ -968,7 +1028,9 @@ void tst_QMetaType::flagsBinaryCompatibility5_0() QFETCH(quint32, id); QFETCH(quint32, flags); - QCOMPARE(quint32(QMetaType::typeFlags(id)), flags); + quint32 mask_5_0 = 0x1ff; // Only compare the values that were already defined in 5.0 + + QCOMPARE(quint32(QMetaType::typeFlags(id)) & mask_5_0, flags); } void tst_QMetaType::construct_data() @@ -1772,17 +1834,41 @@ void tst_QMetaType::saveAndLoadCustom() QCOMPARE(stream.status(), QDataStream::ReadPastEnd); } -void tst_QMetaType::metaObject() +struct MyGadget { + Q_GADGET; +}; + +Q_DECLARE_METATYPE(MyGadget); +Q_DECLARE_METATYPE(const QMetaObject *); + +void tst_QMetaType::metaObject_data() { - QCOMPARE(QMetaType::metaObjectForType(QMetaType::QObjectStar), &QObject::staticMetaObject); - QCOMPARE(QMetaType::metaObjectForType(::qMetaTypeId<QFile*>()), &QFile::staticMetaObject); - QCOMPARE(QMetaType::metaObjectForType(::qMetaTypeId<MyObject*>()), &MyObject::staticMetaObject); - QCOMPARE(QMetaType::metaObjectForType(QMetaType::Int), static_cast<const QMetaObject *>(0)); + QTest::addColumn<int>("type"); + QTest::addColumn<const QMetaObject*>("result"); + QTest::addColumn<bool>("isGadget"); + QTest::addColumn<bool>("isQObjectPtr"); + + QTest::newRow("QObject") << int(QMetaType::QObjectStar) << &QObject::staticMetaObject << false << true; + QTest::newRow("QFile*") << ::qMetaTypeId<QFile*>() << &QFile::staticMetaObject << false << true; + QTest::newRow("MyObject*") << ::qMetaTypeId<MyObject*>() << &MyObject::staticMetaObject << false << true; + QTest::newRow("int") << int(QMetaType::Int) << static_cast<const QMetaObject *>(0) << false << false; + QTest::newRow("QEasingCurve") << ::qMetaTypeId<QEasingCurve>() << &QEasingCurve::staticMetaObject << true << false; + QTest::newRow("MyGadget") << ::qMetaTypeId<MyGadget>() << &MyGadget::staticMetaObject << true << false; +} - QCOMPARE(QMetaType(QMetaType::QObjectStar).metaObject(), &QObject::staticMetaObject); - QCOMPARE(QMetaType(::qMetaTypeId<QFile*>()).metaObject(), &QFile::staticMetaObject); - QCOMPARE(QMetaType(::qMetaTypeId<MyObject*>()).metaObject(), &MyObject::staticMetaObject); - QCOMPARE(QMetaType(QMetaType::Int).metaObject(), static_cast<const QMetaObject *>(0)); + +void tst_QMetaType::metaObject() +{ + QFETCH(int, type); + QFETCH(const QMetaObject *, result); + QFETCH(bool, isGadget); + QFETCH(bool, isQObjectPtr); + + QCOMPARE(QMetaType::metaObjectForType(type), result); + QMetaType mt(type); + QCOMPARE(mt.metaObject(), result); + QCOMPARE(!!(mt.flags() & QMetaType::IsGadget), isGadget); + QCOMPARE(!!(mt.flags() & QMetaType::PointerToQObject), isQObjectPtr); } #define METATYPE_ID_FUNCTION(Type, MetaTypeId, Name) \ diff --git a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp index 41a6de214a..301db37233 100644 --- a/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp +++ b/tests/auto/corelib/kernel/qvariant/tst_qvariant.cpp @@ -43,9 +43,10 @@ #include <qlocale.h> #include <qdebug.h> #include <qjsondocument.h> - +#include <quuid.h> #include <limits.h> +#include <float.h> #include <QLinkedList> #include <QRegularExpression> @@ -213,7 +214,10 @@ private slots: void toIntFromQString() const; void toIntFromDouble() const; void setValue(); + void fpStringRoundtrip_data() const; + void fpStringRoundtrip() const; + void numericalConvert_data(); void numericalConvert(); void moreCustomTypes(); void movabilityTest(); @@ -985,7 +989,7 @@ void tst_QVariant::toByteArray_data() QTest::newRow( "int" ) << QVariant( -123 ) << QByteArray( "-123" ); QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QByteArray( "123" ); QTest::newRow( "double" ) << QVariant( 123.456 ) << QByteArray( "123.456" ); - QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.456" ); + QTest::newRow( "float" ) << QVariant( 123.456f ) << QByteArray( "123.456001" ); QTest::newRow( "longlong" ) << QVariant( (qlonglong)34 ) << QByteArray( "34" ); QTest::newRow( "ulonglong" ) << QVariant( (qulonglong)34 ) << QByteArray( "34" ); } @@ -1011,7 +1015,7 @@ void tst_QVariant::toString_data() QTest::newRow( "int" ) << QVariant( -123 ) << QString( "-123" ); QTest::newRow( "uint" ) << QVariant( (uint)123 ) << QString( "123" ); QTest::newRow( "double" ) << QVariant( 123.456 ) << QString( "123.456" ); - QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456" ); + QTest::newRow( "float" ) << QVariant( 123.456f ) << QString( "123.456001" ); QTest::newRow( "bool" ) << QVariant( true ) << QString( "true" ); QTest::newRow( "qdate" ) << QVariant( QDate( 2002, 1, 1 ) ) << QString( "2002-01-01" ); QTest::newRow( "qtime" ) << QVariant( QTime( 12, 34, 56 ) ) << QString( "12:34:56" ); @@ -1367,12 +1371,12 @@ void tst_QVariant::operator_eq_eq_data() QVariant mUIntQString(QString("42")); QVariant mDouble(42.11); - QVariant mDoubleString(QByteArray("42.11")); - QVariant mDoubleQString(QString("42.11")); + QVariant mDoubleString(QByteArray("42.109999999999999")); + QVariant mDoubleQString(QString("42.109999999999999")); QVariant mFloat(42.11f); - QVariant mFloatString(QByteArray("42.11")); - QVariant mFloatQString(QString("42.11")); + QVariant mFloatString(QByteArray("42.1100006")); + QVariant mFloatQString(QString("42.1100006")); QVariant mLongLong((qlonglong)-42); QVariant mLongLongString(QByteArray("-42")); @@ -1433,6 +1437,13 @@ void tst_QVariant::operator_eq_eq_data() // ### many other combinations missing { + // QUuid can convert to QString, but not the opposite + QUuid uuid(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10); + QTest::newRow("uuidstring") << QVariant(uuid) << QVariant(uuid.toString()) << true; + QTest::newRow("stringuuid") << QVariant(uuid.toString()) << QVariant(uuid) << true; + } + + { QMap<QString, QVariant> map1; map1.insert( "X", 1 ); @@ -2945,41 +2956,72 @@ void tst_QVariant::setValue() QVERIFY( v2.isDetached() ); } +void tst_QVariant::fpStringRoundtrip_data() const +{ + QTest::addColumn<QVariant>("number"); + + QTest::newRow("float") << QVariant(1 + FLT_EPSILON); + QTest::newRow("double") << QVariant(1 + DBL_EPSILON); +} + +void tst_QVariant::fpStringRoundtrip() const +{ + QFETCH(QVariant, number); + + QVariant converted = number; + QVERIFY(converted.convert(QVariant::String)); + QVERIFY(converted.convert(number.type())); + QCOMPARE(converted, number); + + converted = number; + QVERIFY(converted.convert(QVariant::ByteArray)); + QVERIFY(converted.convert(number.type())); + QCOMPARE(converted, number); +} + +void tst_QVariant::numericalConvert_data() +{ + QTest::addColumn<QVariant>("v"); + QTest::addColumn<bool>("isInteger"); + QTest::newRow("float") << QVariant(float(5.3)) << false; + QTest::newRow("double") << QVariant(double(5.3)) << false; + QTest::newRow("qreal") << QVariant(qreal(5.3)) << false; + QTest::newRow("int") << QVariant(int(5)) << true; + QTest::newRow("uint") << QVariant(uint(5)) << true; + QTest::newRow("short") << QVariant(short(5)) << true; + QTest::newRow("longlong") << QVariant(quint64(5)) << true; + QTest::newRow("long") << QVariant::fromValue(long(5)) << true; + QTest::newRow("stringint") << QVariant(QString::fromLatin1("5")) << true; + QTest::newRow("string") << QVariant(QString::fromLatin1("5.30000019")) << false; +} + 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 - QVariant vfloat(float(5.3)); - QVariant vdouble(double(5.3)); - QVariant vreal(qreal(5.3)); - QVariant vint(int(5)); - QVariant vuint(uint(5)); - QVariant vshort(short(5)); - QVariant vlonglong(quint64(5)); - QVariant vlong = QVariant::fromValue(long(5)); - QVariant vstringint(QString::fromLatin1("5")); - QVariant vstring(QString::fromLatin1("5.3")); - - QVector<QVariant *> vect; - vect << &vfloat << &vdouble << &vreal << &vint << &vuint << &vshort<< &vlonglong << &vlong << &vstringint << &vstring; - - for(int i = 0; i < vect.size(); i++) { - double num = 5.3; - if (i >= 3 && i <= 8) - num = 5; - QVariant *v = vect.at(i); - QCOMPARE(v->toFloat() , float(num)); - QCOMPARE(float(v->toReal()) , float(num)); - QCOMPARE(float(v->toDouble()) , float(num)); - if (i != 9) { - QCOMPARE(v->toInt() , int(num)); - QCOMPARE(v->toUInt() , uint(num)); - QCOMPARE(v->toULongLong() , quint64(num)); - QCOMPARE(v->value<ulong>() , ulong(num)); - QCOMPARE(v->value<ushort>() , ushort(num)); - } - QCOMPARE(v->toString() , QString::number(num)); + QFETCH(QVariant, v); + QFETCH(bool, isInteger); + double num = isInteger ? 5 : 5.3; + + QCOMPARE(v.toFloat() , float(num)); + QCOMPARE(float(v.toReal()) , float(num)); + QCOMPARE(float(v.toDouble()) , float(num)); + if (isInteger) { + QCOMPARE(v.toInt() , int(num)); + QCOMPARE(v.toUInt() , uint(num)); + QCOMPARE(v.toULongLong() , quint64(num)); + QCOMPARE(v.value<ulong>() , ulong(num)); + QCOMPARE(v.value<ushort>() , ushort(num)); + } + switch (v.userType()) + { + case QVariant::Double: + QCOMPARE(v.toString() , QString::number(num, 'g', DBL_MANT_DIG * log10(2.) + 2)); + break; + case QMetaType::Float: + QCOMPARE(v.toString() , QString::number(float(num), 'g', FLT_MANT_DIG * log10(2.) + 2)); + break; } } @@ -3214,8 +3256,8 @@ void tst_QVariant::moreCustomTypes() QCOMPARE(MyNotMovable::count, 0); { - PLAY_WITH_VARIANT(12.12, false, "12.12", 12.12, true); - PLAY_WITH_VARIANT(12.12f, false, "12.12", 12.12f, true); + PLAY_WITH_VARIANT(12.12, false, "12.119999999999999", 12.12, true); + PLAY_WITH_VARIANT(12.12f, false, "12.1199999", 12.12f, true); PLAY_WITH_VARIANT('a', false, "a", 'a', true); PLAY_WITH_VARIANT((unsigned char)('a'), false, "a", 'a', true); PLAY_WITH_VARIANT( quint8(12), false, "\xc", 12, true); diff --git a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp index 18739cb4e1..d010ff807d 100644 --- a/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp +++ b/tests/auto/corelib/tools/qbytearray/tst_qbytearray.cpp @@ -2000,6 +2000,9 @@ void tst_QByteArray::toUpperLower_data() QTest::addColumn<QByteArray>("lower"); QTest::newRow("empty") << QByteArray() << QByteArray() << QByteArray(); + QTest::newRow("literal") << QByteArrayLiteral("Hello World") + << QByteArrayLiteral("HELLO WORLD") + << QByteArrayLiteral("hello world"); QTest::newRow("ascii") << QByteArray("Hello World, this is a STRING") << QByteArray("HELLO WORLD, THIS IS A STRING") << QByteArray("hello world, this is a string"); @@ -2014,8 +2017,34 @@ void tst_QByteArray::toUpperLower() QFETCH(QByteArray, input); QFETCH(QByteArray, upper); QFETCH(QByteArray, lower); + QCOMPARE(lower.toLower(), lower); + QCOMPARE(upper.toUpper(), upper); QCOMPARE(input.toUpper(), upper); QCOMPARE(input.toLower(), lower); + + QByteArray copy = input; + QCOMPARE(qMove(copy).toUpper(), upper); + copy = input; + copy.detach(); + QCOMPARE(qMove(copy).toUpper(), upper); + + copy = input; + QCOMPARE(qMove(copy).toLower(), lower); + copy = input; + copy.detach(); + QCOMPARE(qMove(copy).toLower(), lower); + + copy = lower; + QCOMPARE(qMove(copy).toLower(), lower); + copy = lower; + copy.detach(); + QCOMPARE(qMove(copy).toLower(), lower); + + copy = upper; + QCOMPARE(qMove(copy).toUpper(), upper); + copy = upper; + copy.detach(); + QCOMPARE(qMove(copy).toUpper(), upper); } void tst_QByteArray::macTypes() diff --git a/tests/auto/corelib/tools/qdatetime/qdatetime.pro b/tests/auto/corelib/tools/qdatetime/qdatetime.pro index 0a89fe7645..25d11443e4 100644 --- a/tests/auto/corelib/tools/qdatetime/qdatetime.pro +++ b/tests/auto/corelib/tools/qdatetime/qdatetime.pro @@ -11,3 +11,8 @@ win32-msvc|win32-msvc9x { QMAKE_CXXFLAGS_RELEASE -= -O1 } DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0 + +mac { + OBJECTIVE_SOURCES += tst_qdatetime_mac.mm + LIBS += -framework Foundation +} diff --git a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp index 51c3a19d63..8876bb3d34 100644 --- a/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp +++ b/tests/auto/corelib/tools/qdatetime/tst_qdatetime.cpp @@ -145,6 +145,8 @@ private slots: void invalid() const; + void macTypes(); + private: enum { LocalTimeIsUtc = 0, LocalTimeAheadOfUtc = 1, LocalTimeBehindUtc = -1} localTimeType; bool europeanTimeZone; @@ -2981,5 +2983,15 @@ void tst_QDateTime::invalid() const QCOMPARE(tzDate.timeSpec(), Qt::TimeZone); } +void tst_QDateTime::macTypes() +{ +#ifndef Q_OS_MAC + QSKIP("This is a Apple-only test"); +#else + extern void tst_QDateTime_macTypes(); // in qdatetime_mac.mm + tst_QDateTime_macTypes(); +#endif +} + QTEST_APPLESS_MAIN(tst_QDateTime) #include "tst_qdatetime.moc" diff --git a/tests/auto/corelib/tools/qdatetime/tst_qdatetime_mac.mm b/tests/auto/corelib/tools/qdatetime/tst_qdatetime_mac.mm new file mode 100644 index 0000000000..d03ae3faeb --- /dev/null +++ b/tests/auto/corelib/tools/qdatetime/tst_qdatetime_mac.mm @@ -0,0 +1,82 @@ +/**************************************************************************** +** +** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies). +** Copyright (C) 2014 Petroules Corporation. +** Contact: http://www.qt-project.org/legal +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** Commercial License Usage +** Licensees holding valid commercial Qt licenses may use this file in +** accordance with the commercial license agreement provided with the +** Software or, alternatively, in accordance with the terms contained in +** a written agreement between you and Digia. For licensing terms and +** conditions see http://qt.digia.com/licensing. For further information +** use the contact form at http://qt.digia.com/contact-us. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Digia gives you certain additional +** rights. These rights are described in the Digia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** GNU General Public License Usage +** Alternatively, this file may be used under the terms of the GNU +** General Public License version 3.0 as published by the Free Software +** Foundation and appearing in the file LICENSE.GPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU General Public License version 3.0 requirements will be +** met: http://www.gnu.org/copyleft/gpl.html. +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ + +#include <QtCore/QDateTime> +#include <QtTest/QtTest> + +#include <CoreFoundation/CoreFoundation.h> +#include <Foundation/Foundation.h> + +void tst_QDateTime_macTypes() +{ + // QDateTime <-> CFDate + { + QDateTime qtDateTime = QDateTime::fromMSecsSinceEpoch(0); + const CFDateRef cfDate = qtDateTime.toCFDate(); + QCOMPARE(QDateTime::fromCFDate(cfDate), qtDateTime); + CFRelease(cfDate); + } + { + QDateTime qtDateTime = QDateTime::fromMSecsSinceEpoch(0); + const CFDateRef cfDate = qtDateTime.toCFDate(); + QDateTime qtDateTimeCopy(qtDateTime); + qtDateTime.setTime_t(10000); // modify + QCOMPARE(QDateTime::fromCFDate(cfDate), qtDateTimeCopy); + } + // QDateTime <-> NSDate + { + NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; + QDateTime qtDateTime = QDateTime::fromMSecsSinceEpoch(0); + const NSDate *nsDate = qtDateTime.toNSDate(); + QCOMPARE(QDateTime::fromNSDate(nsDate), qtDateTime); + [autoreleasepool release]; + } + { + NSAutoreleasePool *autoreleasepool = [[NSAutoreleasePool alloc] init]; + QDateTime qtDateTime = QDateTime::fromMSecsSinceEpoch(0); + const NSDate *nsDate = qtDateTime.toNSDate(); + QDateTime qtDateTimeCopy(qtDateTime); + qtDateTime.setTime_t(10000); // modify + QCOMPARE(QDateTime::fromNSDate(nsDate), qtDateTimeCopy); + [autoreleasepool release]; + } +} diff --git a/tests/auto/corelib/tools/qeasingcurve/tst_qeasingcurve.cpp b/tests/auto/corelib/tools/qeasingcurve/tst_qeasingcurve.cpp index d1152419c0..c1d6184072 100644 --- a/tests/auto/corelib/tools/qeasingcurve/tst_qeasingcurve.cpp +++ b/tests/auto/corelib/tools/qeasingcurve/tst_qeasingcurve.cpp @@ -51,6 +51,7 @@ private slots: void operators(); void properties(); void metaTypes(); + void propertyOrderIsNotImportant(); void bezierSpline_data(); void bezierSpline(); void tcbSpline_data(); @@ -552,6 +553,25 @@ void tst_QEasingCurve::metaTypes() QVERIFY(qMetaTypeId<QEasingCurve>() == QMetaType::QEasingCurve); } +/* + Test to ensure that regardless of what order properties are set, they should produce the same + behavior. + */ +void tst_QEasingCurve::propertyOrderIsNotImportant() +{ + + QEasingCurve c1; + c1.setPeriod(1); + c1.setType(QEasingCurve::OutSine); + QVERIFY(c1.valueForProgress(0.75) > 0.9); + + QEasingCurve c2; + c2.setType(QEasingCurve::OutSine); + c2.setPeriod(1); + + QCOMPARE(c1.valueForProgress(0.75), c2.valueForProgress(0.75)); +} + void tst_QEasingCurve::bezierSpline_data() { QTest::addColumn<QString>("definition"); diff --git a/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp b/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp index db22f99cb8..1b6fe2aefe 100644 --- a/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp +++ b/tests/auto/corelib/tools/qregularexpression/tst_qregularexpression.cpp @@ -237,6 +237,95 @@ void consistencyCheck(const QRegularExpressionMatchIterator &iterator) } +template<typename Result> +static void prepareResultForNoMatchType(Result *r, const Result &orig) +{ + Q_UNUSED(r); + Q_UNUSED(orig); +} + +static void prepareResultForNoMatchType(Match *m, const Match &orig) +{ + m->isValid = orig.isValid; +} + +template<typename QREMatch, typename QREMatchFunc, typename Subject, typename Result> +static void testMatchImpl(const QRegularExpression ®exp, + QREMatchFunc matchingMethod, + const Subject &subject, + int offset, + QRegularExpression::MatchType matchType, + QRegularExpression::MatchOptions matchOptions, + const Result &result) +{ + { + const QREMatch m = (regexp.*matchingMethod)(subject, offset, matchType, matchOptions); + consistencyCheck(m); + QVERIFY(m == result); + QCOMPARE(m.regularExpression(), regexp); + QCOMPARE(m.matchType(), matchType); + QCOMPARE(m.matchOptions(), matchOptions); + } + { + // ignore the expected results provided by the match object -- + // we'll never get any result when testing the NoMatch type. + // Just check the validity of the match here. + Result realMatch; + prepareResultForNoMatchType(&realMatch, result); + + const QREMatch m = (regexp.*matchingMethod)(subject, offset, QRegularExpression::NoMatch, matchOptions); + consistencyCheck(m); + QVERIFY(m == realMatch); + QCOMPARE(m.regularExpression(), regexp); + QCOMPARE(m.matchType(), QRegularExpression::NoMatch); + QCOMPARE(m.matchOptions(), matchOptions); + } +} + +template<typename QREMatch, typename QREMatchFuncForString, typename QREMatchFuncForStringRef, typename Result> +static void testMatch(const QRegularExpression ®exp, + QREMatchFuncForString matchingMethodForString, + QREMatchFuncForStringRef matchingMethodForStringRef, + const QString &subject, + int offset, + QRegularExpression::MatchType matchType, + QRegularExpression::MatchOptions matchOptions, + const Result &result) +{ + if (forceOptimize) + regexp.optimize(); + + // test with QString as subject type + testMatchImpl<QREMatch>(regexp, matchingMethodForString, subject, offset, matchType, matchOptions, result); + + // test with QStringRef as subject type + testMatchImpl<QREMatch>(regexp, + matchingMethodForStringRef, + QStringRef(&subject, 0, subject.length()), + offset, + matchType, + matchOptions, + result); + + // offset <= 0 tested above; now also test stringrefs not spanning over + // the entire subject. Note that the offset can be negative, hence the above + // tests can't be merged into this one + for (int i = 1; i <= offset; ++i) { + testMatchImpl<QREMatch>(regexp, + matchingMethodForStringRef, + QStringRef(&subject, i, subject.length() - i), + offset - i, + matchType, + matchOptions, + result); + } +} + +typedef QRegularExpressionMatch (QRegularExpression::*QREMatchStringPMF)(const QString &, int, QRegularExpression::MatchType, QRegularExpression::MatchOptions) const; +typedef QRegularExpressionMatch (QRegularExpression::*QREMatchStringRefPMF)(const QStringRef &, int, QRegularExpression::MatchType, QRegularExpression::MatchOptions) const; +typedef QRegularExpressionMatchIterator (QRegularExpression::*QREGlobalMatchStringPMF)(const QString &, int, QRegularExpression::MatchType, QRegularExpression::MatchOptions) const; +typedef QRegularExpressionMatchIterator (QRegularExpression::*QREGlobalMatchStringRefPMF)(const QStringRef &, int, QRegularExpression::MatchType, QRegularExpression::MatchOptions) const; + void tst_QRegularExpression::provideRegularExpressions() { QTest::addColumn<QString>("pattern"); @@ -526,6 +615,7 @@ void tst_QRegularExpression::normalMatch_data() QTest::addColumn<Match>("match"); Match m; + int offset = 0; m.clear(); m.isValid = true; m.hasMatch = true; @@ -577,20 +667,28 @@ void tst_QRegularExpression::normalMatch_data() m.clear(); m.isValid = true; m.hasMatch = true; m.captured << "c123def" << "c12" << "3" << "def"; - QTest::newRow("match06") << QRegularExpression("(\\w*)(\\d+)(\\w*)") - << "abc123def" - << 2 - << QRegularExpression::MatchOptions(QRegularExpression::NoMatchOption) - << m; + offset = 2; + for (int i = 0; i <= offset; ++i) { + QTest::newRow(QStringLiteral("match06-offset%1").arg(i).toUtf8().constData()) + << QRegularExpression("(\\w*)(\\d+)(\\w*)") + << QStringLiteral("abc123def").mid(offset - i) + << i + << QRegularExpression::MatchOptions(QRegularExpression::NoMatchOption) + << m; + } m.clear(); m.isValid = true; m.hasMatch = true; m.captured << QString(""); - QTest::newRow("match07") << QRegularExpression("\\w*") - << "abc123def" - << 9 - << QRegularExpression::MatchOptions(QRegularExpression::NoMatchOption) - << m; + offset = 9; + for (int i = 0; i <= offset; ++i) { + QTest::newRow(QStringLiteral("match07-offset%1").arg(i).toUtf8().constData()) + << QRegularExpression("\\w*") + << QStringLiteral("abc123def").mid(offset - i) + << i + << QRegularExpression::MatchOptions(QRegularExpression::NoMatchOption) + << m; + } m.clear(); m.isValid = true; m.hasMatch = true; @@ -648,19 +746,27 @@ void tst_QRegularExpression::normalMatch_data() m.clear(); m.isValid = true; - QTest::newRow("nomatch02") << QRegularExpression("(\\w+) (\\w+)") - << "a string" - << 1 - << QRegularExpression::MatchOptions(QRegularExpression::NoMatchOption) - << m; + offset = 1; + for (int i = 0; i <= offset; ++i) { + QTest::newRow(QStringLiteral("nomatch02-offset%1").arg(i).toUtf8().constData()) + << QRegularExpression("(\\w+) (\\w+)") + << QStringLiteral("a string").mid(offset - i) + << i + << QRegularExpression::MatchOptions(QRegularExpression::NoMatchOption) + << m; + } m.clear(); m.isValid = true; - QTest::newRow("nomatch03") << QRegularExpression("\\w+") - << "abc123def" - << 9 - << QRegularExpression::MatchOptions(QRegularExpression::NoMatchOption) - << m; + offset = 9; + for (int i = 0; i <= offset; ++i) { + QTest::newRow(QStringLiteral("nomatch03-offset%1").arg(i).toUtf8().constData()) + << QRegularExpression("\\w+") + << QStringLiteral("abc123def").mid(offset - i) + << i + << QRegularExpression::MatchOptions(QRegularExpression::NoMatchOption) + << m; + } // *** @@ -728,32 +834,14 @@ void tst_QRegularExpression::normalMatch() QFETCH(QRegularExpression::MatchOptions, matchOptions); QFETCH(Match, match); - if (forceOptimize) - regexp.optimize(); - - { - QRegularExpressionMatch m = regexp.match(subject, offset, QRegularExpression::NormalMatch, matchOptions); - consistencyCheck(m); - QVERIFY(m == match); - QCOMPARE(m.regularExpression(), regexp); - QCOMPARE(m.matchType(), QRegularExpression::NormalMatch); - QCOMPARE(m.matchOptions(), matchOptions); - } - { - // ignore the expected results provided by the match object -- - // we'll never get any result when testing the NoMatch type. - // Just check the validity of the match here. - Match realMatch; - realMatch.clear(); - realMatch.isValid = match.isValid; - - QRegularExpressionMatch m = regexp.match(subject, offset, QRegularExpression::NoMatch, matchOptions); - consistencyCheck(m); - QVERIFY(m == realMatch); - QCOMPARE(m.regularExpression(), regexp); - QCOMPARE(m.matchType(), QRegularExpression::NoMatch); - QCOMPARE(m.matchOptions(), matchOptions); - } + testMatch<QRegularExpressionMatch>(regexp, + static_cast<QREMatchStringPMF>(&QRegularExpression::match), + static_cast<QREMatchStringRefPMF>(&QRegularExpression::match), + subject, + offset, + QRegularExpression::NormalMatch, + matchOptions, + match); } void tst_QRegularExpression::partialMatch_data() @@ -766,6 +854,7 @@ void tst_QRegularExpression::partialMatch_data() QTest::addColumn<Match>("match"); Match m; + int offset = 0; m.clear(); m.isValid = true; m.hasPartialMatch = true; @@ -840,12 +929,16 @@ void tst_QRegularExpression::partialMatch_data() m.clear(); m.isValid = true; m.hasPartialMatch = true; m.captured << "def"; - QTest::newRow("softmatch08") << QRegularExpression("abc\\w+X|defY") - << "abcdef" - << 1 - << QRegularExpression::PartialPreferCompleteMatch - << QRegularExpression::MatchOptions(QRegularExpression::NoMatchOption) - << m; + offset = 1; + for (int i = 0; i <= offset; ++i) { + QTest::newRow(QStringLiteral("softmatch08-offset%1").arg(i).toUtf8().constData()) + << QRegularExpression("abc\\w+X|defY") + << QStringLiteral("abcdef").mid(offset - i) + << i + << QRegularExpression::PartialPreferCompleteMatch + << QRegularExpression::MatchOptions(QRegularExpression::NoMatchOption) + << m; + } // *** @@ -922,12 +1015,16 @@ void tst_QRegularExpression::partialMatch_data() m.clear(); m.isValid = true; m.hasPartialMatch = true; m.captured << "def"; - QTest::newRow("hardmatch08") << QRegularExpression("abc\\w+X|defY") - << "abcdef" - << 1 - << QRegularExpression::PartialPreferFirstMatch - << QRegularExpression::MatchOptions(QRegularExpression::NoMatchOption) - << m; + offset = 1; + for (int i = 0; i <= offset; ++i) { + QTest::newRow(QStringLiteral("hardmatch08-offset%1").arg(i).toUtf8().constData()) + << QRegularExpression("abc\\w+X|defY") + << QStringLiteral("abcdef").mid(offset - i) + << i + << QRegularExpression::PartialPreferFirstMatch + << QRegularExpression::MatchOptions(QRegularExpression::NoMatchOption) + << m; + } m.clear(); m.isValid = true; m.hasPartialMatch = true; @@ -1009,32 +1106,14 @@ void tst_QRegularExpression::partialMatch() QFETCH(QRegularExpression::MatchOptions, matchOptions); QFETCH(Match, match); - if (forceOptimize) - regexp.optimize(); - - { - QRegularExpressionMatch m = regexp.match(subject, offset, matchType, matchOptions); - consistencyCheck(m); - QVERIFY(m == match); - QCOMPARE(m.regularExpression(), regexp); - QCOMPARE(m.matchType(), matchType); - QCOMPARE(m.matchOptions(), matchOptions); - } - { - // ignore the expected results provided by the match object -- - // we'll never get any result when testing the NoMatch type. - // Just check the validity of the match here. - Match realMatch; - realMatch.clear(); - realMatch.isValid = match.isValid; - - QRegularExpressionMatch m = regexp.match(subject, offset, QRegularExpression::NoMatch, matchOptions); - consistencyCheck(m); - QVERIFY(m == realMatch); - QCOMPARE(m.regularExpression(), regexp); - QCOMPARE(m.matchType(), QRegularExpression::NoMatch); - QCOMPARE(m.matchOptions(), matchOptions); - } + testMatch<QRegularExpressionMatch>(regexp, + static_cast<QREMatchStringPMF>(&QRegularExpression::match), + static_cast<QREMatchStringRefPMF>(&QRegularExpression::match), + subject, + offset, + matchType, + matchOptions, + match); } void tst_QRegularExpression::globalMatch_data() @@ -1304,31 +1383,14 @@ void tst_QRegularExpression::globalMatch() QFETCH(QRegularExpression::MatchOptions, matchOptions); QFETCH(QList<Match>, matchList); - if (forceOptimize) - regexp.optimize(); - - { - QRegularExpressionMatchIterator iterator = regexp.globalMatch(subject, offset, matchType, matchOptions); - consistencyCheck(iterator); - QVERIFY(iterator == matchList); - QCOMPARE(iterator.regularExpression(), regexp); - QCOMPARE(iterator.matchType(), matchType); - QCOMPARE(iterator.matchOptions(), matchOptions); - } - { - // ignore the expected results provided by the match object -- - // we'll never get any result when testing the NoMatch type. - // Just check the validity of the match here. - QList<Match> realMatchList; - - QRegularExpressionMatchIterator iterator = regexp.globalMatch(subject, offset, QRegularExpression::NoMatch, matchOptions); - consistencyCheck(iterator); - QVERIFY(iterator == realMatchList); - QCOMPARE(iterator.regularExpression(), regexp); - QCOMPARE(iterator.matchType(), QRegularExpression::NoMatch); - QCOMPARE(iterator.matchOptions(), matchOptions); - } - + testMatch<QRegularExpressionMatchIterator>(regexp, + static_cast<QREGlobalMatchStringPMF>(&QRegularExpression::globalMatch), + static_cast<QREGlobalMatchStringRefPMF>(&QRegularExpression::globalMatch), + subject, + offset, + matchType, + matchOptions, + matchList); } void tst_QRegularExpression::serialize_data() diff --git a/tests/auto/corelib/tools/qstring/tst_qstring.cpp b/tests/auto/corelib/tools/qstring/tst_qstring.cpp index ea40c64c89..e001440045 100644 --- a/tests/auto/corelib/tools/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/tools/qstring/tst_qstring.cpp @@ -155,6 +155,7 @@ private slots: void lastIndexOfInvalidRegex(); void indexOf_data(); void indexOf(); + void indexOfInvalidRegex(); void indexOf2_data(); void indexOf2(); void indexOf3_data(); @@ -1182,6 +1183,18 @@ void tst_QString::indexOf() QRegularExpression re(QRegularExpression::escape(needle), options); QCOMPARE( haystack.indexOf(re, startpos), resultpos ); + QCOMPARE(haystack.indexOf(re, startpos, Q_NULLPTR), resultpos); + + QRegularExpressionMatch match; + QVERIFY(!match.hasMatch()); + QCOMPARE(haystack.indexOf(re, startpos, &match), resultpos); + QCOMPARE(match.hasMatch(), resultpos != -1); + if (resultpos > -1 && needleIsLatin) { + if (bcs) + QVERIFY(match.captured() == needle); + else + QVERIFY(match.captured().toLower() == needle.toLower()); + } } if (cs == Qt::CaseSensitive) { @@ -1290,6 +1303,20 @@ void tst_QString::indexOf2() } } +void tst_QString::indexOfInvalidRegex() +{ + QTest::ignoreMessage(QtWarningMsg, "QString::indexOf: invalid QRegularExpression object"); + QCOMPARE(QString("invalid regex\\").indexOf(QRegularExpression("invalid regex\\")), -1); + QTest::ignoreMessage(QtWarningMsg, "QString::indexOf: invalid QRegularExpression object"); + QCOMPARE(QString("invalid regex\\").indexOf(QRegularExpression("invalid regex\\"), -1, Q_NULLPTR), -1); + + QRegularExpressionMatch match; + QVERIFY(!match.hasMatch()); + QTest::ignoreMessage(QtWarningMsg, "QString::indexOf: invalid QRegularExpression object"); + QCOMPARE(QString("invalid regex\\").indexOf(QRegularExpression("invalid regex\\"), -1, &match), -1); + QVERIFY(!match.hasMatch()); +} + void tst_QString::lastIndexOf_data() { QTest::addColumn<QString>("haystack" ); @@ -1379,6 +1406,17 @@ void tst_QString::lastIndexOf() QRegularExpression re(QRegularExpression::escape(needle), options); QCOMPARE(haystack.lastIndexOf(re, from), expected); + QCOMPARE(haystack.lastIndexOf(re, from, Q_NULLPTR), expected); + QRegularExpressionMatch match; + QVERIFY(!match.hasMatch()); + QCOMPARE(haystack.lastIndexOf(re, from, &match), expected); + QCOMPARE(match.hasMatch(), expected > -1); + if (expected > -1) { + if (caseSensitive) + QCOMPARE(match.captured(), needle); + else + QCOMPARE(match.captured().toLower(), needle.toLower()); + } } } @@ -1403,7 +1441,15 @@ void tst_QString::lastIndexOf() void tst_QString::lastIndexOfInvalidRegex() { QTest::ignoreMessage(QtWarningMsg, "QString::lastIndexOf: invalid QRegularExpression object"); - QCOMPARE(QString("").lastIndexOf(QRegularExpression("invalid regex\\"), 0), -1); + QCOMPARE(QString("invalid regex\\").lastIndexOf(QRegularExpression("invalid regex\\"), 0), -1); + QTest::ignoreMessage(QtWarningMsg, "QString::lastIndexOf: invalid QRegularExpression object"); + QCOMPARE(QString("invalid regex\\").lastIndexOf(QRegularExpression("invalid regex\\"), -1, Q_NULLPTR), -1); + + QRegularExpressionMatch match; + QVERIFY(!match.hasMatch()); + QTest::ignoreMessage(QtWarningMsg, "QString::lastIndexOf: invalid QRegularExpression object"); + QCOMPARE(QString("invalid regex\\").lastIndexOf(QRegularExpression("invalid regex\\"), -1, &match), -1); + QVERIFY(!match.hasMatch()); } void tst_QString::count() @@ -1838,6 +1884,7 @@ void tst_QString::toUpper() { QCOMPARE( QString().toUpper(), QString() ); QCOMPARE( QString("").toUpper(), QString("") ); + QCOMPARE( QStringLiteral("text").toUpper(), QString("TEXT") ); QCOMPARE( QString("text").toUpper(), QString("TEXT") ); QCOMPARE( QString("Text").toUpper(), QString("TEXT") ); QCOMPARE( QString("tExt").toUpper(), QString("TEXT") ); @@ -1898,6 +1945,7 @@ void tst_QString::toLower() QCOMPARE( QString().toLower(), QString() ); QCOMPARE( QString("").toLower(), QString("") ); QCOMPARE( QString("text").toLower(), QString("text") ); + QCOMPARE( QStringLiteral("Text").toLower(), QString("text") ); QCOMPARE( QString("Text").toLower(), QString("text") ); QCOMPARE( QString("tExt").toLower(), QString("text") ); QCOMPARE( QString("teXt").toLower(), QString("text") ); @@ -2019,6 +2067,13 @@ void tst_QString::trimmed() QCOMPARE(a,(QString)" "); a=" a "; QCOMPARE(a.trimmed(),(QString)"a"); + + a="Text"; + QCOMPARE(qMove(a).trimmed(),(QString)"Text"); + a=" "; + QCOMPARE(qMove(a).trimmed(),(QString)""); + a=" a "; + QCOMPARE(qMove(a).trimmed(),(QString)"a"); } void tst_QString::simplified_data() @@ -2063,9 +2118,12 @@ void tst_QString::simplified() QVERIFY2(result.isEmpty() && !result.isNull(), qPrintable("'" + full + "' did not yield empty: " + result)); } else { QCOMPARE(result, simple); - if (full == simple) - QVERIFY(result.isSharedWith(full)); } + + // force a detach + if (!full.isEmpty()) + full[0] = full[0]; + QCOMPARE(qMove(full).simplified(), simple); } void tst_QString::insert() @@ -4454,6 +4512,8 @@ void tst_QString::section() QCOMPARE( wholeString.section( QRegExp(sep), start, end, QString::SectionFlag(flags) ), sectionString ); QCOMPARE( wholeString.section( QRegularExpression(sep), start, end, QString::SectionFlag(flags) ), sectionString ); } else { + if (sep.size() == 1) + QCOMPARE( wholeString.section( sep[0], start, end, QString::SectionFlag(flags) ), sectionString ); QCOMPARE( wholeString.section( sep, start, end, QString::SectionFlag(flags) ), sectionString ); QCOMPARE( wholeString.section( QRegExp(QRegExp::escape(sep)), start, end, QString::SectionFlag(flags) ), sectionString ); QCOMPARE( wholeString.section( QRegularExpression(QRegularExpression::escape(sep)), start, end, QString::SectionFlag(flags) ), sectionString ); diff --git a/tests/auto/corelib/tools/qvector/tst_qvector.cpp b/tests/auto/corelib/tools/qvector/tst_qvector.cpp index 9a79d48472..c9e8a5f657 100644 --- a/tests/auto/corelib/tools/qvector/tst_qvector.cpp +++ b/tests/auto/corelib/tools/qvector/tst_qvector.cpp @@ -575,6 +575,18 @@ void tst_QVector::append() const QCOMPARE(v.last(), SimpleValue<T>::at(0)); } #endif + { + QVector<int> v; + v << 1 << 2 << 3; + QVector<int> x; + x << 4 << 5 << 6; + v.append(x); + + QVector<int> combined; + combined << 1 << 2 << 3 << 4 << 5 << 6; + + QCOMPARE(v, combined); + } } void tst_QVector::appendInt() const |