From 07f27fcf6d4f046bbea6c3155efa78128e164da4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9rgio=20Martins?= Date: Fri, 24 Jul 2015 20:30:01 +0100 Subject: Introduce QMap key iterators So we can have interoperability with algorithms. Motivated by inefficient code like qDeleteAll(map.keys()) [ChangeLog][QtCore][QMap] Added key iterators, accessible through keyBegin() and keyEnd(). Change-Id: Ieee2f9ad031e9d1e845a71447746699bbe95b96c Reviewed-by: Marc Mutz --- tests/auto/corelib/tools/qmap/tst_qmap.cpp | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'tests/auto/corelib/tools/qmap/tst_qmap.cpp') diff --git a/tests/auto/corelib/tools/qmap/tst_qmap.cpp b/tests/auto/corelib/tools/qmap/tst_qmap.cpp index 619f04337c..bb6535b635 100644 --- a/tests/auto/corelib/tools/qmap/tst_qmap.cpp +++ b/tests/auto/corelib/tools/qmap/tst_qmap.cpp @@ -65,6 +65,7 @@ private slots: void take(); void iterators(); + void keyIterator(); void keys_values_uniqueKeys(); void qmultimap_specific(); @@ -835,6 +836,34 @@ void tst_QMap::iterators() } } +void tst_QMap::keyIterator() +{ + QMap map; + + for (int i = 0; i < 100; ++i) + map.insert(i, i*100); + + QMap::key_iterator key_it = map.keyBegin(); + QMap::const_iterator it = map.cbegin(); + for (int i = 0; i < 100; ++i) { + QCOMPARE(*key_it, it.key()); + ++key_it; + ++it; + } + + key_it = std::find(map.keyBegin(), map.keyEnd(), 50); + it = std::find(map.cbegin(), map.cend(), 50 * 100); + + QVERIFY(key_it != map.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(map.keyBegin(), map.keyEnd(), 99), 1); +} + void tst_QMap::keys_values_uniqueKeys() { QMap map; -- cgit v1.2.3