-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathInlineArrayTests.cs
376 lines (316 loc) · 18.1 KB
/
InlineArrayTests.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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
using Cecilifier.Core.Tests.Tests.Unit.Framework;
using NUnit.Framework;
namespace Cecilifier.Core.Tests.Tests.Unit;
[TestFixture]
public class InlineArrayTests : CecilifierUnitTestBase
{
[Test]
public void Instantiating_InlineArray_EmitsInitObj()
{
var result = RunCecilifier("""
var b = new IntBuffer();
[System.Runtime.CompilerServices.InlineArray(10)]
public struct IntBuffer
{
private int _element0;
}
""");
Assert.That(result.GeneratedCode.ReadToEnd(), Does.Match(
@"m_topLevelStatements_\d+.Body.Variables.Add\((?<ia_var>l_b_\d+)\);\s+"+ // local variable *b*
@"(?<emit>il_topLevelMain_\d+.Emit\(OpCodes\.)Ldloca_S, \k<ia_var>\);\s+" + // Loads *b* address
@"\k<emit>Initobj, st_intBuffer_\d+\);")); // Execute *initobj* on *b*
}
[TestCase("System.Span<int> span = l;", TestName = "Local variable initialization")]
[TestCase("scoped System.Span<int> span; span = l;", TestName = "Local Variable assignment")]
[TestCase("Consume(l);", TestName = "Local passed as argument")]
[TestCase("Consume(p);", TestName = "Parameter passed as argument")]
public void Assigning_InlineArrayToSpan_EmitsPrivateImplementationDetailsType(string triggeringStatements)
{
var result = RunCecilifier($$"""
void TestMethod(IntBuffer p)
{
var l = new IntBuffer();
// This will trigger the emission of <PrivateImplementationDetails>.InlineArrayAsSpan() method
{{triggeringStatements}}
}
void Consume(System.Span<int> span) {}
[System.Runtime.CompilerServices.InlineArray(10)]
public struct IntBuffer
{
private int _element0;
}
""");
var cecilifiedCode = result.GeneratedCode.ReadToEnd();
Assert.That(cecilifiedCode, Does.Match("""new TypeDefinition\("", "<PrivateImplementationDetails>", .+\)"""));
Assert.That(cecilifiedCode, Does.Match("""cls_privateImplementationDetails_\d+.Methods.Add\(m_inlineArrayAsSpan_\d+\);"""));
Assert.That(cecilifiedCode, Does.Match("""l_inlineArrayAsSpan_\d+.Add\(il_inlineArrayAsSpan_\d+.Create\(OpCodes.Call, gi_unsafeAs_\d+\)\);"""));
Assert.That(cecilifiedCode, Does.Match("""l_inlineArrayAsSpan_\d+.Add\(il_inlineArrayAsSpan_\d+.Create\(OpCodes.Call, gi_createSpan_\d+\)\);"""));
}
[Test]
public void AssignmentToFirstElement_MapsTo_PrivateImplementationDetailsInlineArrayFirstElementRefMethod()
{
var result = RunCecilifier("""
var buffer = new IntBuffer();
buffer[0] = 42;
[System.Runtime.CompilerServices.InlineArray(10)]
public struct IntBuffer
{
private int _element0;
}
""");
var cecilified = result.GeneratedCode.ReadToEnd();
// assert that the inline array address is being pushed to the stack...
Assert.That(cecilified, Does.Match("""
il_topLevelMain_\d+\.Emit\(OpCodes\.Ldloca, l_buffer_\d+\);
\s+//<PrivateImplementationDetails> class.
"""));
// and later <PrivateImplementationDetails>.InlineArrayFirstElementRef() static method is being invoked
// and the value 42 stored in the address at the top of the stack.
Assert.That(cecilified, Does.Match("""
(il_topLevelMain_\d+\.Emit\(OpCodes\.)Call, gi_inlineArrayFirstElementRef_\d+\);
\s+\1Ldc_I4, 42\);
\s+\1Stind_I4
"""));
}
[Test]
public void AccessToFirstElement_MapsTo_PrivateImplementationDetailsInlineArrayFirstElementRefMethod()
{
var result = RunCecilifier("""
var buffer = new IntBuffer();
buffer[0] = 42;
System.Console.WriteLine(buffer[0]);
[System.Runtime.CompilerServices.InlineArray(10)]
public struct IntBuffer
{
private int _element0;
}
""");
var cecilified = result.GeneratedCode.ReadToEnd();
Assert.That(cecilified, Does.Match("""
(il_topLevelMain_\d+\.Emit\(OpCodes\.)Call, gi_inlineArrayFirstElementRef_\d+\);
\s+\1Ldind_I4\);
\s+\1Call,.+ImportReference\(.+ResolveMethod\(typeof\(System.Console\), "WriteLine".+\);
"""));
}
[TestCase("field[0] = 42", @"Ldflda, fld_field_\d+", TestName = "Field")]
[TestCase("parameter[0] = 42", @"Ldarga, p_parameter_\d+", TestName = "Parameter")]
public void AccessToFirstElement_Of_StorageLocation(string statement, string expectedOpCode)
{
var result = RunCecilifier($$"""
class C
{
public IntBuffer field;
void M(IntBuffer parameter) { {{statement}}; }
}
[System.Runtime.CompilerServices.InlineArray(10)]
public struct IntBuffer { private int _element0; }
""");
var cecilified = result.GeneratedCode.ReadToEnd();
Assert.That(cecilified, Does.Match($"""
(il_M_\d+\.Emit\(OpCodes\.){expectedOpCode}\);
\s+//\<PrivateImplementationDetails\> class.
"""));
Assert.That(cecilified, Does.Match("""
(il_M_\d+\.Emit\(OpCodes\.)Call, gi_inlineArrayFirstElementRef_\d+\);
\s+\1Ldc_I4, 42\);
\s+\1Stind_I4\);
"""));
}
[TestCase("field[42] = 1", @"Ldflda, fld_field_\d+", TestName = "Field")]
[TestCase("parameter[42] = 1", @"Ldarga, p_parameter_\d+", TestName = "Parameter")]
[TestCase("local[42] = 1", @"Ldloca, l_local_\d+", TestName = "Local")]
public void AccessToNonFirstElement_Of_StorageLocation(string statement, string expectedOpCode)
{
var result = RunCecilifier($$"""
class C
{
public IntBuffer field;
void M(IntBuffer parameter)
{
var local = new IntBuffer();
{{statement}};
}
}
[System.Runtime.CompilerServices.InlineArray(100)]
public struct IntBuffer { private int _element0; }
""");
var cecilified = result.GeneratedCode.ReadToEnd();
Assert.That(cecilified, Does.Match($"""
(\s+il_M_\d+\.Emit\(OpCodes\.){expectedOpCode}\);
\1Ldc_I4, 42\);
\s+//\<PrivateImplementationDetails\> class.
"""));
Assert.That(cecilified, Does.Match("""
(\s+il_M_\d+\.Emit\(OpCodes\.)Call, gi_inlineArrayElementRef_\d+\);
\s+\1Ldc_I4, 1\);
\s+\1Stind_I4\);
"""));
}
[TestCase("string", "\"Foo\"", """
(\s+il_M_\d+\.Emit\(OpCodes\.)Call, gi_inlineArrayFirstElementRef_\d+\);
\1Ldstr, "Foo"\);
\1Stind_Ref\);
""", TestName = "String constant")]
[TestCase("object", "null", """
(\s+il_M_\d+\.Emit\(OpCodes\.)Call, gi_inlineArrayFirstElementRef_\d+\);
\1Ldnull\);
\1Stind_Ref\);
""", TestName = "Object (null)")]
[TestCase("CustomStruct", "new CustomStruct(42)", """
(\s+il_M_\d+\.Emit\(OpCodes\.)Call, gi_inlineArrayFirstElementRef_\d+\);
\1Ldc_I4, 42\);
\1Newobj, ctor_customStruct_\d+\);
\1Stobj\);
""", TestName = "Custom struct (new expression)")]
[TestCase("CustomStruct", "new CustomStruct { name = \"Foo\" }", """
(\s+il_M_\d+\.Emit\(OpCodes\.)Call, gi_inlineArrayFirstElementRef_\d+\);
\s+var (l_vt_\d+) = new VariableDefinition\((?<struct_type>st_customStruct_\d+)\);
\s+m_M_\d+\.Body\.Variables.Add\(\2\);
(?<lsa>\1Ldloca_S, \2\);)
\1Initobj, \k<struct_type>\);
\k<lsa>
\1Dup\);
\1Ldstr, "Foo"\);
\1Stfld, fld_name_\d+\);
\1Pop\);
\1Ldloc, \2\);
\1Stobj, \k<struct_type>\);
""", TestName = "Custom struct (object initializer)")]
public void InlineArray_ElementType(string elementType, string value, string expectedIL)
{
var result = RunCecilifier($$"""
class C
{
void M(Buffer b, {{elementType}} value)
{
b[0] = {{value}};
b[1] = value;
}
}
struct CustomStruct
{
public CustomStruct(int value) {}
public string name;
}
[System.Runtime.CompilerServices.InlineArray(5)]
public struct Buffer { private {{elementType}} _element0; }
""");
var cecilified = result.GeneratedCode.ReadToEnd();
Assert.That(cecilified, Does.Match(expectedIL));
}
[Test]
public void AccessToIndex_ThroughNonLiteral_UsesElementRefMethodAndIndex()
{
var result = RunCecilifier("""
class C
{
int M(int i)
{
Buffer b = new Buffer();
return b[i];
}
}
[System.Runtime.CompilerServices.InlineArray(5)]
public struct Buffer { private int _element0; }
""");
var cecilified = result.GeneratedCode.ReadToEnd();
Assert.That(cecilified, Does.Match("""
\s+//return b\[i\];
(\s+il_M_5.Emit\(OpCodes\.)Ldloca, l_b_7\);
\1Ldarg_1\);
"""));
Assert.That(cecilified, Does.Match("""
(\s+il_M_\d+\.Emit\(OpCodes\.)Call, gi_inlineArrayElementRef_\d+\);
\1Ldind_I4\);
"""));
}
[Test]
public void InlineArray_MemberAccess_OnIndex()
{
var result = RunCecilifier($$"""
class C
{
int M(Buffer b) => b[0].Value;
}
struct CustomStruct
{
public int Value;
}
[System.Runtime.CompilerServices.InlineArray(5)]
public struct Buffer { private CustomStruct _element0; }
""");
var cecilified = result.GeneratedCode.ReadToEnd();
Assert.That(cecilified, Does.Match("""
(\s+il_M_\d+\.Emit\(OpCodes\.)Call, gi_inlineArrayFirstElementRef_\d+\);
\1Ldfld, fld_value_\d+\);
"""));
}
[TestCase("T M<T>(Buffer<T> b) => b[0];",
"""
(\s+il_M_\d+\.Emit\(OpCodes\.)Call, gi_inlineArrayFirstElementRef_\d+\);
\1Ldobj, gp_T_\d+\);
""", TestName = "Open generic method")]
[TestCase("int M(Buffer<int> bi) => bi[0];",
"""
(\s+il_M_\d+.Emit\(OpCodes\.)Call, gi_inlineArrayFirstElementRef_\d+\);
\1Ldind_I4\);
""", TestName = "Closed generic type (primitive type)")]
[TestCase("CustomStruct M(Buffer<CustomStruct> b) => b[0];",
"""
(\s+il_M_\d+\.Emit\(OpCodes\.)Call, gi_inlineArrayFirstElementRef_\d+\);
\1Ldobj, st_customStruct_0\);
""", TestName = "Closed generic type (custom struct type)")]
[TestCase("TC M(Buffer<TC> b) => b[0];",
"""
(\s+il_M_\d+\.Emit\(OpCodes\.)Call, gi_inlineArrayFirstElementRef_\d+\);
\1Ldobj, gp_tC_\d+\);
""", TestName = "Type Parameter from declaring type")]
[TestCase("T M<T>(Buffer<T> b) where T : Itf => b[0];",
"""
(gi_inlineArrayFirstElementRef_\d+).GenericArguments.Add\((gp_T_\d+)\);
(\s+il_M_\d+\.Emit\(OpCodes\.)Call, \1\);
\3Ldobj, \2\);
""", TestName = "Interface as Type Parameter")]
[TestCase("int M<T>(Buffer<T> b) where T : Itf => b[0].Value;",
"""
(gi_inlineArrayFirstElementRef_\d+).GenericArguments.Add\((gp_T_\d+)\);
(\s+il_M_\d+\.Emit\(OpCodes\.)Call, \1\);
\3Constrained, \2\);
\3Callvirt, m_get_\d+\);
""", TestName = "Member access on interface")]
[TestCase("int M<T>(Buffer<T> b) where T : CustomClass => b[0].Value;",
"""
(gi_inlineArrayFirstElementRef_\d+).GenericArguments.Add\((gp_T_\d+)\);
(\s+il_M_\d+\.Emit\(OpCodes\.)Call, \1\);
\3Ldobj, \2\);
\3Box, \2\);
\3Ldfld, fld_value_\d+\);
""", TestName = "Field access on class")]
public void InlineArray_ElementAccess_OnGenericType(string toBeTested, string expecetdIL)
{
var result = RunCecilifier($$"""
struct CustomStruct { public int Value; }
class CustomClass { public int Value; }
public interface Itf { int Value { get; set; } }
[System.Runtime.CompilerServices.InlineArray(5)]
public struct Buffer<T> { private T _element0; }
class C<TC> { {{toBeTested}} }
""");
var cecilified = result.GeneratedCode.ReadToEnd();
Assert.That(cecilified, Does.Match(expecetdIL));
}
[Test]
public void Slice_Range()
{
var result = RunCecilifier("""
class C
{
System.Span<int> M(ref Buffer b) => b[1..3];
}
[System.Runtime.CompilerServices.InlineArray(5)]
public struct Buffer { private int _element0; }
""");
var cecilified = result.GeneratedCode.ReadToEnd();
Assert.Ignore($"Just for testing. Output:\n{cecilified}");
}
}