diff --git a/Containers.Test/OrderedCollectionTests.cs b/Containers.Test/OrderedCollectionTests.cs index e8cb56f..d4c5065 100644 --- a/Containers.Test/OrderedCollectionTests.cs +++ b/Containers.Test/OrderedCollectionTests.cs @@ -471,6 +471,45 @@ public void GetRange_ValidRange_ReturnsCorrectSubset() Assert.AreEqual(6, range[3]); } + [TestMethod] + public void GetRange_ZeroCountAtEnd_ReturnsEmpty() + { + // Arrange + OrderedCollection collection = new([1, 2, 3]); + + // Act: startIndex == Count is valid when count is zero, as it is for List.GetRange + OrderedCollection range = collection.GetRange(collection.Count, 0); + + // Assert + Assert.AreEqual(0, range.Count); + } + + [TestMethod] + public void GetRange_ZeroCountOnEmptyCollection_ReturnsEmpty() + { + // Arrange + OrderedCollection collection = []; + + // Act: the only range an empty collection has is the empty one at index 0 + OrderedCollection range = collection.GetRange(0, 0); + + // Assert + Assert.AreEqual(0, range.Count); + } + + [TestMethod] + public void GetRange_ZeroCountMidCollection_ReturnsEmpty() + { + // Arrange + OrderedCollection collection = new([1, 2, 3]); + + // Act + OrderedCollection range = collection.GetRange(1, 0); + + // Assert + Assert.AreEqual(0, range.Count); + } + [TestMethod] public void GetRange_InvalidStartIndex_ThrowsArgumentOutOfRangeException() { diff --git a/Containers/OrderedCollection.cs b/Containers/OrderedCollection.cs index 01faf2d..55d735d 100644 --- a/Containers/OrderedCollection.cs +++ b/Containers/OrderedCollection.cs @@ -345,11 +345,14 @@ public int IndexOf(T item) /// The zero-based starting index of the range. /// The number of elements in the range. /// A new OrderedCollection containing the elements in the specified range. + /// + /// As with , a equal to + /// is valid when is zero, and returns an empty collection. + /// /// Thrown when parameters are out of range. public OrderedCollection GetRange(int startIndex, int count) { ArgumentOutOfRangeException.ThrowIfNegative(startIndex); - ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(startIndex, Count); ArgumentOutOfRangeException.ThrowIfNegative(count); ArgumentOutOfRangeException.ThrowIfGreaterThan(startIndex + count, Count);