WIP some fixes; add test for a utility function

This commit is contained in:
Fischlurch 2008-04-02 04:06:08 +02:00
parent e9a364f7ad
commit 3d2791b91e
3 changed files with 146 additions and 37 deletions

View file

@ -165,11 +165,6 @@ namespace mobject
{
Table table_;
protected:
DefsRegistry () {};
friend class DefsManager;
public:
/** used for enumerating solutions */
template<class TAR>
@ -208,6 +203,7 @@ namespace mobject
public:
shared_ptr<TAR> operator* () { return ptr; }
bool hasNext () { return next || findNext(); }
Iter operator++ (int) { Iter tmp=*this; operator++(); return tmp; }
Iter& operator++ ()
{
ptr=findNext();

View file

@ -0,0 +1,108 @@
/*
RemoveFromSet(Test) - algorithm removing predicated elements from set
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 <boost/lexical_cast.hpp>
#include <boost/lambda/lambda.hpp>
//#include <boost/function.hpp>
#include <boost/bind.hpp>
#include <iostream>
#include <string>
#include <set>
using std::cout;
using std::string;
namespace util
{
namespace test
{
using util::for_each;
using boost::lambda::_1;
// using boost::function;
using boost::lexical_cast;
using boost::bind;
template<class COLL>
void
show (COLL const& coll)
{
cout << "[ ";
for_each (coll, cout << _1 << ", ");
cout << "]\n";
}
bool
killerselector (string description, uint candidate)
{
return string::npos != description.find( lexical_cast<string> (candidate));
}
class RemoveFromSet_test : public Test
{
virtual void run (Arg arg)
{
test_remove (" nothing ");
test_remove ("0");
test_remove ("9");
test_remove ("5");
test_remove ("0 2 4 6 8");
test_remove ("1 3 5 7 9");
test_remove ("0 1 2 3 4 5 6 7 8 9");
test_remove ("0 1 2 3 4 5 6 7 8 ");
test_remove (" 1 2 3 4 5 6 7 8 9");
test_remove ("0 1 2 3 4 6 7 8 9");
}
/** @test populate a test set, remove the denoted elements
* and print the result
*/
test_remove (string elems_to_remove)
{
std::set<uint> theSet;
for (int i=0; i<10; ++i)
theSet.push_back (i);
util::remove_if (theSet, bind( killerselector, elems_to_remove, _1));
show (theSet);
}
};
LAUNCHER (RemoveFromSet_test, "unit common");
} // namespace test
} // namespace util

View file

@ -34,16 +34,17 @@
#include <map>
using lumiera::Query;
using util::isnil;
using std::tr1::shared_ptr;
using boost::scoped_ptr;
using boost::format;
using util::isnil;
using std::string;
using std::rand;
using std::map;
namespace mobject
{
namespace session
@ -62,7 +63,7 @@ namespace mobject
/** create a random new ID */
string
newID (Symbol prefix)
newID (string prefix)
{
return str (instancePatt % prefix % rand());
}
@ -83,9 +84,19 @@ namespace mobject
/** fabricating (random) query strings */
string
q_str (int degree=0)
garbage_term () ///< yields a random string of 3 letters
{
return str (predicatePatt
% char ('a'+ rand() % 26)
% rand() % 100
% garbage.substr(rand() % 17 , 3)
);
}
string
q_str (int degree=0) ///< fabricating (random) query strings
{
string fake;
if (!degree)
@ -96,15 +107,6 @@ namespace mobject
return fake;
}
string
garbage_term ()
{
return (predicatePatt
% char ('a'+ rand() % 26)
% rand() % 100
% garbage.substr(rand() % 17 , 3)
);
}
/************************************************************************
@ -123,15 +125,20 @@ namespace mobject
typedef Query<Dummy<13> > Q13;
typedef Query<Dummy<23> > Q23;
typedef DefsRegistry::Iter<Dummy<13> > Iter13;
typedef DefsRegistry::Iter<Dummy<23> > Iter23;
// fabricating Objects wrapped into smart-ptrs
lumiera::factory::RefcountPtr<O> oFac;
lumiera::factory::RefcountPtr<P> pFac;
lumiera::factory::RefcountPtr<Dummy<13> > oFac;
lumiera::factory::RefcountPtr<Dummy<23> > pFac;
O o1, o2, o3;
Q13 q1, q2, q3, q4, q5;
map<Q23, P> ps;
public:
DefsRegistryImpl_test ()
: o1 (oFac()), o2 (oFac()), o3 (oFac()),
q1 (q_str (1)),
@ -165,7 +172,7 @@ namespace mobject
reg_->put (o3, q3);
reg_->put (o3, q2);
reg_->put (o2, q1);
reg_->put (o1, Q13); // the empty query
reg_->put (o1, Q13()); // the empty query
ps.clear();
for (int i=0; i<100; ++i)
@ -174,15 +181,14 @@ namespace mobject
Q23 qx (q_str());
ps[qx] = px;
reg_->put (px, qx);
px.instanceID = qx;
px->instanceID = qx;
}
}
void check_query ()
{
DefsRegistry::Iter<Dummy<13> > i;
i = reg_->candidates(Q13 ("irrelevant query"));
Iter13 i (reg_->candidates(Q13 ("irrelevant query")));
ASSERT ( i.hasNext());
ASSERT ( *i++ == o1); // ordered according to the degree of the queries
ASSERT ( *i++ == o2);
@ -202,7 +208,7 @@ namespace mobject
ASSERT ( *i++ == o1);
ASSERT (!i.hasNext());
i = reg_->candidates(Q13);
i = reg_->candidates(Q13());
ASSERT ( *i++ == o1); // found by direct match to the empty query
ASSERT ( *i++ == o1);
ASSERT ( *i++ == o2);
@ -214,22 +220,22 @@ namespace mobject
uint d=0;
uint d_prev=0;
for (i = reg_->candidates(Q23 ("some crap"));
i.hasNext(); ++i )
Iter23 j = reg_->candidates(Q23 ("some crap"));
for ( ; j.hasNext(); ++j )
{
ASSERT ( *i );
Q23 qx = (*i)->instanceID;
ASSERT ( ps[qx] == (*i));
ASSERT ( *j );
Q23 qx ((*j)->instanceID);
ASSERT ( ps[qx] == (*j));
d = lumiera::query::countPraed (qx);
ASSERT ( d_prev <= d );
d_prev = d;
}
ASSERT (!i.hasNext());
ASSERT (!j.hasNext());
// calling with an arbitrary (registered) query
// yields the corresponding object at start of the ennumeration
i = reg_->candidates(ps.begin()->first);
ASSERT ( *i == ps.begin()->second);
j = reg_->candidates(ps.begin()->first);
ASSERT ( *j == ps.begin()->second);
}
@ -238,8 +244,7 @@ namespace mobject
{
reg_->forget (o2);
DefsRegistry::Iter<Dummy<13> > i;
i = reg_->candidates(q4);
Iter13 i (reg_->candidates(q4));
ASSERT ( i.hasNext());
ASSERT ( *i++ == o1); // ordered according to the degree of the queries
// but the o2 entries are missing
@ -287,7 +292,7 @@ namespace mobject
ASSERT ( reg_->forget (o1)); // failure, because it's already removed
ASSERT (!reg_->forget (o2));
o3.reset (oFac()); // another object is another object (it's irrelevant...)
o3 = oFac(); // another object is another object (it's irrelevant...)
i = reg_->candidates(q2);
ASSERT (! (*i)); // empty