summaryrefslogtreecommitdiffstats
path: root/tests/auto/corelib/tools/qhash/tst_qhash.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'tests/auto/corelib/tools/qhash/tst_qhash.cpp')
-rw-r--r--tests/auto/corelib/tools/qhash/tst_qhash.cpp34
1 files changed, 34 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qhash/tst_qhash.cpp b/tests/auto/corelib/tools/qhash/tst_qhash.cpp
index 77baed87c2..7e8fc234de 100644
--- a/tests/auto/corelib/tools/qhash/tst_qhash.cpp
+++ b/tests/auto/corelib/tools/qhash/tst_qhash.cpp
@@ -84,6 +84,14 @@ private slots:
void eraseValidIteratorOnSharedHash();
};
+struct IdentityTracker {
+ int value, id;
+};
+
+inline uint qHash(IdentityTracker key) { return qHash(key.value); }
+inline bool operator==(IdentityTracker lhs, IdentityTracker rhs) { return lhs.value == rhs.value; }
+
+
struct Foo {
static int count;
Foo():c(count) { ++count; }
@@ -443,6 +451,32 @@ void tst_QHash::insert1()
QVERIFY(((const QHash<int,int*>*) &hash)->operator[](7) == 0);
}
}
+ {
+ QHash<IdentityTracker, int> hash;
+ QCOMPARE(hash.size(), 0);
+ const int dummy = -1;
+ IdentityTracker id00 = {0, 0}, id01 = {0, 1}, searchKey = {0, dummy};
+ QCOMPARE(hash.insert(id00, id00.id).key().id, id00.id);
+ QCOMPARE(hash.size(), 1);
+ QCOMPARE(hash.insert(id01, id01.id).key().id, id00.id); // first key inserted is kept
+ QCOMPARE(hash.size(), 1);
+ QCOMPARE(hash.find(searchKey).value(), id01.id); // last-inserted value
+ QCOMPARE(hash.find(searchKey).key().id, id00.id); // but first-inserted key
+ }
+ {
+ QMultiHash<IdentityTracker, int> hash;
+ QCOMPARE(hash.size(), 0);
+ const int dummy = -1;
+ IdentityTracker id00 = {0, 0}, id01 = {0, 1}, searchKey = {0, dummy};
+ QCOMPARE(hash.insert(id00, id00.id).key().id, id00.id);
+ QCOMPARE(hash.size(), 1);
+ QCOMPARE(hash.insert(id01, id01.id).key().id, id01.id);
+ QCOMPARE(hash.size(), 2);
+ QMultiHash<IdentityTracker, int>::const_iterator pos = hash.constFind(searchKey);
+ QCOMPARE(pos.value(), pos.key().id); // key fits to value it was inserted with
+ ++pos;
+ QCOMPARE(pos.value(), pos.key().id); // key fits to value it was inserted with
+ }
}
void tst_QHash::erase()