lumiera_/tests/library/scoped-holder-transfer-test.cpp
Ichthyostega 806db414dd 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

232 lines
6.5 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
ScopedHolderTransfer(Test) - managing noncopyable objects within a growing vector
Copyright (C)
2008, Hermann Vosseler <Ichthyostega@web.de>
  **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.
* *****************************************************************/
/** @file scoped-holder-transfer-test.cpp
** unit test \ref ScopedHolderTransfer_test
*/
#include "lib/test/run.hpp"
#include "lib/util.hpp"
#include "lib/scoped-holder.hpp"
#include "lib/scoped-holder-transfer.hpp"
#include "lib/test/tracking-dummy.hpp"
#include <iostream>
#include <vector>
namespace lib {
namespace test {
using ::Test;
using util::isnil;
using std::vector;
using std::cout;
namespace { // extending the Dummy for our special purpose....
bool throw_in_transfer = false;
class FixedDummy
: public Dummy
{
public:
FixedDummy()
{
TRACE (test, "CTOR FixedDummy() --> this=%p val=%d", this, getVal());
}
~FixedDummy()
{
TRACE (test, "DTOR ~FixedDummy() this=%p val=%d", this, getVal());
}
friend void
transfer_control (FixedDummy& from, FixedDummy& to)
{
TRACE (test, "TRANSFER target=%p <-- source=%p (%d,%d)", &to,&from, to.getVal(),from.getVal());
if (throw_in_transfer)
throw to.getVal();
swap (from,to);
from.setVal(0); // remove the old Dummy from accounting (checksum)
}
};
typedef ScopedHolder<FixedDummy> HolderD;
typedef ScopedPtrHolder<FixedDummy> PtrHolderD;
template<class HOL>
struct Table
{
typedef Allocator_TransferNoncopyable<HOL> Allo;
typedef typename std::vector<HOL,Allo> Type;
};
}//(End) test helpers
/******************************************************************************//**
* @test growing a vector containing noncopyable objects wrapped into ScopedHolder
* instances. This requires the use of a custom allocator, invoking a
* \c transfer_control() function to be provided for the concrete
* noncopyable class type, being invoked when the vector
* needs to reallocate.
*/
class ScopedHolderTransfer_test : public Test
{
virtual void
run (Arg)
{
cout << "checking ScopedHolder<Dummy>...\n";
buildVector<HolderD>();
growVector<HolderD>();
checkErrorHandling<HolderD>();
cout << "checking ScopedPtrHolder<Dummy>...\n";
buildVector<PtrHolderD>();
growVector<PtrHolderD>();
checkErrorHandling<PtrHolderD>();
}
void create_contained_object (HolderD& holder) { holder.create(); }
void create_contained_object (PtrHolderD& holder) { holder.reset(new FixedDummy()); }
template<class HO>
void
buildVector()
{
CHECK (0 == Dummy::checksum());
{
typedef typename Table<HO>::Type Vect;
Vect table(50);
CHECK (0 == Dummy::checksum());
for (uint i=0; i<10; ++i)
create_contained_object (table[i]);
CHECK (0 < Dummy::checksum());
CHECK ( table[9]);
CHECK (!table[10]);
Dummy *rawP = table[5].get();
CHECK (rawP);
CHECK (table[5]);
CHECK (rawP == &(*table[5]));
CHECK (rawP->calc(-555) == table[5]->calc(-555));
}
CHECK (0 == Dummy::checksum());
}
template<class HO>
void
growVector()
{
CHECK (0 == Dummy::checksum());
{
typedef typename Table<HO>::Type Vect;
Vect table;
table.reserve(2);
CHECK (0 == Dummy::checksum());
cout << ".\n..install one element at index[0]\n";
table.push_back(HO());
CHECK (0 == Dummy::checksum());
create_contained_object (table[0]); // switches into "managed" state
CHECK (0 < Dummy::checksum());
int theSum = Dummy::checksum();
cout << ".\n..*** resize table to 16 elements\n";
for (uint i=0; i<15; ++i)
table.push_back(HO());
CHECK (theSum == Dummy::checksum());
}
CHECK (0 == Dummy::checksum());
}
template<class HO>
void
checkErrorHandling()
{
CHECK (0 == Dummy::checksum());
{
typedef typename Table<HO>::Type Vect;
Vect table(5);
table.reserve(5);
CHECK (0 == Dummy::checksum());
create_contained_object (table[2]);
create_contained_object (table[4]);
CHECK (0 < Dummy::checksum());
int theSum = Dummy::checksum();
cout << ".\n.throw some exceptions...\n";
Dummy::activateCtorFailure();
try
{
create_contained_object (table[3]);
NOTREACHED ("ctor should throw");
}
catch (int val)
{
CHECK (theSum < Dummy::checksum());
Dummy::checksum() -= val;
CHECK (theSum == Dummy::checksum());
}
CHECK ( table[2]);
CHECK (!table[3]); // not created because of exception
CHECK ( table[4]);
Dummy::activateCtorFailure(false);
throw_in_transfer=true; // can do this only when using ScopedHolder
try
{
table.resize(10);
}
catch (int val)
{
CHECK ( table.size() < 10);
}
CHECK (theSum == Dummy::checksum());
throw_in_transfer=false;
}
CHECK (0 == Dummy::checksum());
}
};
LAUNCHER (ScopedHolderTransfer_test, "unit common");
}} // namespace lib::test