summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools
diff options
context:
space:
mode:
authorSérgio Martins <sergio.martins@kdab.com>2016-02-07 02:49:33 +0000
committerMarc Mutz <marc.mutz@kdab.com>2016-02-14 06:43:52 +0000
commit6139fbeb5f41843e524ec40ce5be57f0df23e922 (patch)
treeaef02a79ced348171b165830d653b26fef7cc89a /tests/auto/corelib/tools
parent26dca142a75e71baad26583cd467d07e3b116e59 (diff)
Introduce QHash::equal_range()
Similar to QMap::equal_range(). Will allow to easily fix inefficient code such as: foreach (auto value, hash.values(key)) { ... } [ChangeLog][QtCore][QHash] Added QHash::equal_range() Change-Id: I6e19e25de632e897ad83d3141d9d07f0313f7200 Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Diffstat (limited to 'tests/auto/corelib/tools')
-rw-r--r--tests/auto/corelib/tools/qhash/tst_qhash.cpp108
1 files changed, 108 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qhash/tst_qhash.cpp b/tests/auto/corelib/tools/qhash/tst_qhash.cpp
index c62943febc..4336d02b2c 100644
--- a/tests/auto/corelib/tools/qhash/tst_qhash.cpp
+++ b/tests/auto/corelib/tools/qhash/tst_qhash.cpp
@@ -32,6 +32,7 @@
#include <qmap.h>
#include <algorithm>
+#include <vector>
class tst_QHash : public QObject
{
@@ -65,6 +66,7 @@ private slots:
void twoArguments_qHash();
void initializerList();
void eraseValidIteratorOnSharedHash();
+ void equal_range();
};
struct IdentityTracker {
@@ -1355,5 +1357,111 @@ void tst_QHash::eraseValidIteratorOnSharedHash()
QCOMPARE(itemsWith10, 3);
}
+void tst_QHash::equal_range()
+{
+ QHash<int, QString> hash;
+
+ auto result = hash.equal_range(0);
+ QCOMPARE(result.first, hash.end());
+ QCOMPARE(result.second, hash.end());
+
+ hash.insert(1, "one");
+
+ result = hash.equal_range(1);
+
+ QCOMPARE(result.first, hash.find(1));
+ QVERIFY(std::distance(result.first, result.second) == 1);
+
+ QHash<int, int> h1;
+ {
+ auto p = h1.equal_range(0);
+ QVERIFY(p.first == p.second);
+ QVERIFY(p.first == h1.end());
+ }
+
+ h1.insert(1, 2);
+ {
+ auto p1 = h1.equal_range(9);
+ QVERIFY(p1.first == p1.second);
+ QVERIFY(p1.first == h1.end());
+ }
+ {
+ auto p2 = h1.equal_range(1);
+ QVERIFY(p2.first != p2.second);
+ QVERIFY(p2.first == h1.begin());
+ QVERIFY(p2.second == h1.end());
+ }
+
+ QMultiHash<int, int> m1 = h1;
+ m1.insert(1, 0);
+ QCOMPARE(m1.size(), 2);
+ {
+ auto p1 = m1.equal_range(9);
+ QVERIFY(p1.first == p1.second);
+ QVERIFY(p1.first == m1.end());
+ }
+ {
+ auto p2 = m1.equal_range(1);
+ QVERIFY(p2.first != p2.second);
+ QVERIFY(p2.first == m1.begin());
+ QVERIFY(p2.second == m1.end());
+ QCOMPARE(std::distance(p2.first, p2.second), 2);
+ }
+
+ m1.insert(0, 0);
+ QCOMPARE(m1.size(), 3);
+ {
+ auto p1 = m1.equal_range(9);
+ QVERIFY(p1.first == p1.second);
+ QVERIFY(p1.first == m1.end());
+ }
+ {
+ const auto p2 = m1.equal_range(1);
+ QVERIFY(p2.first != p2.second);
+ QCOMPARE(p2.first.key(), 1);
+ QCOMPARE(std::distance(p2.first, p2.second), 2);
+ QVERIFY(p2.first == m1.begin() || p2.second == m1.end());
+ }
+
+ const QHash<int, int> ch1 = h1;
+ {
+ auto p1 = ch1.equal_range(9);
+ QVERIFY(p1.first == p1.second);
+ QVERIFY(p1.first == ch1.end());
+ }
+ {
+ auto p2 = ch1.equal_range(1);
+ QVERIFY(p2.first != p2.second);
+ QVERIFY(p2.first == ch1.begin());
+ QVERIFY(p2.second == ch1.end());
+ }
+
+ const QMultiHash<int, int> cm1 = m1;
+ {
+ auto p1 = cm1.equal_range(9);
+ QVERIFY(p1.first == p1.second);
+ QVERIFY(p1.first == cm1.end());
+ }
+ {
+ auto p2 = cm1.equal_range(1);
+ QVERIFY(p2.first != p2.second);
+ QCOMPARE(std::distance(p2.first, p2.second), 2);
+ QVERIFY(p2.first == cm1.cbegin() || p2.second == cm1.cend());
+ }
+
+ QHash<int, int> h2;
+ for (int i = 0; i < 8; ++i)
+ for (int j = 0; j < 8; ++j)
+ h2.insertMulti(i, i*j);
+
+ for (int i = 0; i < 8; ++i) {
+ auto pair = h2.equal_range(i);
+ std::vector<int> vec(pair.first, pair.second);
+ std::sort(vec.begin(), vec.end());
+ for (int j = 0; j < 8; ++j)
+ QCOMPARE(i*j, vec[j]);
+ }
+}
+
QTEST_APPLESS_MAIN(tst_QHash)
#include "tst_qhash.moc"