From 0a1256f4e31e1881245f02a0ab26021cd753505c Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Sat, 19 Nov 2011 21:41:50 +0100 Subject: [PATCH] cover and verify detaching from ScopedPtrVect --- src/lib/scoped-ptrvect.hpp | 3 +- tests/lib/scoped-ptrvect-test.cpp | 46 +++++++++++++++++++++++++++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/src/lib/scoped-ptrvect.hpp b/src/lib/scoped-ptrvect.hpp index 8ed96b0db..dd04492a6 100644 --- a/src/lib/scoped-ptrvect.hpp +++ b/src/lib/scoped-ptrvect.hpp @@ -134,6 +134,7 @@ namespace lib { * Otherwise, NULL will be returned and the * collection of managed objects remains unaltered * @note EX_STRONG + * @todo TICKET #856 better return a Maybe instead of a pointer? */ T* detach (void* objAddress) @@ -177,8 +178,8 @@ namespace lib { typedef ConstIterType const_iterator; iterator begin() { return iterator (allPtrs()); } - const_iterator begin() const { return const_iterator::build_by_cast (allPtrs()); } iterator end() { return iterator ( RIter() ); } + const_iterator begin() const { return const_iterator::build_by_cast (allPtrs()); } const_iterator end() const { return const_iterator::nil(); } diff --git a/tests/lib/scoped-ptrvect-test.cpp b/tests/lib/scoped-ptrvect-test.cpp index 48461e56b..63772334b 100644 --- a/tests/lib/scoped-ptrvect-test.cpp +++ b/tests/lib/scoped-ptrvect-test.cpp @@ -43,7 +43,9 @@ namespace test{ /******************************************************************** * @test ScopedPtrVect manages the lifecycle of a number of objects. - * @todo implement detaching of objects + * The API is similar to a vector and allows for element access + * and iteration. Individual elements can be detached and thus + * removed from the responsibility of the container. */ class ScopedPtrVect_test : public Test { @@ -53,7 +55,7 @@ namespace test{ { simpleUsage(); iterating(); -// detaching(); + detaching(); } @@ -91,7 +93,7 @@ namespace test{ holder.manage (new Dummy); holder.manage (new Dummy); CHECK (9 == holder.size()); - CHECK (0!=checksum); + CHECK (0 < checksum); } CHECK (0==checksum); } @@ -143,8 +145,46 @@ namespace test{ CHECK (0==checksum); } + + void + detaching() + { + int id2, id3; + Dummy* extracted(0); + CHECK (0==checksum); + { + VectD holder; + CHECK (0 == checksum); + CHECK (isnil (holder)); + + holder.manage (new Dummy); + holder.manage (new Dummy); + holder.manage (new Dummy); + holder.manage (new Dummy); + holder.manage (new Dummy); + CHECK (5 == holder.size()); + CHECK (0 < checksum); + + id2 = holder[2].getVal(); + id3 = holder[3].getVal(); + + extracted = holder.detach(& holder[2]); + CHECK (id2 == extracted->getVal()); + CHECK (id3 == holder[2].getVal()); + CHECK (4 == holder.size()); + } + CHECK (0 < checksum); // not all dummies are dead + CHECK (id2 == checksum); // #2 is alive! + + extracted->setVal(id2+id3); + CHECK (id2+id3 == checksum); + + delete extracted; + CHECK (0 == checksum); + } }; + LAUNCHER (ScopedPtrVect_test, "unit common");