3c71a58cf851f0afc6ad03cad8a48f066ebf0724
[platform/upstream/dotnet/runtime.git] /
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 using System.Runtime.InteropServices;
6
7 using Xunit;
8
9 namespace System.Runtime.CompilerServices.Tests
10 {
11     public static partial class RuntimeHelpersTests
12     {
13         [Fact]
14         public static void TryEnsureSufficientExecutionStack_SpaceAvailable_ReturnsTrue()
15         {
16             Assert.True(RuntimeHelpers.TryEnsureSufficientExecutionStack());
17         }
18
19         [Fact]
20         public static void TryEnsureSufficientExecutionStack_NoSpaceAvailable_ReturnsFalse()
21         {
22             FillStack(depth: 0);
23         }
24
25         [MethodImpl(MethodImplOptions.NoInlining)]
26         private static void FillStack(int depth)
27         {
28             // This test will fail with a StackOverflowException if TryEnsureSufficientExecutionStack() doesn't
29             // return false. No exception is thrown and the test finishes when TryEnsureSufficientExecutionStack()
30             // returns true.
31             if (!RuntimeHelpers.TryEnsureSufficientExecutionStack())
32             {
33                 Assert.Throws<InsufficientExecutionStackException>(() => RuntimeHelpers.EnsureSufficientExecutionStack());
34                 return;
35             }
36             else if (depth < 2048)
37             {
38                 FillStack(depth + 1);
39             }
40         }
41
42         [Fact]
43         public static void GetUninitializedObject_InvalidArguments_ThrowsException()
44         {
45             AssertExtensions.Throws<ArgumentNullException>("type", () => RuntimeHelpers.GetUninitializedObject(null));
46
47             AssertExtensions.Throws<ArgumentException>(null, () => RuntimeHelpers.GetUninitializedObject(typeof(string))); // special type
48             Assert.Throws<MemberAccessException>(() => RuntimeHelpers.GetUninitializedObject(typeof(System.IO.Stream))); // abstract type
49             Assert.Throws<MemberAccessException>(() => RuntimeHelpers.GetUninitializedObject(typeof(System.Collections.IEnumerable))); // interface
50             Assert.Throws<MemberAccessException>(() => RuntimeHelpers.GetUninitializedObject(typeof(System.Collections.Generic.List<>))); // generic definition
51             Assert.Throws<NotSupportedException>(() => RuntimeHelpers.GetUninitializedObject(typeof(TypedReference))); // byref-like type
52         }
53
54         [Fact]
55         public static void GetUninitializedObject_DoesNotRunConstructor()
56         {
57             Assert.Equal(42, new ObjectWithDefaultCtor().Value);
58             Assert.Equal(0, ((ObjectWithDefaultCtor)RuntimeHelpers.GetUninitializedObject(typeof(ObjectWithDefaultCtor))).Value);
59         }
60
61         [Fact]
62         public static void GetUninitializedObject_Nullable()
63         {
64             // Nullable returns the underlying type instead
65             Assert.Equal(typeof(int), RuntimeHelpers.GetUninitializedObject(typeof(Nullable<int>)).GetType());
66         }
67
68         private class ObjectWithDefaultCtor
69         {
70             public int Value = 42;
71         }
72
73         [Fact]
74         public static void IsReferenceOrContainsReferences()
75         {
76             Assert.False(RuntimeHelpers.IsReferenceOrContainsReferences<int>());
77             Assert.True(RuntimeHelpers.IsReferenceOrContainsReferences<string>());
78             Assert.False(RuntimeHelpers.IsReferenceOrContainsReferences<Guid>());
79             Assert.False(RuntimeHelpers.IsReferenceOrContainsReferences<StructWithoutReferences>());
80             Assert.True(RuntimeHelpers.IsReferenceOrContainsReferences<StructWithReferences>());
81         }
82
83         [Fact]
84         public static void ArrayRangeHelperTest()
85         {
86             int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
87             Range range = Range.All;
88             Assert.Equal(a, RuntimeHelpers.GetSubArray(a, range));
89
90             range = new Range(Index.FromStart(1), Index.FromEnd(5));
91             Assert.Equal(new int [] { 2, 3, 4, 5}, RuntimeHelpers.GetSubArray(a, range));
92
93             range = new Range(Index.FromStart(0), Index.FromStart(a.Length + 1));
94             Assert.Throws<ArgumentOutOfRangeException>(() => { int [] array = RuntimeHelpers.GetSubArray(a, range); });
95         }
96
97         [StructLayoutAttribute(LayoutKind.Sequential)]
98         private struct StructWithoutReferences
99         {
100             public int a, b, c;
101         }
102
103         [StructLayoutAttribute(LayoutKind.Sequential)]
104         private struct StructWithReferences
105         {
106             public int a, b, c;
107             public object d;
108         }
109     }
110 }