2009-07-18 19:36:32 +02:00
|
|
|
|
/*
|
2009-07-18 22:07:46 +02:00
|
|
|
|
OpaqueUncheckedBuffer(Test) - passive inline buffer test
|
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)
|
|
|
|
|
|
2009, 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-18 19:36:32 +02:00
|
|
|
|
|
2017-02-22 01:54:20 +01:00
|
|
|
|
/** @file opaque-unchecked-buffer-test.cpp
|
2017-02-22 03:17:18 +01:00
|
|
|
|
** unit test \ref OpaqueUncheckedBuffer_test
|
2016-11-03 18:20:10 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
2009-07-18 19:36:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
|
|
|
|
|
#include "lib/test/test-helper.hpp"
|
2018-03-24 05:35:13 +01:00
|
|
|
|
#include "lib/nocopy.hpp"
|
2009-07-18 19:36:32 +02:00
|
|
|
|
#include "lib/util.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/opaque-holder.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#include <iostream>
|
2009-07-18 22:07:46 +02:00
|
|
|
|
#include <cstring>
|
2009-07-18 19:36:32 +02:00
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
|
namespace test{
|
|
|
|
|
|
|
|
|
|
|
|
using ::Test;
|
2009-07-18 22:07:46 +02:00
|
|
|
|
using util::min;
|
|
|
|
|
|
using lumiera::error::LUMIERA_ERROR_FATAL;
|
2021-05-07 22:50:13 +02:00
|
|
|
|
using lumiera::error::LUMIERA_ERROR_CAPACITY;
|
|
|
|
|
|
using util::MoveOnly;
|
2009-07-18 19:36:32 +02:00
|
|
|
|
|
2009-07-18 22:07:46 +02:00
|
|
|
|
using std::strlen;
|
2009-07-18 19:36:32 +02:00
|
|
|
|
using std::cout;
|
|
|
|
|
|
using std::endl;
|
|
|
|
|
|
|
2009-07-18 22:07:46 +02:00
|
|
|
|
|
|
|
|
|
|
|
2009-07-18 19:36:32 +02:00
|
|
|
|
namespace { // test dummy hierarchy
|
2021-05-07 22:50:13 +02:00
|
|
|
|
// Note: vtable (and virtual dtor), but varying storage requirements
|
2009-07-18 19:36:32 +02:00
|
|
|
|
|
|
|
|
|
|
long _checksum = 0;
|
|
|
|
|
|
uint _create_count = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-07-18 22:07:46 +02:00
|
|
|
|
struct Base
|
2021-05-07 22:50:13 +02:00
|
|
|
|
: MoveOnly
|
2009-07-18 19:36:32 +02:00
|
|
|
|
{
|
|
|
|
|
|
uint id_;
|
2021-05-07 22:50:13 +02:00
|
|
|
|
|
|
|
|
|
|
/** abstract interface */
|
|
|
|
|
|
virtual ~Base() =default;
|
|
|
|
|
|
virtual void confess() =0;
|
2009-07-18 19:36:32 +02:00
|
|
|
|
|
2021-05-07 22:50:13 +02:00
|
|
|
|
Base(uint i)
|
|
|
|
|
|
: id_(i)
|
|
|
|
|
|
{
|
|
|
|
|
|
++_create_count;
|
|
|
|
|
|
_checksum += id_;
|
|
|
|
|
|
}
|
2009-07-18 19:36:32 +02:00
|
|
|
|
|
2021-05-07 22:50:13 +02:00
|
|
|
|
Base(Base&& o) ///< @note move ctor only
|
|
|
|
|
|
: id_{o.id_}
|
|
|
|
|
|
{
|
|
|
|
|
|
_checksum += id_;
|
|
|
|
|
|
};
|
2009-07-18 19:36:32 +02:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<uint ii>
|
|
|
|
|
|
struct DD : Base
|
|
|
|
|
|
{
|
2009-07-18 22:07:46 +02:00
|
|
|
|
char buff_[ii+1];
|
|
|
|
|
|
|
|
|
|
|
|
~DD() { _checksum -= ii; } // verify the correct dtor is called...
|
|
|
|
|
|
|
|
|
|
|
|
DD(Symbol sym = 0)
|
|
|
|
|
|
: Base(ii)
|
|
|
|
|
|
{
|
|
|
|
|
|
memset (&buff_, '*', ii);
|
|
|
|
|
|
if (sym)
|
|
|
|
|
|
memcpy (&buff_, sym, min (ii, strlen (sym)));
|
|
|
|
|
|
buff_[ii] = 0;
|
|
|
|
|
|
}
|
2021-05-07 22:50:13 +02:00
|
|
|
|
DD(DD&&) =default; // note: explicit dtor would suppress generation of move ctor
|
2009-07-18 22:07:46 +02:00
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
confess ()
|
|
|
|
|
|
{
|
|
|
|
|
|
cout << "DD<" << ii << ">: " << buff_ << endl;
|
|
|
|
|
|
}
|
2021-05-07 22:50:13 +02:00
|
|
|
|
};
|
2009-07-18 19:36:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
2009-07-18 22:07:46 +02:00
|
|
|
|
struct D42Sub
|
|
|
|
|
|
: DD<42>
|
2009-07-18 19:36:32 +02:00
|
|
|
|
{
|
2009-07-18 22:07:46 +02:00
|
|
|
|
D42Sub (string s1, string s2)
|
|
|
|
|
|
: DD<42> ((s1+' '+s2).c_str())
|
2009-07-18 19:36:32 +02:00
|
|
|
|
{ }
|
|
|
|
|
|
|
2009-07-18 22:07:46 +02:00
|
|
|
|
void
|
|
|
|
|
|
confess ()
|
2009-07-18 19:36:32 +02:00
|
|
|
|
{
|
2009-07-18 22:07:46 +02:00
|
|
|
|
cout << "I'm special, " << buff_ << endl;
|
2009-07-18 19:36:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-07-18 22:07:46 +02:00
|
|
|
|
struct Killer
|
|
|
|
|
|
: DD<23>
|
|
|
|
|
|
{
|
|
|
|
|
|
Killer () { throw lumiera::error::Fatal ("crisscross"); }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-07-18 19:36:32 +02:00
|
|
|
|
/** maximum additional storage maybe wasted
|
|
|
|
|
|
* due to alignment of the contained object
|
|
|
|
|
|
* within OpaqueHolder's buffer
|
|
|
|
|
|
*/
|
2021-05-02 19:40:11 +02:00
|
|
|
|
const size_t _ALIGNMENT_OVERHEAD_ = sizeof(size_t);
|
2009-07-18 19:36:32 +02:00
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2021-05-07 22:50:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
2013-10-24 23:06:36 +02:00
|
|
|
|
/******************************************************************************//**
|
2009-07-18 22:07:46 +02:00
|
|
|
|
* @test use an inline buffer to place objects of a subclass, without any checks.
|
2021-05-07 22:50:13 +02:00
|
|
|
|
* - InPlaceBuffer only provides minimal service, to be covered here,
|
2009-07-18 22:07:46 +02:00
|
|
|
|
* including automatic dtor invocation and smart-ptr style access.
|
2021-05-07 22:50:13 +02:00
|
|
|
|
* - A PlantingHandle can be exposed through some API, thereby allowing
|
|
|
|
|
|
* an external entity to implant some implementation subclass.
|
2009-07-18 19:36:32 +02:00
|
|
|
|
*/
|
2009-07-18 22:07:46 +02:00
|
|
|
|
class OpaqueUncheckedBuffer_test : public Test
|
2009-07-18 19:36:32 +02:00
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
|
run (Arg)
|
|
|
|
|
|
{
|
|
|
|
|
|
_checksum = 0;
|
|
|
|
|
|
_create_count = 0;
|
|
|
|
|
|
{
|
2021-05-02 19:40:11 +02:00
|
|
|
|
using Buffer = InPlaceBuffer<Base, sizeof(DD<42>), DD<0>>;
|
2009-07-18 22:07:46 +02:00
|
|
|
|
|
|
|
|
|
|
Buffer buff;
|
2021-05-02 19:40:11 +02:00
|
|
|
|
CHECK (sizeof(buff) <= sizeof(DD<42>) + _ALIGNMENT_OVERHEAD_);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (1 == _create_count);
|
|
|
|
|
|
CHECK (0 == _checksum);
|
2009-07-18 22:07:46 +02:00
|
|
|
|
buff->confess(); // one default object of type DD<0> has been created
|
|
|
|
|
|
|
2016-12-23 04:23:03 +01:00
|
|
|
|
buff.create<DD<5>>();
|
2009-07-18 22:07:46 +02:00
|
|
|
|
buff->confess();
|
|
|
|
|
|
|
|
|
|
|
|
VERIFY_ERROR( FATAL, buff.create<Killer> () );
|
|
|
|
|
|
|
2021-05-07 22:50:13 +02:00
|
|
|
|
CHECK (0 == buff->id_); // default object was created, due to exception...
|
|
|
|
|
|
CHECK (4 == _create_count);
|
|
|
|
|
|
|
2009-07-18 22:07:46 +02:00
|
|
|
|
|
2021-05-02 19:40:11 +02:00
|
|
|
|
// as a variation: use a "planting handle" to implant yet another subtype
|
|
|
|
|
|
// into the opaque buffer. This setup helps to expose such a buffer via API
|
|
|
|
|
|
using Handle = Buffer::Handle;
|
|
|
|
|
|
|
|
|
|
|
|
Handle plantingHandle{buff};
|
2021-05-07 22:50:13 +02:00
|
|
|
|
plantingHandle.emplace (std::move (DD<9>{"I'm fine"}));
|
2021-05-02 19:40:11 +02:00
|
|
|
|
|
|
|
|
|
|
// subclass instance was indeed implanted into the opaque buffer
|
2021-05-07 22:50:13 +02:00
|
|
|
|
buff->confess();
|
|
|
|
|
|
CHECK (9 == _checksum); // DD<5> has been properly destroyed, DD<9> created in place
|
|
|
|
|
|
|
|
|
|
|
|
// Handles can be passed and copied freely
|
|
|
|
|
|
Handle copyHandle = plantingHandle;
|
|
|
|
|
|
|
|
|
|
|
|
CHECK (9 == buff->id_); // nothing changed with the buffer, still holding the DD<9>...
|
|
|
|
|
|
VERIFY_ERROR( FATAL, copyHandle.create<Killer>() );
|
|
|
|
|
|
CHECK (0 == buff->id_); // previous object destroyed, Killer-ctor fails, default created DD<0> to fill the void
|
|
|
|
|
|
|
|
|
|
|
|
VERIFY_ERROR( CAPACITY, copyHandle.create<DD<55>>() ); // buffer size constraint observed
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////// does not compile (subclass check)
|
|
|
|
|
|
// copyHandle.create<std::string>();
|
|
|
|
|
|
|
|
|
|
|
|
copyHandle.create<D42Sub> ("what the f**","is going on here?");
|
|
|
|
|
|
|
2009-07-18 22:07:46 +02:00
|
|
|
|
buff->confess();
|
|
|
|
|
|
|
2021-05-07 22:50:13 +02:00
|
|
|
|
CHECK (8 == _create_count);
|
|
|
|
|
|
CHECK (42 == _checksum); // No.42 is alive
|
2009-07-18 19:36:32 +02:00
|
|
|
|
}
|
2021-05-07 22:50:13 +02:00
|
|
|
|
CHECK (0 == _checksum); // all dead
|
2009-07-18 19:36:32 +02:00
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-07-18 22:07:46 +02:00
|
|
|
|
LAUNCHER (OpaqueUncheckedBuffer_test, "unit common");
|
2009-07-18 19:36:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::test
|
|
|
|
|
|
|