1 module ut.ctfe.reflection.functions;
2 
3 
4 import ut.ctfe.reflection;
5 
6 
7 
8 @("callMixin.add1")
9 unittest {
10     import std.traits: Unqual;
11 
12     enum mod = module_!"modules.functions";
13     enum add1 = mod.functionsByOverload[0];
14 
15     mixin(add1.importMixin);
16 
17     mixin(add1.fullyQualifiedName, `(1, 2)`).should == 4;
18     mixin(add1.fullyQualifiedName, `(2, 3)`).should == 6;
19 
20     // or, easier...
21     mixin(add1.callMixin(1, 2)).should == 4;
22     auto arg = 2;
23     mixin(add1.callMixin("arg", 2)).should == 5;
24 }
25 
26 
27 @("pointer.byOverload.add1.0")
28 unittest {
29     enum mod = module_!"modules.functions";
30     enum add1 = mod.functionsByOverload[0];
31 
32     mixin(add1.importMixin);
33 
34     auto ptr = pointer!add1;
35     static assert(is(typeof(ptr) == int function(int, int)));
36 
37     ptr(1, 2).should == 4;
38     ptr(2, 3).should == 6;
39 }
40 
41 @("pointer.byOverload.add1.1")
42 unittest {
43     enum mod = module_!"modules.functions";
44     enum add1 = mod.functionsByOverload[1];
45 
46     mixin(add1.importMixin);
47 
48     auto ptr = pointer!add1;
49     static assert(is(typeof(ptr) == double function(double, double)));
50 
51     ptr(1.0, 2.0).should == 4.0;
52     ptr(2.0, 3.0).should == 6.0;
53 }
54 
55 
56 @("pointer.bySymbol.add1")
57 unittest {
58     enum mod = module_!"modules.functions";
59     enum add1 = mod.functionsBySymbol[0];
60 
61     enum add1Int = add1.overloads[0];
62     enum add1Double = add1.overloads[1];
63 
64     mixin(add1Int.importMixin);
65 
66     auto ptrInt = pointer!add1Int;
67     static assert(is(typeof(ptrInt) == int function(int, int)));
68     ptrInt(1, 2).should == 4;
69     ptrInt(2, 3).should == 6;
70 
71     auto ptrDouble = pointer!add1Double;
72     static assert(is(typeof(ptrDouble) == double function(double, double)));
73     ptrDouble(1.0, 2.0).should == 4.0;
74     ptrDouble(2.0, 3.0).should == 6.0;
75 }
76 
77 
78 @("pointer.byOverload.withDefault")
79 unittest {
80     static import modules.functions;
81 
82     enum mod = module_!"modules.functions";
83     enum withDefault = mod.functionsByOverload[2];
84     static assert(withDefault.identifier == "withDefault");
85 
86     auto ptr = pointer!withDefault;
87     (ptr is &modules.functions.withDefault).should == true;
88 
89     ptr(1.1, 2.2).should ~ 3.3;
90     ptr(1.1).should ~ 34.4;
91 }
92 
93 
94 @("pointerMixin.byOverload.add1.0")
95 unittest {
96     enum mod = module_!"modules.functions";
97     enum add1 = mod.functionsByOverload[0];
98 
99     mixin(add1.importMixin);
100 
101     auto ptr = mixin(add1.pointerMixin);
102     static assert(is(typeof(ptr) == int function(int, int)));
103 
104     ptr(1, 2).should == 4;
105     ptr(2, 3).should == 6;
106 }
107 
108 
109 @("pointerMixin.byOverload.add1.1")
110 unittest {
111     enum mod = module_!"modules.functions";
112     enum add1 = mod.functionsByOverload[1];
113 
114     mixin(add1.importMixin);
115 
116     auto ptr = mixin(add1.pointerMixin);
117     static assert(is(typeof(ptr) == double function(double, double)));
118 
119     ptr(1.0, 2.0).should == 4.0;
120     ptr(2.0, 3.0).should == 6.0;
121 }
122 
123 
124 @("pointerMixin.bySymbol.add1")
125 unittest {
126     enum mod = module_!"modules.functions";
127     enum add1 = mod.functionsBySymbol[0];
128 
129     enum add1Int = add1.overloads[0];
130     enum add1Double = add1.overloads[1];
131 
132     mixin(add1Int.importMixin);
133 
134     auto ptrInt = mixin(add1Int.pointerMixin);
135     static assert(is(typeof(ptrInt) == int function(int, int)));
136     ptrInt(1, 2).should == 4;
137     ptrInt(2, 3).should == 6;
138 
139     auto ptrDouble = mixin(add1Double.pointerMixin);
140     static assert(is(typeof(ptrDouble) == double function(double, double)));
141     ptrDouble(1.0, 2.0).should == 4.0;
142     ptrDouble(2.0, 3.0).should == 6.0;
143 }
144 
145 
146 @("pointerMixin.byOverload.withDefault")
147 unittest {
148     static import modules.functions;
149 
150     enum mod = module_!"modules.functions";
151     enum withDefault = mod.functionsByOverload[2];
152     static assert(withDefault.identifier == "withDefault");
153 
154     auto ptr = mixin(withDefault.pointerMixin);
155     (ptr is &modules.functions.withDefault).should == true;
156 
157     ptr(1.1, 2.2).should ~ 3.3;
158     ptr(1.1).should ~ 34.4;
159 }
160 
161 
162 @("functions.byOverload")
163 @safe pure unittest {
164     static import modules.functions;
165     import std.traits: PSC = ParameterStorageClass, ReturnType;
166 
167     enum mod = module_!"modules.functions";
168     mod.functionsByOverload[].shouldBeSameSetAs(
169         [
170             Function(
171                 "modules.functions",
172                 0,
173                 "add1",
174                 Type("int", int.sizeof),
175                 [
176                     Parameter(type!int, "i"),
177                     Parameter(type!int, "j"),
178                 ],
179             ),
180             Function(
181                 "modules.functions",
182                 1,
183                 "add1",
184                 Type("double", double.sizeof),
185                 [
186                     Parameter(type!double, "d0"),
187                     Parameter(type!double, "d1"),
188                 ],
189             ),
190             Function(
191                 "modules.functions",
192                 0,
193                 "withDefault",
194                 Type("double", double.sizeof),
195                 [
196                     Parameter(type!double, "fst"),
197                     Parameter(type!double, "snd", "33.3"),
198                 ],
199             ),
200             Function(
201                 "modules.functions",
202                 0,
203                 "storageClasses",
204                 Type("void", 1),
205                 [
206                     Parameter(type!int, "normal", "", PSC.none),
207                     Parameter(type!(int*), "returnScope", "", PSC.return_ | PSC.scope_),
208                     Parameter(type!int, "out_", "", PSC.out_),
209                     Parameter(type!int, "ref_", "", PSC.ref_),
210                     Parameter(type!int, "lazy_", "", PSC.lazy_),
211                 ]
212             ),
213             Function(
214                 "modules.functions",
215                 0,
216                 "exportedFunc",
217                 Type("void", 1),
218                 [],
219             ),
220             Function(
221                 "modules.functions",
222                 0,
223                 "externC",
224                 Type("void", 1),
225                 [],
226             ),
227             Function(
228                 "modules.functions",
229                 0,
230                 "externCpp",
231                 Type("void", 1),
232                 [],
233             ),
234             Function(
235                 "modules.functions",
236                 0,
237                 "identityInt",
238                 Type("int", int.sizeof),
239                 [Parameter(type!int, "x", "", PSC.none)],
240             ),
241             Function(
242                 "modules.functions",
243                 0,
244                 "voldermort",
245                 Type("modules.functions.voldermort.Voldermort", ReturnType!(modules.functions.voldermort).sizeof),
246                 [Parameter(type!int, "i", "", PSC.none)],
247             ),
248             Function(
249                 "modules.functions",
250                 0,
251                 "voldermortArray",
252                 Type("modules.functions.voldermortArray.DasVoldermort[]", ReturnType!(modules.functions.voldermortArray).sizeof),
253                 [Parameter(type!int, "i", "", PSC.none)],
254             ),
255             Function(
256                 "modules.functions",
257                 0,
258                 "concatFoo",
259                 Type("string", string.sizeof),
260                 [
261                     Parameter(type!string, "s0", "", PSC.none),
262                     Parameter(type!int,    "i",  "", PSC.none),
263                     Parameter(type!string, "s1", "", PSC.none),
264                 ],
265             ),
266         ]
267     );
268 }
269 
270 
271 @("functions.bySymbol")
272 @safe pure unittest {
273     static import modules.functions;
274     import std.traits: PSC = ParameterStorageClass, ReturnType;
275 
276     enum mod = module_!"modules.functions";
277     mod.functionsBySymbol[].shouldBeSameSetAs(
278         [
279             OverloadSet(
280                 "add1",
281                 [
282                     Function(
283                         "modules.functions",
284                         0,
285                         "add1",
286                         Type("int", int.sizeof),
287                         [
288                             Parameter(type!int, "i"),
289                             Parameter(type!int, "j"),
290                         ],
291                     ),
292                     Function(
293                         "modules.functions",
294                         1,
295                         "add1",
296                         Type("double", double.sizeof),
297                         [
298                             Parameter(type!double, "d0"),
299                             Parameter(type!double, "d1"),
300                         ],
301                     ),
302                 ]
303             ),
304             OverloadSet(
305                 "withDefault",
306                 [
307                     Function(
308                         "modules.functions",
309                         0,
310                         "withDefault",
311                         Type("double", double.sizeof),
312                         [
313                             Parameter(type!double, "fst"),
314                             Parameter(type!double, "snd", "33.3"),
315                         ],
316                     ),
317                 ]
318             ),
319             OverloadSet(
320                 "storageClasses",
321                 [
322                     Function(
323                         "modules.functions",
324                         0,
325                         "storageClasses",
326                         Type("void", 1),
327                         [
328                             Parameter(type!int, "normal", "", PSC.none),
329                             Parameter(type!(int*), "returnScope", "", PSC.return_ | PSC.scope_),
330                             Parameter(type!int, "out_", "", PSC.out_),
331                             Parameter(type!int, "ref_", "", PSC.ref_),
332                             Parameter(type!int, "lazy_", "", PSC.lazy_),
333                         ]
334                     ),
335                 ]
336             ),
337             OverloadSet(
338                 "exportedFunc",
339                 [
340                     Function(
341                         "modules.functions",
342                         0,
343                         "exportedFunc",
344                         Type("void", 1),
345                         [],
346                     ),
347                 ]
348             ),
349             OverloadSet(
350                 "externC",
351                 [
352                     Function(
353                         "modules.functions",
354                         0,
355                         "externC",
356                         Type("void", 1),
357                         [],
358                         ),
359                 ]
360             ),
361             OverloadSet(
362                 "externCpp",
363                 [
364                     Function(
365                         "modules.functions",
366                         0,
367                         "externCpp",
368                         Type("void", 1),
369                         [],
370                         ),
371                 ]
372             ),
373             OverloadSet(
374                 "identityInt",
375                 [
376                     Function(
377                         "modules.functions",
378                         0,
379                         "identityInt",
380                         Type("int", int.sizeof),
381                         [Parameter(type!int, "x", "", PSC.none)],
382                     ),
383                 ]
384             ),
385             OverloadSet(
386                 "voldermort",
387                 [
388                     Function(
389                         "modules.functions",
390                         0,
391                         "voldermort",
392                         Type(
393                             "modules.functions.voldermort.Voldermort",
394                             ReturnType!(modules.functions.voldermort).sizeof,
395                         ),
396                         [Parameter(type!int, "i", "", PSC.none)],
397                     ),
398                 ]
399             ),
400             OverloadSet(
401                 "voldermortArray",
402                 [
403                     Function(
404                         "modules.functions",
405                         0,
406                         "voldermortArray",
407                         Type(
408                             "modules.functions.voldermortArray.DasVoldermort[]",
409                             ReturnType!(modules.functions.voldermortArray).sizeof,
410                         ),
411                         [Parameter(type!int, "i", "", PSC.none)],
412                     ),
413                 ]
414             ),
415             OverloadSet(
416                 "concatFoo",
417                 [
418                     Function(
419                         "modules.functions",
420                         0,
421                         "concatFoo",
422                         Type("string", string.sizeof),
423                         [
424                             Parameter(type!string, "s0", "", PSC.none),
425                             Parameter(type!int,    "i",  "", PSC.none),
426                             Parameter(type!string, "s1", "", PSC.none),
427                         ],
428                     ),
429 
430                 ]
431             ),
432         ]
433     );
434 }
435 
436 
437 @("functions.allAggregates")
438 @safe pure unittest {
439     import std.traits: PSC = ParameterStorageClass;
440 
441     enum mod = module_!"modules.functions";
442 
443     mod.allAggregates[].shouldBeSameSetAs(
444         [
445             Aggregate(
446                 "modules.functions.voldermort.Voldermort",
447                 Aggregate.Kind.struct_,
448                 [Variable("int", "i")],
449             ),
450             Aggregate(
451                 "modules.functions.voldermortArray.DasVoldermort",
452                 Aggregate.Kind.struct_,
453                 [Variable("int", "i")],
454             ),
455         ]
456     );
457 }