diff options
author | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-07-03 01:58:26 +0200 |
---|---|---|
committer | Giuseppe D'Angelo <giuseppe.dangelo@kdab.com> | 2020-08-06 19:15:39 +0200 |
commit | 4fc539bd846f08c5e448810e263f3850593b8dc2 (patch) | |
tree | 29f2ce5a0b609a389b9fc4935560a15a94e20ec3 /tests/auto/corelib/tools/qmap/tst_qmap.cpp | |
parent | 14090760a87f23509b7bb5ad846537c766cb44a5 (diff) |
QMap: fix insert() rvalue overloads
Receiving an rvalue still requires to check whether the parameter
is detached, otherwise we can't steal its backing std::map.
Change-Id: Ie88dbf39fd777112ad7bb20a46d5c2d65be8eb3d
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'tests/auto/corelib/tools/qmap/tst_qmap.cpp')
-rw-r--r-- | tests/auto/corelib/tools/qmap/tst_qmap.cpp | 97 |
1 files changed, 97 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qmap/tst_qmap.cpp b/tests/auto/corelib/tools/qmap/tst_qmap.cpp index c8a228f8dd..4f7ac08c2d 100644 --- a/tests/auto/corelib/tools/qmap/tst_qmap.cpp +++ b/tests/auto/corelib/tools/qmap/tst_qmap.cpp @@ -1205,6 +1205,100 @@ void tst_QMap::insert() } } +template <template <typename K, typename T> typename Map> +void testDetachWhenInsert() +{ + const Map<int, int> referenceSource = { + { 0, 0 }, + { 1, 1 }, + { 2, 2 } + }; + + const Map<int, int> referenceDestination = { + { 0, 0 }, + { 1, 1 }, + { 2, 2 }, + { 3, 3 } + }; + + // copy insertion of non-shared map + { + Map<int, int> source; + source.insert(0, 0); + source.insert(1, 1); + source.insert(2, 2); + + Map<int, int> dest; + dest.insert(3, 3); + Map<int, int> destCopy = dest; + + dest.insert(source); + + QCOMPARE(source, referenceSource); + QCOMPARE(dest, referenceDestination); + + QCOMPARE(destCopy.count(), 1); // unchanged + } + + // copy insertion of shared map + { + Map<int, int> source; + source.insert(0, 0); + source.insert(1, 1); + source.insert(2, 2); + Map<int, int> sourceCopy = source; + + Map<int, int> dest; + dest.insert(3, 3); + Map<int, int> destCopy = dest; + + dest.insert(source); + + QCOMPARE(source, referenceSource); + QCOMPARE(sourceCopy, referenceSource); + + QCOMPARE(dest, referenceDestination); + QCOMPARE(destCopy.count(), 1); // unchanged + } + + // move insertion of non-shared map + { + Map<int, int> source; + source.insert(0, 0); + source.insert(1, 1); + source.insert(2, 2); + + Map<int, int> dest; + dest.insert(3, 3); + Map<int, int> destCopy = dest; + + dest.insert(source); + + QCOMPARE(dest, referenceDestination); + QCOMPARE(destCopy.count(), 1); // unchanged + } + + // move insertion of shared map + { + Map<int, int> source; + source.insert(0, 0); + source.insert(1, 1); + source.insert(2, 2); + Map<int, int> sourceCopy = source; + + Map<int, int> dest; + dest.insert(3, 3); + Map<int, int> destCopy = dest; + + dest.insert(std::move(source)); + + QCOMPARE(sourceCopy, referenceSource); + + QCOMPARE(dest, referenceDestination); + QCOMPARE(destCopy.count(), 1); // unchanged + } +}; + void tst_QMap::insertMap() { { @@ -1298,6 +1392,9 @@ void tst_QMap::insertMap() QCOMPARE(map.count(), 1); } + + testDetachWhenInsert<QMap>(); + testDetachWhenInsert<QMultiMap>(); } void tst_QMap::checkMostLeftNode() |