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
39 changes: 39 additions & 0 deletions Containers.Test/OrderedCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,45 @@ public void GetRange_ValidRange_ReturnsCorrectSubset()
Assert.AreEqual(6, range[3]);
}

[TestMethod]
public void GetRange_ZeroCountAtEnd_ReturnsEmpty()
{
// Arrange
OrderedCollection<int> collection = new([1, 2, 3]);

// Act: startIndex == Count is valid when count is zero, as it is for List<T>.GetRange
OrderedCollection<int> range = collection.GetRange(collection.Count, 0);

// Assert
Assert.AreEqual(0, range.Count);
}

[TestMethod]
public void GetRange_ZeroCountOnEmptyCollection_ReturnsEmpty()
{
// Arrange
OrderedCollection<int> collection = [];

// Act: the only range an empty collection has is the empty one at index 0
OrderedCollection<int> range = collection.GetRange(0, 0);

// Assert
Assert.AreEqual(0, range.Count);
}

[TestMethod]
public void GetRange_ZeroCountMidCollection_ReturnsEmpty()
{
// Arrange
OrderedCollection<int> collection = new([1, 2, 3]);

// Act
OrderedCollection<int> range = collection.GetRange(1, 0);

// Assert
Assert.AreEqual(0, range.Count);
}

[TestMethod]
public void GetRange_InvalidStartIndex_ThrowsArgumentOutOfRangeException()
{
Expand Down
5 changes: 4 additions & 1 deletion Containers/OrderedCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,14 @@ public int IndexOf(T item)
/// <param name="startIndex">The zero-based starting index of the range.</param>
/// <param name="count">The number of elements in the range.</param>
/// <returns>A new OrderedCollection containing the elements in the specified range.</returns>
/// <remarks>
/// As with <see cref="List{T}.GetRange"/>, a <paramref name="startIndex"/> equal to
/// <see cref="Count"/> is valid when <paramref name="count"/> is zero, and returns an empty collection.
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">Thrown when parameters are out of range.</exception>
public OrderedCollection<T> GetRange(int startIndex, int count)
{
ArgumentOutOfRangeException.ThrowIfNegative(startIndex);
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(startIndex, Count);
ArgumentOutOfRangeException.ThrowIfNegative(count);
ArgumentOutOfRangeException.ThrowIfGreaterThan(startIndex + count, Count);

Expand Down
Loading