summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/serialization/qcborvalue/tst_qcborvalue.cpp
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@intel.com>2021-11-09 15:24:45 -0800
committerThiago Macieira <thiago.macieira@intel.com>2022-02-03 14:45:46 -0800
commitde6ced66920600e659dbaa2509526a3bcb0b3360 (patch)
treed7b8d017a11b676473888ba102502db826f458eb /tests/auto/corelib/serialization/qcborvalue/tst_qcborvalue.cpp
parent1e62f089985f0377c72f922d267839ce6f46c3cd (diff)
QCborValue: fix incorrect to{Array,Map} when the value is empty
When QCborValue referred to an empty array or map, toArray() and toMap() would respectively return the default value instead of the empty object, as expected. Pick-to: 6.2 6.3 Change-Id: I5e52dc5b093c43a3b678fffd16b60456d0037ad7 Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'tests/auto/corelib/serialization/qcborvalue/tst_qcborvalue.cpp')
-rw-r--r--tests/auto/corelib/serialization/qcborvalue/tst_qcborvalue.cpp87
1 files changed, 87 insertions, 0 deletions
diff --git a/tests/auto/corelib/serialization/qcborvalue/tst_qcborvalue.cpp b/tests/auto/corelib/serialization/qcborvalue/tst_qcborvalue.cpp
index f5a3e4817c..753346f706 100644
--- a/tests/auto/corelib/serialization/qcborvalue/tst_qcborvalue.cpp
+++ b/tests/auto/corelib/serialization/qcborvalue/tst_qcborvalue.cpp
@@ -79,6 +79,7 @@ private slots:
void arrayStringElements();
void arraySelfAssign_data() { basics_data(); }
void arraySelfAssign();
+ void arrayNested();
void mapDefaultInitialization();
void mapEmptyInitializerList();
@@ -97,6 +98,7 @@ private slots:
void mapSelfAssign();
void mapComplexKeys_data() { basics_data(); }
void mapComplexKeys();
+ void mapNested();
void sorting();
@@ -1581,6 +1583,91 @@ void tst_QCborValue::mapComplexKeys()
QVERIFY(!m.contains(tagged));
}
+void tst_QCborValue::arrayNested()
+{
+ const QCborArray wrongArray = { false, nullptr, QCborValue() };
+ {
+ QCborArray a1 = { 42, 47 };
+ QCborArray a2 = { QCborValue(a1) };
+ QCOMPARE(a2.size(), 1);
+ const QCborValue &first = qAsConst(a2).first();
+ QVERIFY(first.isArray());
+ QCOMPARE(first.toArray(wrongArray).size(), 2);
+ QCOMPARE(first.toArray(wrongArray).first(), 42);
+ QCOMPARE(first.toArray(wrongArray).last(), 47);
+ }
+ {
+ QCborArray a1 = { 42, 47 };
+ QCborArray a2 = { QCborValue(a1) };
+ QCOMPARE(a2.size(), 1);
+ QCborValueRef first = a2.first();
+ QVERIFY(first.isArray());
+ QCOMPARE(first.toArray(wrongArray).size(), 2);
+ QCOMPARE(first.toArray(wrongArray).first(), 42);
+ QCOMPARE(first.toArray(wrongArray).last(), 47);
+ }
+
+ {
+ QCborArray a1;
+ a1 = { QCborValue(a1) }; // insert it into itself
+ QCOMPARE(a1.size(), 1);
+ const QCborValue &first = qAsConst(a1).first();
+ QVERIFY(first.isArray());
+ QCOMPARE(first, QCborArray());
+ QCOMPARE(first.toArray(wrongArray), QCborArray());
+ }
+ {
+ QCborArray a1;
+ a1 = { QCborValue(a1) }; // insert it into itself
+ QCborValueRef first = a1.first();
+ QVERIFY(first.isArray());
+ QCOMPARE(first, QCborArray());
+ QCOMPARE(first.toArray(wrongArray), QCborArray());
+ }
+ {
+ QCborArray a1;
+ a1.append(a1); // insert into itself
+ QCOMPARE(a1.size(), 1);
+ const QCborValue &first = qAsConst(a1).first();
+ QVERIFY(first.isArray());
+ QCOMPARE(first, QCborArray());
+ QCOMPARE(first.toArray(), QCborArray());
+ }
+ {
+ QCborArray a1;
+ a1.append(a1); // insert into itself
+ QCborValueRef first = a1.first();
+ QVERIFY(first.isArray());
+ QCOMPARE(first, QCborArray());
+ QCOMPARE(first.toArray(), QCborArray());
+ }
+}
+
+void tst_QCborValue::mapNested()
+{
+ const QCborMap wrongMap = { { -1, false }, {-2, nullptr }, { -3, QCborValue() } };
+ {
+ QCborMap m1 = { {1, 42}, {2, 47} };
+ QCborMap m2 = { { QString(), m1 } };
+ QCOMPARE(m2.size(), 1);
+ const QCborValue &first = m2.constBegin().value();
+ QVERIFY(first.isMap());
+ QCOMPARE(first.toMap(wrongMap).size(), 2);
+ QCOMPARE(first.toMap(wrongMap).begin().key(), 1);
+ QCOMPARE(first.toMap(wrongMap).begin().value(), 42);
+ }
+
+ {
+ QCborMap m1;
+ m1 = { { QString(), QCborValue(m1) } }; // insert it into itself
+ QCOMPARE(m1.size(), 1);
+ const QCborValue &first = m1.constBegin().value();
+ QVERIFY(first.isMap());
+ QCOMPARE(first, QCborMap());
+ QCOMPARE(first.toMap(wrongMap), QCborMap());
+ }
+}
+
void tst_QCborValue::sorting()
{
QCborValue vundef, vnull(nullptr);