* 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.
161 lines
4.9 KiB
C++
161 lines
4.9 KiB
C++
/*
|
||
SimpleAllocator(Test) - check interface for simple custom allocations
|
||
|
||
Copyright (C)
|
||
2011, 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 simple-allocator-test.cpp
|
||
** unit test \ref SimpleAllocator_test
|
||
*/
|
||
|
||
|
||
#include "lib/test/run.hpp"
|
||
#include "lib/simple-allocator.hpp"
|
||
#include "lib/util.hpp"
|
||
|
||
#include <string>
|
||
|
||
|
||
namespace lib {
|
||
namespace test{
|
||
|
||
using util::isSameObject;
|
||
using std::string;
|
||
|
||
|
||
|
||
namespace { // test data and helpers...
|
||
|
||
long checksum_ = 0;
|
||
|
||
/**
|
||
* Yet-another ctor/dtor-tracking test dummy object....
|
||
*/
|
||
template<uint siz>
|
||
class DummyObj
|
||
{
|
||
char crap_[siz];
|
||
|
||
public:
|
||
DummyObj()
|
||
{
|
||
REQUIRE (siz);
|
||
for (uint i=0; i<siz; ++i)
|
||
checksum_ += (crap_[i] = rani(128));
|
||
}
|
||
|
||
DummyObj (DummyObj const& o)
|
||
{
|
||
REQUIRE (siz);
|
||
for (uint i=0; i<siz; ++i)
|
||
checksum_ += (crap_[i] = o.crap_[i]);
|
||
}
|
||
|
||
~DummyObj()
|
||
{
|
||
for (uint i=0; i<siz; ++i)
|
||
checksum_ -= crap_[i];
|
||
}
|
||
};
|
||
|
||
typedef Types<DummyObj<1>,DummyObj<23>,string> SupportedTypes;
|
||
typedef SimpleAllocator<SupportedTypes, UseInstantiationCounting> TestAllocator;
|
||
}
|
||
|
||
|
||
|
||
/***********************************************************************************//**
|
||
* @test cover the basic operations of a custom allocator, delegating to mpool.
|
||
* The SimpleAllocator doesn't provide any ref-counting or tracking facilities,
|
||
* nor does he support bulk de-allocation. The advantage over using std::allocator
|
||
* directly is the shortcut for (placement) construction, and -- of course -- the
|
||
* ability to exchange the memory model at one central location.
|
||
*
|
||
* @todo as of 9/11 we do heap allocation, but we should use mpool -- see also Ticket #835
|
||
*
|
||
* @see engine::BufferMetadata
|
||
* @see TypedAllocationManager_test
|
||
*/
|
||
class SimpleAllocator_test : public Test
|
||
{
|
||
|
||
virtual void
|
||
run (Arg)
|
||
{
|
||
CHECK (0 == checksum_);
|
||
seedRand();
|
||
|
||
TestAllocator allocator;
|
||
|
||
typedef string * PS;
|
||
typedef DummyObj<1> * PD1;
|
||
typedef DummyObj<23> * PD23;
|
||
CHECK (sizeof(DummyObj<1>) != sizeof(DummyObj<23>));
|
||
|
||
PD1 pD11 = allocator.create<DummyObj<1>>();
|
||
PD1 pD12 = allocator.create<DummyObj<1>>();
|
||
PD23 pD21 = allocator.create<DummyObj<23>>();
|
||
PD23 pD22 = allocator.create<DummyObj<23>>();
|
||
PS pS11 = allocator.create<string> ("Lumiera");
|
||
PS pS12 = allocator.create<string> ("the paradox");
|
||
|
||
CHECK (pD11);
|
||
CHECK (pD12);
|
||
CHECK (pD21);
|
||
CHECK (pD22);
|
||
CHECK (pS11);
|
||
CHECK (pS12);
|
||
CHECK (!isSameObject (*pD11, *pD12));
|
||
CHECK (!isSameObject (*pD11, *pD21));
|
||
CHECK (!isSameObject (*pD11, *pD22));
|
||
CHECK (!isSameObject (*pD11, *pS11));
|
||
CHECK (!isSameObject (*pD11, *pS12));
|
||
CHECK (!isSameObject (*pD12, *pD21));
|
||
CHECK (!isSameObject (*pD12, *pD22));
|
||
CHECK (!isSameObject (*pD12, *pS11));
|
||
CHECK (!isSameObject (*pD12, *pS12));
|
||
CHECK (!isSameObject (*pD21, *pD22));
|
||
CHECK (!isSameObject (*pD21, *pS11));
|
||
CHECK (!isSameObject (*pD21, *pS12));
|
||
CHECK (!isSameObject (*pD22, *pS11));
|
||
CHECK (!isSameObject (*pD22, *pS12));
|
||
CHECK (!isSameObject (*pS11, *pS12));
|
||
|
||
CHECK (*pS11 == "Lumiera");
|
||
CHECK (*pS12 == "the paradox");
|
||
|
||
PD23 pDxx = allocator.create<DummyObj<23>> (*pD21);
|
||
PS pSxx = allocator.create<string> (*pS12);
|
||
|
||
CHECK (*pS12 == *pSxx);
|
||
CHECK (!isSameObject (*pS12, *pSxx));
|
||
|
||
allocator.destroy (pD11);
|
||
allocator.destroy (pD12);
|
||
allocator.destroy (pD21);
|
||
allocator.destroy (pD22);
|
||
allocator.destroy (pS11);
|
||
allocator.destroy (pS12);
|
||
allocator.destroy (pDxx);
|
||
allocator.destroy (pSxx);
|
||
|
||
CHECK (0 == allocator.numSlots<DummyObj<1>>());
|
||
CHECK (0 == allocator.numSlots<DummyObj<23>>());
|
||
CHECK (0 == allocator.numSlots<string>());
|
||
CHECK (0 == checksum_);
|
||
}
|
||
};
|
||
|
||
|
||
/** Register this test class... */
|
||
LAUNCHER (SimpleAllocator_test, "unit common");
|
||
|
||
|
||
}} // namespace lib::test
|