2009-07-04 00:24:55 +02:00
|
|
|
|
/*
|
|
|
|
|
|
ScopedHolder(Test) - holding and owning 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-07-04 00:24:55 +02:00
|
|
|
|
|
2017-02-22 01:54:20 +01:00
|
|
|
|
/** @file scoped-holder-test.cpp
|
2017-02-22 03:17:18 +01:00
|
|
|
|
** unit test \ref ScopedHolder_test
|
2016-11-03 18:20:10 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
2009-07-04 00:24:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
2010-12-03 02:28:29 +01:00
|
|
|
|
#include "lib/test/test-helper.hpp"
|
2018-03-24 05:35:13 +01:00
|
|
|
|
#include "lib/format-cout.hpp"
|
2009-07-04 00:24:55 +02:00
|
|
|
|
#include "lib/util.hpp"
|
2010-12-03 02:28:29 +01:00
|
|
|
|
#include "lib/error.hpp"
|
2009-07-04 00:24:55 +02:00
|
|
|
|
|
|
|
|
|
|
#include "lib/scoped-holder.hpp"
|
2024-06-15 18:14:00 +02:00
|
|
|
|
#include "lib/test/tracking-dummy.hpp"
|
2009-07-04 00:24:55 +02:00
|
|
|
|
|
|
|
|
|
|
#include <map>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
|
namespace test{
|
|
|
|
|
|
|
|
|
|
|
|
using ::Test;
|
|
|
|
|
|
using util::isnil;
|
2010-12-03 02:28:29 +01:00
|
|
|
|
using lumiera::error::LUMIERA_ERROR_LOGIC;
|
2009-07-04 00:24:55 +02:00
|
|
|
|
|
|
|
|
|
|
using std::map;
|
|
|
|
|
|
|
|
|
|
|
|
typedef ScopedHolder<Dummy> HolderD;
|
|
|
|
|
|
typedef ScopedPtrHolder<Dummy> PtrHolderD;
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-24 23:06:36 +02:00
|
|
|
|
/******************************************************************************//**
|
2010-12-17 22:51:27 +01:00
|
|
|
|
* @test ScopedHolder and ScopedPtrHolder are initially empty and copyable.
|
2009-07-04 00:24:55 +02:00
|
|
|
|
* After taking ownership, they prohibit copy operations, manage the
|
|
|
|
|
|
* lifecycle of the contained object and provide smart-ptr like access.
|
|
|
|
|
|
* A series of identical tests is conducted both with the ScopedPtrHolder
|
|
|
|
|
|
* (the contained objects are heap allocated but managed by the holder)
|
|
|
|
|
|
* and with the ScopedHolder (objects placed inline)
|
|
|
|
|
|
*/
|
|
|
|
|
|
class ScopedHolder_test : public Test
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
|
run (Arg)
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
cout << "checking ScopedHolder<Dummy>...\n";
|
|
|
|
|
|
checkAllocation<HolderD>();
|
|
|
|
|
|
checkErrorHandling<HolderD>();
|
|
|
|
|
|
checkCopyProtocol<HolderD>();
|
|
|
|
|
|
checkSTLContainer<HolderD>();
|
|
|
|
|
|
|
|
|
|
|
|
cout << "checking ScopedPtrHolder<Dummy>...\n";
|
|
|
|
|
|
checkAllocation<PtrHolderD>();
|
|
|
|
|
|
checkErrorHandling<PtrHolderD>();
|
|
|
|
|
|
checkCopyProtocol<PtrHolderD>();
|
|
|
|
|
|
checkSTLContainer<PtrHolderD>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void create_contained_object (HolderD& holder) { holder.create(); }
|
|
|
|
|
|
void create_contained_object (PtrHolderD& holder) { holder.reset(new Dummy()); }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class HO>
|
|
|
|
|
|
void
|
|
|
|
|
|
checkAllocation()
|
|
|
|
|
|
{
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2009-07-04 00:24:55 +02:00
|
|
|
|
{
|
|
|
|
|
|
HO holder;
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!holder);
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2009-07-04 00:24:55 +02:00
|
|
|
|
|
|
|
|
|
|
create_contained_object (holder);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (holder);
|
2014-08-27 04:23:21 +02:00
|
|
|
|
CHECK (false != bool(holder));
|
|
|
|
|
|
CHECK (bool(holder) != false);
|
2009-07-04 00:24:55 +02:00
|
|
|
|
|
2012-01-01 06:20:42 +01:00
|
|
|
|
CHECK (0 < Dummy::checksum());
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK ( &(*holder));
|
2024-06-11 18:23:48 +02:00
|
|
|
|
CHECK (holder->calc(2) == 2 + Dummy::checksum());
|
2009-07-04 00:24:55 +02:00
|
|
|
|
|
|
|
|
|
|
Dummy *rawP = holder.get();
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (rawP);
|
|
|
|
|
|
CHECK (holder);
|
|
|
|
|
|
CHECK (rawP == &(*holder));
|
2024-06-11 18:23:48 +02:00
|
|
|
|
CHECK (rawP->calc(-5) == holder->calc(-5));
|
2009-07-04 00:24:55 +02:00
|
|
|
|
|
2009-09-05 19:11:17 +02:00
|
|
|
|
TRACE (test, "holder at %p", &holder);
|
|
|
|
|
|
TRACE (test, "object at %p", holder.get() );
|
2011-12-19 03:02:48 +01:00
|
|
|
|
TRACE (test, "size(object) = %zu", sizeof(*holder));
|
|
|
|
|
|
TRACE (test, "size(holder) = %zu", sizeof(holder));
|
2009-07-04 00:24:55 +02:00
|
|
|
|
}
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2009-07-04 00:24:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class HO>
|
|
|
|
|
|
void
|
|
|
|
|
|
checkErrorHandling()
|
|
|
|
|
|
{
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2009-07-04 00:24:55 +02:00
|
|
|
|
{
|
|
|
|
|
|
HO holder;
|
|
|
|
|
|
|
2011-11-25 22:05:12 +01:00
|
|
|
|
Dummy::activateCtorFailure();
|
2009-07-04 00:24:55 +02:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
create_contained_object (holder);
|
2012-01-01 06:20:42 +01:00
|
|
|
|
NOTREACHED ("expect failure in ctor");
|
2009-07-04 00:24:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
catch (int val)
|
|
|
|
|
|
{
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 != Dummy::checksum());
|
|
|
|
|
|
Dummy::checksum() -= val;
|
|
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2009-07-04 00:24:55 +02:00
|
|
|
|
}
|
2010-12-17 22:51:27 +01:00
|
|
|
|
CHECK (!holder); /* because the exception happens in ctor
|
2009-07-04 00:24:55 +02:00
|
|
|
|
object doesn't count as "created" */
|
2011-11-25 22:05:12 +01:00
|
|
|
|
Dummy::activateCtorFailure(false);
|
2009-07-04 00:24:55 +02:00
|
|
|
|
}
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2009-07-04 00:24:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class HO>
|
|
|
|
|
|
void
|
|
|
|
|
|
checkCopyProtocol()
|
|
|
|
|
|
{
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2009-07-04 00:24:55 +02:00
|
|
|
|
{
|
|
|
|
|
|
HO holder;
|
|
|
|
|
|
HO holder2 (holder);
|
|
|
|
|
|
holder2 = holder;
|
|
|
|
|
|
// copy and assignment of empty holders is tolerated
|
|
|
|
|
|
|
|
|
|
|
|
// but after enclosing an object it will be copy protected...
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!holder);
|
2009-07-04 00:24:55 +02:00
|
|
|
|
create_contained_object (holder);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (holder);
|
2011-11-25 22:05:12 +01:00
|
|
|
|
long currSum = Dummy::checksum();
|
2009-07-04 00:24:55 +02:00
|
|
|
|
void* adr = holder.get();
|
|
|
|
|
|
|
2010-12-03 02:28:29 +01:00
|
|
|
|
VERIFY_ERROR(LOGIC, holder2 = holder );
|
2010-12-17 22:51:27 +01:00
|
|
|
|
CHECK (holder);
|
|
|
|
|
|
CHECK (!holder2);
|
|
|
|
|
|
CHECK (holder.get()==adr);
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (Dummy::checksum()==currSum);
|
2010-12-03 02:28:29 +01:00
|
|
|
|
|
|
|
|
|
|
VERIFY_ERROR(LOGIC, holder = holder2 );
|
2010-12-17 22:51:27 +01:00
|
|
|
|
CHECK (holder);
|
|
|
|
|
|
CHECK (!holder2);
|
|
|
|
|
|
CHECK (holder.get()==adr);
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (Dummy::checksum()==currSum);
|
2009-07-04 00:24:55 +02:00
|
|
|
|
|
|
|
|
|
|
create_contained_object (holder2);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (holder2);
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (Dummy::checksum() != currSum);
|
|
|
|
|
|
currSum = Dummy::checksum();
|
2009-07-04 00:24:55 +02:00
|
|
|
|
|
2010-12-03 02:28:29 +01:00
|
|
|
|
VERIFY_ERROR(LOGIC, holder = holder2 );
|
2010-12-17 22:51:27 +01:00
|
|
|
|
CHECK (holder);
|
|
|
|
|
|
CHECK (holder2);
|
|
|
|
|
|
CHECK (holder.get()==adr);
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (Dummy::checksum()==currSum);
|
2009-07-04 00:24:55 +02:00
|
|
|
|
|
2010-12-03 02:28:29 +01:00
|
|
|
|
VERIFY_ERROR(LOGIC, HO holder3 (holder2) );
|
2010-12-17 22:51:27 +01:00
|
|
|
|
CHECK (holder);
|
|
|
|
|
|
CHECK (holder2);
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (Dummy::checksum()==currSum);
|
2009-07-04 00:24:55 +02:00
|
|
|
|
}
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2009-07-04 00:24:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** @test collection of noncopyable objects
|
|
|
|
|
|
* maintained within a STL map
|
|
|
|
|
|
*/
|
|
|
|
|
|
template<class HO>
|
|
|
|
|
|
void
|
|
|
|
|
|
checkSTLContainer()
|
|
|
|
|
|
{
|
|
|
|
|
|
typedef std::map<int,HO> MapHO;
|
|
|
|
|
|
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2009-07-04 00:24:55 +02:00
|
|
|
|
{
|
|
|
|
|
|
MapHO maph;
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (isnil (maph));
|
2009-07-04 00:24:55 +02:00
|
|
|
|
|
|
|
|
|
|
for (uint i=0; i<100; ++i)
|
|
|
|
|
|
{
|
|
|
|
|
|
HO & contained = maph[i];
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!contained);
|
2009-07-04 00:24:55 +02:00
|
|
|
|
} // 100 holder objects created by sideeffect
|
2024-06-11 18:23:48 +02:00
|
|
|
|
// ..... without creating any contained object!
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (!isnil (maph));
|
|
|
|
|
|
CHECK (100==maph.size());
|
2009-07-04 00:24:55 +02:00
|
|
|
|
|
|
|
|
|
|
for (uint i=0; i<100; ++i)
|
|
|
|
|
|
{
|
|
|
|
|
|
create_contained_object (maph[i]);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (maph[i]);
|
2024-06-11 18:23:48 +02:00
|
|
|
|
CHECK (0 < maph[i]->calc(12));
|
2009-07-04 00:24:55 +02:00
|
|
|
|
}
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (100==maph.size());
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 != Dummy::checksum());
|
2009-07-04 00:24:55 +02:00
|
|
|
|
|
|
|
|
|
|
|
2024-06-11 18:23:48 +02:00
|
|
|
|
long value55 = maph[55]->calc(0);
|
2011-11-25 22:05:12 +01:00
|
|
|
|
long currSum = Dummy::checksum();
|
2009-07-04 00:24:55 +02:00
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (1 == maph.erase(55));
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (Dummy::checksum() == currSum - value55); // proves object#55's dtor has been invoked
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (maph.size() == 99);
|
2009-07-04 00:24:55 +02:00
|
|
|
|
|
2010-12-17 22:51:27 +01:00
|
|
|
|
maph[55]; // create new empty holder by sideeffect...
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (&maph[55]);
|
|
|
|
|
|
CHECK (!maph[55]);
|
|
|
|
|
|
CHECK (maph.size() == 100);
|
2009-07-04 00:24:55 +02:00
|
|
|
|
}
|
2011-11-25 22:05:12 +01:00
|
|
|
|
CHECK (0 == Dummy::checksum());
|
2009-07-04 00:24:55 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
LAUNCHER (ScopedHolder_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::test
|