Implement recyclable sorted list using pooled arrays
This commit is contained in:
1 parent
9aa96ccb19
commit
0d28f6515d
2 files changed
+357
-154
No files matched your search
@@ -1,93 +1,97 @@
|
||||
using FluentAssertions;
|
||||
using FluentAssertions;
|
||||
using Recyclable.Collections;
|
||||
using System.Linq;
|
||||
|
||||
namespace Recyclable.CollectionsTests
|
||||
{
|
||||
public class RecyclableSortedListTests
|
||||
{
|
||||
private static readonly (int, string)[] _testData = new[] { (5, "a"), (4, "d"), (3, "c"), (2, "b"), (1, "a") };
|
||||
public class RecyclableSortedListTests
|
||||
{
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void AddShouldStoreItemsSorted(int itemsCount)
|
||||
{
|
||||
using var list = new RecyclableSortedList<long, long>();
|
||||
foreach (var key in RecyclableLongListTestData.CreateTestData(itemsCount).Reverse())
|
||||
{
|
||||
list.Add(key, -key);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void ShouldBeSortedWhenCreated(int itemsCount)
|
||||
{
|
||||
// Act
|
||||
var data = RecyclableLongListTestData.CreateTestData(itemsCount)
|
||||
.Select(x => (itemsCount - x + 1, x))
|
||||
.ToArray();
|
||||
using var sortedList = new RecyclableSortedList<long, long>(data, 2);
|
||||
|
||||
// Validate
|
||||
_ = sortedList.Should().HaveCount(itemsCount)
|
||||
.And.BeInAscendingOrder(item => item.Key);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldBeEmptyWhenNotInitialized()
|
||||
{
|
||||
// Act
|
||||
using var sortedList = new RecyclableSortedList<int, string>(2);
|
||||
|
||||
// Validate
|
||||
_ = sortedList.Should().BeEmpty();
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void ShouldNotBeSortedWhenInitialized()
|
||||
{
|
||||
// Act
|
||||
using var sortedList = new RecyclableSortedList<int, string>(2)
|
||||
{
|
||||
_testData[0],
|
||||
_testData[1],
|
||||
_testData[2],
|
||||
_testData[3],
|
||||
_testData[4]
|
||||
};
|
||||
|
||||
// Validate
|
||||
_ = sortedList.Should().NotBeEmpty()
|
||||
.And.ContainInConsecutiveOrder(_testData);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void ShouldBeSortedWhenUpdateEnds(int itemsCount)
|
||||
{
|
||||
// Prepare
|
||||
using var sortedList = new RecyclableSortedList<long, long>(2);
|
||||
|
||||
// Act
|
||||
sortedList.BeginUpdate();
|
||||
foreach (var value in RecyclableLongListTestData.CreateTestData(itemsCount))
|
||||
{
|
||||
sortedList.Add((itemsCount - value + 1, value));
|
||||
}
|
||||
|
||||
// Validate
|
||||
_ = sortedList.Should().BeInDescendingOrder(x => x.Key);
|
||||
|
||||
// Act
|
||||
sortedList.EndUpdate();
|
||||
|
||||
// Validate
|
||||
_ = sortedList.Should().BeInAscendingOrder(x => x.Key);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void ShouldBeEmptyAfterClear(int itemsCount)
|
||||
{
|
||||
// Prepare
|
||||
var data = RecyclableLongListTestData.CreateTestData(itemsCount)
|
||||
.Select(x => (x, x));
|
||||
using var sortedList = new RecyclableSortedList<long, long>(data);
|
||||
|
||||
// Act
|
||||
sortedList.Clear();
|
||||
|
||||
// Validate
|
||||
_ = sortedList.Should().BeEmpty();
|
||||
}
|
||||
_ = list.Should().HaveCount(itemsCount)
|
||||
.And.BeInAscendingOrder(x => x.Key);
|
||||
for (int i = 0; i < itemsCount; i++)
|
||||
{
|
||||
long expectedKey = i + 1;
|
||||
list.GetKey(i).Should().Be(expectedKey);
|
||||
list.GetValue(i).Should().Be(-expectedKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void IndexerShouldUpdateValue()
|
||||
{
|
||||
using var list = new RecyclableSortedList<int, string>();
|
||||
list.Add(1, "a");
|
||||
|
||||
list[1] = "b";
|
||||
|
||||
_ = list[1].Should().Be("b");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void RemoveShouldDeleteItem(int itemsCount)
|
||||
{
|
||||
using var list = new RecyclableSortedList<long, long>();
|
||||
foreach (var key in RecyclableLongListTestData.CreateTestData(itemsCount))
|
||||
{
|
||||
list.Add(key, key);
|
||||
}
|
||||
|
||||
if (itemsCount > 0)
|
||||
{
|
||||
long keyToRemove = (itemsCount + 1) / 2;
|
||||
list.Remove(keyToRemove).Should().BeTrue();
|
||||
_ = list.ContainsKey(keyToRemove).Should().BeFalse();
|
||||
_ = list.Should().HaveCount(itemsCount - 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
list.Remove(0).Should().BeFalse();
|
||||
_ = list.Should().BeEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void TryGetValueShouldReturnValue()
|
||||
{
|
||||
using var list = new RecyclableSortedList<int, string>();
|
||||
list.Add(1, "a");
|
||||
|
||||
var found = list.TryGetValue(1, out var value);
|
||||
|
||||
_ = found.Should().BeTrue();
|
||||
_ = value.Should().Be("a");
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(RecyclableLongListTestData.ItemsCountTestCases), MemberType = typeof(RecyclableLongListTestData))]
|
||||
public void EnumeratorShouldYieldItemsSorted(int itemsCount)
|
||||
{
|
||||
using var list = new RecyclableSortedList<long, long>();
|
||||
foreach (var key in RecyclableLongListTestData.CreateTestData(itemsCount).Reverse())
|
||||
{
|
||||
list.Add(key, key);
|
||||
}
|
||||
|
||||
var actual = new List<long>();
|
||||
var enumerator = list.GetEnumerator();
|
||||
while (enumerator.MoveNext())
|
||||
{
|
||||
actual.Add(enumerator.Current.Key);
|
||||
}
|
||||
|
||||
var expected = RecyclableLongListTestData.CreateTestData(itemsCount).ToList();
|
||||
_ = actual.Should().ContainInConsecutiveOrder(expected);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user