RecyclableList.Remove modified to allow removing the last item.

This commit is contained in:
Marek Lemańczyk committed 2023-03-05 18:51:06 +01:00
1 parent ec355160f9
commit 2558f57934
2 files changed
+34 -2

No files matched your search

+12 -1
View File
@@ -569,7 +569,18 @@ namespace Recyclable.Collections
? Array.IndexOf(_memoryBlocks[0], item, 0, (int)_longCount)
: DoIndexOf(item, _memoryBlocks, _lastBlockIndex, _blockSize, _nextItemIndex);
public bool Remove(T item) => throw new NotSupportedException();
public bool Remove(T item)
{
var itemIndex = LongIndexOf(item);
if (itemIndex >= 0)
{
RemoveAt(this, itemIndex, _defaultBlockArrayPool);
return true;
}
return false;
}
public void RemoveBlock(int index)
{
if (_blockSize < RecyclableDefaults.MinPooledArrayLength)
@@ -186,6 +186,27 @@ namespace Recyclable.CollectionsTests
_ = Assert.Throws<NotSupportedException>(() => list.Insert(0, -1));
}
[Fact]
public void RemoveShouldRemoveLastItem()
{
// Prepare
using var list = NewRecyclableList;
int removedCount = 0;
var testData = _testData.ToArray();
foreach (var item in _testData.Reverse())
{
// Act & Validate
_ = list.Remove(item).Should().BeTrue($"we search for {item} which should exist");
removedCount++;
var expectedList = testData[0..(testData.Length - removedCount)];
_ = list.LongCount.Should().Be(expectedList.Length);
_ = list.Should().ContainInConsecutiveOrder(expectedList);
}
_ = list.Should().BeEmpty();
}
[Fact]
public void RemoveShouldRaiseNotSupportedException()
{
@@ -193,7 +214,7 @@ namespace Recyclable.CollectionsTests
using var list = NewRecyclableList;
// Act
_ = Assert.Throws<NotSupportedException>(() => list.Remove(_testData.First()));
_ = Assert.Throws<ArgumentOutOfRangeException>(() => list.Remove(_testData.First()));
}
[Fact]