From 4d680ee453e5ef4ee54811bfaa5792c716472d9d Mon Sep 17 00:00:00 2001 From: mlemanczyk Date: Sat, 5 Aug 2023 22:29:57 +0200 Subject: [PATCH] RecyclableList.Insert fixed to allow inserting at the end. --- Recyclable.Collections/RecyclableList.cs | 2 +- .../RecyclableListTests.cs | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) 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)