Skip to content

Commit b2ec55a

Browse files
committed
[BCL] add tests to understand error on azure pipelines.
1 parent b7838e7 commit b2ec55a

File tree

4 files changed

+72
-7
lines changed

4 files changed

+72
-7
lines changed

src/StructLinq.BCL.Tests/ListLayoutTests.cs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,73 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.Diagnostics;
34
using System.Linq;
5+
using System.Reflection;
6+
using System.Reflection.Emit;
7+
using System.Runtime.InteropServices;
8+
using System.Runtime.Serialization;
49
using FluentAssertions;
10+
using ObjectLayoutInspector;
511
using StructLinq.BCL.List;
612
using Xunit;
713

814
namespace StructLinq.BCL.Tests
915
{
16+
1017
public class ListLayoutTests
1118
{
19+
[Fact]
20+
public void PrintLayout()
21+
{
22+
TypeLayout.PrintLayout<List<int>>();
23+
TypeLayout.PrintLayout<ListLayout<int>>();
24+
}
25+
26+
[Fact]
27+
public void ItemsOffsetForInt()
28+
{
29+
var fieldOffsets = TypeInspector.GetFieldOffsets(typeof(List<int>));
30+
var layoutFieldOffsets = TypeInspector.GetFieldOffsets(typeof(ListLayout<int>));
31+
32+
var (_, offset) = fieldOffsets.Single(x=> x.fieldInfo.Name == "_items");
33+
var (_, layoutOffset) = layoutFieldOffsets.Single(x=> x.fieldInfo.Name == "Items");
34+
layoutOffset.Should().Be(offset);
35+
}
36+
37+
[Fact]
38+
public void SizeOffsetForInt()
39+
{
40+
var fieldOffsets = TypeInspector.GetFieldOffsets(typeof(List<int>));
41+
var layoutFieldOffsets = TypeInspector.GetFieldOffsets(typeof(ListLayout<int>));
42+
43+
var (_, offset) = fieldOffsets.Single(x=> x.fieldInfo.Name == "_size");
44+
var (_, layoutOffset) = layoutFieldOffsets.Single(x=> x.fieldInfo.Name == "Size");
45+
layoutOffset.Should().Be(offset);
46+
}
47+
48+
[Fact]
49+
public void ItemsOffsetForString()
50+
{
51+
var fieldOffsets = TypeInspector.GetFieldOffsets(typeof(List<string>));
52+
var layoutFieldOffsets = TypeInspector.GetFieldOffsets(typeof(ListLayout<string>));
53+
54+
var (_, offset) = fieldOffsets.Single(x=> x.fieldInfo.Name == "_items");
55+
var (_, layoutOffset) = layoutFieldOffsets.Single(x=> x.fieldInfo.Name == "Items");
56+
layoutOffset.Should().Be(offset);
57+
}
58+
59+
[Fact]
60+
public void SizeOffsetForString()
61+
{
62+
var fieldOffsets = TypeInspector.GetFieldOffsets(typeof(List<string>));
63+
var layoutFieldOffsets = TypeInspector.GetFieldOffsets(typeof(ListLayout<string>));
64+
65+
var (_, offset) = fieldOffsets.Single(x=> x.fieldInfo.Name == "_size");
66+
var (_, layoutOffset) = layoutFieldOffsets.Single(x=> x.fieldInfo.Name == "Size");
67+
layoutOffset.Should().Be(offset);
68+
}
69+
70+
1271
[Theory]
1372
[InlineData(0)]
1473
[InlineData(10)]
@@ -17,7 +76,9 @@ public void ShouldCountMustBe(int size)
1776
{
1877
var list = Enumerable.Range(-1, size).ToList();
1978
var layout = Unsafe.As<List<int>, ListLayout<int>>(ref list);
20-
layout.Size.Should().Be(size);
79+
var typeLayout = TypeLayout.GetLayout(list.GetType());
80+
var layoutLayout = TypeLayout.GetLayout<ListLayout<int>>();
81+
layout.Size.Should().Be(size, typeLayout.ToString() + layoutLayout);
2182
}
2283

2384
[Theory]

src/StructLinq.BCL.Tests/StructLinq.BCL.x64.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net452;net472;net48;netcoreapp2.1;netcoreapp2.2;netcoreapp3.0;netcoreapp3.1;net50</TargetFrameworks>
4+
<TargetFrameworks>net472;net48;netcoreapp2.1;netcoreapp2.2;netcoreapp3.0;netcoreapp3.1;net50</TargetFrameworks>
55
<Platform>x64</Platform>
66
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
77
<Platforms>x64</Platforms>
@@ -12,6 +12,7 @@
1212

1313
<ItemGroup>
1414
<ProjectReference Include="..\StructLinq.BCL\StructLinq.BCL.csproj" />
15+
<PackageReference Include="ObjectLayoutInspector" Version="0.1.2" />
1516
</ItemGroup>
1617

1718
</Project>

src/StructLinq.BCL.Tests/StructLinq.BCL.x86.Tests.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>net452;net472;net48;netcoreapp2.1;netcoreapp2.2;netcoreapp3.0;netcoreapp3.1;net50</TargetFrameworks>
4+
<TargetFrameworks>net472;net48;netcoreapp2.1;netcoreapp2.2;netcoreapp3.0;netcoreapp3.1;net50</TargetFrameworks>
55
<Platform>x86</Platform>
66
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
77
<Platforms>x86</Platforms>
@@ -12,6 +12,7 @@
1212

1313
<ItemGroup>
1414
<ProjectReference Include="..\StructLinq.BCL\StructLinq.BCL.csproj" />
15+
<PackageReference Include="ObjectLayoutInspector" Version="0.1.2" />
1516
</ItemGroup>
1617

1718
</Project>

src/StructLinq.BCL/List/ListLayout.cs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// ReSharper disable InconsistentNaming
2+
3+
using System;
4+
25
namespace StructLinq.BCL.List
36
{
47
#if (NETCOREAPP3_0 || NETCOREAPP3_1 || NET5_0)
@@ -12,11 +15,10 @@ internal class ListLayout<T>
1215
internal class ListLayout<T>
1316
{
1417
internal T[] Items;
18+
#pragma warning disable 649
19+
internal Object SyncRoot;
20+
#pragma warning restore 649
1521
internal int Size;
16-
#pragma warning disable 169
17-
private int Version;
18-
private object SyncRoot;
19-
#pragma warning restore 169
2022
}
2123
#endif
2224

0 commit comments

Comments
 (0)