From 3d2791b91eafebd495d307d47e03b520db5f1215 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Wed, 2 Apr 2008 04:06:08 +0200 Subject: [PATCH] WIP some fixes; add test for a utility function --- src/proc/mobject/session/defsregistry.hpp | 6 +- tests/components/common/removefromsettest.cpp | 108 ++++++++++++++++++ .../mobject/session/defsregistryimpltest.cpp | 69 +++++------ 3 files changed, 146 insertions(+), 37 deletions(-) create mode 100644 tests/components/common/removefromsettest.cpp diff --git a/src/proc/mobject/session/defsregistry.hpp b/src/proc/mobject/session/defsregistry.hpp index d52867f42..5a2328867 100644 --- a/src/proc/mobject/session/defsregistry.hpp +++ b/src/proc/mobject/session/defsregistry.hpp @@ -165,11 +165,6 @@ namespace mobject { Table table_; - protected: - - DefsRegistry () {}; - friend class DefsManager; - public: /** used for enumerating solutions */ template @@ -208,6 +203,7 @@ namespace mobject public: shared_ptr operator* () { return ptr; } bool hasNext () { return next || findNext(); } + Iter operator++ (int) { Iter tmp=*this; operator++(); return tmp; } Iter& operator++ () { ptr=findNext(); diff --git a/tests/components/common/removefromsettest.cpp b/tests/components/common/removefromsettest.cpp new file mode 100644 index 000000000..bf1629378 --- /dev/null +++ b/tests/components/common/removefromsettest.cpp @@ -0,0 +1,108 @@ +/* + RemoveFromSet(Test) - algorithm removing predicated elements from set + + Copyright (C) Lumiera.org + 2008, 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 "common/test/run.hpp" +#include "common/util.hpp" + +#include +#include +//#include +#include +#include +#include +#include + +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 + 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 (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 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 + diff --git a/tests/components/proc/mobject/session/defsregistryimpltest.cpp b/tests/components/proc/mobject/session/defsregistryimpltest.cpp index 49c2fb343..6a01f15c0 100644 --- a/tests/components/proc/mobject/session/defsregistryimpltest.cpp +++ b/tests/components/proc/mobject/session/defsregistryimpltest.cpp @@ -34,16 +34,17 @@ #include 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 > Q13; typedef Query > Q23; - + + typedef DefsRegistry::Iter > Iter13; + typedef DefsRegistry::Iter > Iter23; + + // fabricating Objects wrapped into smart-ptrs - lumiera::factory::RefcountPtr oFac; - lumiera::factory::RefcountPtr

pFac; + lumiera::factory::RefcountPtr > oFac; + lumiera::factory::RefcountPtr > pFac; O o1, o2, o3; Q13 q1, q2, q3, q4, q5; map 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 > 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 > 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