This repository was archived by the owner on Jan 23, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Add IL tests for cases where Span is not allowed #15746
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
tests/src/baseservices/collections/span/SpanDisallowedTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System; | ||
|
||
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
31
tests/src/baseservices/collections/span/SpanDisallowedTests.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
165
tests/src/baseservices/collections/span/SpanDisallowedTestsIL.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,165 @@ | ||
using System; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||
{ | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same here (license header)