Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriktsarpalis committed Oct 7, 2022
1 parent ca50108 commit 6b6cc68
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,28 @@ public void AddRange_AddSelfAsEnumerable_ThrowsExceptionWhenNotEmpty()
Assert.Throws<InvalidOperationException>(() => list.AddRange(list.Where(_ => true)));
Assert.Equal(6, list.Count);
}

[Fact]
public void AddRange_CollectionWithLargeCount_ThrowsOverflowException()
{
List<T> list = GenericListFactory(count: 1);
ICollection<T> collection = new CollectionWithLargeCount();

Assert.Throws<OverflowException>(() => list.AddRange(collection));
}

private class CollectionWithLargeCount : ICollection<T>
{
public int Count => int.MaxValue;

public bool IsReadOnly => throw new NotImplementedException();
public void Add(T item) => throw new NotImplementedException();
public void Clear() => throw new NotImplementedException();
public bool Contains(T item) => throw new NotImplementedException();
public void CopyTo(T[] array, int arrayIndex) => throw new NotImplementedException();
public IEnumerator<T> GetEnumerator() => throw new NotImplementedException();
public bool Remove(T item) => throw new NotImplementedException();
IEnumerator IEnumerable.GetEnumerator() => throw new NotImplementedException();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,14 @@ public void InsertRange_MatchesExpectedContents()
Assert.Equal(12, list.Count);
Assert.Equal(new[] { 6, 5, 4, 100, 99, 98, 3, 2, 1, 0, -1, -2 }, list);
}

[Fact]
public void InsertRange_CollectionWithLargeCount_ThrowsOverflowException()
{
List<T> list = GenericListFactory(count: 1);
ICollection<T> collection = new CollectionWithLargeCount();

Assert.Throws<OverflowException>(() => list.InsertRange(0, collection));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public void AddRange(IEnumerable<T> collection)
{
if (_items.Length - _size < count)
{
Grow(_size + count);
Grow(checked(_size + count));
}

c.CopyTo(_items, _size);
Expand Down Expand Up @@ -787,7 +787,7 @@ public void InsertRange(int index, IEnumerable<T> collection)
{
if (_items.Length - _size < count)
{
Grow(_size + count);
Grow(checked(_size + count));
}
if (index < _size)
{
Expand Down

0 comments on commit 6b6cc68

Please sign in to comment.