diff options
author | Sérgio Martins <sergio.martins@kdab.com> | 2015-06-22 17:21:34 +0100 |
---|---|---|
committer | Sérgio Martins <sergio.martins@kdab.com> | 2015-07-28 19:11:17 +0000 |
commit | 4bced6e7a6928137bb7ac965d2b07223880a3e39 (patch) | |
tree | 4ac4b404574c671286ebae8fdad3e5d0b0b60120 /tests | |
parent | 07f27fcf6d4f046bbea6c3155efa78128e164da4 (diff) |
Introduce QHash key iterators
So we can have interoperability with algorithms.
Motivated by inefficient code like qDeleteAll(hash.keys())
[ChangeLog][QtCore][QHash] Added key iterators, accessible through
keyBegin() and keyEnd().
Change-Id: I1f9db8a7a4294e1556cbb50b8fe5ebdcf0dc29a2
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/auto/corelib/tools/qhash/tst_qhash.cpp | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qhash/tst_qhash.cpp b/tests/auto/corelib/tools/qhash/tst_qhash.cpp index 6ea33fb37f..6a5c6b5670 100644 --- a/tests/auto/corelib/tools/qhash/tst_qhash.cpp +++ b/tests/auto/corelib/tools/qhash/tst_qhash.cpp @@ -62,6 +62,7 @@ private slots: void compare(); void compare2(); void iterators(); // sligthly modified from tst_QMap + void keyIterator(); void keys_values_uniqueKeys(); // slightly modified from tst_QMap void noNeedlessRehashes(); @@ -965,6 +966,34 @@ void tst_QHash::iterators() } } +void tst_QHash::keyIterator() +{ + QHash<int, int> hash; + + for (int i = 0; i < 100; ++i) + hash.insert(i, i*100); + + QHash<int, int>::key_iterator key_it = hash.keyBegin(); + QHash<int, int>::const_iterator it = hash.cbegin(); + for (int i = 0; i < 100; ++i) { + QCOMPARE(*key_it, it.key()); + key_it++; + it++; + } + + key_it = std::find(hash.keyBegin(), hash.keyEnd(), 50); + it = std::find(hash.cbegin(), hash.cend(), 50 * 100); + + QVERIFY(key_it != hash.keyEnd()); + QCOMPARE(*key_it, it.key()); + QCOMPARE(*(key_it++), (it++).key()); + QCOMPARE(*(key_it--), (it--).key()); + QCOMPARE(*(++key_it), (++it).key()); + QCOMPARE(*(--key_it), (--it).key()); + + QCOMPARE(std::count(hash.keyBegin(), hash.keyEnd(), 99), 1); +} + void tst_QHash::rehash_isnt_quadratic() { // this test should be incredibly slow if rehash() is quadratic |