Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Add IL tests for cases where Span is not allowed #15746

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions tests/src/baseservices/collections/span/SpanDisallowedTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here (license header)


internal static class Program
{
public static int Main()
{
try
{
Console.WriteLine("ClassStaticSpanTest");
SpanDisallowedTests.ClassStaticSpanTest();
Console.WriteLine("ClassInstanceSpanTest");
SpanDisallowedTests.ClassInstanceSpanTest();
Console.WriteLine("GenericClassInstanceSpanTest");
SpanDisallowedTests.GenericClassInstanceSpanTest();
Console.WriteLine("GenericInterfaceOfSpanTest");
SpanDisallowedTests.GenericInterfaceOfSpanTest();
Console.WriteLine("GenericStructInstanceSpanTest");
SpanDisallowedTests.GenericStructInstanceSpanTest();
Console.WriteLine("GenericDelegateOfSpanTest");
SpanDisallowedTests.GenericDelegateOfSpanTest();
Console.WriteLine("ArrayOfSpanTest");
SpanDisallowedTests.ArrayOfSpanTest();
Console.WriteLine("BoxSpanTest");
SpanDisallowedTests.BoxSpanTest();
return 100; // pass
}
catch (Exception ex)
{
Console.WriteLine("FAIL: {0}", ex);
return 1; // fail
}
}
}
31 changes: 31 additions & 0 deletions tests/src/baseservices/collections/span/SpanDisallowedTests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.props))\dir.props" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{17946353-D214-4777-8ADA-E101B9143AC7}</ProjectGuid>
<OutputType>Exe</OutputType>
<ProjectTypeGuids>{786C830F-07A1-408B-BD7F-6EE04809D6DB};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\..\</SolutionDir>
<CLRTestKind>BuildAndRun</CLRTestKind>
<CLRTestPriority>0</CLRTestPriority>
</PropertyGroup>
<!-- Default configurations to help VS understand the configurations -->
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "></PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "></PropertyGroup>
<ItemGroup>
<CodeAnalysisDependentAssemblyPaths Condition=" '$(VS100COMNTOOLS)' != '' " Include="$(VS100COMNTOOLS)..\IDE\PrivateAssemblies">
<Visible>False</Visible>
</CodeAnalysisDependentAssemblyPaths>
</ItemGroup>
<ItemGroup>
<Compile Include="SpanDisallowedTests.cs" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="SpanDisallowedTestsIL.ilproj" />
</ItemGroup>
<Import Project="$([MSBuild]::GetDirectoryNameOfFileAbove($(MSBuildThisFileDirectory), dir.targets))\dir.targets" />
<PropertyGroup Condition=" '$(MsBuildProjectDirOverride)' != '' "></PropertyGroup>
</Project>
165 changes: 165 additions & 0 deletions tests/src/baseservices/collections/span/SpanDisallowedTestsIL.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
using System;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

License headers

using System.Globalization;
using System.Runtime.CompilerServices;

public static class SpanDisallowedTests
{
public static void ClassStaticSpanTest()
{
Assert.Throws<TypeLoadException>(() => ClassStaticSpanTestCore());
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void ClassStaticSpanTestCore()
{
ClassStaticSpanTest_SpanContainer.s_span = new Span<int>(new int[] { 1, 2 });
}

private static class ClassStaticSpanTest_SpanContainer
{
public static Span<int> s_span;
}

public static void ClassInstanceSpanTest()
{
Assert.Throws<TypeLoadException>(() => ClassInstanceSpanTestCore());
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void ClassInstanceSpanTestCore()
{
var spanContainer = new ClassInstanceSpanTest_SpanContainer();
spanContainer._span = new Span<int>(new int[] { 1, 2 });
}

private sealed class ClassInstanceSpanTest_SpanContainer
{
public Span<int> _span;
}

public static void GenericClassInstanceSpanTest()
{
Assert.Throws<TypeLoadException>(() => GenericClassInstanceSpanTestCore());
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void GenericClassInstanceSpanTestCore()
{
var spanContainer = new GenericClassInstanceSpanTest_SpanContainer<Span<int>>();
spanContainer._t = new Span<int>(new int[] { 1, 2 });
}

private sealed class GenericClassInstanceSpanTest_SpanContainer<T>
{
public T _t;
}

public static void GenericInterfaceOfSpanTest()
{
Assert.Throws<TypeLoadException>(() => GenericInterfaceOfSpanTestCore());
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void GenericInterfaceOfSpanTestCore()
{
Assert.NotNull(typeof(GenericInterfaceOfSpanTest_ISpanContainer<Span<int>>));
}

private interface GenericInterfaceOfSpanTest_ISpanContainer<T>
{
T Foo(T t);
}

public static void GenericStructInstanceSpanTest()
{
Assert.Throws<TypeLoadException>(() => GenericStructInstanceSpanTestCore());
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void GenericStructInstanceSpanTestCore()
{
var spanContainer = new GenericStructInstanceSpanTest_SpanContainer<Span<int>>();
spanContainer._t = new Span<int>(new int[] { 1, 2 });
}

private struct GenericStructInstanceSpanTest_SpanContainer<T>
{
public T _t;
}

public static void GenericDelegateOfSpanTest()
{
Assert.Throws<TypeLoadException>(() => GenericDelegateOfSpanTestCore());
}

[MethodImpl(MethodImplOptions.NoInlining)]
public static void GenericDelegateOfSpanTestCore()
{
Assert.NotNull(typeof(GenericDelegateOfSpan_Delegate<Span<int>>));
}

private delegate T GenericDelegateOfSpan_Delegate<T>(T span);

public static void ArrayOfSpanTest()
{
Assert.Throws<TypeLoadException>(() => ArrayOfSpanTestCore());
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void ArrayOfSpanTestCore()
{
Assert.NotNull(new Span<int>[2]);
}

public static void BoxSpanTest()
{
Assert.Throws<InvalidProgramException>(() => BoxSpanTestCore());
}

[MethodImpl(MethodImplOptions.NoInlining)]
private static void BoxSpanTestCore()
{
var span = new Span<int>(new int[] { 1, 2 });
object o = span;
Assert.NotNull(o);
}
}

public static class Assert
{
public static void NotNull(object value)
{
if (value != null)
return;
throw new AssertionFailureException("Expected null, got value of type '{1}'.", value.GetType().FullName);
}

public static void Throws<T>(Action action) where T : Exception
{
try
{
action();
}
catch (T ex) when (ex.GetType() == typeof(T))
{
return;
}
catch (Exception ex)
{
throw new AssertionFailureException(ex, "Expected exception of type '{0}', got '{1}'.", ex.GetType().FullName);
}
throw new AssertionFailureException("Exception was not thrown, expected '{0}'.", typeof(T).FullName);
}
}

public class AssertionFailureException : Exception
{
public AssertionFailureException(string format, params object[] args) : this(null, format, args)
{
}

public AssertionFailureException(Exception innerException, string format, params object[] args)
: base(string.Format(CultureInfo.InvariantCulture, format, args), innerException)
{
}
}
Loading