Declare sliced Fill for IList<T> not T[]

This commit is contained in:
Oliver Booth 2022-04-25 10:34:17 +01:00
parent 1f9bbe9319
commit 9a089a9ae2
No known key found for this signature in database
GPG Key ID: 32A00B35503AF634
4 changed files with 101 additions and 69 deletions

View File

@ -68,39 +68,4 @@ public class ArrayTests
Assert.ThrowsException<ArgumentNullException>(array!.Clear);
Assert.ThrowsException<ArgumentNullException>(() => array!.Clear(0, 0));
}
[TestMethod]
[DataRow]
[DataRow(1)]
[DataRow(1, 2, 3)]
[DataRow(1, 2, 3, 4, 5)]
public void FillShouldBeCorrect(params int[] args)
{
args.Fill(1);
int[] comparison = Enumerable.Repeat(1, args.Length).ToArray();
CollectionAssert.AreEqual(comparison, args);
}
[TestMethod]
public void FillNullShouldThrow()
{
int[]? array = null;
Assert.ThrowsException<ArgumentNullException>(() => array!.Fill(0));
Assert.ThrowsException<ArgumentNullException>(() => array!.Fill(0, 0, 0));
}
[TestMethod]
[DataRow(1)]
[DataRow(1, 2, 3)]
[DataRow(1, 2, 3, 4, 5)]
public void FillSlicedShouldBeCorrect(params int[] args)
{
int first = args[0];
args.Fill(1, 1, args.Length - 1);
int[] comparison = Enumerable.Repeat(1, args.Length - 1).ToArray();
Assert.AreEqual(first, args[0]);
CollectionAssert.AreEqual(comparison, args[1..]);
}
}

View File

@ -7,7 +7,49 @@ namespace X10D.Tests.Collections;
public class ListTests
{
[TestMethod]
public void ListShuffleShouldReorder()
[DataRow(1)]
[DataRow(1, 2, 3)]
[DataRow(1, 2, 3, 4, 5)]
public void Fill_ShouldGiveHomogenousList_GivenValue(params int[] args)
{
int[] all42 = Enumerable.Repeat(42, args.Length).ToArray();
var list = new List<int>(args);
CollectionAssert.AreEqual(args, list);
args.Fill(42);
list.Fill(42);
CollectionAssert.AreEqual(args, list);
CollectionAssert.AreEqual(all42, args);
CollectionAssert.AreEqual(all42, list);
}
[TestMethod]
[DataRow(1)]
[DataRow(1, 2, 3)]
[DataRow(1, 2, 3, 4, 5)]
public void SlicedFill_ShouldLeaveFirstElement_GivenStartIndex1(params int[] args)
{
int first = args[0];
args.Fill(1, 1, args.Length - 1);
int[] comparison = Enumerable.Repeat(1, args.Length - 1).ToArray();
Assert.AreEqual(first, args[0]);
CollectionAssert.AreEqual(comparison, args[1..]);
}
[TestMethod]
public void Fill_ShouldThrow_GivenNull()
{
int[]? array = null;
List<int>? list = null;
Assert.ThrowsException<ArgumentNullException>(() => array!.Fill(0));
Assert.ThrowsException<ArgumentNullException>(() => list!.Fill(0));
}
[TestMethod]
public void Shuffle_ShouldReorder_GivenNotNull()
{
var list = new List<int>(Enumerable.Range(1, 52)); // 52! chance of being shuffled to the same order
var shuffled = new List<int>(list);
@ -20,7 +62,13 @@ public class ListTests
}
[TestMethod]
public void ListRandomShouldReturnExistingObject()
public void Shuffle_ShouldThrow_GivenNull()
{
Assert.ThrowsException<ArgumentNullException>(() => ((List<int>?)null)!.Shuffle());
}
[TestMethod]
public void Random_ShouldReturnContainedObject_GivenNotNull()
{
var list = new List<int>(Enumerable.Range(1, 52)); // 52! chance of being shuffled to the same order
int random = list.Random();
@ -29,13 +77,7 @@ public class ListTests
}
[TestMethod]
public void NullShuffleShouldThrow()
{
Assert.ThrowsException<ArgumentNullException>(() => ((List<int>?)null)!.Shuffle());
}
[TestMethod]
public void NullRandomShouldThrow()
public void Random_ShouldThrow_GivenNull()
{
Assert.ThrowsException<ArgumentNullException>(() => ((List<int>?)null)!.Random());
}

View File

@ -86,29 +86,4 @@ public static class ArrayExtensions
Array.Clear(array, index, length);
}
/// <summary>
/// Assigns the given value to the elements of the array which are within the range of <paramref name="startIndex" />
/// (inclusive) and the next <paramref name="count" /> number of indices.
/// </summary>
/// <param name="array">The array to be filled.</param>
/// <param name="value">The value to assign to each array element.</param>
/// <param name="startIndex">A 32-bit integer that represents the index in the array at which filling begins.</param>
/// <param name="count">The number of elements to fill.</param>
/// <typeparam name="T">The type of the elements in the array.</typeparam>
/// <exception cref="ArgumentNullException"><paramref name="array" /> is <see langword="null" />.</exception>
public static void Fill<T>(this T?[] array, T value, int startIndex, int count)
{
if (array is null)
{
throw new ArgumentNullException(nameof(array));
}
if (count == 0 || array.Length == 0)
{
return;
}
Array.Fill(array, value, startIndex, count);
}
}

View File

@ -25,6 +25,56 @@ public static class ListExtensions
}
}
/// <summary>
/// Assigns the given value to the elements of the list which are within the range of <paramref name="startIndex" />
/// (inclusive) and the next <paramref name="count" /> number of indices.
/// </summary>
/// <param name="source">The list to be filled.</param>
/// <param name="value">The value to assign to each list element.</param>
/// <param name="startIndex">A 32-bit integer that represents the index in the list at which filling begins.</param>
/// <param name="count">The number of elements to fill.</param>
/// <typeparam name="T">The type of the elements in the list.</typeparam>
/// <exception cref="ArgumentNullException"><paramref name="source" /> is <see langword="null" />.</exception>
/// <exception cref="ArgumentOutOfRangeException">
/// <para><paramref name="startIndex" /> is less than 0.</para>
/// -or-
/// <para><paramref name="count" /> is less than 0.</para>
/// -or-
/// <para><paramref name="startIndex" /> + <paramref name="count" /> exceeds the bounds of the list.</para>
/// </exception>
public static void Fill<T>(this IList<T> source, T value, int startIndex, int count)
{
if (source is null)
{
throw new ArgumentNullException(nameof(source));
}
if (startIndex < 0)
{
throw new ArgumentOutOfRangeException(nameof(startIndex));
}
if (count < 0)
{
throw new ArgumentOutOfRangeException(nameof(count));
}
if (startIndex + count > source.Count)
{
throw new ArgumentOutOfRangeException(nameof(count));
}
if (count == 0 || source.Count == 0)
{
return;
}
for (int index = startIndex; index < startIndex + count; index++)
{
source[index] = value;
}
}
/// <summary>
/// Returns a random element from the current list using a specified <see cref="System.Random" /> instance.
/// </summary>