/* FunctionClosure(Test) - appending, mixing and filtering typelists Copyright (C) 2009, Hermann Vosseler   **Lumiera** 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. See the file COPYING for further details. * *****************************************************************/ /** @file function-closure-test.cpp ** Testing a combination of std::function objects and metaprogramming. ** Argument types will be extracted and represented as typelist, so they ** can be manipulated at compile time. This test uses some test functions ** and systematically applies or binds them to corresponding data tuples. ** Moreover, closure objects will be constructed in various flavours, ** combining a function object and a set of parameters. ** ** @see function-closure.hpp ** @see control::CmdClosure real world usage example ** */ #include "lib/test/run.hpp" #include "lib/test/test-helper.hpp" #include "lib/meta/typelist.hpp" #include "lib/meta/typelist-manip.hpp" #include "lib/meta/function.hpp" #include "lib/meta/function-closure.hpp" #include "meta/typelist-diagnostics.hpp" #include "meta/tuple-diagnostics.hpp" #include using ::test::Test; using std::string; using std::cout; using std::endl; namespace lib { namespace meta { namespace test { namespace { // test data typedef TySeq< Num<1> , Num<2> , Num<3> >::List List1; typedef TySeq< Num<5> , Num<6> , Num<7> >::List List2; /** special test fun * accepting the terrific Num types */ template int getNumberz (Num one, Num two, Num three) { return one.o_ + two.o_ + three.o_; } int fun0 () { return -1; } int fun1 (int i1) { return i1; } int fun2 (int i1, int i2) { return i1+i2; } int fun3 (int i1, int i2, int i3) { return i1+i2+i3; } } // (End) test data using func::Apply; using func::TupleApplicator; using func::FunctionClosure; using func::closure; using func::apply; /*********************************************************************//** * @test building a function closure for a given function or functor, * while arguments are passed in as tuple * - accessing signatures as typelists * - apply free function to tuple * - apply functor to tuple * - bind free function to tuple * - bind functor to tuple * - build a simple "tuple closure" * @remark this test is _rather low-level_ and documents the construction * of the implementation */ class FunctionClosure_test : public Test { virtual void run (Arg) { verify_setup(); check_signatureTypeManip(); check_applyFree(); check_applyFunc(); check_bindFree(); check_bindFunc(); build_closure(); } /** verify the test input data * @see TypeListManipl_test#check_diagnostics() for * explanation of the DISPLAY and EXPECT macros. */ void verify_setup() { DISPLAY (List1); DISPLAY (List2); ; CHECK (6 == (getNumberz<1,2,3> (Num<1>(), Num<2>(), Num<3>()))); CHECK (6 == (getNumberz<1,1,1> (Num<1>(), Num<1>(2), Num<1>(3)))); } void check_signatureTypeManip () { typedef int someFunc(Num<5>,Num<9>); typedef _Fun::Ret RetType; // should be int typedef _Fun::Args Args; DISPLAY (Args); typedef Prepend, Args>::Seq NewArgs; // manipulate the argument type(s) DISPLAY (NewArgs); typedef BuildFunType::Sig NewSig; // re-build a new function signature NewSig& fun = getNumberz<1,5,9>; //...which is compatible to an existing real function signature! CHECK (1+5+9 == fun(Num<1>(), Num<5>(), Num<9>())); } void check_applyFree () { cout << "\t:\n\t: ---Apply---\n"; Tuple> tup0 ; Tuple> tup1 (11); Tuple> tup2 (11,12); Tuple> tup3 (11,12,13); DUMPVAL (tup0); DUMPVAL (tup1); DUMPVAL (tup2); DUMPVAL (tup3); CHECK (-1 == Apply<0>::invoke (fun0, tup0) ); CHECK (11 == Apply<1>::invoke (fun1, tup1) ); CHECK (11+12 == Apply<2>::invoke (fun2, tup2) ); CHECK (11+12+13 == Apply<3>::invoke (fun3, tup3) ); CHECK (-1 == TupleApplicator (tup0) (fun0) ); CHECK (11 == TupleApplicator (tup1) (fun1) ); CHECK (11+12 == TupleApplicator (tup2) (fun2) ); CHECK (11+12+13 == TupleApplicator (tup3) (fun3) ); CHECK (-1 == apply(fun0, tup0) ); CHECK (11 == apply(fun1, tup1) ); CHECK (11+12 == apply(fun2, tup2) ); CHECK (11+12+13 == apply(fun3, tup3) ); } void check_applyFunc () { Tuple> tup0 ; Tuple> tup1 (11); Tuple> tup2 (11,12); Tuple> tup3 (11,12,13); function functor0 (fun0); function functor1 (fun1); function functor2 (fun2); function functor3 (fun3); CHECK (-1 == Apply<0>::invoke (functor0, tup0) ); CHECK (11 == Apply<1>::invoke (functor1, tup1) ); CHECK (11+12 == Apply<2>::invoke (functor2, tup2) ); CHECK (11+12+13 == Apply<3>::invoke (functor3, tup3) ); CHECK (-1 == TupleApplicator (tup0) (functor0) ); CHECK (11 == TupleApplicator (tup1) (functor1) ); CHECK (11+12 == TupleApplicator (tup2) (functor2) ); CHECK (11+12+13 == TupleApplicator (tup3) (functor3) ); CHECK (-1 == apply(functor0, tup0) ); CHECK (11 == apply(functor1, tup1) ); CHECK (11+12 == apply(functor2, tup2) ); CHECK (11+12+13 == apply(functor3, tup3) ); } void check_bindFree () { cout << "\t:\n\t: ---Bind----\n"; Tuple> tup0 ; Tuple> tup1 (11); Tuple> tup2 (11,12); Tuple> tup3 (11,12,13); typedef function BoundFun; BoundFun functor0 = Apply<0>::bind (fun0, tup0); BoundFun functor1 = Apply<1>::bind (fun1, tup1); BoundFun functor2 = Apply<2>::bind (fun2, tup3); BoundFun functor3 = Apply<3>::bind (fun3, tup3); CHECK (-1 == functor0() ); CHECK (11 == functor1() ); CHECK (11+12 == functor2() ); CHECK (11+12+13 == functor3() ); functor0 = TupleApplicator (tup0).bind (fun0); functor1 = TupleApplicator (tup1).bind (fun1); functor2 = TupleApplicator (tup2).bind (fun2); functor3 = TupleApplicator (tup3).bind (fun3); CHECK (-1 == functor0() ); CHECK (11 == functor1() ); CHECK (11+12 == functor2() ); CHECK (11+12+13 == functor3() ); } void check_bindFunc () { Tuple> tup0 ; Tuple> tup1 (11); Tuple> tup2 (11,12); Tuple> tup3 (11,12,13); function unbound_functor0 (fun0); function unbound_functor1 (fun1); function unbound_functor2 (fun2); function unbound_functor3 (fun3); typedef function BoundFun; BoundFun functor0 = Apply<0>::bind (unbound_functor0, tup0); BoundFun functor1 = Apply<1>::bind (unbound_functor1, tup1); BoundFun functor2 = Apply<2>::bind (unbound_functor2, tup3); BoundFun functor3 = Apply<3>::bind (unbound_functor3, tup3); CHECK (-1 == functor0() ); CHECK (11 == functor1() ); CHECK (11+12 == functor2() ); CHECK (11+12+13 == functor3() ); functor0 = TupleApplicator (tup0).bind (unbound_functor0); functor1 = TupleApplicator (tup1).bind (unbound_functor1); functor2 = TupleApplicator (tup2).bind (unbound_functor2); functor3 = TupleApplicator (tup3).bind (unbound_functor3); CHECK (-1 == functor0() ); CHECK (11 == functor1() ); CHECK (11+12 == functor2() ); CHECK (11+12+13 == functor3() ); } void build_closure () { Tuple> tup0 ; Tuple> tup1 (11); Tuple> tup2 (11,12); Tuple> tup3 (11,12,13); FunctionClosure clo0 (fun0,tup0); FunctionClosure clo1 (fun1,tup1); FunctionClosure clo2 (fun2,tup2); FunctionClosure clo3 (fun3,tup3); CHECK (-1 == clo0() ); CHECK (11 == clo1() ); CHECK (11+12 == clo2() ); CHECK (11+12+13 == clo3() ); function unbound_functor0 (fun0); function unbound_functor1 (fun1); function unbound_functor2 (fun2); function unbound_functor3 (fun3); clo0 = FunctionClosure (unbound_functor0,tup0); clo1 = FunctionClosure (unbound_functor1,tup1); clo2 = FunctionClosure (unbound_functor2,tup2); clo3 = FunctionClosure (unbound_functor3,tup3); CHECK (-1 == clo0() ); CHECK (11 == clo1() ); CHECK (11+12 == clo2() ); CHECK (11+12+13 == clo3() ); CHECK (-1 == closure(fun0,tup0) () ); CHECK (11 == closure(fun1,tup1) () ); CHECK (11+12 == closure(fun2,tup2) () ); CHECK (11+12+13 == closure(fun3,tup3) () ); CHECK (-1 == closure(unbound_functor0,tup0) () ); CHECK (11 == closure(unbound_functor1,tup1) () ); CHECK (11+12 == closure(unbound_functor2,tup2) () ); CHECK (11+12+13 == closure(unbound_functor3,tup3) () ); // finally combine all techniques.... using NumberzArg = TySeq::Seq; using NumberzSig = BuildFunType::Sig; Tuple numberzTup (Num<5>(22), Num<6>(33), Num<7>(44)); FunctionClosure numClo (getNumberz<5,6,7>, numberzTup ); CHECK (22+33+44 == numClo() ); } }; /** Register this test class... */ LAUNCHER (FunctionClosure_test, "unit common"); }}} // namespace lib::meta::test