LUMIERA.clone/tests/library/opaque-holder-test.cpp

297 lines
9.1 KiB
C++
Raw Permalink Normal View History

/*
OpaqueHolder(Test) - check the inline type erasure helper
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
* *****************************************************************/
/** @file opaque-holder-test.cpp
** unit test \ref OpaqueHolder_test
*/
#include "lib/test/run.hpp"
#include "lib/test/test-helper.hpp"
#include "lib/util.hpp"
#include "lib/util-foreach.hpp"
#include "lib/opaque-holder.hpp"
#include <iostream>
#include <vector>
namespace lib {
namespace test{
using ::Test;
using util::isnil;
using util::for_each;
using util::isSameObject;
using LERR_(BOTTOM_VALUE);
using LERR_(WRONG_TYPE);
using LERR_(ASSERTION);
using std::vector;
using std::cout;
using std::endl;
namespace { // test dummy hierarchy
2025-06-07 23:59:57 +02:00
// Note: common storage but no vtable
long _checksum = 0;
uint _create_count = 0;
2025-06-07 23:59:57 +02:00
struct Base
{
uint id_;
Base(uint i=0) : id_(i) { _checksum +=id_; ++_create_count; }
Base(Base const& o) : id_(o.id_) { _checksum +=id_; ++_create_count; }
uint getIt() { return id_; }
};
template<uint ii>
struct DD : Base
{
DD() : Base(ii) { }
2009-07-05 02:26:59 +02:00
~DD() { _checksum -= ii; } // doing the decrement here
}; // verifies the correct dtor is called
struct Special
: DD<7>
{
ulong myVal_;
Special (uint val)
: myVal_(val)
{ }
explicit
operator bool() const ///< custom boolean "validity" check
{
return myVal_ % 2;
}
};
/** maximum additional storage maybe wasted
* due to alignment of the contained object
* within OpaqueHolder's buffer
*/
const size_t _ALIGN_ = sizeof(size_t);
}
typedef OpaqueHolder<Base> Opaque;
typedef vector<Opaque> TestList;
2009-07-04 04:35:17 +02:00
/******************************************************************************//**
* @test use the OpaqueHolder inline buffer to handle objects of a family of types
* through a common interface, without being forced to use heap storage
* or a custom allocator.
*
* @todo this test doesn't cover automatic conversions and conversions using RTTI
* from the target objects, while `OpaqueHolder.template get()`
* would allow for such conversions. This is similar to Ticket #141, and
* actually based on the same code as variant.hpp (access-casted.hpp)
*/
class OpaqueHolder_test : public Test
{
virtual void
run (Arg)
{
_checksum = 0;
_create_count = 0;
{
TestList objs = createDummies ();
for_each (objs, reAccess);
checkHandling (objs);
checkSpecialSubclass ();
}
CHECK (0 == _checksum); // all dead
}
TestList
createDummies ()
{
TestList list;
list.push_back (DD<1>());
list.push_back (DD<3>());
list.push_back (DD<5>());
list.push_back (DD<7>());
return list;
2009-07-05 02:26:59 +02:00
} //note: copy
static void
reAccess (Opaque& elm)
{
cout << elm->getIt() << endl;
}
/** @test cover the basic situations of object handling,
* especially copy operations and re-assignments
*/
void
checkHandling (TestList& objs)
{
Opaque oo;
CHECK (!oo);
CHECK (isnil(oo));
oo = objs[1];
CHECK (oo);
CHECK (!isnil(oo));
typedef DD<3> D3;
typedef DD<5> D5;
D3 d3 (oo.get<D3>() );
CHECK (3 == oo->getIt()); // re-access through Base interface
CHECK (!isSameObject (d3, *oo));
VERIFY_ERROR (WRONG_TYPE, oo.get<D5>() );
// direct assignment of target into Buffer
oo = D5();
CHECK (oo);
CHECK (5 == oo->getIt());
VERIFY_ERROR (WRONG_TYPE, oo.get<D3>() );
// can get a direct reference to contained object
2025-06-07 23:59:57 +02:00
D5 &rd5 (oo.get<D5>());
CHECK (isSameObject (rd5, *oo));
CHECK (!isnil(oo));
oo = objs[3]; // copy construction also works on non-empty object
CHECK (7 == oo->getIt());
// WARNING: direct ref has been messed up through the backdoor!
CHECK (7 == rd5.getIt());
CHECK (isSameObject (rd5, *oo));
uint cnt_before = _create_count;
oo.clear();
CHECK (!oo);
oo = D5(); // direct assignment also works on empty object
CHECK (oo);
CHECK (5 == oo->getIt());
CHECK (_create_count == 2 + cnt_before);
// one within buff and one for the anonymous temporary D5()
2009-07-04 04:35:17 +02:00
// verify that self-assignment is properly detected...
cnt_before = _create_count;
2009-07-04 04:35:17 +02:00
oo = oo;
CHECK (oo);
CHECK (_create_count == cnt_before);
2009-07-04 04:35:17 +02:00
oo = oo.get<D5>();
CHECK (_create_count == cnt_before);
2009-07-04 04:35:17 +02:00
oo = *oo;
CHECK (_create_count == cnt_before);
CHECK (oo);
2009-07-04 04:35:17 +02:00
oo.clear();
CHECK (!oo);
CHECK (isnil(oo));
VERIFY_ERROR (BOTTOM_VALUE, oo.get<D5>() );
2010-04-02 21:51:39 +02:00
#if false ///////////////////////////////////////////////////////////////////////////////////////////////TICKET #537 : restore throwing ASSERT
VERIFY_ERROR (ASSERTION, oo->getIt() );
2010-04-02 21:51:39 +02:00
#endif ///////////////////////////////////////////////////////////////////////////////////////////////TICKET #537 : restore throwing ASSERT
// can't access empty holder...
Opaque o1 (oo);
CHECK (!o1);
Opaque o2 (d3);
CHECK (!isSameObject (d3, *o2));
CHECK (3 == o2->getIt());
CHECK (sizeof(Opaque) <= sizeof(Base) + sizeof(void*) + _ALIGN_);
}
/** @test OpaqueHolder with additional storage for subclass.
* When a subclass requires more storage than the base class or
* Interface, we need to create a custom OpaqueHolder, specifying the
* actually necessary storage. Such a custom OpaqueHolder behaves exactly
* like the standard variant, but there is protection against accidentally
* using a standard variant to hold an instance of the larger subclass.
*
* @test Moreover, if the concrete class has a custom operator bool(), it
* will be invoked automatically from OpaqueHolder's operator bool()
*
2025-06-07 23:59:57 +02:00
*/
void
checkSpecialSubclass ()
{
typedef OpaqueHolder<Base, sizeof(Special)> SpecialOpaque;
cout << showSizeof<Base>() << endl;
cout << showSizeof<Special>() << endl;
cout << showSizeof<Opaque>() << endl;
cout << showSizeof<SpecialOpaque>() << endl;
CHECK (sizeof(Special) > sizeof(Base));
CHECK (sizeof(SpecialOpaque) > sizeof(Opaque));
CHECK (sizeof(SpecialOpaque) <= sizeof(Special) + sizeof(void*) + _ALIGN_);
Special s1 (6);
Special s2 (3);
CHECK (!s1); // even value
CHECK (s2); // odd value
CHECK (7 == s1.getIt()); // indeed subclass of DD<7>
CHECK (7 == s2.getIt());
SpecialOpaque ospe0;
SpecialOpaque ospe1 (s1);
SpecialOpaque ospe2 (s2);
CHECK (!ospe0); // note: bool test (isValid)
CHECK (!ospe1); // also forwarded to contained object (myVal_==6 is even)
CHECK ( ospe2);
CHECK ( isnil(ospe0)); // while isnil just checks the empty state
CHECK (!isnil(ospe1));
CHECK (!isnil(ospe2));
CHECK (7 == ospe1->getIt());
CHECK (6 == ospe1.get<Special>().myVal_);
CHECK (3 == ospe2.get<Special>().myVal_);
ospe1 = DD<5>(); // but can be reassigned like any normal Opaque
CHECK (ospe1);
CHECK (5 == ospe1->getIt());
VERIFY_ERROR (WRONG_TYPE, ospe1.get<Special>() );
Opaque normal = DD<5>();
CHECK (normal);
CHECK (5 == normal->getIt());
#if false ////////////////////////////////////////////////////////TODO: restore throwing ASSERT
// Assertion protects against SEGV
VERIFY_ERROR (ASSERTION, normal = s1 );
#endif////////////////////////////////////////////////////////////
}
};
LAUNCHER (OpaqueHolder_test, "unit common");
}} // namespace lib::test