2010-01-05 02:53:20 +01:00
|
|
|
/*
|
|
|
|
|
IterSource(Test) - how to build an opaque iterator-based data source
|
|
|
|
|
|
|
|
|
|
Copyright (C) Lumiera.org
|
|
|
|
|
2010, 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 "lib/test/run.hpp"
|
2010-01-05 05:21:13 +01:00
|
|
|
#include "lib/test/test-helper.hpp"
|
2010-01-05 02:53:20 +01:00
|
|
|
#include "lib/util.hpp"
|
|
|
|
|
|
|
|
|
|
#include "lib/iter-source.hpp"
|
|
|
|
|
|
|
|
|
|
#include <boost/lexical_cast.hpp>
|
2010-01-05 04:10:23 +01:00
|
|
|
#include <boost/noncopyable.hpp>
|
2010-01-05 02:53:20 +01:00
|
|
|
#include <iostream>
|
2010-01-05 05:21:13 +01:00
|
|
|
#include <string>
|
2010-01-05 02:53:20 +01:00
|
|
|
#include <list>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace lib {
|
|
|
|
|
namespace test{
|
|
|
|
|
|
|
|
|
|
using ::Test;
|
|
|
|
|
using boost::lexical_cast;
|
2010-01-05 04:10:23 +01:00
|
|
|
using boost::noncopyable;
|
2010-01-05 05:21:13 +01:00
|
|
|
using lib::test::randStr;
|
2010-01-05 02:53:20 +01:00
|
|
|
using util::isnil;
|
2010-01-05 05:21:13 +01:00
|
|
|
using util::cStr;
|
|
|
|
|
using std::string;
|
2010-01-05 02:53:20 +01:00
|
|
|
using std::list;
|
|
|
|
|
using std::cout;
|
|
|
|
|
using std::endl;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
namespace { // Subject of test
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint NUM_ELMS = 10;
|
|
|
|
|
|
2010-01-05 05:21:13 +01:00
|
|
|
typedef const char* CStr;
|
|
|
|
|
|
|
|
|
|
/**
|
2010-01-05 02:53:20 +01:00
|
|
|
* Explicit implementation of the IterSource interface (test dummy)
|
2010-01-05 05:21:13 +01:00
|
|
|
* Creates a random string and chops off a character on each iteration
|
2010-01-05 02:53:20 +01:00
|
|
|
*/
|
|
|
|
|
class TestSource
|
2010-01-05 05:21:13 +01:00
|
|
|
: public IterSource<CStr>
|
2010-01-05 04:10:23 +01:00
|
|
|
, noncopyable
|
2010-01-05 02:53:20 +01:00
|
|
|
{
|
2010-01-05 04:10:23 +01:00
|
|
|
|
2010-01-05 05:21:13 +01:00
|
|
|
string buffer_;
|
|
|
|
|
CStr current_;
|
|
|
|
|
|
|
|
|
|
virtual Pos
|
|
|
|
|
firstResult ()
|
|
|
|
|
{
|
|
|
|
|
current_ = buffer_.c_str();
|
|
|
|
|
ENSURE (current_);
|
|
|
|
|
return ¤t_;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
virtual void
|
|
|
|
|
nextResult (Pos& pos)
|
|
|
|
|
{
|
|
|
|
|
if (pos && *pos && **pos)
|
|
|
|
|
++(*pos);
|
|
|
|
|
|
|
|
|
|
if (!(pos && *pos && **pos))
|
|
|
|
|
pos = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2010-01-05 04:10:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
TestSource (uint num)
|
2010-01-05 05:21:13 +01:00
|
|
|
: buffer_(randStr (num))
|
|
|
|
|
, current_(0)
|
2010-01-05 04:10:23 +01:00
|
|
|
{
|
2010-01-05 05:21:13 +01:00
|
|
|
INFO (test, "created TestSource(\"%s\")", cStr(buffer_));
|
2010-01-05 04:10:23 +01:00
|
|
|
}
|
2010-01-05 02:53:20 +01:00
|
|
|
};
|
|
|
|
|
|
2010-01-05 05:21:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/** test dummy: simply wrapping an STL container
|
2010-01-05 02:53:20 +01:00
|
|
|
* and exposing a range as Lumiera Forward Iterator
|
|
|
|
|
*/
|
|
|
|
|
struct WrappedList
|
|
|
|
|
{
|
|
|
|
|
list<int> data_;
|
|
|
|
|
|
|
|
|
|
WrappedList(uint num)
|
|
|
|
|
{
|
|
|
|
|
while (num)
|
|
|
|
|
data_.push_back(num--);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
typedef list<int>::iterator sourceIter;
|
|
|
|
|
typedef RangeIter<sourceIter> iterator;
|
|
|
|
|
|
2010-01-05 05:21:13 +01:00
|
|
|
iterator begin() { return iterator(data_.begin(),data_.end()); }
|
|
|
|
|
iterator end() { return iterator(); }
|
2010-01-05 02:53:20 +01:00
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // (END) impl test dummy containers
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************************
|
|
|
|
|
* @test create some (opaque) data sources,
|
|
|
|
|
* and then pull the data out by iteration.
|
2010-01-05 05:21:13 +01:00
|
|
|
* Demonstrates simple usage of the IterSource interface
|
2010-01-05 02:53:20 +01:00
|
|
|
*
|
2010-01-05 05:21:13 +01:00
|
|
|
* @see IterSource
|
|
|
|
|
* @see PlacementIndex::Table#_eachEntry_4check real world usage example
|
2010-01-05 02:53:20 +01:00
|
|
|
*/
|
|
|
|
|
class IterSource_test : public Test
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
typedef IterSource<int>::iterator IntIter;
|
2010-01-05 05:21:13 +01:00
|
|
|
typedef IterSource<CStr>::iterator StrIter;
|
|
|
|
|
|
2010-01-05 02:53:20 +01:00
|
|
|
virtual void
|
|
|
|
|
run (Arg arg)
|
|
|
|
|
{
|
|
|
|
|
if (0 < arg.size()) NUM_ELMS = lexical_cast<uint> (arg[0]);
|
|
|
|
|
|
|
|
|
|
// build the test data sources
|
|
|
|
|
WrappedList customList(NUM_ELMS);
|
|
|
|
|
TestSource dedicatedSource(NUM_ELMS);
|
|
|
|
|
|
|
|
|
|
IntIter iii (eachEntry (customList));
|
2010-01-05 05:21:13 +01:00
|
|
|
StrIter cii (IterSource<CStr>::build(dedicatedSource));
|
2010-01-05 02:53:20 +01:00
|
|
|
|
|
|
|
|
ASSERT (!isnil (iii));
|
|
|
|
|
ASSERT (!isnil (cii));
|
|
|
|
|
|
|
|
|
|
pullOut (iii);
|
|
|
|
|
pullOut (cii);
|
|
|
|
|
|
|
|
|
|
ASSERT (!iii);
|
|
|
|
|
ASSERT (!cii);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
template<class IT>
|
|
|
|
|
void
|
2010-01-05 05:21:13 +01:00
|
|
|
pullOut (IT& iter)
|
2010-01-05 02:53:20 +01:00
|
|
|
{
|
2010-01-05 05:21:13 +01:00
|
|
|
for ( ; iter; ++iter )
|
2010-01-05 02:53:20 +01:00
|
|
|
cout << "::" << *iter;
|
|
|
|
|
cout << endl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
LAUNCHER (IterSource_test, "unit common");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}} // namespace lib::test
|
|
|
|
|
|