diff --git a/Recyclable.Collections/RecyclableList.cs b/Recyclable.Collections/RecyclableList.cs index 1451540..a862503 100644 --- a/Recyclable.Collections/RecyclableList.cs +++ b/Recyclable.Collections/RecyclableList.cs @@ -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 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 items) { + if (items.LongCount == 0) + { + return; + } + long itemsCount = items.LongCount, targetCapacity = _longCount + itemsCount; @@ -410,6 +425,11 @@ namespace Recyclable.Collections public void AddRange(List 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 items) { + if (items.Count == 0) + { + return; + } + long targetCapacity = _longCount + items.Count; if (_capacity < targetCapacity) { diff --git a/Recyclable.CollectionsTests/EnumerableWithoutCount.cs b/Recyclable.CollectionsTests/EnumerableWithoutCount.cs new file mode 100644 index 0000000..b6c08b3 --- /dev/null +++ b/Recyclable.CollectionsTests/EnumerableWithoutCount.cs @@ -0,0 +1,17 @@ +using System.Collections; + +namespace Recyclable.CollectionsTests +{ + public class EnumerableWithoutCount : IEnumerable + { + private IEnumerable _testData; + + public EnumerableWithoutCount(IEnumerable testData) + { + _testData = testData; + } + + public IEnumerator GetEnumerator() => _testData.GetEnumerator(); + IEnumerator IEnumerable.GetEnumerator() => _testData.GetEnumerator(); + } +} \ No newline at end of file diff --git a/Recyclable.CollectionsTests/RecyclableListTests.cs b/Recyclable.CollectionsTests/RecyclableListTests.cs index ad9b1b9..8b538cd 100644 --- a/Recyclable.CollectionsTests/RecyclableListTests.cs +++ b/Recyclable.CollectionsTests/RecyclableListTests.cs @@ -323,6 +323,17 @@ namespace Recyclable.CollectionsTests new object[] { "IEnumerable", _testData }, }; + public static IEnumerable AddRangeEmptyCollectionTypes => new[] + { + new object[] { "int[]", Array.Empty() }, + new object[] { "List", Array.Empty().ToList() }, + new object[] { "RecyclableArrayList", Array.Empty().ToRecyclableArrayList() }, + new object[] { "RecyclableList", Array.Empty().ToRecyclableList() }, + new object[] { "IList", new CustomIList(Array.Empty()) }, + new object[] { "IEnumerable with non-enumerated count", Array.Empty().AsEnumerable() }, + new object[] { "IEnumerable without non-enumerated count", new EnumerableWithoutCount(Array.Empty()) } + }; + [Theory] [MemberData(nameof(AddRangeCollectionTypes))] public void AddRangeShouldAddItemsInCorrectOrder(string testCase, IEnumerable testData) @@ -428,6 +439,48 @@ namespace Recyclable.CollectionsTests .And.BeEquivalentTo(expectedData); } + [Theory] + [MemberData(nameof(AddRangeEmptyCollectionTypes))] + public void AddRangeShouldDoNothingWhenSourceIsEmpty(string testCase, IEnumerable testData) + { + // Prepare + var list = new RecyclableList(); + + // Act + if (testData is int[] testDataArray) + { + list.AddRange(testDataArray); + } + else if (testData is List testDataList) + { + list.AddRange(testDataList); + } + else if (testData is RecyclableArrayList testDataRecyclableArrayList) + { + list.AddRange(testDataRecyclableArrayList); + } + else if (testData is RecyclableList testDataRecyclableList) + { + list.AddRange(testDataRecyclableList); + } + else if (testData is IList testDataIList) + { + list.AddRange(testDataIList); + } + else if (testData is IEnumerable 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() {