Skip to content

Commit 5f3a991

Browse files
committed
Add Struct layout test
1 parent ec2daa7 commit 5f3a991

File tree

3 files changed

+115
-17
lines changed

3 files changed

+115
-17
lines changed

src/Tests/StructTest/Program.cs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,33 @@ internal static class Program
77

88
private static void Main(string[] args)
99
{
10-
var m1 = new Measurement();
11-
Console.WriteLine(m1); // output: NaN (Undefined)
10+
//var m1 = new Measurement();
11+
//Console.WriteLine(m1); // output: NaN (Undefined)
1212

13-
var m2 = default(Measurement);
14-
Console.WriteLine(m2); // output: 0 ()
13+
//var m2 = default(Measurement);
14+
//Console.WriteLine(m2); // output: 0 ()
1515

16-
var ms = new Measurement[2];
17-
Console.WriteLine(string.Join(", ", ms)); // output: 0 (), 0 ()
16+
//var ms = new Measurement[2];
17+
//Console.WriteLine(string.Join(", ", ms)); // output: 0 (), 0 ()
1818

19-
ValueTypeTest.ShowSize();
19+
//ValueTypeTest.ShowSize();
2020

21-
ValueTypeTest.OutOfPrecision_True_Test();
22-
ValueTypeTest.OutOfPrecision_True2_Test();
23-
ValueTypeTest.OutOfPrecision_False_Test();
21+
//ValueTypeTest.OutOfPrecision_True_Test();
22+
//ValueTypeTest.OutOfPrecision_True2_Test();
23+
//ValueTypeTest.OutOfPrecision_False_Test();
2424

25-
Console.WriteLine();
26-
ValueTypeTest.EffectiveLength_4();
25+
//Console.WriteLine();
26+
//ValueTypeTest.EffectiveLength_4();
2727

28-
Console.WriteLine();
29-
ValueTypeTest.RoundAlgorithm();
28+
//Console.WriteLine();
29+
//ValueTypeTest.RoundAlgorithm();
3030

31-
ValidationTest.ValidationError();
32-
Console.WriteLine();
31+
//ValidationTest.ValidationError();
32+
//Console.WriteLine();
3333
//ValidationTest.ValidationAllow();
3434

35+
StructTest.Test();
36+
3537
//var config = DefaultConfig.Instance
3638
// .WithArtifactsPath(
3739
// $".\\BenchmarkDotNet.Aritfacts.{DateTime.Now.ToString("u").Replace(' ', '_').Replace(':', '-')}")

src/Tests/StructTest/StructTest.cs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
using ObjectLayoutInspector;
2+
using System.Runtime.InteropServices;
3+
4+
namespace StructTest;
5+
6+
internal static class StructTest
7+
{
8+
9+
#region Constants & Statics
10+
11+
public static void Test()
12+
{
13+
var box = new BoxStruct(false, 0, new InnerStruct(100, true));
14+
15+
TypeLayout.PrintLayout(box.GetType(), true);
16+
17+
// LayoutKind.Auto
18+
//Type layout for 'BoxStruct'
19+
//Size: 16 bytes. Paddings: 5 bytes (%31 of empty space)
20+
//|=========================================|
21+
//| 0-3: Int32 _intField (4 bytes) |
22+
//|-----------------------------------------|
23+
//| 4: Boolean _boolField (1 byte) |
24+
//|-----------------------------------------|
25+
//| 5-7: padding (3 bytes) |
26+
//|-----------------------------------------|
27+
//| 8-15: InnerStruct _inner (8 bytes) |
28+
//| |=====================================| |
29+
//| | 0-3: Int32 _intField (4 bytes) | |
30+
//| |-------------------------------------| |
31+
//| | 4: Boolean _boolField2 (1 byte) | |
32+
//| |-------------------------------------| |
33+
//| | 5: Boolean _boolField (1 byte) | |
34+
//| |-------------------------------------| |
35+
//| | 6-7: padding (2 bytes) | |
36+
//| |=====================================| |
37+
//|=========================================|
38+
39+
//LayoutKind.Sequential
40+
//Type layout for 'BoxStruct'
41+
//Size: 20 bytes. Paddings: 9 bytes (%45 of empty space)
42+
//|=========================================|
43+
//| 0: Boolean _boolField (1 byte) |
44+
//|-----------------------------------------|
45+
//| 1-3: padding (3 bytes) |
46+
//|-----------------------------------------|
47+
//| 4-7: Int32 _intField (4 bytes) |
48+
//|-----------------------------------------|
49+
//| 8-19: InnerStruct _inner (12 bytes) |
50+
//| |=====================================| |
51+
//| | 0: Boolean _boolField2 (1 byte) | |
52+
//| |-------------------------------------| |
53+
//| | 1-3: padding (3 bytes) | |
54+
//| |-------------------------------------| |
55+
//| | 4-7: Int32 _intField (4 bytes) | |
56+
//| |-------------------------------------| |
57+
//| | 8: Boolean _boolField (1 byte) | |
58+
//| |-------------------------------------| |
59+
//| | 9-11: padding (3 bytes) | |
60+
//| |=====================================| |
61+
//|=========================================|
62+
}
63+
64+
#endregion
65+
66+
}
67+
68+
[StructLayout(LayoutKind.Sequential)]
69+
public readonly record struct BoxStruct
70+
{
71+
public readonly bool _boolField;
72+
public readonly int _intField;
73+
public readonly InnerStruct _inner;
74+
75+
public BoxStruct(bool boolField, int intField, InnerStruct inner)
76+
{
77+
_boolField = boolField;
78+
_intField = intField;
79+
_inner = inner;
80+
}
81+
}
82+
83+
[StructLayout(LayoutKind.Sequential)]
84+
public readonly record struct InnerStruct
85+
{
86+
public readonly bool _boolField2;
87+
public readonly int _intField;
88+
public readonly bool _boolField;
89+
90+
public InnerStruct(int intField, bool boolField)
91+
{
92+
_intField = intField;
93+
_boolField = boolField;
94+
}
95+
}

src/Tests/StructTest/StructTest.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
</PropertyGroup>
99

1010
<ItemGroup>
11-
<PackageReference Include="BenchmarkDotNet" Version="0.15.2" />
11+
<PackageReference Include="BenchmarkDotNet" Version="0.15.6" />
12+
<PackageReference Include="ObjectLayoutInspector" Version="0.1.4" />
1213
</ItemGroup>
1314

1415
</Project>

0 commit comments

Comments
 (0)