summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools/qmap/tst_qmap.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/corelib/tools/qmap/tst_qmap.cpp')
-rw-r--r--tests/auto/corelib/tools/qmap/tst_qmap.cpp154
1 files changed, 89 insertions, 65 deletions
diff --git a/tests/auto/corelib/tools/qmap/tst_qmap.cpp b/tests/auto/corelib/tools/qmap/tst_qmap.cpp
index d4f886784d..6950dcf705 100644
--- a/tests/auto/corelib/tools/qmap/tst_qmap.cpp
+++ b/tests/auto/corelib/tools/qmap/tst_qmap.cpp
@@ -1,9 +1,13 @@
// Copyright (C) 2016 The Qt Company Ltd.
-// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0
+// 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
@@ -61,6 +65,8 @@ private slots:
void removeElementsInMap();
void toStdMap();
+ void multiMapStoresInReverseInsertionOrder();
+
// Tests for deprecated APIs.
#if QT_DEPRECATED_SINCE(6, 0)
void deprecatedInsertMulti();
@@ -166,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);
@@ -176,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
@@ -190,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") );
@@ -202,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") );
@@ -210,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
@@ -286,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 );
{
@@ -318,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);
@@ -329,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);
}
@@ -634,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:
@@ -666,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");
@@ -674,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");
@@ -690,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));
}
@@ -1589,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);
@@ -1667,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);
@@ -1802,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) {
@@ -1893,7 +1899,7 @@ void testDetachWhenInsert()
QCOMPARE(source, referenceSource);
QCOMPARE(dest, referenceDestination);
- QCOMPARE(destCopy.count(), 1); // unchanged
+ QCOMPARE(destCopy.size(), 1); // unchanged
}
// copy insertion of shared map
@@ -1917,7 +1923,7 @@ void testDetachWhenInsert()
QCOMPARE(sourceCopy, referenceSource);
QCOMPARE(dest, referenceDestination);
- QCOMPARE(destCopy.count(), 1); // unchanged
+ QCOMPARE(destCopy.size(), 1); // unchanged
}
// move insertion of non-shared map
@@ -1937,7 +1943,7 @@ void testDetachWhenInsert()
dest.unite(source); // QMultiMap
QCOMPARE(dest, referenceDestination);
- QCOMPARE(destCopy.count(), 1); // unchanged
+ QCOMPARE(destCopy.size(), 1); // unchanged
}
// move insertion of shared map
@@ -1960,7 +1966,7 @@ void testDetachWhenInsert()
QCOMPARE(sourceCopy, referenceSource);
QCOMPARE(dest, referenceDestination);
- QCOMPARE(destCopy.count(), 1); // unchanged
+ QCOMPARE(destCopy.size(), 1); // unchanged
}
};
@@ -1991,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);
}
@@ -2006,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);
@@ -2028,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);
}
{
@@ -2037,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;
@@ -2053,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);
}
@@ -2071,7 +2077,7 @@ void tst_QMap::insertMap()
map.insert(map2);
- QCOMPARE(map.count(), 1);
+ QCOMPARE(map.size(), 1);
}
testDetachWhenInsert<QMap>();
@@ -2133,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"));
@@ -2143,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());
@@ -2554,6 +2560,24 @@ 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()
{