Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
EgorBo committed Jan 11, 2023
1 parent 53716c8 commit feced0f
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/tests/JIT/opt/ValueNumbering/StaticReadonlyStructWithGC.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System;
using System.Runtime.CompilerServices;
using System.Threading;

class StaticReadonlyStructWithGC
{
static int Main()
{
for (int i = 0; i < 100; i++)
{
if (!Test1()) throw new Exception("Test1 failed");
if (!Test2()) throw new Exception("Test2 failed");
if (!Test3()) throw new Exception("Test3 failed");
if (!Test4()) throw new Exception("Test4 failed");
if (!Test5()) throw new Exception("Test5 failed");
if (!Test6()) throw new Exception("Test6 failed");
if (!Test7()) throw new Exception("Test7 failed");
if (!Test8()) throw new Exception("Test8 failed");
if (!Test9()) throw new Exception("Test9 failed");

GC.Collect();
GC.WaitForPendingFinalizers();
GC.Collect();
Thread.Sleep(16);
}
return 100;
}

static readonly MyStruct MyStructFld = new()
{
A = "A",
B = 111111.ToString(), // non-literal
C = new MyStruct2 { A = "AA" },
D = typeof(int),
E = () => 42,
F = new MyStruct3 { A = typeof(double), B = typeof(string) },
G = new int[0],
H = null
};

[MethodImpl(MethodImplOptions.NoInlining)] static bool Test1() => MyStructFld.A == "A";
[MethodImpl(MethodImplOptions.NoInlining)] static bool Test2() => MyStructFld.B == "111111";
[MethodImpl(MethodImplOptions.NoInlining)] static bool Test3() => MyStructFld.C.A == "AA";
[MethodImpl(MethodImplOptions.NoInlining)] static bool Test4() => MyStructFld.D == typeof(int);
[MethodImpl(MethodImplOptions.NoInlining)] static bool Test5() => MyStructFld.E() == 42;
[MethodImpl(MethodImplOptions.NoInlining)] static bool Test6() => MyStructFld.F.A == typeof(double);
[MethodImpl(MethodImplOptions.NoInlining)] static bool Test7() => MyStructFld.F.B == typeof(string);
[MethodImpl(MethodImplOptions.NoInlining)] static bool Test8() => MyStructFld.G.Length == 0;
[MethodImpl(MethodImplOptions.NoInlining)] static bool Test9() => MyStructFld.H == null;

struct MyStruct
{
public string A;
public string B;
public MyStruct2 C;
public Type D;
public Func<int> E;
public MyStruct3 F;
public int[] G;
public object H;
}

struct MyStruct2
{
public string A;
}

struct MyStruct3
{
public Type A;
public Type B;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<Optimize>True</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Include="$(MSBuildProjectName).cs" />
</ItemGroup>
</Project>

0 comments on commit feced0f

Please sign in to comment.