From 766a36476d1a038bc9b3b3181c754244379278ce Mon Sep 17 00:00:00 2001 From: Marek Safar Date: Tue, 15 May 2018 21:05:03 +0200 Subject: [PATCH] Factors out few GC specific tests (dotnet/corefx#29647) * Factors out few GC specific tests * Tweaks to address the review Commit migrated from https://github.com/dotnet/corefx/commit/ae9c3608e6ecb0389c668e8e6fbc6ee8f6b7ddfa --- .../ref/CoreFx.Private.TestUtilities.cs | 1 + .../src/System/PlatformDetection.cs | 19 +++++++++++++++++++ .../tests/CollectionBaseTests.cs | 7 ++++++- .../tests/SortedListTests.cs | 9 +++++++++ 4 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/libraries/CoreFx.Private.TestUtilities/ref/CoreFx.Private.TestUtilities.cs b/src/libraries/CoreFx.Private.TestUtilities/ref/CoreFx.Private.TestUtilities.cs index 152ebc6..43486b8 100644 --- a/src/libraries/CoreFx.Private.TestUtilities/ref/CoreFx.Private.TestUtilities.cs +++ b/src/libraries/CoreFx.Private.TestUtilities/ref/CoreFx.Private.TestUtilities.cs @@ -60,6 +60,7 @@ namespace System public static bool IsNetfx472OrNewer { get { throw null; } } public static bool IsNetNative { get { throw null; } } public static bool IsNonZeroLowerBoundArraySupported { get { throw null; } } + public static bool IsNotIntMaxValueArrayIndexSupported { get { throw null; } } public static bool IsNotArmProcess { get { throw null; } } public static bool IsNotFedoraOrRedHatFamily { get { throw null; } } public static bool IsNotMacOsHighSierraOrHigher { get { throw null; } } diff --git a/src/libraries/CoreFx.Private.TestUtilities/src/System/PlatformDetection.cs b/src/libraries/CoreFx.Private.TestUtilities/src/System/PlatformDetection.cs index 9161a05..e7cc213 100644 --- a/src/libraries/CoreFx.Private.TestUtilities/src/System/PlatformDetection.cs +++ b/src/libraries/CoreFx.Private.TestUtilities/src/System/PlatformDetection.cs @@ -6,6 +6,7 @@ using System.Diagnostics; using System.IO; using System.Runtime.InteropServices; using System.Text; +using System.Runtime.CompilerServices; using Xunit; namespace System @@ -73,6 +74,24 @@ namespace System return false; } + private static Lazy s_largeArrayIsNotSupported = new Lazy(IsLargeArrayNotSupported); + + [MethodImpl(MethodImplOptions.NoOptimization)] + private static bool IsLargeArrayNotSupported() + { + try + { + var tmp = new byte[int.MaxValue]; + return tmp == null; + } + catch (OutOfMemoryException) + { + return true; + } + } + + public static bool IsNotIntMaxValueArrayIndexSupported => s_largeArrayIsNotSupported.Value; + public static bool IsNonZeroLowerBoundArraySupported { get diff --git a/src/libraries/System.Collections.NonGeneric/tests/CollectionBaseTests.cs b/src/libraries/System.Collections.NonGeneric/tests/CollectionBaseTests.cs index 4d6f540..56f4e63 100644 --- a/src/libraries/System.Collections.NonGeneric/tests/CollectionBaseTests.cs +++ b/src/libraries/System.Collections.NonGeneric/tests/CollectionBaseTests.cs @@ -40,7 +40,13 @@ namespace System.Collections.Tests public static void CtorCapacity_Invalid() { AssertExtensions.Throws("capacity", () => new MyCollection(-1)); // Capacity < 0 + } + + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotIntMaxValueArrayIndexSupported))] + public static void Capacity_Excessive () + { Assert.Throws(() => new MyCollection(int.MaxValue)); // Capacity is too large + Assert.Throws(() => CreateCollection (100).Capacity = int.MaxValue); // Capacity is very large } private static Foo CreateValue(int i) => new Foo(i, i.ToString()); @@ -330,7 +336,6 @@ namespace System.Collections.Tests { var collBase = new MyCollection(new string[10]); AssertExtensions.Throws("value", () => collBase.Capacity = -1); // Capacity < 0 - Assert.Throws(() => collBase.Capacity = int.MaxValue); // Capacity is very large AssertExtensions.Throws("value", () => collBase.Capacity = collBase.Count - 1); // Capacity < list.Count } diff --git a/src/libraries/System.Collections.NonGeneric/tests/SortedListTests.cs b/src/libraries/System.Collections.NonGeneric/tests/SortedListTests.cs index e8c62cf..abeac8a 100644 --- a/src/libraries/System.Collections.NonGeneric/tests/SortedListTests.cs +++ b/src/libraries/System.Collections.NonGeneric/tests/SortedListTests.cs @@ -1300,6 +1300,15 @@ namespace System.Collections.Tests Helpers.PerformActionOnAllSortedListWrappers(sortList1, sortList2 => { AssertExtensions.Throws("value", () => sortList2.Capacity = -1); // Capacity < 0 + }); + } + + [ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotIntMaxValueArrayIndexSupported))] + public void Capacity_Excessive() + { + var sortList1 = new SortedList(); + Helpers.PerformActionOnAllSortedListWrappers(sortList1, sortList2 => + { Assert.Throws(() => sortList2.Capacity = int.MaxValue); // Capacity is too large }); } -- 2.7.4