-
-
Notifications
You must be signed in to change notification settings - Fork 94
Expand file tree
/
Copy pathIssue363_ActionFunc16Generics.cs
More file actions
117 lines (92 loc) · 3.92 KB
/
Issue363_ActionFunc16Generics.cs
File metadata and controls
117 lines (92 loc) · 3.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using static FastExpressionCompiler.LightExpression.Expression;
namespace FastExpressionCompiler.LightExpression.IssueTests
{
public class Issue363_ActionFunc16Generics : ITest
{
public static readonly object[] TestCases = Enumerable.Range(0, 16).Cast<object>().ToArray();
public int Run()
{
foreach (var testCase in TestCases)
{
Can_Create_Func((int)testCase);
Can_Create_Action((int)testCase);
}
Supports_16_Func_Params();
Supports_16_Action_Params();
return 4;
}
public void Supports_16_Func_Params()
{
LambdaExpression lambda = Lambda(
Constant(0L),
Enumerable.Range(0, 16)
.Select(i => Parameter(typeof(int), $"p{i}")));
var compiled = lambda.CompileFast<
Func<
int, int, int, int,
int, int, int, int,
int, int, int, int,
int, int, int, int,
long>>(flags: CompilerFlags.ThrowOnNotSupportedExpression);
Asserts.IsNotNull(compiled);
Asserts.AreEqual(0L, compiled(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15));
}
public void Supports_16_Action_Params()
{
LambdaExpression lambda = Lambda(
Empty(),
Enumerable.Range(0, 16)
.Select(i => Parameter(typeof(int), $"p{i}")));
var compiled = lambda.CompileFast<
Action<
int, int, int, int,
int, int, int, int,
int, int, int, int,
int, int, int, int
>>(flags: CompilerFlags.ThrowOnNotSupportedExpression);
Asserts.IsNotNull(compiled);
}
public void Can_Create_Func(int paramCount)
{
var parameters = Enumerable
.Range(0, paramCount)
.Select(i => Parameter(typeof(int), $"p{i}"))
.ToArray();
var arrayInit = NewArrayInit(typeof(int), parameters);
var expr = Lambda(arrayInit, parameters);
expr.PrintCSharp();
#if LIGHT_EXPRESSION
var sysExpr = expr.ToLambdaExpression();
var restoredExpr = sysExpr.ToLightExpression();
restoredExpr.PrintCSharp();
Asserts.IsTrue(expr.EqualsTo(restoredExpr));
#endif
// (a, b, c, ...) => new[] { a, b, c, ... }
var fs = expr.CompileFast(flags: CompilerFlags.ThrowOnNotSupportedExpression);
var result = (int[])fs.DynamicInvoke(parameters.Select(_ => (object)1).ToArray());
Asserts.AreEqual(paramCount, result.Length);
}
public void Can_Create_Action(int paramCount)
{
ParameterExpression[] parameters = Enumerable
.Range(0, paramCount)
.Select(i => Parameter(typeof(int), $"p{i}"))
.ToArray();
List<int> list = new();
MethodInfo addMethod = typeof(List<int>).GetMethod(nameof(List<int>.Add));
ConstantExpression listExp = Constant(list);
BlockExpression body = Block(
typeof(void),
parameters.Select((p) => (Expression)Call(listExp, addMethod, p)).ToArray());
LambdaExpression lambda = Lambda(body, parameters);
// (a, b, c, ...) => list.Add(a); list.Add(b); list.Add(c); ...
Delegate compiled = lambda.CompileFast(flags: CompilerFlags.ThrowOnNotSupportedExpression);
compiled.DynamicInvoke(parameters.Select(_ => (object)1).ToArray());
Asserts.AreEqual(paramCount, list.Sum());
}
}
}