diff options
author | Samuel Gaist <samuel.gaist@edeltech.ch> | 2016-11-29 11:06:24 +0100 |
---|---|---|
committer | Samuel Gaist <samuel.gaist@edeltech.ch> | 2017-05-05 14:25:56 +0000 |
commit | ae292be8690109940fdd9862321b2389a0957404 (patch) | |
tree | 594a2391a33ba8361efc914d3cf4c0a867d10a81 /tests/auto/corelib/tools/qmap | |
parent | c2c50268912bc49d104a8d04a54f2e9d5e35d6f8 (diff) |
Implement key-value iterator
This patch implements an iterator that returns a pair containing both the
key and the value of an entry in QHash/QMap.
[ChangeLog][QtCore][Containers] Added an stl-like iterator to go through
QHash/QMap returning both the key and the value of the element pointed to.
That lets QHash/QMap interoperate better with stl's algorithms like
std::set_union.
Change-Id: Idbf8a8581510b3493648c34ab04c556de9fa4aa7
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Diffstat (limited to 'tests/auto/corelib/tools/qmap')
-rw-r--r-- | tests/auto/corelib/tools/qmap/tst_qmap.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/tests/auto/corelib/tools/qmap/tst_qmap.cpp b/tests/auto/corelib/tools/qmap/tst_qmap.cpp index f42ffc0471..b39444e76f 100644 --- a/tests/auto/corelib/tools/qmap/tst_qmap.cpp +++ b/tests/auto/corelib/tools/qmap/tst_qmap.cpp @@ -61,6 +61,7 @@ private slots: void iterators(); void keyIterator(); + void keyValueIterator(); void keys_values_uniqueKeys(); void qmultimap_specific(); @@ -863,6 +864,59 @@ void tst_QMap::keyIterator() Q_STATIC_ASSERT(std::is_default_constructible<keyIterator>::value); } +void tst_QMap::keyValueIterator() +{ + QMap<int, int> map; + typedef QMap<int, int>::const_key_value_iterator::value_type entry_type; + + for (int i = 0; i < 100; ++i) + map.insert(i, i * 100); + + auto key_value_it = map.constKeyValueBegin(); + auto it = map.cbegin(); + + for (int i = 0; i < map.size(); ++i) { + QVERIFY(key_value_it != map.constKeyValueEnd()); + QVERIFY(it != map.cend()); + + entry_type pair(it.key(), it.value()); + QCOMPARE(*key_value_it, pair); + ++key_value_it; + ++it; + } + + QVERIFY(key_value_it == map.constKeyValueEnd()); + QVERIFY(it == map.cend()); + + int key = 50; + int value = 50 * 100; + entry_type pair(key, value); + key_value_it = std::find(map.constKeyValueBegin(), map.constKeyValueEnd(), pair); + it = std::find(map.cbegin(), map.cend(), value); + + QVERIFY(key_value_it != map.constKeyValueEnd()); + QCOMPARE(*key_value_it, entry_type(it.key(), it.value())); + + ++it; + ++key_value_it; + QCOMPARE(*key_value_it, entry_type(it.key(), it.value())); + + --it; + --key_value_it; + QCOMPARE(*key_value_it, entry_type(it.key(), it.value())); + + ++it; + ++key_value_it; + QCOMPARE(*key_value_it, entry_type(it.key(), it.value())); + + --it; + --key_value_it; + QCOMPARE(*key_value_it, entry_type(it.key(), it.value())); + key = 99; + value = 99 * 100; + QCOMPARE(std::count(map.constKeyValueBegin(), map.constKeyValueEnd(), entry_type(key, value)), 1); +} + void tst_QMap::keys_values_uniqueKeys() { QMap<QString, int> map; |