diff --git a/Recyclable.Collections/RecyclableList.cs b/Recyclable.Collections/RecyclableList.cs index a2c8827..cb0b8aa 100644 --- a/Recyclable.Collections/RecyclableList.cs +++ b/Recyclable.Collections/RecyclableList.cs @@ -385,7 +385,7 @@ namespace Recyclable.Collections _capacity = RecyclableListHelpers.ResizeAndCopy(this, checked((int)BitOperations.RoundUpToPowerOf2((uint)oldCount + 1))); } - if (oldCount > 0) + if (index < oldCount) { new Span(_memoryBlock, index, oldCount) .CopyTo(new Span(_memoryBlock, index + 1, oldCount)); diff --git a/Recyclable.CollectionsTests/RecyclableListTests.cs b/Recyclable.CollectionsTests/RecyclableListTests.cs index 30555f1..e340bfc 100644 --- a/Recyclable.CollectionsTests/RecyclableListTests.cs +++ b/Recyclable.CollectionsTests/RecyclableListTests.cs @@ -471,6 +471,21 @@ namespace Recyclable.CollectionsTests _ = list.Should().ContainInConsecutiveOrder(testData.Reverse()); } + [Theory] + [MemberData(nameof(RecyclableLongListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableLongListTestData))] + public void InsertAtTheEndShouldAppend(string testCase, IEnumerable testData, long itemsCount, int targetBlockSize) + { + // Prepare + using var list = new RecyclableList(testData); + + // Act + list.Insert((int)itemsCount, -1); + + // Assert + _ = list.Count.Should().Be((int)itemsCount + 1); + _ = list[^1].Should().Be(-1); + } + [Theory] [MemberData(nameof(RecyclableLongListTestData.SourceDataVariants), MemberType = typeof(RecyclableLongListTestData))] public void RemoveAtFromTheBeginningShouldSucceed(string testCase, IEnumerable testData, long itemsCount)