-
-
Notifications
You must be signed in to change notification settings - Fork 986
/
Copy pathParamSourceTests.cs
208 lines (176 loc) · 8.15 KB
/
ParamSourceTests.cs
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
using System;
using System.Collections.Generic;
using System.Linq;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Code;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Jobs;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;
using BenchmarkDotNet.Toolchains;
using BenchmarkDotNet.Toolchains.InProcess.Emit;
using Xunit;
using Xunit.Abstractions;
namespace BenchmarkDotNet.IntegrationTests
{
public class ParamSourceTests: BenchmarkTestExecutor
{
public ParamSourceTests(ITestOutputHelper output) : base(output) { }
public static IEnumerable<object[]> GetToolchains()
=> new[]
{
new object[] { Job.Default.GetToolchain() },
new object[] { InProcessEmitToolchain.Instance },
};
[Fact]
public void ParamSourceCanHandleStringWithSurrogates()
{
CanExecute<ParamSourceIsStringWithSurrogates>(CreateSimpleConfig());
}
public class ParamSourceIsStringWithSurrogates
{
public IEnumerable<string> StringValues
{
get
{
yield return "a" + string.Join("", Enumerable.Repeat("😀", 40)) + "a";
yield return "a" + string.Join("", Enumerable.Repeat("😀", 40));
yield return string.Join("", Enumerable.Repeat("😀", 40)) + "a";
yield return string.Join("", Enumerable.Repeat("😀", 40));
}
}
[ParamsSource(nameof(StringValues))]
public string _ { get; set; }
[Benchmark]
public void Method() { }
}
private Summary CanExecuteWithExtraInfo(Type type, IToolchain toolchain)
{
IConfig config = CreateSimpleConfig(job: Job.Dry.WithToolchain(toolchain));
if (!toolchain.IsInProcess)
{
// Show the relevant codegen excerpt in test results (the *.notcs is not part of the logs)
Output.WriteLine("// Benchmarks and CodeGenerator.GetParamsContent()");
BenchmarkRunInfo runInfo = BenchmarkConverter.TypeToBenchmarks(type, config);
foreach (BenchmarkCase benchmarkCase in runInfo.BenchmarksCases)
{
Output.WriteLine("// " + benchmarkCase.DisplayInfo);
Output.WriteLine(CodeGenerator.GetParamsContent(benchmarkCase));
}
}
return CanExecute(type, config);
}
public interface ITargetInterface
{
int Data { get; }
}
private class NonPublicSource : ITargetInterface
{
public int Data { get; }
public NonPublicSource(int data) => Data = data;
public override string ToString() => "src " + Data.ToString();
}
public class PrivateClassWithPublicInterface
{
public static IEnumerable<ITargetInterface> GetSource()
{
yield return null;
yield return new NonPublicSource(1);
yield return new NonPublicSource(2);
}
[ParamsSource(nameof(GetSource))]
public ITargetInterface ParamsTarget { get; set; }
[Benchmark]
public int Benchmark() => ParamsTarget?.Data ?? 0;
}
[Theory, MemberData(nameof(GetToolchains))]
public void PrivateClassWithPublicInterface_Succeeds(IToolchain toolchain) => CanExecuteWithExtraInfo(typeof(PrivateClassWithPublicInterface), toolchain);
public class PrivateClassWithPublicInterface_Array
{
public IEnumerable<ITargetInterface[]> GetSource()
{
yield return null;
yield return Array.Empty<NonPublicSource>();
yield return new NonPublicSource[] { null };
yield return new[] { new NonPublicSource(1), new NonPublicSource(2) };
}
[ParamsSource(nameof(GetSource))]
public ITargetInterface[] ParamsTarget { get; set; }
[Benchmark]
public int Benchmark() => ParamsTarget?.Sum(p => p?.Data ?? 0) ?? 0;
}
[Theory, MemberData(nameof(GetToolchains))]
public void PrivateClassWithPublicInterface_Array_Succeeds(IToolchain toolchain) => CanExecuteWithExtraInfo(typeof(PrivateClassWithPublicInterface_Array), toolchain);
public class PrivateClassWithPublicInterface_Enumerable
{
public IEnumerable<IEnumerable<ITargetInterface>> GetSource()
{
static IEnumerable<ITargetInterface> YieldNull() { yield return null; }
yield return null;
yield return Enumerable.Empty<NonPublicSource>();
yield return YieldNull();
yield return PrivateClassWithPublicInterface.GetSource();
}
[ParamsSource(nameof(GetSource))]
public IEnumerable<ITargetInterface> ParamsTarget { get; set; }
[Benchmark]
public int Benchmark() => ParamsTarget?.Sum(p => p?.Data ?? 0) ?? 0;
}
[Theory, MemberData(nameof(GetToolchains))]
public void PrivateClassWithPublicInterface_Enumerable_Succeeds(IToolchain toolchain) => CanExecuteWithExtraInfo(typeof(PrivateClassWithPublicInterface_Enumerable), toolchain);
public class PrivateClassWithPublicInterface_AsObject
{
public static IEnumerable<object> GetSource()
{
yield return null;
yield return new NonPublicSource(1);
yield return new NonPublicSource(2);
}
[ParamsSource(nameof(GetSource))]
public ITargetInterface ParamsTarget { get; set; }
[Benchmark]
public int Benchmark() => ParamsTarget?.Data ?? 0;
}
[Theory, MemberData(nameof(GetToolchains))]
public void PrivateClassWithPublicInterface_AsObject_Succeeds(IToolchain toolchain) => CanExecuteWithExtraInfo(typeof(PrivateClassWithPublicInterface_AsObject), toolchain);
public class PublicSource
{
public int Data { get; }
public PublicSource(int data) => Data = data;
// op_Implicit would be meaningless because codegen wouldn't have to do anything.
public static explicit operator TargetType(PublicSource @this) => @this != null ? new TargetType(@this.Data) : null;
public override string ToString() => "src " + Data.ToString();
}
public class TargetType
{
public int Data { get; }
public TargetType(int data) => Data = data;
public override string ToString() => "target " + Data.ToString();
}
public class SourceWithExplicitCastToTarget
{
public static IEnumerable<PublicSource> GetSource()
{
yield return null;
yield return new PublicSource(1);
yield return new PublicSource(2);
}
[ParamsSource(nameof(GetSource))]
public TargetType ParamsTarget { get; set; }
[Benchmark]
public int Benchmark() => ParamsTarget?.Data ?? 0;
}
[Fact]
public void SourceWithExplicitCastToTarget_DefaultToolchain_Succeeds() => CanExecuteWithExtraInfo(typeof(SourceWithExplicitCastToTarget), Job.Default.GetToolchain());
[Fact]
public void SourceWithExplicitCastToTarget_InProcessToolchain_Throws()
{
// op_Explicit is currently not supported by InProcessEmitToolchain
// See TryChangeType() in Toolchains/InProcess.Emit.Implementation/Runnable/RunnableReflectionHelpers.cs
// If that changes, this test and the one above should be merged into:
// [Theory, MemberData(nameof(GetToolchains))]
// public void SourceWithExplicitCastToTarget_Succeeds(IToolchain toolchain) => CanExecuteWithExtraInfo(typeof(SourceWithExplicitCastToTarget), toolchain);
Assert.ThrowsAny<Exception>(() => CanExecuteWithExtraInfo(typeof(SourceWithExplicitCastToTarget), InProcessEmitToolchain.Instance));
}
}
}