From 0b621e71c55e30954419080014454642285353d4 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sun, 6 Aug 2017 16:58:22 +0200 Subject: [PATCH] Library: fix a suptle misconception in the design of IterAdapter again surprising how such fundamental bugs can hide for years... Here the reason is that IterAdapter leaves the representation of "NIL" to its instantiation / users; some users (here in for example the ScopedCollection) can choose to allow for different representations of "NIL", but the comparison provided by IterAdapter just compares the embedded pos by face value. --- src/lib/iter-adapter.hpp | 4 ++- src/lib/scoped-collection.hpp | 4 +-- tests/library/iter-adapter-test.cpp | 33 +++++++++++++++++++++++- tests/library/scoped-collection-test.cpp | 15 +++++++++++ 4 files changed, 52 insertions(+), 4 deletions(-) diff --git a/src/lib/iter-adapter.hpp b/src/lib/iter-adapter.hpp index 5a9014425..168595ce9 100644 --- a/src/lib/iter-adapter.hpp +++ b/src/lib/iter-adapter.hpp @@ -240,7 +240,9 @@ namespace lib { { if (check()) iterNext (source_,pos_); // extension point: free function iterNext(...) - } + check(); + } // checkPoint() might mark end condition + // for comparison with IterAdapter{} private: diff --git a/src/lib/scoped-collection.hpp b/src/lib/scoped-collection.hpp index 6de7e52a6..b4662c48b 100644 --- a/src/lib/scoped-collection.hpp +++ b/src/lib/scoped-collection.hpp @@ -470,8 +470,8 @@ namespace lib { - typedef IterAdapter< I *, const ScopedCollection *> iterator; - typedef IterAdapter const_iterator; + using iterator = IterAdapter< I *, const ScopedCollection *>; + using const_iterator = IterAdapter; iterator begin() { return iterator (this, _access_begin()); } diff --git a/tests/library/iter-adapter-test.cpp b/tests/library/iter-adapter-test.cpp index b60f268cc..d783a7da0 100644 --- a/tests/library/iter-adapter-test.cpp +++ b/tests/library/iter-adapter-test.cpp @@ -78,7 +78,6 @@ namespace test{ iterator end() { return iterator(); } const_iterator begin() const { return const_iterator(data_.begin(),data_.end()); } const_iterator end() const { return const_iterator(); } - }; @@ -130,6 +129,8 @@ namespace test{ iterator end () { return iterator(); } const_iterator end () const { return const_iterator(); } + size_t size() const { return numberz_.size(); } + protected: /* ==== API for the IterAdapter ==== */ @@ -351,6 +352,36 @@ namespace test{ // *iter = i+1; ///////////TODO this should be const, but it isn't } + + + //---- verify support for C++11 element iteration + i = 0; + for (auto& elm : elms) // NOTE: TestContainer exposes pointers + { + ++elm; // can indeed modify contents + --elm; + CHECK (*elm == i); + ++i; + } + CHECK (size_t(i) == elms.size()); + + i = 0; + for (auto const& elm : elms) + { + CHECK (*elm == i); + // ++elm; // can not modify contents + ++i; + } + CHECK (size_t(i) == elms.size()); + + i = 0; + for (auto const& elm : const_elms) + { + CHECK (*elm == i); + // ++elm; // can not modify contents + ++i; + } + CHECK (size_t(i) == elms.size()); } diff --git a/tests/library/scoped-collection-test.cpp b/tests/library/scoped-collection-test.cpp index 0e1c5e707..48735dc94 100644 --- a/tests/library/scoped-collection-test.cpp +++ b/tests/library/scoped-collection-test.cpp @@ -182,6 +182,21 @@ namespace test{ } + // Test c++11 foreach iteration + check = 0; + for (auto& entry : coll) + { + CHECK (check == entry.getVal()); + ++check; + } + check = 0; + for (auto const& entry : const_coll) + { + CHECK (check == entry.getVal()); + ++check; + } + + // Verify correct behaviour of iteration end CHECK (! (coll.end())); CHECK (isnil (coll.end()));