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.
5 using System.Runtime.InteropServices;
9 namespace System.Runtime.CompilerServices.Tests
11 public static partial class RuntimeHelpersTests
14 public static void TryEnsureSufficientExecutionStack_SpaceAvailable_ReturnsTrue()
16 Assert.True(RuntimeHelpers.TryEnsureSufficientExecutionStack());
20 public static void TryEnsureSufficientExecutionStack_NoSpaceAvailable_ReturnsFalse()
25 [MethodImpl(MethodImplOptions.NoInlining)]
26 private static void FillStack(int depth)
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()
31 if (!RuntimeHelpers.TryEnsureSufficientExecutionStack())
33 Assert.Throws<InsufficientExecutionStackException>(() => RuntimeHelpers.EnsureSufficientExecutionStack());
36 else if (depth < 2048)
43 public static void GetUninitializedObject_InvalidArguments_ThrowsException()
45 AssertExtensions.Throws<ArgumentNullException>("type", () => RuntimeHelpers.GetUninitializedObject(null));
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
55 public static void GetUninitializedObject_DoesNotRunConstructor()
57 Assert.Equal(42, new ObjectWithDefaultCtor().Value);
58 Assert.Equal(0, ((ObjectWithDefaultCtor)RuntimeHelpers.GetUninitializedObject(typeof(ObjectWithDefaultCtor))).Value);
62 public static void GetUninitializedObject_Nullable()
64 // Nullable returns the underlying type instead
65 Assert.Equal(typeof(int), RuntimeHelpers.GetUninitializedObject(typeof(Nullable<int>)).GetType());
68 private class ObjectWithDefaultCtor
70 public int Value = 42;
74 public static void IsReferenceOrContainsReferences()
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>());
84 public static void ArrayRangeHelperTest()
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));
90 range = new Range(Index.FromStart(1), Index.FromEnd(5));
91 Assert.Equal(new int [] { 2, 3, 4, 5}, RuntimeHelpers.GetSubArray(a, range));
93 range = new Range(Index.FromStart(0), Index.FromStart(a.Length + 1));
94 Assert.Throws<ArgumentOutOfRangeException>(() => { int [] array = RuntimeHelpers.GetSubArray(a, range); });
97 [StructLayoutAttribute(LayoutKind.Sequential)]
98 private struct StructWithoutReferences
103 [StructLayoutAttribute(LayoutKind.Sequential)]
104 private struct StructWithReferences