From a744c08f422574fcb73406a88a20fdc1ea52a814 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jan=20Kundr=C3=A1t?= Date: Wed, 19 Oct 2016 00:24:33 +0200 Subject: [PATCH] Check that QaimDfsIterator's operator++ is reversible Change-Id: I71690c6e2c5d678787686f260517253c8bb65ff1 --- src/UiUtils/QaimDfsIterator.cpp | 9 +++++++++ src/UiUtils/QaimDfsIterator.h | 1 + tests/Misc/test_QaimDfsIterator.cpp | 13 +++++++++++-- 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/src/UiUtils/QaimDfsIterator.cpp b/src/UiUtils/QaimDfsIterator.cpp index fdc0beea..913c3a89 100644 --- a/src/UiUtils/QaimDfsIterator.cpp +++ b/src/UiUtils/QaimDfsIterator.cpp @@ -118,6 +118,15 @@ const QModelIndex *QaimDfsIterator::operator->() const return &m_current; } +bool QaimDfsIterator::operator==(const QaimDfsIterator &other) +{ + if (m_model) { + return m_current == other.m_current; + } else { + return !other.m_model || !other.m_current.isValid(); + } +} + bool QaimDfsIterator::operator!=(const QaimDfsIterator &other) { return m_current != other.m_current; diff --git a/src/UiUtils/QaimDfsIterator.h b/src/UiUtils/QaimDfsIterator.h index 628d816c..6cab726d 100644 --- a/src/UiUtils/QaimDfsIterator.h +++ b/src/UiUtils/QaimDfsIterator.h @@ -39,6 +39,7 @@ public: QaimDfsIterator & operator--(); const QModelIndex &operator*() const; const QModelIndex *operator->() const; + bool operator==(const QaimDfsIterator &other); bool operator!=(const QaimDfsIterator &other); private: QModelIndex m_current; diff --git a/tests/Misc/test_QaimDfsIterator.cpp b/tests/Misc/test_QaimDfsIterator.cpp index 31118bb9..4aa18901 100644 --- a/tests/Misc/test_QaimDfsIterator.cpp +++ b/tests/Misc/test_QaimDfsIterator.cpp @@ -37,10 +37,17 @@ void TestQaimDfsIterator::testQaimDfsIterator() std::transform(begin, end, std::back_inserter(buf), [](const QModelIndex &what) -> QString { return what.data().toString(); - } - ); + }); QCOMPARE(buf.join(QLatin1Char(' ')), order); + // Check that operator++ can be reversed (while it points to a valid element or to end) + for (auto it = begin; it != end; ++it) { + auto another = it; + ++it; + --it; + QVERIFY(it == another); + } + if (begin != end) { // check iteration in reverse QStringList reversed; @@ -63,7 +70,9 @@ void TestQaimDfsIterator::testQaimDfsIterator() auto it = end; --it; ++it; + QVERIFY(it == end); QVERIFY(!(it != end)); + QVERIFY(it == begin); QVERIFY(!(it != begin)); } -- 2.11.4.GIT