diff options
Diffstat (limited to 'tests/auto/corelib/serialization/json')
-rw-r--r-- | tests/auto/corelib/serialization/json/json.pro | 1 | ||||
-rw-r--r-- | tests/auto/corelib/serialization/json/tst_qtjson.cpp | 72 |
2 files changed, 73 insertions, 0 deletions
diff --git a/tests/auto/corelib/serialization/json/json.pro b/tests/auto/corelib/serialization/json/json.pro index 8fa17c5c38..390a674f7a 100644 --- a/tests/auto/corelib/serialization/json/json.pro +++ b/tests/auto/corelib/serialization/json/json.pro @@ -1,6 +1,7 @@ TARGET = tst_json QT = core-private testlib CONFIG += testcase +contains(QT_CONFIG, c++2a):CONFIG *= c++2a !android:TESTDATA += bom.json test.json test.bjson test3.json test2.json else:RESOURCES += json.qrc diff --git a/tests/auto/corelib/serialization/json/tst_qtjson.cpp b/tests/auto/corelib/serialization/json/tst_qtjson.cpp index 3e58dd03cc..3f242ba2a0 100644 --- a/tests/auto/corelib/serialization/json/tst_qtjson.cpp +++ b/tests/auto/corelib/serialization/json/tst_qtjson.cpp @@ -68,6 +68,7 @@ private Q_SLOTS: void testObjectNestedEmpty(); void testValueRef(); + void testValueRefComparison(); void testObjectIteration(); void testArrayIteration(); @@ -176,6 +177,8 @@ private Q_SLOTS: void fromToVariantConversions_data(); void fromToVariantConversions(); + void noLeakOnNameClash(); + private: QString testDataDir; }; @@ -908,6 +911,57 @@ void tst_QtJson::testValueRef() QCOMPARE(object.value(QLatin1String("key")), QJsonValue(42)); } +void tst_QtJson::testValueRefComparison() +{ + QJsonValue a0 = 42.; + QJsonValue a1 = QStringLiteral("142"); + +#define CHECK_IMPL(lhs, rhs, ineq) \ + QCOMPARE(lhs, rhs); \ + QVERIFY(!(lhs != rhs)); \ + QVERIFY(lhs != ineq); \ + QVERIFY(!(lhs == ineq)); \ + QVERIFY(ineq != rhs); \ + QVERIFY(!(ineq == rhs)); \ + /* end */ + +#define CHECK(lhs, rhs, ineq) \ + do { \ + CHECK_IMPL(lhs, rhs, ineq) \ + CHECK_IMPL(qAsConst(lhs), rhs, ineq) \ + CHECK_IMPL(lhs, qAsConst(rhs), ineq) \ + CHECK_IMPL(qAsConst(lhs), qAsConst(rhs), ineq) \ + } while (0) + + // check that the (in)equality operators aren't ambiguous in C++20: + QJsonArray a = {a0, a1}; + + Q_STATIC_ASSERT((std::is_same<decltype(a[0]), QJsonValueRef>::value)); + + auto r0 = a.begin()[0]; + auto r1 = a.begin()[1]; + auto c0 = qAsConst(a).begin()[0]; + // ref <> ref + CHECK(r0, r0, r1); + // cref <> ref + CHECK(c0, r0, r1); + // ref <> cref + CHECK(r0, c0, r1); + // ref <> val + CHECK(r0, a0, r1); + // cref <> val + CHECK(c0, a0, r1); + // val <> ref + CHECK(a0, r0, a1); + // val <> cref + CHECK(a0, c0, a1); + // val <> val + CHECK(a0, a0, a1); + +#undef CHECK +#undef CHECK_IMPL +} + void tst_QtJson::testObjectIteration() { QJsonObject object; @@ -3685,5 +3739,23 @@ void tst_QtJson::fromToVariantConversions() } } +void tst_QtJson::noLeakOnNameClash() +{ + QJsonDocument doc = QJsonDocument::fromJson("{\"\":{\"\":0},\"\":0}"); + QVERIFY(!doc.isNull()); + const QJsonObject obj = doc.object(); + + // Removed the duplicate key. + QCOMPARE(obj.length(), 1); + + // Retained the last of the duplicates. + const QJsonValue val = obj.begin().value(); + QVERIFY(val.isDouble()); + QCOMPARE(val.toDouble(), 0.0); + + // It should not leak. + // In particular it should not forget to deref the container for the inner object. +} + QTEST_MAIN(tst_QtJson) #include "tst_qtjson.moc" |