2009-02-08 01:31:01 +01:00
|
|
|
|
/*
|
|
|
|
|
|
ScopedPtrVect(Test) - holding and owning a collection of noncopyable objects
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
Copyright (C)
|
|
|
|
|
|
2008, Hermann Vosseler <Ichthyostega@web.de>
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
**Lumiera** is free software; you can redistribute it and/or modify it
|
|
|
|
|
|
under the terms of the GNU General Public License as published by the
|
|
|
|
|
|
Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
|
|
option) any later version. See the file COPYING for further details.
|
2010-12-17 23:28:49 +01:00
|
|
|
|
|
Copyright: clarify and simplify the file headers
* Lumiera source code always was copyrighted by individual contributors
* there is no entity "Lumiera.org" which holds any copyrights
* Lumiera source code is provided under the GPL Version 2+
== Explanations ==
Lumiera as a whole is distributed under Copyleft, GNU General Public License Version 2 or above.
For this to become legally effective, the ''File COPYING in the root directory is sufficient.''
The licensing header in each file is not strictly necessary, yet considered good practice;
attaching a licence notice increases the likeliness that this information is retained
in case someone extracts individual code files. However, it is not by the presence of some
text, that legally binding licensing terms become effective; rather the fact matters that a
given piece of code was provably copyrighted and published under a license. Even reformatting
the code, renaming some variables or deleting parts of the code will not alter this legal
situation, but rather creates a derivative work, which is likewise covered by the GPL!
The most relevant information in the file header is the notice regarding the
time of the first individual copyright claim. By virtue of this initial copyright,
the first author is entitled to choose the terms of licensing. All further
modifications are permitted and covered by the License. The specific wording
or format of the copyright header is not legally relevant, as long as the
intention to publish under the GPL remains clear. The extended wording was
based on a recommendation by the FSF. It can be shortened, because the full terms
of the license are provided alongside the distribution, in the file COPYING.
2024-11-17 23:42:55 +01:00
|
|
|
|
* *****************************************************************/
|
2009-02-08 01:31:01 +01:00
|
|
|
|
|
2017-02-22 01:54:20 +01:00
|
|
|
|
/** @file scoped-ptrvect-test.cpp
|
2017-02-22 03:17:18 +01:00
|
|
|
|
** unit test \ref ScopedPtrVect_test
|
2016-11-03 18:20:10 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
2009-02-08 01:31:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
2009-07-17 20:22:48 +02:00
|
|
|
|
#include "lib/test/test-helper.hpp"
|
2009-02-08 01:31:01 +01:00
|
|
|
|
#include "lib/util.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/scoped-ptrvect.hpp"
|
2024-06-15 18:14:00 +02:00
|
|
|
|
#include "lib/test/tracking-dummy.hpp"
|
2009-02-08 01:31:01 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
2009-07-11 20:06:35 +02:00
|
|
|
|
namespace test{
|
|
|
|
|
|
|
|
|
|
|
|
using ::Test;
|
|
|
|
|
|
using util::isnil;
|
2009-07-17 20:22:48 +02:00
|
|
|
|
using lumiera::error::LUMIERA_ERROR_ITER_EXHAUST;
|
|
|
|
|
|
|
2009-07-11 20:06:35 +02:00
|
|
|
|
|
|
|
|
|
|
typedef ScopedPtrVect<Dummy> VectD;
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-24 23:06:36 +02:00
|
|
|
|
/****************************************************************//**
|
2009-07-11 20:06:35 +02:00
|
|
|
|
* @test ScopedPtrVect manages the lifecycle of a number of objects.
|
2011-11-19 21:41:50 +01:00
|
|
|
|
* 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.
|
2009-07-11 20:06:35 +02:00
|
|
|
|
*/
|
|
|
|
|
|
class ScopedPtrVect_test : public Test
|
|
|
|
|
|
{
|
2009-02-08 01:31:01 +01:00
|
|
|
|
|
2009-07-11 20:06:35 +02:00
|
|
|
|
virtual void
|
|
|
|
|
|
run (Arg)
|
|
|
|
|
|
{
|
|
|
|
|
|
simpleUsage();
|
2009-07-16 19:36:01 +02:00
|
|
|
|
iterating();
|
2011-11-19 21:41:50 +01:00
|
|
|
|
detaching();
|
2025-05-31 19:10:19 +02:00
|
|
|
|
moving();
|
2009-07-11 20:06:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
simpleUsage()
|
|
|
|
|
|
{
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2009-02-08 01:31:01 +01:00
|
|
|
|
{
|
2009-07-11 20:06:35 +02:00
|
|
|
|
VectD holder;
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (isnil (holder));
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2009-07-11 20:06:35 +02:00
|
|
|
|
|
|
|
|
|
|
Dummy* ptr = new Dummy();
|
|
|
|
|
|
Dummy& ref = holder.manage (ptr);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!isnil (holder));
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 != Dummy::checksum());
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (&ref==ptr);
|
2009-07-11 20:06:35 +02:00
|
|
|
|
|
|
|
|
|
|
holder.manage (new Dummy);
|
|
|
|
|
|
holder.manage (new Dummy);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (3 == holder.size());
|
2009-07-11 20:06:35 +02:00
|
|
|
|
|
|
|
|
|
|
holder.clear();
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (isnil (holder));
|
2009-07-11 20:06:35 +02:00
|
|
|
|
|
|
|
|
|
|
holder.manage (new Dummy);
|
|
|
|
|
|
holder.manage (new Dummy);
|
|
|
|
|
|
holder.manage (new Dummy);
|
|
|
|
|
|
holder.manage (new Dummy);
|
|
|
|
|
|
holder.manage (new Dummy);
|
|
|
|
|
|
holder.manage (new Dummy);
|
|
|
|
|
|
holder.manage (new Dummy);
|
|
|
|
|
|
holder.manage (new Dummy);
|
|
|
|
|
|
holder.manage (new Dummy);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (9 == holder.size());
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 < Dummy::checksum());
|
2009-02-08 01:31:01 +01:00
|
|
|
|
}
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2009-07-11 20:06:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2009-07-16 19:36:01 +02:00
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
iterating()
|
|
|
|
|
|
{
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2009-07-16 19:36:01 +02:00
|
|
|
|
{
|
|
|
|
|
|
VectD holder;
|
|
|
|
|
|
for (int i=0; i<16; ++i)
|
|
|
|
|
|
holder.manage(new Dummy(i));
|
|
|
|
|
|
|
|
|
|
|
|
int check=0;
|
|
|
|
|
|
VectD::iterator ii = holder.begin();
|
|
|
|
|
|
while (ii)
|
|
|
|
|
|
{
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (check == ii->getVal());
|
2009-07-16 19:36:01 +02:00
|
|
|
|
++check;
|
|
|
|
|
|
++ii;
|
|
|
|
|
|
}
|
2009-07-17 04:13:14 +02:00
|
|
|
|
|
|
|
|
|
|
|
2009-07-17 20:22:48 +02:00
|
|
|
|
// Test the const iterator
|
|
|
|
|
|
check = 0;
|
2009-07-17 04:13:14 +02:00
|
|
|
|
VectD::const_iterator cii = holder.begin();
|
|
|
|
|
|
while (cii)
|
|
|
|
|
|
{
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (check == cii->getVal());
|
2009-07-17 04:13:14 +02:00
|
|
|
|
++check;
|
|
|
|
|
|
++cii;
|
|
|
|
|
|
}
|
2009-07-17 20:22:48 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Verify correct behaviour of iteration end
|
2025-05-31 01:40:36 +02:00
|
|
|
|
CHECK (not holder.end());
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (isnil (holder.end()));
|
2009-07-17 20:22:48 +02:00
|
|
|
|
|
|
|
|
|
|
VERIFY_ERROR (ITER_EXHAUST, *holder.end() );
|
|
|
|
|
|
VERIFY_ERROR (ITER_EXHAUST, ++holder.end() );
|
|
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (ii == holder.end());
|
|
|
|
|
|
CHECK (cii == holder.end());
|
2009-07-17 20:22:48 +02:00
|
|
|
|
VERIFY_ERROR (ITER_EXHAUST, ++ii );
|
|
|
|
|
|
VERIFY_ERROR (ITER_EXHAUST, ++cii );
|
|
|
|
|
|
|
2009-07-16 19:36:01 +02:00
|
|
|
|
}
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2009-07-16 19:36:01 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
2011-11-19 21:41:50 +01:00
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
detaching()
|
|
|
|
|
|
{
|
|
|
|
|
|
int id2, id3;
|
2025-05-31 19:10:19 +02:00
|
|
|
|
Dummy* extracted{nullptr};
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2011-11-19 21:41:50 +01:00
|
|
|
|
{
|
|
|
|
|
|
VectD holder;
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2011-11-19 21:41:50 +01:00
|
|
|
|
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());
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 < Dummy::checksum());
|
2011-11-19 21:41:50 +01:00
|
|
|
|
|
|
|
|
|
|
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());
|
|
|
|
|
|
}
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 < Dummy::checksum()); // not all dummies are dead
|
|
|
|
|
|
CHECK (id2 == Dummy::checksum()); // #2 is alive!
|
2011-11-19 21:41:50 +01:00
|
|
|
|
|
|
|
|
|
|
extracted->setVal(id2+id3);
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (id2+id3 == Dummy::checksum());
|
2011-11-19 21:41:50 +01:00
|
|
|
|
|
|
|
|
|
|
delete extracted;
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2011-11-19 21:41:50 +01:00
|
|
|
|
}
|
2025-05-31 19:10:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
moving()
|
|
|
|
|
|
{ {
|
|
|
|
|
|
VectD org;
|
|
|
|
|
|
VectD left;
|
|
|
|
|
|
CHECK (0 == Dummy::checksum());
|
|
|
|
|
|
|
|
|
|
|
|
org.manage (new Dummy);
|
|
|
|
|
|
org.manage (new Dummy);
|
|
|
|
|
|
org.manage (new Dummy);
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (not isnil (org));
|
|
|
|
|
|
CHECK ( isnil (left));
|
|
|
|
|
|
auto sum = Dummy::checksum();
|
|
|
|
|
|
CHECK (sum > 0);
|
|
|
|
|
|
int id0 = org[0].getVal(),
|
|
|
|
|
|
id1 = org[1].getVal(),
|
|
|
|
|
|
id2 = org[2].getVal();
|
|
|
|
|
|
|
|
|
|
|
|
// create by move
|
|
|
|
|
|
VectD right{std::move (org)};
|
|
|
|
|
|
CHECK ( isnil (org));
|
|
|
|
|
|
CHECK ( isnil (left));
|
|
|
|
|
|
CHECK (not isnil (right));
|
|
|
|
|
|
CHECK (sum == Dummy::checksum());
|
|
|
|
|
|
|
|
|
|
|
|
// move-assignment
|
|
|
|
|
|
left = std::move (right);
|
|
|
|
|
|
CHECK ( isnil (org));
|
|
|
|
|
|
CHECK (not isnil (left));
|
|
|
|
|
|
CHECK ( isnil (right));
|
|
|
|
|
|
CHECK (sum == Dummy::checksum());
|
|
|
|
|
|
CHECK (id0 == left[0].getVal());
|
|
|
|
|
|
CHECK (id1 == left[1].getVal());
|
|
|
|
|
|
CHECK (id2 == left[2].getVal());
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
CHECK (0 == Dummy::checksum());
|
|
|
|
|
|
}
|
2009-07-11 20:06:35 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
2011-11-19 21:41:50 +01:00
|
|
|
|
|
2009-07-11 20:06:35 +02:00
|
|
|
|
LAUNCHER (ScopedPtrVect_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::test
|
2009-02-08 01:31:01 +01:00
|
|
|
|
|