Lumiera 0.pre.04
»edit your freedom«
Loading...
Searching...
No Matches
function-closure-test.cpp
Go to the documentation of this file.
1/*
2 FunctionClosure(Test) - appending, mixing and filtering typelists
3
4 Copyright (C)
5 2009, Hermann Vosseler <Ichthyostega@web.de>
6
7  **Lumiera** is free software; you can redistribute it and/or modify it
8  under the terms of the GNU General Public License as published by the
9  Free Software Foundation; either version 2 of the License, or (at your
10  option) any later version. See the file COPYING for further details.
11
12* *****************************************************************/
13
14
29#include "lib/test/run.hpp"
31#include "lib/meta/typelist.hpp"
33#include "lib/meta/function.hpp"
37
38
39using std::string;
40
41
42namespace lib {
43namespace meta {
44namespace test {
45
46
47 namespace { // test data
48
49 using List1 = Types<Num<1>, Num<2>, Num<3> >::List;
50 using List2 = Types<Num<5>, Num<6>, Num<7> >::List;
51
52
55 template<char i,char ii, char iii>
56 int
58 {
59 return one.o_ + two.o_ + three.o_;
60 }
61
62
63 int fun0 () { return -1; }
64 int fun1 (int i1) { return i1; }
65 int fun2 (int i1, int i2) { return i1+i2; }
66 int fun3 (int i1, int i2, int i3) { return i1+i2+i3; }
67
68 } // (End) test data
69
70
71
72
74
75
76 /*********************************************************************/
92 class FunctionClosure_test : public Test
93 {
94 virtual void
95 run (Arg)
96 {
101 }
102
103
108 void
110 {
111 DISPLAY (List1);
112 DISPLAY (List2);
113 ;
114 CHECK (6 == (getNumberz<1,2,3> (Num<1>(), Num<2>(), Num<3>())));
115 CHECK (6 == (getNumberz<1,1,1> (Num<1>(), Num<1>(2), Num<1>(3))));
116 }
117
118
119 void
121 {
122 typedef int someFunc(Num<5>,Num<9>);
123 typedef _Fun<someFunc>::Ret RetType; // should be int
124 typedef _Fun<someFunc>::Args Args;
125 DISPLAY (Args);
126
127 typedef Prepend<Num<1>, Args>::Seq NewArgs; // manipulate the argument type(s)
129
130 typedef BuildFunType<RetType,NewArgs>::Sig NewSig; // re-build a new function signature
131
132 NewSig& fun = getNumberz<1,5,9>; //...which is compatible to an existing real function signature!
133
134 CHECK (1+5+9 == fun(Num<1>(), Num<5>(), Num<9>()));
135 }
136
137
138 void
140 {
141 cout << "\t:\n\t: ---Bind----\n";
142
145 Tuple<Types<int,int>> tup2 (11,12);
146 Tuple<Types<int,int,int>> tup3 (11,12,13);
147 DUMPVAL (tup0);
148 DUMPVAL (tup1);
149 DUMPVAL (tup2);
150 DUMPVAL (tup3);
151
152 using BoundFun = function<int()>;
153
154 BoundFun functor0 = bindArgTuple (fun0, tup0);
155 BoundFun functor1 = bindArgTuple (fun1, tup1);
156 BoundFun functor2 = bindArgTuple (fun2, tup2);
157 BoundFun functor3 = bindArgTuple (fun3, tup3);
158
159 CHECK (-1 == functor0() );
160 CHECK (11 == functor1() );
161 CHECK (11+12 == functor2() );
162 CHECK (11+12+13 == functor3() );
163
164 }
165
166
167 void
169 {
172 Tuple<Types<int,int>> tup2 (11,12);
173 Tuple<Types<int,int,int>> tup3 (11,12,13);
174 function<int()> unbound_functor0 (fun0);
175 function<int(int)> unbound_functor1 (fun1);
176 function<int(int,int)> unbound_functor2 (fun2);
177 function<int(int,int,int)> unbound_functor3 (fun3);
178
179 using BoundFun = function<int()>;
180
181 BoundFun functor0 = bindArgTuple (unbound_functor0, tup0);
182 BoundFun functor1 = bindArgTuple (unbound_functor1, tup1);
183 BoundFun functor2 = bindArgTuple (unbound_functor2, tup2);
184 BoundFun functor3 = bindArgTuple (unbound_functor3, tup3);
185
186 CHECK (-1 == functor0() );
187 CHECK (11 == functor1() );
188 CHECK (11+12 == functor2() );
189 CHECK (11+12+13 == functor3() );
190
191 }
192 };
193
194
197
198
199
200}}} // namespace lib::meta::test
void verify_setup()
verify the test input data
Partial function application and building a complete function closure.
Metaprogramming tools for detecting and transforming function types.
auto bindArgTuple(FUN &&fun, TUP &&tuple)
Base technique: build a binder with arguments from a tuple.
int getNumberz(Num< i > one, Num< ii > two, Num< iii > three)
special test fun accepting the terrific Num types
enable_if_c< Cond::value, T >::type enable_if
SFINAE helper to control the visibility of specialisations and overloads.
Definition meta/util.hpp:87
Build function types from given Argument types.
Definition function.hpp:236
Implementation namespace for support and library code.
Test runner and basic definitions for tests.
Simplistic test class runner.
#define LAUNCHER(_TEST_CLASS_, _GROUPS_)
Definition run.hpp:116
Trait template for uniform access to function signature types.
Definition function.hpp:144
A collection of frequently used helper functions to support unit testing.
an extension to typelist-diagnostics.hpp, allowing to dump the contents of a Tuple datatype.
Support for writing metaprogramming unit-tests dealing with typelists and flags.
#define DISPLAY(_IT_)
#define DUMPVAL(_IT_)
Metaprogramming: Helpers for manipulating lists-of-types.
A template metaprogramming technique for manipulating collections of types.