summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2021-07-07 14:22:13 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-07-08 18:41:58 +0000
commit946e379b85efe264d13549389d6f68811da201d8 (patch)
tree45bd54c0b6a9c4f8d29887f22292901e385e233c /tests/auto/corelib/tools
parentd2546c4daf8cc57d25521d1d60148248493162fd (diff)
Fix QMultiHash::keys(const T&) overload
The method was never tested, but it failed to compile after QMultiHash was introduced as a separate class in 6.0. This patch fixes it and adds some unit-tests to cover the case. Task-number: QTBUG-91736 Change-Id: I5dd989d4775efc6a9bb13c5ed1d892e499d95dc2 Reviewed-by: Lars Knoll <lars.knoll@qt.io> Reviewed-by: Edward Welbourne <edward.welbourne@qt.io> (cherry picked from commit a8bcf68a5ec91b4ca4209c36310def145c8afe97) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
Diffstat (limited to 'tests/auto/corelib/tools')
-rw-r--r--tests/auto/corelib/tools/qhash/tst_qhash.cpp65
1 files changed, 57 insertions, 8 deletions
diff --git a/tests/auto/corelib/tools/qhash/tst_qhash.cpp b/tests/auto/corelib/tools/qhash/tst_qhash.cpp
index 375f9048c0..cd25f417d9 100644
--- a/tests/auto/corelib/tools/qhash/tst_qhash.cpp
+++ b/tests/auto/corelib/tools/qhash/tst_qhash.cpp
@@ -44,6 +44,7 @@ private slots:
void erase();
void erase_edge_case();
void key();
+ void keys();
void swap();
void count(); // copied from tst_QMap
@@ -620,6 +621,62 @@ void tst_QHash::key()
QCOMPARE(hash2.key("zero"), 0);
QCOMPARE(hash2.key("zero", def), 0);
}
+
+ {
+ const int def = -1;
+ QMultiHash<int, QString> hash;
+ QCOMPARE(hash.key("val"), 0);
+ QCOMPARE(hash.key("val", def), def);
+ QVERIFY(!hash.isDetached());
+
+ hash.insert(1, "value1");
+ hash.insert(1, "value2");
+ hash.insert(2, "value1");
+
+ QCOMPARE(hash.key("value2"), 1);
+ const auto key = hash.key("value1");
+ QVERIFY(key == 1 || key == 2);
+ QCOMPARE(hash.key("value"), 0);
+ QCOMPARE(hash.key("value", def), def);
+ }
+}
+
+template <typename T>
+QList<T> sorted(const QList<T> &list)
+{
+ QList<T> res = list;
+ std::sort(res.begin(), res.end());
+ return res;
+}
+
+void tst_QHash::keys()
+{
+ {
+ QHash<QString, int> hash;
+ QVERIFY(hash.keys().isEmpty());
+ QVERIFY(hash.keys(1).isEmpty());
+ QVERIFY(!hash.isDetached());
+
+ hash.insert("key1", 1);
+ hash.insert("key2", 2);
+ hash.insert("key3", 1);
+
+ QCOMPARE(sorted(hash.keys()), QStringList({ "key1", "key2", "key3" }));
+ QCOMPARE(sorted(hash.keys(1)), QStringList({ "key1", "key3" }));
+ }
+ {
+ QMultiHash<QString, int> hash;
+ QVERIFY(hash.keys().isEmpty());
+ QVERIFY(hash.keys(1).isEmpty());
+ QVERIFY(!hash.isDetached());
+
+ hash.insert("key1", 1);
+ hash.insert("key2", 1);
+ hash.insert("key1", 2);
+
+ QCOMPARE(sorted(hash.keys()), QStringList({ "key1", "key1", "key2" }));
+ QCOMPARE(sorted(hash.keys(1)), QStringList({ "key1", "key2" }));
+ }
}
void tst_QHash::swap()
@@ -1484,14 +1541,6 @@ void tst_QHash::qmultihash_qhash_rvalue_ref_unite()
}
}
-template <typename T>
-QList<T> sorted(const QList<T> &list)
-{
- QList<T> res = list;
- std::sort(res.begin(), res.end());
- return res;
-}
-
void tst_QHash::keys_values_uniqueKeys()
{
QMultiHash<QString, int> hash;