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
23 changes: 23 additions & 0 deletions Containers.Test/ContiguousCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,6 +417,29 @@ public void GetRange_InvalidParameters_ThrowsArgumentOutOfRangeException()
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => collection.GetRange(1, 3));
}

[TestMethod]
public void GetRange_CountOverflowingStartIndex_ThrowsArgumentOutOfRangeException()
{
// Arrange
ContiguousCollection<int> collection = [.. new int[] { 1, 2, 3 }];

// Act & Assert: startIndex + count overflows int
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => collection.GetRange(1, int.MaxValue));
}

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

// Act
ContiguousCollection<int> range = collection.GetRange(collection.Count, 0);

// Assert
Assert.IsEmpty(range);
}

[TestMethod]
public void Clone_CreatesShallowCopy()
{
Expand Down
23 changes: 23 additions & 0 deletions Containers.Test/InsertionOrderCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,29 @@ public void GetRange_InvalidCount_ThrowsArgumentOutOfRangeException()
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => collection.GetRange(1, 3));
}

[TestMethod]
public void GetRange_CountOverflowingStartIndex_ThrowsArgumentOutOfRangeException()
{
// Arrange
InsertionOrderCollection<int> collection = [.. new int[] { 1, 2, 3 }];

// Act & Assert: startIndex + count overflows int
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => collection.GetRange(1, int.MaxValue));
}

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

// Act
InsertionOrderCollection<int> range = collection.GetRange(collection.Count, 0);

// Assert
Assert.IsEmpty(range);
}

[TestMethod]
public void Clone_CreatesShallowCopy()
{
Expand Down
10 changes: 10 additions & 0 deletions Containers.Test/OrderedCollectionTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -484,6 +484,16 @@ public void GetRange_ZeroCountAtEnd_ReturnsEmpty()
Assert.AreEqual(0, range.Count);
}

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

// Act & Assert: startIndex + count overflows int
Assert.ThrowsExactly<ArgumentOutOfRangeException>(() => collection.GetRange(1, int.MaxValue));
}

[TestMethod]
public void GetRange_ZeroCountOnEmptyCollection_ReturnsEmpty()
{
Expand Down
2 changes: 1 addition & 1 deletion Containers/ContiguousCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public ContiguousCollection<T> GetRange(int startIndex, int count)
{
ArgumentOutOfRangeException.ThrowIfNegative(startIndex);
ArgumentOutOfRangeException.ThrowIfNegative(count);
ArgumentOutOfRangeException.ThrowIfGreaterThan(startIndex + count, Count);
ArgumentOutOfRangeException.ThrowIfGreaterThan(count, Count - startIndex);
ContiguousCollection<T> result = new(count);
Array.Copy(items, startIndex, result.items, 0, count);
result.Count = count;
Expand Down
2 changes: 1 addition & 1 deletion Containers/InsertionOrderCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ public InsertionOrderCollection<T> GetRange(int startIndex, int count)
{
ArgumentOutOfRangeException.ThrowIfNegative(startIndex);
ArgumentOutOfRangeException.ThrowIfNegative(count);
ArgumentOutOfRangeException.ThrowIfGreaterThan(startIndex + count, Count);
ArgumentOutOfRangeException.ThrowIfGreaterThan(count, Count - startIndex);

return [.. items.GetRange(startIndex, count)];
}
Expand Down
2 changes: 1 addition & 1 deletion Containers/OrderedCollection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ public OrderedCollection<T> GetRange(int startIndex, int count)
{
ArgumentOutOfRangeException.ThrowIfNegative(startIndex);
ArgumentOutOfRangeException.ThrowIfNegative(count);
ArgumentOutOfRangeException.ThrowIfGreaterThan(startIndex + count, Count);
ArgumentOutOfRangeException.ThrowIfGreaterThan(count, Count - startIndex);

List<T> rangeItems = items.GetRange(startIndex, count);
return new OrderedCollection<T>(rangeItems, comparer);
Expand Down
Loading