diff options
Diffstat (limited to 'tests/auto/corelib/tools/qmap/tst_qmap.cpp')
-rw-r--r-- | tests/auto/corelib/tools/qmap/tst_qmap.cpp | 368 |
1 files changed, 206 insertions, 162 deletions
diff --git a/tests/auto/corelib/tools/qmap/tst_qmap.cpp b/tests/auto/corelib/tools/qmap/tst_qmap.cpp index e5bddb5d8d..6950dcf705 100644 --- a/tests/auto/corelib/tools/qmap/tst_qmap.cpp +++ b/tests/auto/corelib/tools/qmap/tst_qmap.cpp @@ -1,34 +1,13 @@ -/**************************************************************************** -** -** Copyright (C) 2016 The Qt Company Ltd. -** Contact: https://www.qt.io/licensing/ -** -** This file is part of the test suite of the Qt Toolkit. -** -** $QT_BEGIN_LICENSE:GPL-EXCEPT$ -** 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 The Qt Company. For licensing terms -** and conditions see https://www.qt.io/terms-conditions. For further -** information use the contact form at https://www.qt.io/contact-us. -** -** GNU General Public License Usage -** Alternatively, this file may be used under the terms of the GNU -** General Public License version 3 as published by the Free Software -** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT -** included in the packaging of this file. Please review the following -** information to ensure the GNU General Public License requirements will -** be met: https://www.gnu.org/licenses/gpl-3.0.html. -** -** $QT_END_LICENSE$ -** -****************************************************************************/ +// Copyright (C) 2016 The Qt Company Ltd. +// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only #include <qmap.h> #include <QTest> + #include <QDebug> +#include <QScopeGuard> + +using namespace Qt::StringLiterals; QT_WARNING_DISABLE_DEPRECATED @@ -85,6 +64,15 @@ private slots: void eraseValidIteratorOnSharedMap(); void removeElementsInMap(); void toStdMap(); + + void multiMapStoresInReverseInsertionOrder(); + + // Tests for deprecated APIs. +#if QT_DEPRECATED_SINCE(6, 0) + void deprecatedInsertMulti(); + void deprecatedIteratorApis(); + void deprecatedInsert(); +#endif // QT_DEPRECATED_SINCE(6, 0) }; struct IdentityTracker { @@ -184,8 +172,8 @@ void tst_QMap::count() { MyMap map; MyMap map2( map ); - QCOMPARE( map.count(), 0 ); - QCOMPARE( map2.count(), 0 ); + QCOMPARE( map.size(), 0 ); + QCOMPARE( map2.size(), 0 ); QCOMPARE( MyClass::count, int(0) ); QCOMPARE(map.count("key"), 0); QCOMPARE(map.size(), 0); @@ -194,9 +182,9 @@ void tst_QMap::count() QVERIFY(!map2.isDetached()); // detach map2["Hallo"] = MyClass( "Fritz" ); - QCOMPARE( map.count(), 0 ); QCOMPARE( map.size(), 0 ); - QCOMPARE( map2.count(), 1 ); + QCOMPARE( map.size(), 0 ); + QCOMPARE( map2.size(), 1 ); QCOMPARE( map2.size(), 1 ); QVERIFY(!map.isDetached()); #ifndef Q_CC_SUN @@ -208,11 +196,11 @@ void tst_QMap::count() { typedef QMap<QString, MyClass> Map; Map map; - QCOMPARE( map.count(), 0); + QCOMPARE( map.size(), 0); map.insert( "Torben", MyClass("Weis") ); - QCOMPARE( map.count(), 1 ); + QCOMPARE( map.size(), 1 ); map.insert( "Claudia", MyClass("Sorg") ); - QCOMPARE( map.count(), 2 ); + QCOMPARE( map.size(), 2 ); map.insert( "Lars", MyClass("Linzbach") ); map.insert( "Matthias", MyClass("Ettrich") ); map.insert( "Sue", MyClass("Paludo") ); @@ -220,7 +208,7 @@ void tst_QMap::count() map.insert( "Haavard", MyClass("Nord") ); map.insert( "Arnt", MyClass("Gulbrandsen") ); map.insert( "Paul", MyClass("Tvete") ); - QCOMPARE( map.count(), 9 ); + QCOMPARE( map.size(), 9 ); map.insert( "Paul", MyClass("Tvete 1") ); map.insert( "Paul", MyClass("Tvete 2") ); map.insert( "Paul", MyClass("Tvete 3") ); @@ -228,69 +216,69 @@ void tst_QMap::count() map.insert( "Paul", MyClass("Tvete 5") ); map.insert( "Paul", MyClass("Tvete 6") ); - QCOMPARE( map.count(), 9 ); + QCOMPARE( map.size(), 9 ); QCOMPARE( map.count("Paul"), 1 ); #ifndef Q_CC_SUN QCOMPARE( MyClass::count, 9 ); #endif Map map2( map ); - QVERIFY( map2.count() == 9 ); + QVERIFY( map2.size() == 9 ); #ifndef Q_CC_SUN QCOMPARE( MyClass::count, 9 ); #endif map2.insert( "Kay", MyClass("Roemer") ); - QVERIFY( map2.count() == 10 ); - QVERIFY( map.count() == 9 ); + QVERIFY( map2.size() == 10 ); + QVERIFY( map.size() == 9 ); #ifndef Q_CC_SUN QCOMPARE( MyClass::count, 19 ); #endif map2 = map; - QVERIFY( map.count() == 9 ); - QVERIFY( map2.count() == 9 ); + QVERIFY( map.size() == 9 ); + QVERIFY( map2.size() == 9 ); #ifndef Q_CC_SUN QCOMPARE( MyClass::count, 9 ); #endif map2.insert( "Kay", MyClass("Roemer") ); - QVERIFY( map2.count() == 10 ); + QVERIFY( map2.size() == 10 ); #ifndef Q_CC_SUN QCOMPARE( MyClass::count, 19 ); #endif map2.clear(); - QVERIFY( map.count() == 9 ); - QVERIFY( map2.count() == 0 ); + QVERIFY( map.size() == 9 ); + QVERIFY( map2.size() == 0 ); #ifndef Q_CC_SUN QCOMPARE( MyClass::count, 9 ); #endif map2 = map; - QVERIFY( map.count() == 9 ); - QVERIFY( map2.count() == 9 ); + QVERIFY( map.size() == 9 ); + QVERIFY( map2.size() == 9 ); #ifndef Q_CC_SUN QCOMPARE( MyClass::count, 9 ); #endif map2.clear(); - QVERIFY( map.count() == 9 ); - QVERIFY( map2.count() == 0 ); + QVERIFY( map.size() == 9 ); + QVERIFY( map2.size() == 0 ); #ifndef Q_CC_SUN QCOMPARE( MyClass::count, 9 ); #endif map.remove( "Lars" ); - QVERIFY( map.count() == 8 ); - QVERIFY( map2.count() == 0 ); + QVERIFY( map.size() == 8 ); + QVERIFY( map2.size() == 0 ); #ifndef Q_CC_SUN QCOMPARE( MyClass::count, 8 ); #endif map.remove( "Mist" ); - QVERIFY( map.count() == 8 ); - QVERIFY( map2.count() == 0 ); + QVERIFY( map.size() == 8 ); + QVERIFY( map2.size() == 0 ); #ifndef Q_CC_SUN QCOMPARE( MyClass::count, 8 ); #endif @@ -304,22 +292,22 @@ void tst_QMap::count() #ifndef Q_CC_SUN QVERIFY( MyClass::count == 1 ); #endif - QVERIFY( map.count() == 1 ); + QVERIFY( map.size() == 1 ); (void)map["Torben"].str; (void)map["Lars"].str; #ifndef Q_CC_SUN QVERIFY( MyClass::count == 2 ); #endif - QVERIFY( map.count() == 2 ); + QVERIFY( map.size() == 2 ); const Map& cmap = map; (void)cmap["Depp"].str; #ifndef Q_CC_SUN QVERIFY( MyClass::count == 2 ); #endif - QVERIFY( map.count() == 2 ); - QVERIFY( cmap.count() == 2 ); + QVERIFY( map.size() == 2 ); + QVERIFY( cmap.size() == 2 ); } QCOMPARE( MyClass::count, 0 ); { @@ -336,8 +324,8 @@ void tst_QMap::count() { QMultiMap<int, MyClass> map; QMultiMap<int, MyClass> map2(map); - QCOMPARE(map.count(), 0); - QCOMPARE(map2.count(), 0); + QCOMPARE(map.size(), 0); + QCOMPARE(map2.size(), 0); QCOMPARE(MyClass::count, 0); QCOMPARE(map.count(1), 0); QCOMPARE(map.size(), 0); @@ -347,26 +335,26 @@ void tst_QMap::count() // detach map2.insert(0, MyClass("value0")); - QCOMPARE(map.count(), 0); QCOMPARE(map.size(), 0); - QCOMPARE(map2.count(), 1); + QCOMPARE(map.size(), 0); + QCOMPARE(map2.size(), 1); QCOMPARE(map2.size(), 1); QVERIFY(!map.isDetached()); QCOMPARE(MyClass::count, 1); map2.insert(1, MyClass("value1")); map2.insert(2, MyClass("value2")); - QCOMPARE(map2.count(), 3); + QCOMPARE(map2.size(), 3); QCOMPARE(MyClass::count, 3); map2.insert(0, MyClass("value0_1")); map2.insert(0, MyClass("value0_2")); - QCOMPARE(map2.count(), 5); + QCOMPARE(map2.size(), 5); QCOMPARE(map2.count(0), 3); QCOMPARE(MyClass::count, 5); map2.clear(); - QCOMPARE(map2.count(), 0); + QCOMPARE(map2.size(), 0); QCOMPARE(MyClass::count, 0); } @@ -447,7 +435,12 @@ void tst_QMap::beginEnd() // detach map2.insert( "2", "c" ); QVERIFY( map.constBegin() == map.constBegin() ); - QVERIFY( map.constBegin() != map2.constBegin() ); + + // comparing iterators between two different std::map is UB (and raises an + // assertion failure with MSVC debug-mode iterators), so we compare the + // elements' addresses. + QVERIFY(&map.constBegin().key() != &map2.constBegin().key()); + QVERIFY(&map.constBegin().value() != &map2.constBegin().value()); } void tst_QMap::firstLast() @@ -647,16 +640,19 @@ void tst_QMap::operator_eq() QMap<int, int> b; QVERIFY(a == b); + QCOMPARE(qHash(a), qHash(b)); QVERIFY(!(a != b)); a.insert(1,1); b.insert(1,1); QVERIFY(a == b); + QCOMPARE(qHash(a), qHash(b)); QVERIFY(!(a != b)); a.insert(0,1); b.insert(0,1); QVERIFY(a == b); + QCOMPARE(qHash(a), qHash(b)); QVERIFY(!(a != b)); // compare for inequality: @@ -679,6 +675,7 @@ void tst_QMap::operator_eq() QMap<QString, QString> b; QVERIFY(a == b); + QCOMPARE(qHash(a), qHash(b)); QVERIFY(!(a != b)); a.insert("Hello", "World"); @@ -687,6 +684,7 @@ void tst_QMap::operator_eq() b.insert("Hello", "World"); QVERIFY(a == b); + QCOMPARE(qHash(a), qHash(b)); QVERIFY(!(a != b)); a.insert("Goodbye", "cruel world"); @@ -703,6 +701,7 @@ void tst_QMap::operator_eq() // empty keys and null keys match: b.insert(QString(""), QString()); QVERIFY(a == b); + QCOMPARE(qHash(a), qHash(b)); QVERIFY(!(a != b)); } @@ -892,7 +891,7 @@ void tst_QMap::constFind() for (i = 3; i < 10; ++i) { compareString = testString.arg(i); - map.insertMulti(4, compareString); + map.insert(4, compareString); } QMultiMap<int, QString>::const_iterator it = map.constFind(4); @@ -998,7 +997,7 @@ void tst_QMap::lowerUpperBound() map.insert(3, "three"); map.insert(7, "seven"); - map.insertMulti(7, "seven_2"); + map.insert(7, "seven_2"); QCOMPARE(map.upperBound(0).key(), 1); QCOMPARE(map.upperBound(1).key(), 3); @@ -1129,29 +1128,6 @@ void tst_QMap::iterators() QVERIFY(stlIt.value() == testString.arg(i)); QCOMPARE(i, 100); - // Same but exercising deprecated APIs -QT_WARNING_PUSH -QT_WARNING_DISABLE_DEPRECATED - stlIt = map.begin(); - QCOMPARE(stlIt.value(), QLatin1String("Teststring 1")); - - stlIt += 5; - QCOMPARE(stlIt.value(), QLatin1String("Teststring 6")); - - stlIt++; - QCOMPARE(stlIt.value(), QLatin1String("Teststring 7")); - - stlIt = stlIt - 3; - QCOMPARE(stlIt.value(), QLatin1String("Teststring 4")); - - stlIt--; - QCOMPARE(stlIt.value(), QLatin1String("Teststring 3")); - - for(stlIt = map.begin(), i = 1; stlIt != map.end(); ++stlIt, ++i) - QVERIFY(stlIt.value() == testString.arg(i)); - QCOMPARE(i, 100); -QT_WARNING_POP - //STL-Style const-iterators QMap<int, QString>::const_iterator cstlIt = map.constBegin(); @@ -1173,29 +1149,6 @@ QT_WARNING_POP QVERIFY(cstlIt.value() == testString.arg(i)); QCOMPARE(i, 100); - // Same but exercising deprecated APIs -QT_WARNING_PUSH -QT_WARNING_DISABLE_DEPRECATED - cstlIt = map.constBegin(); - QCOMPARE(cstlIt.value(), QLatin1String("Teststring 1")); - - cstlIt += 5; - QCOMPARE(cstlIt.value(), QLatin1String("Teststring 6")); - - cstlIt++; - QCOMPARE(cstlIt.value(), QLatin1String("Teststring 7")); - - cstlIt = cstlIt - 3; - QCOMPARE(cstlIt.value(), QLatin1String("Teststring 4")); - - cstlIt--; - QCOMPARE(cstlIt.value(), QLatin1String("Teststring 3")); - - for(cstlIt = map.constBegin(), i = 1; cstlIt != map.constEnd(); ++cstlIt, ++i) - QVERIFY(cstlIt.value() == testString.arg(i)); - QCOMPARE(i, 100); -QT_WARNING_POP - //Java-Style iterators QMapIterator<int, QString> javaIt(map); @@ -1648,26 +1601,26 @@ void tst_QMap::qmultimap_specific() } QVERIFY(map1.contains(9, 99)); - QCOMPARE(map1.count(), 45); + QCOMPARE(map1.size(), 45); map1.remove(9, 99); QVERIFY(!map1.contains(9, 99)); - QCOMPARE(map1.count(), 44); + QCOMPARE(map1.size(), 44); map1.remove(9, 99); QVERIFY(!map1.contains(9, 99)); - QCOMPARE(map1.count(), 44); + QCOMPARE(map1.size(), 44); map1.remove(1, 99); - QCOMPARE(map1.count(), 44); + QCOMPARE(map1.size(), 44); map1.insert(1, 99); map1.insert(1, 99); - QCOMPARE(map1.count(), 46); + QCOMPARE(map1.size(), 46); map1.remove(1, 99); - QCOMPARE(map1.count(), 44); + QCOMPARE(map1.size(), 44); map1.remove(1, 99); - QCOMPARE(map1.count(), 44); + QCOMPARE(map1.size(), 44); { QMultiMap<int, int>::const_iterator i = map1.constFind(1, 11); @@ -1726,7 +1679,7 @@ void tst_QMap::qmultimap_specific() map2.insert(42, 1); map2.insert(10, 2); map2.insert(48, 3); - QCOMPARE(map1.count(), map2.count()); + QCOMPARE(map1.size(), map2.size()); QVERIFY(map1.remove(42,5)); QVERIFY(map2.remove(42,5)); QVERIFY(map1 == map2); @@ -1848,7 +1801,7 @@ void tst_QMap::equal_range() QCOMPARE(cresult.first, cmap.find(2)); QCOMPARE(cresult.second, cmap.find(4)); - map.insertMulti(1, "another one"); + map.insert(1, "another one"); result = map.equal_range(1); QCOMPARE(result.first, map.find(1)); @@ -1861,19 +1814,13 @@ void tst_QMap::equal_range() QCOMPARE(map.count(1), 2); } -template <class T> -const T &const_(const T &t) -{ - return t; -} - void tst_QMap::insert() { QMap<QString, float> map; map.insert("cs/key1", 1); map.insert("cs/key2", 2); map.insert("cs/key1", 3); - QCOMPARE(map.count(), 2); + QCOMPARE(map.size(), 2); QMap<int, int> intMap; for (int i = 0; i < 1000; ++i) { @@ -1944,12 +1891,15 @@ void testDetachWhenInsert() dest.insert(3, 3); Map<int, int> destCopy = dest; - dest.insert(source); + if constexpr (std::is_same_v<decltype(dest), QMap<int, int>>) + dest.insert(source); // QMap + else + dest.unite(source); // QMultiMap QCOMPARE(source, referenceSource); QCOMPARE(dest, referenceDestination); - QCOMPARE(destCopy.count(), 1); // unchanged + QCOMPARE(destCopy.size(), 1); // unchanged } // copy insertion of shared map @@ -1964,13 +1914,16 @@ void testDetachWhenInsert() dest.insert(3, 3); Map<int, int> destCopy = dest; - dest.insert(source); + if constexpr (std::is_same_v<decltype(dest), QMap<int, int>>) + dest.insert(source); // QMap + else + dest.unite(source); // QMultiMap QCOMPARE(source, referenceSource); QCOMPARE(sourceCopy, referenceSource); QCOMPARE(dest, referenceDestination); - QCOMPARE(destCopy.count(), 1); // unchanged + QCOMPARE(destCopy.size(), 1); // unchanged } // move insertion of non-shared map @@ -1984,10 +1937,13 @@ void testDetachWhenInsert() dest.insert(3, 3); Map<int, int> destCopy = dest; - dest.insert(source); + if constexpr (std::is_same_v<decltype(dest), QMap<int, int>>) + dest.insert(source); // QMap + else + dest.unite(source); // QMultiMap QCOMPARE(dest, referenceDestination); - QCOMPARE(destCopy.count(), 1); // unchanged + QCOMPARE(destCopy.size(), 1); // unchanged } // move insertion of shared map @@ -2002,12 +1958,15 @@ void testDetachWhenInsert() dest.insert(3, 3); Map<int, int> destCopy = dest; - dest.insert(std::move(source)); + if constexpr (std::is_same_v<decltype(dest), QMap<int, int>>) + dest.insert(std::move(source)); // QMap + else + dest.unite(std::move(source)); // QMultiMap QCOMPARE(sourceCopy, referenceSource); QCOMPARE(dest, referenceDestination); - QCOMPARE(destCopy.count(), 1); // unchanged + QCOMPARE(destCopy.size(), 1); // unchanged } }; @@ -2038,7 +1997,7 @@ void tst_QMap::insertMap() map.insert(map2); - QCOMPARE(map.count(), 5); + QCOMPARE(map.size(), 5); for (int i = 0; i < 5; ++i) QCOMPARE(map[i], i); } @@ -2053,7 +2012,7 @@ void tst_QMap::insertMap() map.insert(map2); - QCOMPARE(map.count(), 17); + QCOMPARE(map.size(), 17); for (int i = 0; i < 10; ++i) { // i * 3 == i except for i = 4, 8 QCOMPARE(map[i * 3], (i && i % 4 == 0) ? i - (i / 4) : i); @@ -2075,7 +2034,7 @@ void tst_QMap::insertMap() QMap<int, int> map2; map.insert(map2); - QCOMPARE(map.count(), 1); + QCOMPARE(map.size(), 1); QCOMPARE(map[1], 1); } { @@ -2084,7 +2043,7 @@ void tst_QMap::insertMap() map2.insert(1, 1); map.insert(map2); - QCOMPARE(map.count(), 1); + QCOMPARE(map.size(), 1); QCOMPARE(map[1], 1); QMap<int, int> map3; @@ -2100,7 +2059,7 @@ void tst_QMap::insertMap() // Test inserting into self, nothing should happen map.insert(map); - QCOMPARE(map.count(), 3); + QCOMPARE(map.size(), 3); for (int i = 0; i < 3; ++i) QCOMPARE(map[i], i); } @@ -2118,7 +2077,7 @@ void tst_QMap::insertMap() map.insert(map2); - QCOMPARE(map.count(), 1); + QCOMPARE(map.size(), 1); } testDetachWhenInsert<QMap>(); @@ -2180,7 +2139,7 @@ void tst_QMap::checkMostLeftNode() void tst_QMap::initializerList() { QMap<int, QString> map = {{1, "bar"}, {1, "hello"}, {2, "initializer_list"}}; - QCOMPARE(map.count(), 2); + QCOMPARE(map.size(), 2); QCOMPARE(map[1], QString("hello")); QCOMPARE(map[2], QString("initializer_list")); @@ -2190,9 +2149,9 @@ void tst_QMap::initializerList() // QCOMPARE(stdm[1], QString("bar")); QMultiMap<QString, int> multiMap{{"il", 1}, {"il", 2}, {"il", 3}}; - QCOMPARE(multiMap.count(), 3); + QCOMPARE(multiMap.size(), 3); QList<int> values = multiMap.values("il"); - QCOMPARE(values.count(), 3); + QCOMPARE(values.size(), 3); QMap<int, int> emptyMap{}; QVERIFY(emptyMap.isEmpty()); @@ -2274,48 +2233,48 @@ void tst_QMap::testInsertMultiWithHint() { QMultiMap<int, int> map; - map.insertMulti(map.end(), 64, 65); + map.insert(map.end(), 64, 65); map.insert(128, 129); map.insert(256, 257); sanityCheckTree(map, __LINE__); - map.insertMulti(map.end(), 512, 513); - map.insertMulti(map.end(), 512, 513 * 2); + map.insert(map.end(), 512, 513); + map.insert(map.end(), 512, 513 * 2); sanityCheckTree(map, __LINE__); QCOMPARE(map.size(), 5); - map.insertMulti(map.end(), 256, 258); // wrong hint + map.insert(map.end(), 256, 258); // wrong hint sanityCheckTree(map, __LINE__); QCOMPARE(map.size(), 6); - QMultiMap<int, int>::iterator i = map.insertMulti(map.constBegin(), 256, 259); // wrong hint + QMultiMap<int, int>::iterator i = map.insert(map.constBegin(), 256, 259); // wrong hint sanityCheckTree(map, __LINE__); QCOMPARE(map.size(), 7); - QMultiMap<int, int>::iterator j = map.insertMulti(map.constBegin(), 69, 66); + QMultiMap<int, int>::iterator j = map.insert(map.constBegin(), 69, 66); sanityCheckTree(map, __LINE__); QCOMPARE(map.size(), 8); - j = map.insertMulti(j, 68, 259); + j = map.insert(j, 68, 259); sanityCheckTree(map, __LINE__); QCOMPARE(map.size(), 9); - j = map.insertMulti(j, 67, 67); + j = map.insert(j, 67, 67); sanityCheckTree(map, __LINE__); QCOMPARE(map.size(), 10); - i = map.insertMulti(i, 256, 259); + i = map.insert(i, 256, 259); sanityCheckTree(map, __LINE__); QCOMPARE(map.size(), 11); - i = map.insertMulti(i, 256, 260); + i = map.insert(i, 256, 260); sanityCheckTree(map, __LINE__); QCOMPARE(map.size(), 12); - map.insertMulti(i, 64, 67); + map.insert(i, 64, 67); sanityCheckTree(map, __LINE__); QCOMPARE(map.size(), 13); - map.insertMulti(map.constBegin(), 20, 20); + map.insert(map.constBegin(), 20, 20); sanityCheckTree(map, __LINE__); QCOMPARE(map.size(), 14); } @@ -2324,9 +2283,9 @@ void tst_QMap::eraseValidIteratorOnSharedMap() { QMultiMap<int, int> a, b; a.insert(10, 10); - a.insertMulti(10, 40); - a.insertMulti(10, 25); - a.insertMulti(10, 30); + a.insert(10, 40); + a.insert(10, 25); + a.insert(10, 30); a.insert(20, 20); QMultiMap<int, int>::iterator i = a.begin(); @@ -2351,8 +2310,8 @@ void tst_QMap::eraseValidIteratorOnSharedMap() // Border cases QMultiMap <QString, QString> ms1, ms2, ms3; ms1.insert("foo", "bar"); - ms1.insertMulti("foo", "quux"); - ms1.insertMulti("foo", "bar"); + ms1.insert("foo", "quux"); + ms1.insert("foo", "bar"); QMultiMap <QString, QString>::iterator si = ms1.begin(); ms2 = ms1; @@ -2601,5 +2560,90 @@ void tst_QMap::toStdMap() toStdMapTestMethod<QMultiMap<int, QString>>(expectedMultiMap); } +void tst_QMap::multiMapStoresInReverseInsertionOrder() +{ + const QString strings[] = { + u"zero"_s, + u"null"_s, + u"nada"_s, + }; + { + QMultiMap<int, QString> map; + for (const QString &string : strings) + map.insert(0, string); + auto printOnFailure = qScopeGuard([&] { qDebug() << map; }); + QVERIFY(std::equal(map.begin(), map.end(), + std::rbegin(strings), std::rend(strings))); + printOnFailure.dismiss(); + } +} + +#if QT_DEPRECATED_SINCE(6, 0) +void tst_QMap::deprecatedInsertMulti() +{ + QMultiMap<int, QString> referenceMap; + referenceMap.insert(1, "value1"); + referenceMap.insert(2, "value2"); + referenceMap.insert(3, "value3"); + referenceMap.insert(1, "value1_2"); + referenceMap.insert(referenceMap.find(2), 2, "value2_2"); + referenceMap.insert(referenceMap.end(), 1, "value1_3"); + + QMultiMap<int, QString> deprecatedMap; +QT_WARNING_PUSH QT_WARNING_DISABLE_DEPRECATED + deprecatedMap.insertMulti(1, "value1"); + deprecatedMap.insertMulti(2, "value2"); + deprecatedMap.insertMulti(3, "value3"); + deprecatedMap.insertMulti(1, "value1_2"); + deprecatedMap.insertMulti(deprecatedMap.find(2), 2, "value2_2"); + deprecatedMap.insertMulti(deprecatedMap.end(), 1, "value1_3"); +QT_WARNING_POP + + QCOMPARE(deprecatedMap, referenceMap); +} + +void tst_QMap::deprecatedIteratorApis() +{ + QMap<int, QString> map; + QString testString = "Teststring %1"; + for (int i = 1; i < 100; ++i) + map.insert(i, testString.arg(i)); + + auto it = map.begin(); + QCOMPARE(it.value(), QLatin1String("Teststring 1")); + QT_IGNORE_DEPRECATIONS(it += 5;) + QCOMPARE(it.value(), QLatin1String("Teststring 6")); + QT_IGNORE_DEPRECATIONS(it = it - 3;) + QCOMPARE(it.value(), QLatin1String("Teststring 3")); + + auto cit = map.constBegin(); + QCOMPARE(cit.value(), QLatin1String("Teststring 1")); + QT_IGNORE_DEPRECATIONS(cit += 5;) + QCOMPARE(cit.value(), QLatin1String("Teststring 6")); + QT_IGNORE_DEPRECATIONS(cit = cit - 3;) + QCOMPARE(cit.value(), QLatin1String("Teststring 3")); +} + +void tst_QMap::deprecatedInsert() +{ + QMultiMap<int, QString> refMap; + refMap.insert(1, "value1"); + refMap.insert(2, "value2"); + refMap.insert(3, "value3"); + + QMultiMap<int, QString> depMap = refMap; + + QMultiMap<int, QString> otherMap; + otherMap.insert(1, "value1_2"); + otherMap.insert(3, "value3_2"); + otherMap.insert(4, "value4"); + + refMap.unite(otherMap); + QT_IGNORE_DEPRECATIONS(depMap.insert(otherMap);) + + QCOMPARE(refMap, depMap); +} +#endif // QT_DEPRECATED_SINCE(6, 0) + QTEST_APPLESS_MAIN(tst_QMap) #include "tst_qmap.moc" |