summaryrefslogtreecommitdiffstats
path: root/tests/auto
diff options
context:
space:
mode:
authorMarc Mutz <marc.mutz@kdab.com>2014-07-25 14:50:55 +0200
committerMarc Mutz <marc.mutz@kdab.com>2014-08-05 00:02:26 +0200
commite1fed5dc3156f32f22d78dc57fa5ab8fcedaa804 (patch)
tree6bc45492ed29acc5255fc85d2b31720500a414bd /tests/auto
parent85011b82f08e0be09012d0e766f5550e262ec594 (diff)
tst_QSet: check which of several equal elements is inserted
Add a test that checks that QSet keeps the first of the elements that have equal value. This is documented, but inconsistent with values in a QHash, which keeps the last element with equal key. Document this as a test. That way, we'll be informed when the behavior changes (e.g. by a port to std::unordered_set). Change-Id: I4ca1718bb86599b925b3ccd13b0856917cd4ce67 Reviewed-by: Olivier Goffart <ogoffart@woboq.com>
Diffstat (limited to 'tests/auto')
-rw-r--r--tests/auto/corelib/tools/qset/tst_qset.cpp26
1 files changed, 26 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qset/tst_qset.cpp b/tests/auto/corelib/tools/qset/tst_qset.cpp
index 5ef1b44b6f..11873a7661 100644
--- a/tests/auto/corelib/tools/qset/tst_qset.cpp
+++ b/tests/auto/corelib/tools/qset/tst_qset.cpp
@@ -82,6 +82,13 @@ private slots:
void initializerList();
};
+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; }
+
void tst_QSet::operator_eq()
{
{
@@ -530,6 +537,18 @@ void tst_QSet::insert()
QVERIFY(set1.size() == 2);
QVERIFY(set1.contains(2));
}
+
+ {
+ QSet<IdentityTracker> set;
+ QCOMPARE(set.size(), 0);
+ const int dummy = -1;
+ IdentityTracker id00 = {0, 0}, id01 = {0, 1}, searchKey = {0, dummy};
+ QCOMPARE(set.insert(id00)->id, id00.id);
+ QCOMPARE(set.size(), 1);
+ QCOMPARE(set.insert(id01)->id, id00.id); // first inserted is kept
+ QCOMPARE(set.size(), 1);
+ QCOMPARE(set.find(searchKey)->id, id00.id);
+ }
}
void tst_QSet::setOperations()
@@ -930,6 +949,13 @@ void tst_QSet::initializerList()
QVERIFY(set.contains(4));
QVERIFY(set.contains(5));
+ // check _which_ of the equal elements gets inserted (in the QHash/QMap case, it's the last):
+ const QSet<IdentityTracker> set2 = {{1, 0}, {1, 1}, {2, 2}, {3, 3}, {4, 4}, {5, 5}};
+ QCOMPARE(set2.count(), 5);
+ const int dummy = -1;
+ const IdentityTracker searchKey = {1, dummy};
+ QCOMPARE(set2.find(searchKey)->id, 0);
+
QSet<int> emptySet{};
QVERIFY(emptySet.isEmpty());