Recyclable.AddRange fixed to ignore empty sources

This commit is contained in:
Marek Lemańczyk committed 2023-03-12 03:23:13 +01:00
1 parent 025cdb3073
commit efb393e13f
3 files changed
+95

No files matched your search

+25
View File
@@ -275,6 +275,11 @@ namespace Recyclable.Collections
public void AddRange(in T[] items)
{
if (items.LongLength == 0)
{
return;
}
long targetCapacity = _longCount + items.LongLength;
if (_capacity < targetCapacity)
{
@@ -309,6 +314,11 @@ namespace Recyclable.Collections
public void AddRange(RecyclableArrayList<T> items)
{
if (items.Count == 0)
{
return;
}
long targetCapacity = _longCount + items.Count;
if (_capacity < targetCapacity)
{
@@ -343,6 +353,11 @@ namespace Recyclable.Collections
public void AddRange(RecyclableList<T> items)
{
if (items.LongCount == 0)
{
return;
}
long itemsCount = items.LongCount,
targetCapacity = _longCount + itemsCount;
@@ -410,6 +425,11 @@ namespace Recyclable.Collections
public void AddRange(List<T> items)
{
if (items.Count == 0)
{
return;
}
long targetCapacity = _longCount + items.Count;
if (_capacity < targetCapacity)
{
@@ -445,6 +465,11 @@ namespace Recyclable.Collections
public void AddRange(IList<T> items)
{
if (items.Count == 0)
{
return;
}
long targetCapacity = _longCount + items.Count;
if (_capacity < targetCapacity)
{
@@ -0,0 +1,17 @@
using System.Collections;
namespace Recyclable.CollectionsTests
{
public class EnumerableWithoutCount<T> : IEnumerable<T>
{
private IEnumerable<T> _testData;
public EnumerableWithoutCount(IEnumerable<T> testData)
{
_testData = testData;
}
public IEnumerator<T> GetEnumerator() => _testData.GetEnumerator();
IEnumerator IEnumerable.GetEnumerator() => _testData.GetEnumerator();
}
}
@@ -323,6 +323,17 @@ namespace Recyclable.CollectionsTests
new object[] { "IEnumerable<int>", _testData },
};
public static IEnumerable<object[]> AddRangeEmptyCollectionTypes => new[]
{
new object[] { "int[]", Array.Empty<int>() },
new object[] { "List<int>", Array.Empty<int>().ToList() },
new object[] { "RecyclableArrayList<int>", Array.Empty<int>().ToRecyclableArrayList() },
new object[] { "RecyclableList<int>", Array.Empty<int>().ToRecyclableList() },
new object[] { "IList<int>", new CustomIList<int>(Array.Empty<int>()) },
new object[] { "IEnumerable<int> with non-enumerated count", Array.Empty<int>().AsEnumerable() },
new object[] { "IEnumerable<int> without non-enumerated count", new EnumerableWithoutCount<int>(Array.Empty<int>()) }
};
[Theory]
[MemberData(nameof(AddRangeCollectionTypes))]
public void AddRangeShouldAddItemsInCorrectOrder(string testCase, IEnumerable<int> testData)
@@ -428,6 +439,48 @@ namespace Recyclable.CollectionsTests
.And.BeEquivalentTo(expectedData);
}
[Theory]
[MemberData(nameof(AddRangeEmptyCollectionTypes))]
public void AddRangeShouldDoNothingWhenSourceIsEmpty(string testCase, IEnumerable<int> testData)
{
// Prepare
var list = new RecyclableList<int>();
// Act
if (testData is int[] testDataArray)
{
list.AddRange(testDataArray);
}
else if (testData is List<int> testDataList)
{
list.AddRange(testDataList);
}
else if (testData is RecyclableArrayList<int> testDataRecyclableArrayList)
{
list.AddRange(testDataRecyclableArrayList);
}
else if (testData is RecyclableList<int> testDataRecyclableList)
{
list.AddRange(testDataRecyclableList);
}
else if (testData is IList<int> testDataIList)
{
list.AddRange(testDataIList);
}
else if (testData is IEnumerable<int> testDataIEnumerable)
{
list.AddRange(testDataIEnumerable);
}
else
{
throw new InvalidCastException("Unknown type of test data");
}
// Validate
_ = list.Capacity.Should().Be(0);
_ = list.Should().BeEmpty().And.HaveCount(0);
}
[Fact]
public void InsertAtTheBeginningShouldMoveItems()
{