Repository navigation
Expand file tree
/
Copy pathFunctionalInterfacesTests.cs
More file actions
99 lines (76 loc) · 2.39 KB
/
Copy pathFunctionalInterfacesTests.cs
File metadata and controls
99 lines (76 loc) · 2.39 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
using System.Runtime.InteropServices;
namespace DotNext;
using Runtime;
using Runtime.CompilerServices;
public sealed class FunctionalInterfacesTests : Test
{
private static int staticValue1, staticValue2;
[Fact]
public static void FunctionPointerAsConsumer()
{
Consumer<int> consumer = default;
True(consumer.IsEmpty);
unsafe
{
delegate*<int, void> funcPtr = &Consume;
consumer = funcPtr;
}
False(consumer.IsEmpty);
consumer.As<IConsumer<int>>().Invoke(42);
Equal(42, staticValue1);
static void Consume(int value) => staticValue1 = value;
}
[Fact]
public static void DelegateAsConsumer()
{
DelegatingConsumer<int> consumer = default;
True(consumer.IsEmpty);
consumer = new Action<int>(Consume);
False(consumer.IsEmpty);
consumer.As<IConsumer<int>>().Invoke(42);
Equal(42, staticValue2);
static void Consume(int value) => staticValue2 = value;
}
[Fact]
public static void ValueAsProducer()
{
ValueSupplier<int> supplier = 42;
Equal(42, supplier.As<ISupplier<int>>().Invoke());
Equal("42", supplier.ToString());
}
[Fact]
public static void CtorAsProducer()
{
Activator<object> activator = default;
NotNull(activator.As<ISupplier<object>>().Invoke());
}
[Fact]
public static unsafe void Supplier()
{
True(default(Supplier<int>).IsEmpty);
delegate*<int> funcPtr = &GetValue;
Supplier<int> supplier = funcPtr;
False(supplier.IsEmpty);
Equal(42, ((Func<int>)supplier).Invoke());
Equal(42, supplier.As<ISupplier<int>>().Invoke());
static int GetValue() => 42;
}
[Fact]
public static void DynamicInvoke()
{
IFunctional functional = new DelegatingSupplier<int, int, int>(Sum);
var x = 10;
var y = 20;
var result = 0;
scoped var args = new Variant2 { Var1 = Variant.Immutable(in x), Var2 = Variant.Immutable(in y) };
functional.DynamicInvoke(in args.Var1, count: 2, Variant.Mutable(ref result));
Equal(30, result);
static int Sum(int x, int y) => x + y;
}
[StructLayout(LayoutKind.Sequential)]
private ref struct Variant2
{
public Variant Var1;
public Variant Var2;
}
}