diff --git a/X10D.Tests/src/Collections/ArrayTests.cs b/X10D.Tests/src/Collections/ArrayTests.cs index c72d5ec..29c6472 100644 --- a/X10D.Tests/src/Collections/ArrayTests.cs +++ b/X10D.Tests/src/Collections/ArrayTests.cs @@ -68,39 +68,4 @@ public class ArrayTests Assert.ThrowsException(array!.Clear); Assert.ThrowsException(() => 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(() => array!.Fill(0)); - Assert.ThrowsException(() => 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..]); - } } diff --git a/X10D.Tests/src/Collections/ListTests.cs b/X10D.Tests/src/Collections/ListTests.cs index 53d5906..6455361 100644 --- a/X10D.Tests/src/Collections/ListTests.cs +++ b/X10D.Tests/src/Collections/ListTests.cs @@ -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(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? list = null; + Assert.ThrowsException(() => array!.Fill(0)); + Assert.ThrowsException(() => list!.Fill(0)); + } + + [TestMethod] + public void Shuffle_ShouldReorder_GivenNotNull() { var list = new List(Enumerable.Range(1, 52)); // 52! chance of being shuffled to the same order var shuffled = new List(list); @@ -20,7 +62,13 @@ public class ListTests } [TestMethod] - public void ListRandomShouldReturnExistingObject() + public void Shuffle_ShouldThrow_GivenNull() + { + Assert.ThrowsException(() => ((List?)null)!.Shuffle()); + } + + [TestMethod] + public void Random_ShouldReturnContainedObject_GivenNotNull() { var list = new List(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(() => ((List?)null)!.Shuffle()); - } - - [TestMethod] - public void NullRandomShouldThrow() + public void Random_ShouldThrow_GivenNull() { Assert.ThrowsException(() => ((List?)null)!.Random()); } diff --git a/X10D/src/Collections/ArrayExtensions.cs b/X10D/src/Collections/ArrayExtensions.cs index 32e094b..9d437ab 100644 --- a/X10D/src/Collections/ArrayExtensions.cs +++ b/X10D/src/Collections/ArrayExtensions.cs @@ -86,29 +86,4 @@ public static class ArrayExtensions Array.Clear(array, index, length); } - - /// - /// Assigns the given value to the elements of the array which are within the range of - /// (inclusive) and the next number of indices. - /// - /// The array to be filled. - /// The value to assign to each array element. - /// A 32-bit integer that represents the index in the array at which filling begins. - /// The number of elements to fill. - /// The type of the elements in the array. - /// is . - public static void Fill(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); - } } diff --git a/X10D/src/Collections/ListExtensions.cs b/X10D/src/Collections/ListExtensions.cs index 1444287..6f8a7af 100644 --- a/X10D/src/Collections/ListExtensions.cs +++ b/X10D/src/Collections/ListExtensions.cs @@ -25,6 +25,56 @@ public static class ListExtensions } } + /// + /// Assigns the given value to the elements of the list which are within the range of + /// (inclusive) and the next number of indices. + /// + /// The list to be filled. + /// The value to assign to each list element. + /// A 32-bit integer that represents the index in the list at which filling begins. + /// The number of elements to fill. + /// The type of the elements in the list. + /// is . + /// + /// is less than 0. + /// -or- + /// is less than 0. + /// -or- + /// + exceeds the bounds of the list. + /// + public static void Fill(this IList 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; + } + } + /// /// Returns a random element from the current list using a specified instance. ///