Added test cases for various block sizes & items counts.

This commit is contained in:
Marek Lemańczyk committed 2023-03-12 18:01:30 +01:00
1 parent bf392d60ed
commit bdf69f300d
2 files changed
+85 -28

No files matched your search

@@ -137,7 +137,7 @@ namespace Recyclable.Collections
public static RecyclableArrayList<T> ToRecyclableArrayList<T>(this RecyclableArrayList<T> values) => new(values); public static RecyclableArrayList<T> ToRecyclableArrayList<T>(this RecyclableArrayList<T> values) => new(values);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static RecyclableList<T> ToRecyclableList<T>(this IEnumerable<T> values, int blockSize = RecyclableDefaults.BlockSize) => new(values, blockSize); public static RecyclableList<T> ToRecyclableList<T>(this IEnumerable<T> values, int minBlockSize = RecyclableDefaults.BlockSize) => new(values, minBlockSize: minBlockSize);
[MethodImpl(MethodImplOptions.AggressiveInlining)] [MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T[] RentArrayFromPool<T>(this int minSize, ArrayPool<T> arrayPool) => (minSize >= _minPooledArraySize) public static T[] RentArrayFromPool<T>(this int minSize, ArrayPool<T> arrayPool) => (minSize >= _minPooledArraySize)
@@ -10,44 +10,93 @@ namespace Recyclable.CollectionsTests
private static readonly IEnumerable<int> _testData = Enumerable.Range(1, _totalObjectCount); private static readonly IEnumerable<int> _testData = Enumerable.Range(1, _totalObjectCount);
private static RecyclableList<int> CreateReversedRecyclableList(IEnumerable<int> source) => new(source, 1024); private static RecyclableList<int> CreateReversedRecyclableList(IEnumerable<int> source) => new(source, 1024);
[Fact] private static IEnumerable<int> ItemsCountVariants => new[]
public void AddShouldAddItems() {
0, 1, 2, 3, 10, 16, 32, 64, 128, 256, 512, 1_024, 2_048, 4_096, 8_192, 16_384,
32_768, 65_536, 131_072, 262_144, 524_288, 1_048_576, 10_000_000
};
private static IEnumerable<int> BlockSizeVariants = new[]
{
1, 2, 3, 10, 16, 32, 64, 128, 256, 512, 1_024, 2_048, 4_096,
8_192, 16_384, 32_768, 65_536, 131_072, 262_144, 524_288, 1_048_576
};
public static IEnumerable<object[]> TargetTestDataVariants()
{
foreach (var itemsCount in ItemsCountVariants)
{
foreach (var targetBlockSize in BlockSizeVariants)
{
yield return new object[] { itemsCount, targetBlockSize };
}
}
}
public static IEnumerable<object[]> TestDataVariants()
{
foreach (var itemsCount in ItemsCountVariants)
{
foreach (var sourceBlockSize in BlockSizeVariants)
{
foreach (var targetBlockSize in BlockSizeVariants)
{
yield return new object[] { itemsCount, sourceBlockSize, targetBlockSize };
}
}
}
}
public static IEnumerable<object[]> TestDataVariantsWithData()
{
foreach (var testData in AddRangeCollectionTypes)
{
foreach (var itemsCount in ItemsCountVariants)
{
foreach (var targetBlockSize in BlockSizeVariants)
{
yield return new object[] { testData[0], testData[1], itemsCount, targetBlockSize };
}
}
}
}
[Theory]
[MemberData(nameof(TargetTestDataVariants))]
public void AddShouldAddItems(int itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
using var list = new RecyclableList<int>(); using var list = new RecyclableList<int>(minBlockSize: targetBlockSize);
var testData = Enumerable.Range(1, itemsCount);
// Act // Act
const int itemsCount = 5; foreach (var index in testData)
foreach (var index in Enumerable.Range(1, itemsCount))
{ {
list.Add(index); list.Add(index);
} }
// Validate // Validate
_ = list.LongCount.Should().Be(itemsCount, "we added so many items"); _ = list.LongCount.Should().Be(itemsCount, "we added so many items");
for (var index = 0; index < itemsCount; index++) _ = list.Should().ContainInConsecutiveOrder(testData);
{
var actual = list[index];
_ = actual.Should().Be(index + 1);
}
} }
[Fact] [Theory]
public void AddShouldAcceptDuplicates() [MemberData(nameof(TargetTestDataVariants))]
public void AddShouldAcceptDuplicates(int itemsCount, int targetBlockSize)
{ {
// Prepare IEnumerable<int> testNumbers = Enumerable.Range(1, itemsCount)
int[] testNumbers = new[] { 1, 2, 2, 3 }; .Concat(Enumerable.Range(1, itemsCount))
.ToArray();
// Act // Act
using var list = new RecyclableList<int>(); using var list = new RecyclableList<int>(minBlockSize: targetBlockSize);
foreach (var index in testNumbers) foreach (var index in testNumbers)
{ {
list.Add(index); list.Add(index);
} }
// Validate // Validate
_ = list.LongCount.Should().Be(testNumbers.Length); _ = list.LongCount.Should().Be(itemsCount * 2);
_ = list.Should().BeEquivalentTo(testNumbers); _ = list.Should().ContainInConsecutiveOrder(testNumbers);
} }
[Fact] [Fact]
@@ -313,16 +362,24 @@ namespace Recyclable.CollectionsTests
_ = list.LongCount.Should().Be(0); _ = list.LongCount.Should().Be(0);
} }
public static IEnumerable<object[]> AddRangeCollectionTypes => new[] public static IEnumerable<object[]> AddRangeCollectionTypes
{ {
new object[] { "int[]", _testData.ToArray() }, get
new object[] { "List<int>", _testData.ToList() }, {
new object[] { "RecyclableArrayList<int>", _testData.ToRecyclableArrayList() }, yield return new object[] { "int[]", _testData.ToArray() };
new object[] { "RecyclableList<int>", _testData.ToRecyclableList() }, yield return new object[] { "List<int>", _testData.ToList() };
new object[] { "IList<int>", new CustomIList<int>(_testData) }, yield return new object[] { "RecyclableArrayList<int>", _testData.ToRecyclableArrayList() };
new object[] { "IEnumerable<int> with non-enumerated count", _testData }, foreach (var sourceBlockSize in BlockSizeVariants)
new object[] { "IEnumerable<int> without non-enumerated count", new EnumerableWithoutCount<int>(_testData) } {
}; yield return new object[] { $"RecyclableList<int>(minBlockSize: {sourceBlockSize})", _testData.ToRecyclableList(sourceBlockSize) };
}
yield return new object[] { "IList<int>", new CustomIList<int>(_testData) };
yield return new object[] { "IEnumerable<int> with non-enumerated count", _testData };
yield return new object[] { "IEnumerable<int> without non-enumerated count", new EnumerableWithoutCount<int>(_testData) };
}
}
public static IEnumerable<object[]> AddRangeEmptyCollectionTypes => new[] public static IEnumerable<object[]> AddRangeEmptyCollectionTypes => new[]
{ {