From ec4b2eef00f3e2ec29a349bc57055b9aa6f4a651 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Mon, 26 Oct 2009 01:39:25 +0100 Subject: [PATCH] WIP: test-driven brainstorming: how to build the query dispatcher table? --- src/proc/mobject/session/query-resolver.cpp | 6 +- src/proc/mobject/session/query-resolver.hpp | 15 +- .../mobject/session/query-resolver-test.cpp | 74 +++++++++- tests/lib/multifact-argument-test.cpp | 137 ++++++++++++++++++ 4 files changed, 224 insertions(+), 8 deletions(-) create mode 100644 tests/lib/multifact-argument-test.cpp diff --git a/src/proc/mobject/session/query-resolver.cpp b/src/proc/mobject/session/query-resolver.cpp index 323e1e3d7..3c5974b49 100644 --- a/src/proc/mobject/session/query-resolver.cpp +++ b/src/proc/mobject/session/query-resolver.cpp @@ -41,10 +41,14 @@ namespace session { QueryResolver::Resolution::~Resolution() { } + struct QueryDispatcher + { + + }; /** TODO??? */ - QueryResolver::Resolution + PReso QueryResolver::issue (Goal& query) { if (!canHandleQuery (query.getQID())) diff --git a/src/proc/mobject/session/query-resolver.hpp b/src/proc/mobject/session/query-resolver.hpp index a4e8d3544..6d095323b 100644 --- a/src/proc/mobject/session/query-resolver.hpp +++ b/src/proc/mobject/session/query-resolver.hpp @@ -31,6 +31,7 @@ #include "lib/iter-adapter.hpp" #include "lib/util.hpp" +#include #include //#include //#include @@ -46,6 +47,7 @@ namespace session { class Goal; class QueryResolver; + class QueryDispatcher; class QueryResolver::Resolution; /** Allow for taking ownership of a result set */ @@ -82,7 +84,7 @@ namespace session { void *cur_; protected: - void pointAt(void* p) { cur_ = p; } + void point_at(void* p) { cur_ = p; } template RES& @@ -141,10 +143,10 @@ namespace session { : public Goal::Result { public: - RES& operator* () { return access(); } - RES* operator->() { return & access(); } + RES& operator* () { return access(); } + RES* operator->() { return & access(); } - void pointAt(RES* r){ Goal::Result::pointAt(r);} + void point_at(RES* r){ Goal::Result::pointAt(r);} }; @@ -161,6 +163,7 @@ namespace session { */ class QueryResolver { + boost::scoped_ptr dispatcher_; public: @@ -189,11 +192,11 @@ namespace session { resultSet->nextResult(pos); } - virtual Result prepareResolution() =0; + virtual Result prepareResolution() =0; protected: - virtual Result const& nextResult(Result& pos) =0; + virtual void nextResult(Result& pos) =0; }; diff --git a/tests/components/proc/mobject/session/query-resolver-test.cpp b/tests/components/proc/mobject/session/query-resolver-test.cpp index c8301cbe7..9acb18ac6 100644 --- a/tests/components/proc/mobject/session/query-resolver-test.cpp +++ b/tests/components/proc/mobject/session/query-resolver-test.cpp @@ -28,6 +28,7 @@ //#include "proc/mobject/session/test-scopes.hpp" //#include "proc/mobject/placement-index.hpp" #include "proc/mobject/session/query-resolver.hpp" +//#include "lib/symbol.hpp" //#include "lib/access-casted.hpp" //#include "lib/variant.hpp" //#include "lib/meta/typelist.hpp" @@ -51,10 +52,81 @@ namespace test { namespace { // a test query resolving facility - + + template + class DummySolution; + + class DummySolution + { + int resNr_; + + public: + DummySolution() : resNr_(7) {} + + int* next () { --resNr_; return &resNr_; } + bool exhausted() { return bool(resNr_); } + }; + + class DummySolution + : DummySolution + { + string currentText_; + + public: + string* + next () + { + static char* lumi ="Lumiera"; + currentText_ = string (lumi[*DummySolution::next()]); + return ¤tText_; + } + }; + + template + struct DummyResultSet + : QueryResolver::Resolution + { + DummySolution solution_; + + typedef typename Query::Cursor Cursor; + + Result + prepareResolution() + { + Cursor cursor; + cursor.pointAt (solution_.next()); + return cursor; + } + + void + nextResult(Result& pos) + { + Cursor& cursor = static_cast (pos); + + if (solution_.exhausted()) + cursor.point_at (0); + else + cursor.point_at (solution_.next()); + } + }; + class TypeMatchFilter : public QueryResolver { + bool + canHandleQuery (Goal::QueryID qID) const + { + return Goal::GENERIC == qID.kind + && (wantResultType (qID) + ||wantResultType(qID)); + } + + template + bool + wantResultType (Goal::QueryID qID) + { + return qID.type == ResultType::ID::get(); + } }; diff --git a/tests/lib/multifact-argument-test.cpp b/tests/lib/multifact-argument-test.cpp new file mode 100644 index 000000000..d2df7c886 --- /dev/null +++ b/tests/lib/multifact-argument-test.cpp @@ -0,0 +1,137 @@ +/* + MultiFact(Test) - unittest for the configurable object-family creating factory + + Copyright (C) Lumiera.org + 2009, 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/multifact.hpp" +#include "lib/util.hpp" + +#include +#include +#include + + + +namespace lib { +namespace test{ + + using boost::lexical_cast; + using lib::test::showSizeof; + using util::isSameObject; + using util::isnil; + using std::ostream; + using std::string; + using std::cout; + using std::endl; + + using lumiera::error::LUMIERA_ERROR_INVALID; + + + namespace { // hierarchy of test dummy objects + + struct Interface + { + virtual ~Interface() {}; + virtual operator string () =0; + }; + + inline ostream& operator<< (ostream& os, Interface& ifa) { return os << string(ifa); } + + + enum theID + { ONE = 1 + , TWO + , THR + , FOU + }; + + typedef factory::MultiFact TestFactory; + + + template + class Implementation + : public Interface + { + operator string() + { + return "Impl-"+lexical_cast (ii); + } + + public: + static theID getTypeID() { return ii; } + }; + + /** Factory instance for the tests... */ + TestFactory theFact; + + // Configure the products to be fabricated.... + TestFactory::Singleton > holder1 (theFact,ONE); + TestFactory::Singleton > holder2 (theFact,TWO); + TestFactory::Singleton > holder3 (theFact,THR); + TestFactory::Singleton > holder4 (theFact,FOU); + } + + + + + + /******************************************************************* + * @test verify simple setup of the MultiFact template. + * Define a hierarchy of test dummy objects, such as to + * register them automatically for creation through a suitable + * instantiation of MultiFact. Verify we get the correct product + * when invoking this MultiFac flavour. + * @see lib::MultiFact + */ + class MultiFact_test : public Test + { + void + run (Arg) + { + cout << theFact(ONE) << endl; + cout << theFact(TWO) << endl; + cout << theFact(THR) << endl; + cout << theFact(FOU) << endl; + cout << showSizeof (theFact) << endl; + + Interface & o1 = theFact(ONE); + Interface & o2 = theFact(ONE); + ASSERT (isSameObject(o1,o2)); + + TestFactory anotherFact; + ASSERT (isnil (anotherFact)); + VERIFY_ERROR (INVALID, anotherFact(ONE) ); + + TestFactory::Singleton > anotherSingletonHolder (anotherFact,ONE); + Interface & o3 = anotherFact(ONE); + ASSERT (isSameObject(o2,o3)); + } + }; + + + /** Register this test class... */ + LAUNCHER (MultiFact_test, "unit common"); + + + +}} // namespace lib::test