Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
71 changes: 39 additions & 32 deletions src/libraries/Common/tests/System/Collections/TestBase.Generic.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,42 +37,49 @@ public abstract class TestBase<T> : TestBase
/// MemberData to be passed to tests that take an IEnumerable{T}. This method returns every permutation of
/// EnumerableType to test on (e.g. HashSet, Queue), and size of set to test with (e.g. 0, 1, etc.).
/// </summary>
public static IEnumerable<object[]> EnumerableTestData()
public static IEnumerable<object[]> EnumerableTestData() =>
((IEnumerable<EnumerableType>)Enum.GetValues(typeof(EnumerableType))).SelectMany(GetEnumerableTestData);

/// <summary>
/// MemberData to be passed to tests that take an IEnumerable{T}. This method returns results for various
/// sizes of sets to test with (e.g. 0, 1, etc.) but only for List.
/// </summary>
public static IEnumerable<object[]> ListTestData() =>
GetEnumerableTestData(EnumerableType.List);

private static IEnumerable<object[]> GetEnumerableTestData(EnumerableType enumerableType)
{
foreach (object[] collectionSizeArray in ValidCollectionSizes())
{
foreach (EnumerableType enumerableType in Enum.GetValues(typeof(EnumerableType)))
int count = (int)collectionSizeArray[0];
yield return new object[] { enumerableType, count, 0, 0, 0 }; // Empty Enumerable
yield return new object[] { enumerableType, count, count + 1, 0, 0 }; // Enumerable that is 1 larger

if (count >= 1)
{
yield return new object[] { enumerableType, count, count, 0, 0 }; // Enumerable of the same size
yield return new object[] { enumerableType, count, count - 1, 0, 0 }; // Enumerable that is 1 smaller
yield return new object[] { enumerableType, count, count, 1, 0 }; // Enumerable of the same size with 1 matching element
yield return new object[] { enumerableType, count, count + 1, 1, 0 }; // Enumerable that is 1 longer with 1 matching element
yield return new object[] { enumerableType, count, count, count, 0 }; // Enumerable with all elements matching
yield return new object[] { enumerableType, count, count + 1, count, 0 }; // Enumerable with all elements matching plus one extra
}

if (count >= 2)
{
yield return new object[] { enumerableType, count, count - 1, 1, 0 }; // Enumerable that is 1 smaller with 1 matching element
yield return new object[] { enumerableType, count, count + 2, 2, 0 }; // Enumerable that is 2 longer with 2 matching element
yield return new object[] { enumerableType, count, count - 1, count - 1, 0 }; // Enumerable with all elements matching minus one
yield return new object[] { enumerableType, count, count, 2, 0 }; // Enumerable of the same size with 2 matching element
if ((enumerableType == EnumerableType.List || enumerableType == EnumerableType.Queue))
yield return new object[] { enumerableType, count, count, 0, 1 }; // Enumerable with 1 element duplicated
}

if (count >= 3)
{
int count = (int)collectionSizeArray[0];
yield return new object[] { enumerableType, count, 0, 0, 0 }; // Empty Enumerable
yield return new object[] { enumerableType, count, count + 1, 0, 0 }; // Enumerable that is 1 larger

if (count >= 1)
{
yield return new object[] { enumerableType, count, count, 0, 0 }; // Enumerable of the same size
yield return new object[] { enumerableType, count, count - 1, 0, 0 }; // Enumerable that is 1 smaller
yield return new object[] { enumerableType, count, count, 1, 0 }; // Enumerable of the same size with 1 matching element
yield return new object[] { enumerableType, count, count + 1, 1, 0 }; // Enumerable that is 1 longer with 1 matching element
yield return new object[] { enumerableType, count, count, count, 0 }; // Enumerable with all elements matching
yield return new object[] { enumerableType, count, count + 1, count, 0 }; // Enumerable with all elements matching plus one extra
}

if (count >= 2)
{
yield return new object[] { enumerableType, count, count - 1, 1, 0 }; // Enumerable that is 1 smaller with 1 matching element
yield return new object[] { enumerableType, count, count + 2, 2, 0 }; // Enumerable that is 2 longer with 2 matching element
yield return new object[] { enumerableType, count, count - 1, count - 1, 0 }; // Enumerable with all elements matching minus one
yield return new object[] { enumerableType, count, count, 2, 0 }; // Enumerable of the same size with 2 matching element
if ((enumerableType == EnumerableType.List || enumerableType == EnumerableType.Queue))
yield return new object[] { enumerableType, count, count, 0, 1 }; // Enumerable with 1 element duplicated
}

if (count >= 3)
{
if ((enumerableType == EnumerableType.List || enumerableType == EnumerableType.Queue))
yield return new object[] { enumerableType, count, count, 0, 1 }; // Enumerable with all elements duplicated
yield return new object[] { enumerableType, count, count - 1, 2, 0 }; // Enumerable that is 1 smaller with 2 matching elements
}
if ((enumerableType == EnumerableType.List || enumerableType == EnumerableType.Queue))
yield return new object[] { enumerableType, count, count, 0, 1 }; // Enumerable with all elements duplicated
yield return new object[] { enumerableType, count, count - 1, 2, 0 }; // Enumerable that is 1 smaller with 2 matching elements
}
}
}
Expand Down
21 changes: 12 additions & 9 deletions src/libraries/System.Collections/ref/System.Collections.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,6 @@ public static partial class StructuralComparisons
}
namespace System.Collections.Generic
{
public static partial class CollectionExtensions
{
public static TValue? GetValueOrDefault<TKey, TValue>(this System.Collections.Generic.IReadOnlyDictionary<TKey, TValue> dictionary, TKey key) { throw null; }
public static TValue GetValueOrDefault<TKey, TValue>(this System.Collections.Generic.IReadOnlyDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue) { throw null; }
public static bool Remove<TKey, TValue>(this System.Collections.Generic.IDictionary<TKey, TValue> dictionary, TKey key, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out TValue value) { throw null; }
public static bool TryAdd<TKey, TValue>(this System.Collections.Generic.IDictionary<TKey, TValue> dictionary, TKey key, TValue value) { throw null; }
public static System.Collections.ObjectModel.ReadOnlyCollection<T> AsReadOnly<T>(this IList<T> list) { throw null; }
public static System.Collections.ObjectModel.ReadOnlyDictionary<TKey, TValue> AsReadOnly<TKey, TValue>(this IDictionary<TKey, TValue> dictionary) where TKey : notnull { throw null; }
}
public sealed partial class LinkedListNode<T>
{
public LinkedListNode(T value) { }
Expand Down Expand Up @@ -411,6 +402,18 @@ void System.Collections.IEnumerator.Reset() { }
#endif // !BUILDING_CORELIB_REFERENCE
namespace System.Collections.Generic
{
public static partial class CollectionExtensions
{
public static void AddRange<T>(this System.Collections.Generic.List<T> list, System.ReadOnlySpan<T> source) { }
public static void CopyTo<T>(this System.Collections.Generic.List<T> list, System.Span<T> destination) { }
public static TValue? GetValueOrDefault<TKey, TValue>(this System.Collections.Generic.IReadOnlyDictionary<TKey, TValue> dictionary, TKey key) { throw null; }
public static TValue GetValueOrDefault<TKey, TValue>(this System.Collections.Generic.IReadOnlyDictionary<TKey, TValue> dictionary, TKey key, TValue defaultValue) { throw null; }
public static void InsertRange<T>(this System.Collections.Generic.List<T> list, int index, System.ReadOnlySpan<T> source) { }
public static bool Remove<TKey, TValue>(this System.Collections.Generic.IDictionary<TKey, TValue> dictionary, TKey key, [System.Diagnostics.CodeAnalysis.MaybeNullWhenAttribute(false)] out TValue value) { throw null; }
public static bool TryAdd<TKey, TValue>(this System.Collections.Generic.IDictionary<TKey, TValue> dictionary, TKey key, TValue value) { throw null; }
public static System.Collections.ObjectModel.ReadOnlyCollection<T> AsReadOnly<T>(this IList<T> list) { throw null; }
public static System.Collections.ObjectModel.ReadOnlyDictionary<TKey, TValue> AsReadOnly<TKey, TValue>(this IDictionary<TKey, TValue> dictionary) where TKey : notnull { throw null; }
}
public abstract partial class Comparer<T> : System.Collections.Generic.IComparer<T>, System.Collections.IComparer
{
protected Comparer() { }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
</PropertyGroup>
<ItemGroup>
<Compile Include="System\Collections\BitArray.cs" />
<Compile Include="System\Collections\Generic\CollectionExtensions.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\ICollectionDebugView.cs"
Link="Common\System\Collections\Generic\ICollectionDebugView.cs" />
<Compile Include="$(CoreLibSharedDir)System\Collections\Generic\IDictionaryDebugView.cs"
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,35 @@ public void AddRange(EnumerableType enumerableType, int listLength, int enumerab
});
}

[Theory]
[MemberData(nameof(ListTestData))]
public void AddRange_Span(EnumerableType enumerableType, int listLength, int enumerableLength, int numberOfMatchingElements, int numberOfDuplicateElements)
{
List<T> list = GenericListFactory(listLength);
List<T> listBeforeAdd = list.ToList();
Span<T> span = CreateEnumerable(enumerableType, list, enumerableLength, numberOfMatchingElements, numberOfDuplicateElements).ToArray();
list.AddRange(span);

// Check that the first section of the List is unchanged
Assert.All(Enumerable.Range(0, listLength), index =>
{
Assert.Equal(listBeforeAdd[index], list[index]);
});

// Check that the added elements are correct
for (int i = 0; i < enumerableLength; i++)
{
Assert.Equal(span[i], list[i + listLength]);
};
}

[Fact]
public void AddRange_NullList_ThrowsArgumentNullException()
{
AssertExtensions.Throws<ArgumentNullException>("list", () => CollectionExtensions.AddRange<int>(null, default));
AssertExtensions.Throws<ArgumentNullException>("list", () => CollectionExtensions.AddRange<int>(null, new int[1]));
}

[Theory]
[MemberData(nameof(ValidCollectionSizes))]
public void AddRange_NullEnumerable_ThrowsArgumentNullException(int count)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using Xunit;

namespace System.Collections.Tests
{
/// <summary>
/// Contains tests that ensure the correctness of the List class.
/// </summary>
public abstract partial class List_Generic_Tests<T> : IList_Generic_Tests<T>
{
[Fact]
public void CopyTo_InvalidArgs_Throws()
{
AssertExtensions.Throws<ArgumentNullException>("list", () => CollectionExtensions.CopyTo(null, Span<int>.Empty));
AssertExtensions.Throws<ArgumentNullException>("list", () => CollectionExtensions.CopyTo(null, new Span<int>(new int[1])));

var list = new List<int>() { 1, 2, 3 };
Assert.Throws<ArgumentException>(() => CollectionExtensions.CopyTo(list, (Span<int>)new int[2]));
}

[Fact]
public void CopyTo_ItemsCopiedCorrectly()
{
List<int> list;
Span<int> destination;

list = new List<int>();
destination = Span<int>.Empty;
list.CopyTo(destination);

list = new List<int>() { 1, 2, 3 };
destination = new int[3];
list.CopyTo(destination);
Assert.Equal(new[] { 1, 2, 3 }, destination.ToArray());

list = new List<int>() { 1, 2, 3 };
destination = new int[4];
list.CopyTo(destination);
Assert.Equal(new[] { 1, 2, 3, 0 }, destination.ToArray());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace System.Collections.Tests
{
/// <summary>
/// Contains tests that ensure the correctness of the List class.
/// </summary>
public abstract partial class List_Generic_Tests<T> : IList_Generic_Tests<T>
{
[Fact]
public void InsertRange_InvalidArgs_Throws()
{
AssertExtensions.Throws<ArgumentNullException>("list", () => CollectionExtensions.InsertRange(null, 0, ReadOnlySpan<int>.Empty));
AssertExtensions.Throws<ArgumentNullException>("list", () => CollectionExtensions.InsertRange(null, 0, new ReadOnlySpan<int>(new int[1])));

var list = new List<int>();
AssertExtensions.Throws<ArgumentOutOfRangeException>("index", () => CollectionExtensions.InsertRange(list, 1, new int[0]));
AssertExtensions.Throws<ArgumentOutOfRangeException>("index", () => CollectionExtensions.InsertRange(list, -1, new int[0]));
}

[Fact]
public void InsertRange_MatchesExpectedContents()
{
var list = new List<int>();

list.InsertRange(0, ReadOnlySpan<int>.Empty);
Assert.Equal(0, list.Count);

list.InsertRange(0, (ReadOnlySpan<int>)new int[] { 3, 2, 1 });
Assert.Equal(3, list.Count);
Assert.Equal(new[] { 3, 2, 1 }, list);

list.InsertRange(0, (ReadOnlySpan<int>)new int[] { 6, 5, 4 });
Assert.Equal(6, list.Count);
Assert.Equal(new[] { 6, 5, 4, 3, 2, 1 }, list);

list.InsertRange(6, (ReadOnlySpan<int>)new int[] { 0, -1, -2 });
Assert.Equal(9, list.Count);
Assert.Equal(new[] { 6, 5, 4, 3, 2, 1, 0, -1, -2 }, list);

list.InsertRange(3, (ReadOnlySpan<int>)new int[] { 100, 99, 98 });
Assert.Equal(12, list.Count);
Assert.Equal(new[] { 6, 5, 4, 100, 99, 98, 3, 2, 1, 0, -1, -2 }, list);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>$(NetCoreAppCurrent)</TargetFramework>
<TestRuntime>true</TestRuntime>
Expand Down Expand Up @@ -98,8 +98,10 @@
<Compile Include="Generic\List\List.Generic.Tests.BinarySearch.cs" />
<Compile Include="Generic\List\List.Generic.Tests.Constructor.cs" />
<Compile Include="Generic\List\List.Generic.Tests.ConvertAll.cs" />
<Compile Include="Generic\List\List.Generic.Tests.CopyTo.cs" />
<Compile Include="Generic\List\List.Generic.Tests.Find.cs" />
<Compile Include="Generic\List\List.Generic.Tests.EnsureCapacity.cs" />
<Compile Include="Generic\List\List.Generic.Tests.InsertRange.cs" />
<Compile Include="Generic\List\List.Generic.Tests.Remove.cs" />
<Compile Include="Generic\List\List.Generic.Tests.Sort.cs" />
<Compile Include="Generic\PriorityQueue\PriorityQueue.Generic.cs" />
Expand Down
Loading