/* IterSource(Test) - how to build an opaque iterator-based data source Copyright (C) Lumiera.org 2010, Hermann Vosseler 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" #include "lib/test/test-helper.hpp" #include "lib/lumitime-fmt.hpp" #include "lib/util.hpp" #include "lib/iter-source.hpp" #include #include #include #include #include #include #include namespace lib { namespace test{ using ::Test; using lumiera::Time; using boost::lexical_cast; using boost::noncopyable; using lib::test::randStr; using lib::test::randTime; using util::isnil; using util::cStr; using std::string; using std::list; using std::cout; using std::endl; namespace { // Subject of test uint NUM_ELMS = 10; typedef const char* CStr; /** * Explicit implementation of the IterSource interface (test dummy) * Creates a random string and chops off a character on each iteration */ class TestSource : public IterSource , noncopyable { 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; } public: TestSource (uint num) : buffer_(randStr (num)) , current_(0) { INFO (test, "created TestSource(\"%s\")", cStr(buffer_)); } }; /** test dummy: simply wrapping an STL container * and exposing a range as Lumiera Forward Iterator */ struct WrappedList { list data_; WrappedList(uint num) { while (num) data_.push_back(num--); } typedef list::iterator sourceIter; typedef RangeIter iterator; iterator begin() { return iterator(data_.begin(),data_.end()); } iterator end() { return iterator(); } }; } // (END) impl test dummy containers /****************************************************************** * @test create some (opaque) data sources, * and then pull the data out by iteration. * Demonstrates simple usage of the IterSource interface * * @see IterSource * @see PlacementIndex::Table#_eachEntry_4check real world usage example */ class IterSource_test : public Test { typedef IterSource::iterator IntIter; typedef IterSource::iterator StrIter; typedef IterSource::iterator StringIter; typedef IterSource