2010-03-08 04:43:24 +01:00
|
|
|
|
/*
|
|
|
|
|
|
RefArray(Test) - unittest for wrapping with array-of-refs access
|
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
|
|
|
|
* *****************************************************************/
|
2010-03-08 04:43:24 +01:00
|
|
|
|
|
2017-02-22 01:54:20 +01:00
|
|
|
|
/** @file ref-array-test.cpp
|
2017-02-22 03:17:18 +01:00
|
|
|
|
** unit test \ref RefArray_test
|
2024-05-12 19:05:50 +02:00
|
|
|
|
** @deprecated 5/2024 rework underway
|
|
|
|
|
|
** @see several-buider-test.cpp
|
2016-11-03 18:20:10 +01:00
|
|
|
|
*/
|
|
|
|
|
|
|
2010-03-08 04:43:24 +01:00
|
|
|
|
|
|
|
|
|
|
#include "lib/test/run.hpp"
|
|
|
|
|
|
#include "lib/util.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#include "lib/ref-array-impl.hpp"
|
|
|
|
|
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
|
|
|
|
using ::test::Test;
|
|
|
|
|
|
using std::vector;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
|
namespace test{
|
|
|
|
|
|
|
|
|
|
|
|
namespace { // test types
|
|
|
|
|
|
|
|
|
|
|
|
struct I
|
|
|
|
|
|
{
|
|
|
|
|
|
virtual int op(int i) const =0;
|
|
|
|
|
|
virtual ~I() {}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct Sub1 : I
|
|
|
|
|
|
{
|
|
|
|
|
|
int offs_;
|
|
|
|
|
|
|
|
|
|
|
|
Sub1 (int o=1) : offs_(o) {}
|
|
|
|
|
|
|
|
|
|
|
|
int op (int i) const { return i+offs_; }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct Sub2 : I
|
|
|
|
|
|
{
|
|
|
|
|
|
const char* letterz_;
|
|
|
|
|
|
Sub2() : letterz_("ABCDEFGHIJKLMNOPQRSTUVWXYZ") {}
|
|
|
|
|
|
|
|
|
|
|
|
int op (int i) const { return (int)letterz_[i % 26]; }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
struct Sub3 : I
|
|
|
|
|
|
{
|
|
|
|
|
|
int id_;
|
|
|
|
|
|
static long sum;
|
|
|
|
|
|
static long trigger;
|
|
|
|
|
|
|
|
|
|
|
|
Sub3(int id) : id_(id)
|
|
|
|
|
|
{
|
|
|
|
|
|
sum +=id_;
|
|
|
|
|
|
if ( id_ == trigger )
|
|
|
|
|
|
throw trigger; // fail while in construction
|
|
|
|
|
|
}
|
|
|
|
|
|
~Sub3()
|
|
|
|
|
|
{
|
|
|
|
|
|
sum -=id_;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int op (int i) const { return i + id_; }
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
long Sub3::sum = 0;
|
|
|
|
|
|
long Sub3::trigger = -1;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} // (END) test types
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-10-24 23:06:36 +02:00
|
|
|
|
/***************************************************************//**
|
2010-03-08 04:43:24 +01:00
|
|
|
|
* @test build several wrappers, each based on a different storage,
|
|
|
|
|
|
* all providing RefArray access to a given vector. The rationale
|
|
|
|
|
|
* for RefArray is to expose just the interface: the data structure
|
|
|
|
|
|
* within the actual implementation holds subclass instances of
|
|
|
|
|
|
* the specified interface.
|
|
|
|
|
|
* - RefArrayVectorWrapper is a ref to an existing vector
|
|
|
|
|
|
* - RefArrayVector subclasses std::vector
|
|
|
|
|
|
* - RefArrayTable holds a fix sized table, i.e. embedded storage
|
|
|
|
|
|
*
|
|
|
|
|
|
* @see ref-array-impl.hpp
|
|
|
|
|
|
*/
|
|
|
|
|
|
class RefArray_test : public Test
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
|
run (Arg)
|
|
|
|
|
|
{
|
2024-11-13 02:23:23 +01:00
|
|
|
|
seedRand();
|
|
|
|
|
|
|
2010-03-08 04:43:24 +01:00
|
|
|
|
checkWrapper();
|
|
|
|
|
|
checkVector();
|
|
|
|
|
|
checkTable();
|
|
|
|
|
|
checkTable_inplaceCreation();
|
|
|
|
|
|
checkTable_errorHandling();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
checkWrapper()
|
|
|
|
|
|
{
|
|
|
|
|
|
vector<Sub2> subz(10);
|
|
|
|
|
|
RefArrayVectorWrapper<I,Sub2> subWrap (subz);
|
|
|
|
|
|
|
|
|
|
|
|
RefArray<I> & rArr (subWrap);
|
|
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (subWrap.size()==subz.size());
|
|
|
|
|
|
CHECK (INSTANCEOF(I, &rArr[0]));
|
2010-03-08 04:43:24 +01:00
|
|
|
|
for (size_t i=0; i<rArr.size(); ++i)
|
|
|
|
|
|
{
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (&rArr[i] == &subz[i]);
|
|
|
|
|
|
CHECK (rArr[i].op(i) == subz[i].op(i));
|
2010-03-08 04:43:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
checkVector()
|
|
|
|
|
|
{
|
|
|
|
|
|
RefArrayVector<I,Sub2> subz(10);
|
|
|
|
|
|
|
|
|
|
|
|
vector<Sub2> & vect (subz);
|
|
|
|
|
|
RefArray<I> & rArr (subz);
|
|
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (vect.size()==subz.size());
|
|
|
|
|
|
CHECK (INSTANCEOF(I, &rArr[0]));
|
2010-03-08 04:43:24 +01:00
|
|
|
|
for (size_t i=0; i<rArr.size(); ++i)
|
|
|
|
|
|
{
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (&rArr[i] == &vect[i]);
|
|
|
|
|
|
CHECK (rArr[i].op(i) == vect[i].op(i));
|
2010-03-08 04:43:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#define ADR(OBJ) (ulong)&(OBJ)
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
checkTable()
|
|
|
|
|
|
{
|
|
|
|
|
|
RefArrayTable<I,20,Sub1> tab;
|
|
|
|
|
|
// creates 20 Sub1-objects in-place
|
|
|
|
|
|
// which are indeed located within the object
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (sizeof(tab) >= 20 * sizeof(Sub1));
|
|
|
|
|
|
CHECK (ADR(tab) < ADR(tab[19]) && ADR(tab[19]) < ADR(tab) + sizeof(tab));
|
2010-03-08 04:43:24 +01:00
|
|
|
|
|
|
|
|
|
|
RefArray<I> & rArr (tab);
|
|
|
|
|
|
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (20 == tab.size());
|
|
|
|
|
|
CHECK (INSTANCEOF(I, &rArr[0]));
|
2010-03-08 04:43:24 +01:00
|
|
|
|
for (size_t i=0; i<rArr.size(); ++i)
|
|
|
|
|
|
{
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (i*sizeof(Sub1) == ADR(rArr[i]) - ADR(rArr[0]) ); // indeed array-like storage
|
|
|
|
|
|
CHECK (int(i+1) == rArr[i].op(i)); // check the known result
|
2010-03-08 04:43:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
template<class SUB>
|
|
|
|
|
|
struct Fac ///< fabricating a series of subclass instances with varying ctor parameter
|
|
|
|
|
|
{
|
|
|
|
|
|
int offset_;
|
|
|
|
|
|
Fac ( ) : offset_ (0) {}
|
|
|
|
|
|
|
|
|
|
|
|
void operator() (void* place)
|
|
|
|
|
|
{
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (place);
|
2010-03-08 04:43:24 +01:00
|
|
|
|
new(place) SUB (offset_++); // note: varying ctor parameter
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
checkTable_inplaceCreation()
|
|
|
|
|
|
{
|
|
|
|
|
|
Fac<Sub1> theFact;
|
|
|
|
|
|
RefArrayTable<I,30,Sub1> tab (theFact);
|
|
|
|
|
|
RefArray<I> & rArr (tab);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (30 == tab.size());
|
2010-03-08 04:43:24 +01:00
|
|
|
|
for (size_t i=0; i<rArr.size(); ++i)
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (int(i+i) == rArr[i].op(i)); // each one has gotten another offset ctor parameter
|
2010-03-08 04:43:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
|
checkTable_errorHandling()
|
|
|
|
|
|
{
|
|
|
|
|
|
for (uint i=0; i<500; ++i)
|
|
|
|
|
|
{
|
|
|
|
|
|
Sub3::sum = 0;
|
2024-11-13 02:23:23 +01:00
|
|
|
|
Sub3::trigger = rani(50); // when hitting the trigger Sub3 throws
|
2010-03-08 04:43:24 +01:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
{
|
|
|
|
|
|
Fac<Sub3> factory;
|
|
|
|
|
|
RefArrayTable<I,30,Sub3> table (factory);
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (Sub3::sum == (29+1)*29/2);
|
2010-03-08 04:43:24 +01:00
|
|
|
|
}
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (Sub3::sum == 0);
|
2010-03-08 04:43:24 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
catch(long id)
|
|
|
|
|
|
{
|
2010-12-10 02:55:40 +01:00
|
|
|
|
CHECK (id == Sub3::trigger);
|
|
|
|
|
|
CHECK (Sub3::sum == id);
|
2010-03-08 04:43:24 +01:00
|
|
|
|
// meaning: all objects have been cleaned up,
|
|
|
|
|
|
// with the exception of the one hitting the trigger
|
|
|
|
|
|
} }
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** Register this test class... */
|
|
|
|
|
|
LAUNCHER (RefArray_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::test
|