simple vector wrapping implementation of the RefArray interface
This commit is contained in:
parent
1b4aa98cdf
commit
1c388287c9
3 changed files with 182 additions and 0 deletions
69
src/lib/refarrayimpl.hpp
Normal file
69
src/lib/refarrayimpl.hpp
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
/*
|
||||
REFARRAYIMPL.hpp - some implementations of the ref-array interface
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2008, Hermann Vosseler <Ichthyostega@web.de>
|
||||
|
||||
This program 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.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
*/
|
||||
|
||||
|
||||
#ifndef LIB_REFARRAYIMPL_H
|
||||
#define LIB_REFARRAYIMPL_H
|
||||
|
||||
|
||||
#include "lib/refarray.hpp"
|
||||
#include "proc/nobugcfg.hpp"
|
||||
|
||||
#include <vector>
|
||||
using std::vector;
|
||||
|
||||
|
||||
|
||||
namespace lib {
|
||||
|
||||
/**
|
||||
* Wrap a vector holding objects of a subtype
|
||||
* and provide array-like access using the interface type.
|
||||
*/
|
||||
template<class E, class IM = E>
|
||||
class RefArrayVectorWrapper
|
||||
: public RefArray<E>
|
||||
{
|
||||
typedef vector<IM> const& Tab;
|
||||
Tab table_;
|
||||
|
||||
public:
|
||||
|
||||
RefArrayVectorWrapper (Tab toWrap)
|
||||
: table_(toWrap)
|
||||
{ }
|
||||
|
||||
virtual size_t size() const
|
||||
{
|
||||
return table_.size();
|
||||
}
|
||||
|
||||
virtual E const& operator[] (uint i) const
|
||||
{
|
||||
REQUIRE (i < size());
|
||||
return table_[i];
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
} // namespace lib
|
||||
#endif
|
||||
|
|
@ -308,6 +308,11 @@ out: ::0::1::2::3::4::5::6::7::8::9::10::11::12::13::14::15::16::17::18::19
|
|||
END
|
||||
|
||||
|
||||
TEST "RefArray_test" RefArray_test <<END
|
||||
return: 0
|
||||
END
|
||||
|
||||
|
||||
TEST "RemoveFromSet_test" RemoveFromSet_test <<END
|
||||
out: removed nothing ---> \[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, \]
|
||||
out: removed 0 ---> \[ 1, 2, 3, 4, 5, 6, 7, 8, 9, \]
|
||||
|
|
|
|||
108
tests/common/refarraytest.cpp
Normal file
108
tests/common/refarraytest.cpp
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
/*
|
||||
RefArray(Test) - unittest for wrapping with array-of-refs access
|
||||
|
||||
Copyright (C) Lumiera.org
|
||||
2008, Hermann Vosseler <Ichthyostega@web.de>
|
||||
|
||||
This program 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.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program; if not, write to the Free Software
|
||||
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
|
||||
|
||||
* *****************************************************/
|
||||
|
||||
|
||||
#include "common/test/run.hpp"
|
||||
#include "common/util.hpp"
|
||||
|
||||
#include "lib/refarrayimpl.hpp"
|
||||
|
||||
//#include <boost/lexical_cast.hpp>
|
||||
//#include <boost/format.hpp>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
//using boost::lexical_cast;
|
||||
//using boost::format;
|
||||
//using util::isnil;
|
||||
//using std::string;
|
||||
using test::Test;
|
||||
using std::cout;
|
||||
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 op (int i) { return i+1; }
|
||||
};
|
||||
|
||||
struct Sub2 : I
|
||||
{
|
||||
const char* letterz_;
|
||||
Sub2() : letterz_("ABCDEFGHIJKLMNOPQRSTUVWXYZ") {}
|
||||
|
||||
int op (int i) const { return (int)letterz_[i % 26]; }
|
||||
};
|
||||
|
||||
} // (END) test types
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/*******************************************************************
|
||||
* @test build a wrapper providing RefArray access to a given vector
|
||||
* @see refarrayimpl.hpp
|
||||
*/
|
||||
class RefArray_test : public Test
|
||||
{
|
||||
|
||||
virtual void
|
||||
run (Arg arg)
|
||||
{
|
||||
vector<Sub2> subz(10);
|
||||
RefArrayVectorWrapper<I,Sub2> subWrapz (subz);
|
||||
|
||||
RefArray<I> & rArr (subWrapz);
|
||||
|
||||
ASSERT (subWrapz.size()==subz.size());
|
||||
ASSERT (INSTANCEOF(I, &rArr[0]));
|
||||
for (size_t i=0; i<subz.size(); ++i)
|
||||
ASSERT (rArr[i].op(i) == subz[i].op(i));
|
||||
}
|
||||
|
||||
|
||||
};
|
||||
|
||||
|
||||
|
||||
/** Register this test class... */
|
||||
LAUNCHER (RefArray_test, "unit common");
|
||||
|
||||
|
||||
|
||||
} // namespace test
|
||||
|
||||
} // namespace lib
|
||||
Loading…
Reference in a new issue