RecyclableList tests extended & parameterized for various combinations of block sizes in source & target lists, including different no. of items.

This commit is contained in:
Marek Lemańczyk committed 2023-03-17 01:42:30 +01:00
1 parent 5235ea12bc
commit 3e8f03e59f
2 files changed
+393 -280

No files matched your search

@@ -0,0 +1,177 @@
using Recyclable.Collections;
namespace Recyclable.CollectionsTests
{
public class RecyclableListTestData : IDisposable
{
private static void DisposeTestData(IEnumerable<object[]>? testCases)
{
if (testCases != null)
{
foreach (var testData in testCases)
{
foreach (var item in testData)
{
if (item is IDisposable disposable)
{
disposable.Dispose();
}
}
}
}
}
private static void DisposeTestData(IEnumerable<IEnumerable<long>>? testCases)
{
if (testCases != null)
{
foreach (var testData in testCases)
{
if (testData is IDisposable disposable)
{
disposable.Dispose();
}
}
}
}
private static Dictionary<long, IEnumerable<long>>? _testCases = new();
public RecyclableListTestData()
{
_testCases ??= new();
}
private static IEnumerable<long> DoCreateTestData(long itemsCount)
{
for (long index = 1; index <= itemsCount; index++)
{
yield return index;
}
}
public static IEnumerable<long> CreateTestData(long itemsCount)
{
if (_testCases!.TryGetValue(itemsCount, out var testCases))
{
return testCases;
}
testCases = DoCreateTestData(itemsCount).ToArray();
_testCases.Add(itemsCount, testCases);
return testCases;
}
public void Dispose()
{
DisposeTestData(_testCases?.Values);
DisposeTestData(_sourceDataVariants);
DisposeTestData(_emptySourceDataVariants);
_testCases = null;
_sourceDataVariants = null;
_targetDataVariants = null;
_testDataVariants = null;
_sourceTargetDataVariants = null;
_emptySourceDataVariants = null;
GC.SuppressFinalize(this);
}
public static readonly IEnumerable<long> ItemsCountVariants = new long[]
{
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
};
public static readonly IEnumerable<int> BlockSizeVariants = new int[]
{
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
};
private static IEnumerable<object[]> GetTargetDataVariants()
{
foreach (var itemsCount in ItemsCountVariants)
{
foreach (var targetBlockSize in BlockSizeVariants)
{
yield return new object[] { itemsCount, targetBlockSize };
}
}
}
private static IEnumerable<object[]>? _targetDataVariants;
public static IEnumerable<object[]> TargetDataVariants => _targetDataVariants ??= GetTargetDataVariants().ToArray();
private static IEnumerable<object[]> GetTestDataVariants()
{
foreach (var itemsCount in ItemsCountVariants)
{
foreach (var sourceBlockSize in BlockSizeVariants)
{
foreach (var targetBlockSize in BlockSizeVariants)
{
yield return new object[] { itemsCount, sourceBlockSize, targetBlockSize };
}
}
}
}
private static IEnumerable<object[]>? _testDataVariants;
public static IEnumerable<object[]> TestDataVariants => _testDataVariants ??= GetTestDataVariants().ToArray();
private static IEnumerable<object[]> CreateSourceDataVariants()
{
foreach (var itemsCount in ItemsCountVariants)
{
yield return new object[] { $"int[{itemsCount}]", CreateTestData(itemsCount).ToArray(), itemsCount };
yield return new object[] { $"List<int>(itemsCount: {itemsCount}", CreateTestData(itemsCount).ToList(), itemsCount };
yield return new object[] { $"RecyclableArrayList<int>(itemsCount: {itemsCount})", CreateTestData(itemsCount).ToRecyclableArrayList(), itemsCount };
foreach (var sourceBlockSize in BlockSizeVariants)
{
yield return new object[]
{
$"RecyclableList<int>(itemsCount: {itemsCount}, minBlockSize: {sourceBlockSize})",
CreateTestData(itemsCount).ToRecyclableList(sourceBlockSize),
itemsCount
};
}
yield return new object[] { $"IList<int>(itemsCount: {itemsCount})", new CustomIList<long>(CreateTestData(itemsCount)), itemsCount };
yield return new object[] { $"IEnumerable<int>(itemsCount: {itemsCount}) with non-enumerated count", CreateTestData(itemsCount), itemsCount };
yield return new object[] { $"IEnumerable<int>(itemsCount: {itemsCount}) without non-enumerated count", new EnumerableWithoutCount<long>(CreateTestData(itemsCount)), itemsCount };
}
}
private static IEnumerable<object[]>? _sourceDataVariants;
public static IEnumerable<object[]> SourceDataVariants => _sourceDataVariants ??= CreateSourceDataVariants().ToArray();
private static IEnumerable<object[]> CreateSourceEmptyDataVariants() => new[]
{
new object[] { "int[]", Array.Empty<long>() },
new object[] { "List<int>", Array.Empty<long>().ToList() },
new object[] { "RecyclableArrayList<int>", Array.Empty<long>().ToRecyclableArrayList() },
new object[] { "RecyclableList<int>", Array.Empty<long>().ToRecyclableList() },
new object[] { "IList<int>", new CustomIList<long>(Array.Empty<long>()) },
new object[] { "IEnumerable<int> with non-enumerated count", Array.Empty<long>().AsEnumerable() },
new object[] { "IEnumerable<int> without non-enumerated count", new EnumerableWithoutCount<long>(Array.Empty<long>()) }
};
private static IEnumerable<object[]>? _emptySourceDataVariants;
public static IEnumerable<object[]> EmptySourceDataVariants => _emptySourceDataVariants ??= CreateSourceEmptyDataVariants().ToArray();
private static IEnumerable<object[]> CreateSourceTargetDataVariants()
{
foreach (var testData in SourceDataVariants)
{
foreach (var targetBlockSize in BlockSizeVariants)
{
yield return new object[] { testData[0], testData[1], testData[2], targetBlockSize };
}
}
}
private static IEnumerable<object[]>? _sourceTargetDataVariants;
public static IEnumerable<object[]> SourceTargetDataVariants => _sourceTargetDataVariants ??= CreateSourceTargetDataVariants().ToArray();
}
}
+216 -280
View File
@@ -5,69 +5,12 @@ namespace Recyclable.CollectionsTests
{ {
public class RecyclableListTests public class RecyclableListTests
{ {
private const int _totalObjectCount = 100_000;
private static RecyclableList<int> NewRecyclableList => CreateReversedRecyclableList(_testData);
private static readonly IEnumerable<int> _testData = Enumerable.Range(1, _totalObjectCount);
private static RecyclableList<int> CreateReversedRecyclableList(IEnumerable<int> source) => new(source, 1024);
private static IEnumerable<int> ItemsCountVariants => new[]
{
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] [Theory]
[MemberData(nameof(TargetTestDataVariants))] [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void AddShouldAddItems(int itemsCount, int targetBlockSize) public void AddShouldAddItems(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
using var list = new RecyclableList<int>(minBlockSize: targetBlockSize); using var list = new RecyclableList<long>(minBlockSize: targetBlockSize);
var testData = Enumerable.Range(1, itemsCount);
// Act // Act
foreach (var index in testData) foreach (var index in testData)
@@ -81,70 +24,69 @@ namespace Recyclable.CollectionsTests
} }
[Theory] [Theory]
[MemberData(nameof(TargetTestDataVariants))] [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void AddShouldAcceptDuplicates(int itemsCount, int targetBlockSize) public void AddShouldAcceptDuplicates(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
IEnumerable<int> testNumbers = Enumerable.Range(1, itemsCount) // Prepare
.Concat(Enumerable.Range(1, itemsCount)) testData = testData.Concat(testData).ToArray();
.ToArray();
// Act // Act
using var list = new RecyclableList<int>(minBlockSize: targetBlockSize); using var list = new RecyclableList<long>(minBlockSize: targetBlockSize);
foreach (var index in testNumbers) foreach (var index in testData)
{ {
list.Add(index); list.Add(index);
} }
// Validate // Validate
_ = list.LongCount.Should().Be(itemsCount * 2); _ = list.LongCount.Should().Be(itemsCount * 2);
_ = list.Should().ContainInConsecutiveOrder(testNumbers); _ = list.Should().ContainInConsecutiveOrder(testData);
} }
[Fact] [Theory]
public void AddShouldAddItemWhenAfterAddRange() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
//[InlineData("Debugging case", new long[1] { 1 }, 1, 2)]
public void AddShouldAddItemWhenAfterAddRange(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare
int[] testNumbers = new[] { 1, 2, 2, 3 };
// Act // Act
using var list = new RecyclableList<int>(); using var list = new RecyclableList<long>(minBlockSize: targetBlockSize);
list.AddRange(_testData); list.AddRange(testData);
foreach (var index in testNumbers) foreach (var index in testData)
{ {
list.Add(index); list.Add(index);
} }
// Validate // Validate
_ = list.LongCount.Should().Be(_totalObjectCount + testNumbers.Length); _ = list.LongCount.Should().Be(itemsCount * 2);
_ = list.Should().BeEquivalentTo(_testData.Concat(testNumbers)); _ = list.Should().ContainInConsecutiveOrder(testData.Concat(testData));
} }
[Fact] [Theory]
public void ConstructorShouldAcceptDuplicates() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void ConstructorShouldAcceptDuplicates(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
int[] testNumbers = new[] { 1, 2, 2, 3 }; testData = testData.Concat(testData).ToArray();
// Act // Act
using var list = new RecyclableList<int>(testNumbers); using var list = new RecyclableList<long>(testData, minBlockSize: targetBlockSize);
// Validate // Validate
_ = list.LongCount.Should().Be(testNumbers.Length); _ = list.LongCount.Should().Be(itemsCount * 2);
_ = list.Should().BeEquivalentTo(testNumbers); _ = list.Should().Equal(testData);
} }
[Fact] [Theory]
public void InitializeSortClearShouldSucceed() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void InitializeSortClearShouldSucceed(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
var expectedList = _testData;
// Act // Act
using RecyclableList<int> list = NewRecyclableList; using RecyclableList<long> list = new(testData, minBlockSize: targetBlockSize);
list.QuickSort(); list.QuickSort();
// Validate // Validate
_ = list.LongCount.Should().Be(_totalObjectCount); _ = list.LongCount.Should().Be(itemsCount);
_ = list.Should().Equal(expectedList); _ = list.Should().Equal(testData);
_ = expectedList.Should().AllSatisfy(expected => list.Contains(expected)); _ = testData.Should().AllSatisfy(expected => list.Contains(expected));
// Act // Act
list.Clear(); list.Clear();
@@ -153,12 +95,18 @@ namespace Recyclable.CollectionsTests
_ = list.LongCount.Should().Be(0); _ = list.LongCount.Should().Be(0);
} }
[Fact] [Theory]
public void ContainsShouldFindAllItems() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void ContainsShouldFindAllItems(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
var testData = _testData; if (itemsCount == 0)
using var list = NewRecyclableList; {
// There is nothing to search for when our lists are empty
return;
}
using var list = new RecyclableList<long>(testData, minBlockSize: targetBlockSize);
_ = testData.Any().Should().BeTrue("we need items on the list that we can look for"); _ = testData.Any().Should().BeTrue("we need items on the list that we can look for");
// Act // Act
@@ -169,12 +117,18 @@ namespace Recyclable.CollectionsTests
} }
} }
[Fact] [Theory]
public void ContainsShouldNotFindNonExistingItems() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void ContainsShouldNotFindNonExistingItems(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
var testData = _testData; if (itemsCount == 0)
using var list = NewRecyclableList; {
// There is nothing to search for when our lists are empty
return;
}
using var list = new RecyclableList<long>(testData, minBlockSize: targetBlockSize);
_ = testData.Any().Should().BeTrue("we need items on the list that we can look for"); _ = testData.Any().Should().BeTrue("we need items on the list that we can look for");
// Act // Act
@@ -184,41 +138,45 @@ namespace Recyclable.CollectionsTests
_ = list.Contains(-item).Should().BeFalse(); _ = list.Contains(-item).Should().BeFalse();
} }
// Validate // Validate - this case is special, because this is the default item value
_ = list.Contains(0).Should().BeFalse(); _ = list.Contains(0).Should().BeFalse();
} }
[Fact] [Theory]
public void ContainsShouldNotFindAnythingWhenListEmpty() [MemberData(nameof(RecyclableListTestData.TargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void ContainsShouldNotFindAnythingWhenListEmpty(long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
using var list = new RecyclableList<int>(); using var list = new RecyclableList<int>(minBlockSize: targetBlockSize, expectedItemsCount: itemsCount);
// Validate // Validate
_ = list.Contains(0).Should().BeFalse(); _ = list.Contains(0).Should().BeFalse();
_ = list.Contains(1).Should().BeFalse(); _ = list.Contains(1).Should().BeFalse();
} }
[Fact] [Theory]
public void CopyToShouldCopyAllItems() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
//[InlineData("Debuging case", new[] { 1L, 2, }, 2, 2)]
public void CopyToShouldCopyAllItems(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
using var list = NewRecyclableList; using var list = new RecyclableList<long>(testData, minBlockSize: targetBlockSize);
// Act // Act
var actual = new int[list.LongCount]; var actual = new long[itemsCount];
list.CopyTo(actual, 0); list.CopyTo(actual, 0);
_ = actual.Should().BeEquivalentTo(_testData); _ = actual.Should().ContainInConsecutiveOrder(testData);
} }
[Fact] [Theory]
public void GetEnumeratorShouldYieldAllItems() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void EnumeratorShouldYieldAllItems(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
using var list = NewRecyclableList; using var list = new RecyclableList<long>(testData, minBlockSize: targetBlockSize);
// Act // Act
var actual = new List<int>(); var actual = new List<long>();
var enumerator = list.GetEnumerator(); var enumerator = list.GetEnumerator();
while (enumerator.MoveNext()) while (enumerator.MoveNext())
{ {
@@ -226,29 +184,37 @@ namespace Recyclable.CollectionsTests
} }
// Validate // Validate
_ = actual.Should().BeEquivalentTo(_testData); _ = actual.Should().ContainInConsecutiveOrder(testData);
} }
[Fact] [Theory]
public void IndexOfShouldReturnCorrectIndexes() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void IndexOfShouldReturnCorrectIndexes(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
using var list = NewRecyclableList; using var list = new RecyclableList<long>(testData, minBlockSize: targetBlockSize);
// Act & Validate // Act & Validate
foreach (var item in _testData) foreach (var item in testData)
{ {
var actual = list.IndexOf(item); var actual = list.IndexOf(item);
_ = actual.Should().Be(item - 1); _ = actual.Should().Be((int)item - 1);
} }
} }
[Fact] [Theory]
public void IndexOfShouldNotFindNonExistingItems() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void IndexOfShouldNotFindNonExistingItems(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
var testData = _testData; if (itemsCount == 0)
using var list = NewRecyclableList; {
// There is nothing to search for when our lists are empty
return;
}
// Prepare
using var list = new RecyclableList<long>(testData, minBlockSize: targetBlockSize);
_ = testData.Any().Should().BeTrue("we need items on the list that we can look for"); _ = testData.Any().Should().BeTrue("we need items on the list that we can look for");
// Act // Act
@@ -273,30 +239,31 @@ namespace Recyclable.CollectionsTests
_ = list.IndexOf(1).Should().Be(-1); _ = list.IndexOf(1).Should().Be(-1);
} }
[Fact] [Theory]
public void InsertShouldRaiseNotSupportedException() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void InsertShouldRaiseNotSupportedException(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
using var list = NewRecyclableList; using var list = new RecyclableList<long>(testData, minBlockSize: targetBlockSize);
// Act // Act
_ = Assert.Throws<NotSupportedException>(() => list.Insert(0, -1)); _ = Assert.Throws<NotSupportedException>(() => list.Insert(0, -1));
} }
[Fact] [Theory]
public void RemoveShouldRemoveLastItem() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void RemoveShouldRemoveLastItem(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
using var list = NewRecyclableList; using var list = new RecyclableList<long>(testData, minBlockSize: targetBlockSize);
var expectedData = testData.ToArray();
int removedCount = 0; int removedCount = 0;
var testData = _testData.ToArray(); foreach (var item in testData.Reverse())
foreach (var item in _testData.Reverse())
{ {
// Act & Validate // Act & Validate
_ = list.Remove(item).Should().BeTrue($"we search for {item} which should exist"); _ = list.Remove(item).Should().BeTrue($"we search for {item} which should exist");
removedCount++; removedCount++;
var expectedList = testData[0..(testData.Length - removedCount)]; var expectedList = expectedData[0..((int)itemsCount - removedCount)];
_ = list.LongCount.Should().Be(expectedList.Length); _ = list.LongCount.Should().Be(expectedList.Length);
_ = list.Should().ContainInConsecutiveOrder(expectedList); _ = list.Should().ContainInConsecutiveOrder(expectedList);
} }
@@ -304,182 +271,150 @@ namespace Recyclable.CollectionsTests
_ = list.Should().BeEmpty(); _ = list.Should().BeEmpty();
} }
[Fact] [Theory]
public void RemoveShouldRaiseNotSupportedException() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void RemoveShouldRaiseNotSupportedException(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
using var list = NewRecyclableList; using var list = new RecyclableList<long>(testData, minBlockSize: targetBlockSize);
// Act // Act
_ = Assert.Throws<ArgumentOutOfRangeException>(() => list.Remove(_testData.First())); _ = Assert.Throws<ArgumentOutOfRangeException>(() => list.Remove(testData.First()));
} }
[Fact] [Theory]
public void RemoveAtShouldRaiseNotSupportedException() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void RemoveAtShouldRaiseNotSupportedException(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
using var list = NewRecyclableList; using var list = new RecyclableList<long>(testData, minBlockSize: targetBlockSize);
// Act // Act
_ = Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt(0)); _ = Assert.Throws<ArgumentOutOfRangeException>(() => list.RemoveAt(0));
} }
[Fact] [Theory]
public void DisposeShouldSucceed() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void DisposeShouldSucceed(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
using var list = NewRecyclableList; using var list = new RecyclableList<long>(testData, minBlockSize: targetBlockSize);
// Act // Act
list.Dispose(); list.Dispose();
// Validate // Validate
_ = list.LongCount.Should().Be(0L); _ = list.LongCount.Should().Be(0L);
_ = list.Should().BeEmpty();
} }
[Fact] [Theory]
public void ConsecutiveDisposeShouldSucceed() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void ConsecutiveDisposeShouldSucceed(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
using var list = NewRecyclableList; using var list = new RecyclableList<long>(testData, minBlockSize: targetBlockSize);
list.Dispose(); list.Dispose();
// Act // Act
list.Dispose(); list.Dispose();
} }
[Fact] [Theory]
public void ClearShouldRemoveAllItems() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void ClearShouldRemoveAllItems(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
using var list = NewRecyclableList; using var list = new RecyclableList<long>(testData, minBlockSize: targetBlockSize, expectedItemsCount: itemsCount);
_ = list.LongCount.Should().NotBe(0); _ = list.LongCount.Should().Be(itemsCount);
// Act // Act
list.Clear(); list.Clear();
list.Clear();
// Validate // Validate
_ = list.LongCount.Should().Be(0); _ = list.LongCount.Should().Be(0);
} _ = list.Should().BeEmpty();
public static IEnumerable<object[]> AddRangeCollectionTypes
{
get
{
yield return new object[] { "int[]", _testData.ToArray() };
yield return new object[] { "List<int>", _testData.ToList() };
yield return new object[] { "RecyclableArrayList<int>", _testData.ToRecyclableArrayList() };
foreach (var sourceBlockSize in BlockSizeVariants)
{
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[]
{
new object[] { "int[]", Array.Empty<int>() },
new object[] { "List<int>", Array.Empty<int>().ToList() },
new object[] { "RecyclableArrayList<int>", Array.Empty<int>().ToRecyclableArrayList() },
new object[] { "RecyclableList<int>", Array.Empty<int>().ToRecyclableList() },
new object[] { "IList<int>", new CustomIList<int>(Array.Empty<int>()) },
new object[] { "IEnumerable<int> with non-enumerated count", Array.Empty<int>().AsEnumerable() },
new object[] { "IEnumerable<int> without non-enumerated count", new EnumerableWithoutCount<int>(Array.Empty<int>()) }
};
[Theory]
[MemberData(nameof(AddRangeCollectionTypes))]
public void AddRangeShouldAddItemsInCorrectOrder(string testCase, IEnumerable<int> testData)
{
try
{
// Prepare
using var list = new RecyclableList<int>();
// Act
if (testData is int[] testDataArray)
{
list.AddRange(testDataArray);
}
else if (testData is List<int> testDataList)
{
list.AddRange(testDataList);
}
else if (testData is RecyclableArrayList<int> testDataRecyclableArrayList)
{
list.AddRange(testDataRecyclableArrayList);
}
else if (testData is RecyclableList<int> testDataRecyclableList)
{
list.AddRange(testDataRecyclableList);
}
else if (testData is IList<int> testDataIList)
{
list.AddRange(testDataIList);
}
else if (testData is IEnumerable<int> testDataIEnumerable)
{
list.AddRange(testDataIEnumerable);
}
else
{
throw new InvalidCastException("Unknown type of test data");
}
// Validate
int expectedItemsCount = testData.Count();
_ = list.Capacity.Should().BeGreaterThanOrEqualTo(expectedItemsCount, "when capacity == 0, then we should allocate as much memory as needed, only");
_ = list.Should().HaveCount(expectedItemsCount)
.And.ContainInConsecutiveOrder(testData);
}
finally
{
if (testData is IDisposable disposable)
{
disposable.Dispose();
}
}
} }
[Theory] [Theory]
[MemberData(nameof(AddRangeCollectionTypes))] [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void AddRangeShouldNotOverrideItems(string testCase, IEnumerable<int> testData) public void AddRangeShouldAddItemsInCorrectOrder(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
var list = new RecyclableList<int>(); using var list = new RecyclableList<long>(minBlockSize: targetBlockSize);
// Act // Act
if (testData is int[] testDataArray) if (testData is long[] testDataArray)
{
list.AddRange(testDataArray);
}
else if (testData is List<long> testDataList)
{
list.AddRange(testDataList);
}
else if (testData is RecyclableArrayList<long> testDataRecyclableArrayList)
{
list.AddRange(testDataRecyclableArrayList);
}
else if (testData is RecyclableList<long> testDataRecyclableList)
{
list.AddRange(testDataRecyclableList);
}
else if (testData is IList<long> testDataIList)
{
list.AddRange(testDataIList);
}
else if (testData is IEnumerable<long> testDataIEnumerable)
{
list.AddRange(testDataIEnumerable);
}
else
{
throw new InvalidCastException("Unknown type of test data");
}
// Validate
int expectedItemsCount = testData.Count();
_ = list.Capacity.Should().BeGreaterThanOrEqualTo(expectedItemsCount, "when capacity == 0, then we should allocate as much memory as needed, only");
_ = list.LongCount.Should().Be(expectedItemsCount);
_ = list.Should().ContainInConsecutiveOrder(testData);
}
[Theory]
[MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void AddRangeShouldNotOverrideItems(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{
// Prepare
using var list = new RecyclableList<long>(minBlockSize: targetBlockSize);
// Act
if (testData is long[] testDataArray)
{ {
list.AddRange(testDataArray); list.AddRange(testDataArray);
list.AddRange(testDataArray); list.AddRange(testDataArray);
} }
else if (testData is List<int> testDataList) else if (testData is List<long> testDataList)
{ {
list.AddRange(testDataList); list.AddRange(testDataList);
list.AddRange(testDataList); list.AddRange(testDataList);
} }
else if (testData is RecyclableArrayList<int> testDataRecyclableArrayList) else if (testData is RecyclableArrayList<long> testDataRecyclableArrayList)
{ {
list.AddRange(testDataRecyclableArrayList); list.AddRange(testDataRecyclableArrayList);
list.AddRange(testDataRecyclableArrayList); list.AddRange(testDataRecyclableArrayList);
} }
else if (testData is RecyclableList<int> testDataRecyclableList) else if (testData is RecyclableList<long> testDataRecyclableList)
{ {
list.AddRange(testDataRecyclableList); list.AddRange(testDataRecyclableList);
list.AddRange(testDataRecyclableList); list.AddRange(testDataRecyclableList);
} }
else if (testData is IList<int> testDataIList) else if (testData is IList<long> testDataIList)
{ {
list.AddRange(testDataIList); list.AddRange(testDataIList);
list.AddRange(testDataIList); list.AddRange(testDataIList);
} }
else if (testData is IEnumerable<int> testDataIEnumerable) else if (testData is IEnumerable<long> testDataIEnumerable)
{ {
list.AddRange(testDataIEnumerable); list.AddRange(testDataIEnumerable);
list.AddRange(testDataIEnumerable); list.AddRange(testDataIEnumerable);
@@ -491,42 +426,45 @@ namespace Recyclable.CollectionsTests
// Validate // Validate
var expectedData = testData.Concat(testData).ToArray(); var expectedData = testData.Concat(testData).ToArray();
_ = list.Capacity.Should().BeGreaterThanOrEqualTo(expectedData.Length)
.And.BeLessThan((int)(expectedData.Length * 1.5)); if (itemsCount > 0)
_ = list.Should().HaveCount(expectedData.Length) {
.And.ContainInConsecutiveOrder(expectedData) _ = list.Capacity.Should().BeGreaterThanOrEqualTo(targetBlockSize);
.And.BeEquivalentTo(expectedData); }
_ = list.LongCount.Should().Be(expectedData.Length);
_ = list.Should().ContainInConsecutiveOrder(expectedData);
} }
[Theory] [Theory]
[MemberData(nameof(AddRangeEmptyCollectionTypes))] [MemberData(nameof(RecyclableListTestData.EmptySourceDataVariants), MemberType = typeof(RecyclableListTestData))]
public void AddRangeShouldDoNothingWhenSourceIsEmpty(string testCase, IEnumerable<int> testData) public void AddRangeShouldDoNothingWhenSourceIsEmpty(string testCase, IEnumerable<long> testData)
{ {
// Prepare // Prepare
var list = new RecyclableList<int>(); using var list = new RecyclableList<long>();
// Act // Act
if (testData is int[] testDataArray) if (testData is long[] testDataArray)
{ {
list.AddRange(testDataArray); list.AddRange(testDataArray);
} }
else if (testData is List<int> testDataList) else if (testData is List<long> testDataList)
{ {
list.AddRange(testDataList); list.AddRange(testDataList);
} }
else if (testData is RecyclableArrayList<int> testDataRecyclableArrayList) else if (testData is RecyclableArrayList<long> testDataRecyclableArrayList)
{ {
list.AddRange(testDataRecyclableArrayList); list.AddRange(testDataRecyclableArrayList);
} }
else if (testData is RecyclableList<int> testDataRecyclableList) else if (testData is RecyclableList<long> testDataRecyclableList)
{ {
list.AddRange(testDataRecyclableList); list.AddRange(testDataRecyclableList);
} }
else if (testData is IList<int> testDataIList) else if (testData is IList<long> testDataIList)
{ {
list.AddRange(testDataIList); list.AddRange(testDataIList);
} }
else if (testData is IEnumerable<int> testDataIEnumerable) else if (testData is IEnumerable<long> testDataIEnumerable)
{ {
list.AddRange(testDataIEnumerable); list.AddRange(testDataIEnumerable);
} }
@@ -537,26 +475,27 @@ namespace Recyclable.CollectionsTests
// Validate // Validate
_ = list.Capacity.Should().Be(0); _ = list.Capacity.Should().Be(0);
_ = list.Should().BeEmpty().And.HaveCount(0); _ = list.LongCount.Should().Be(0);
_ = list.Should().BeEmpty();
} }
[Fact] [Theory]
public void InsertAtTheBeginningShouldMoveItems() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void InsertAtTheBeginningShouldMoveItems(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
var testData = _testData.ToArray(); using var list = new RecyclableList<long>(testData, minBlockSize: targetBlockSize);
var list = new RecyclableList<int>();
// Act // Act
_ = Assert.Throws<NotSupportedException>(() => list.Insert(0, -1)); _ = Assert.Throws<NotSupportedException>(() => list.Insert(0, -1));
} }
[Fact] [Theory]
public void LongIndexOfShouldReturnCorrectIndexes() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void LongIndexOfShouldReturnCorrectIndexes(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
var testData = _testData.ToArray(); using var list = new RecyclableList<long>(testData, minBlockSize: targetBlockSize);
var list = new RecyclableList<int>(testData);
// Act & Validate // Act & Validate
foreach (var item in testData) foreach (var item in testData)
@@ -569,12 +508,12 @@ namespace Recyclable.CollectionsTests
} }
} }
[Fact] [Theory]
public void LongIndexOfShouldNotFindNonExistingItems() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void LongIndexOfShouldNotFindNonExistingItems(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare // Prepare
var testData = _testData; using var list = new RecyclableList<long>(testData, minBlockSize: targetBlockSize);
using var list = NewRecyclableList;
_ = testData.Any().Should().BeTrue("we need items on the list that we can look for"); _ = testData.Any().Should().BeTrue("we need items on the list that we can look for");
// Act // Act
@@ -592,26 +531,23 @@ namespace Recyclable.CollectionsTests
public void LongIndexOfShouldNotFindAnythingWhenListEmpty() public void LongIndexOfShouldNotFindAnythingWhenListEmpty()
{ {
// Prepare // Prepare
using var list = new RecyclableList<int>(); using var list = new RecyclableList<long>();
// Validate // Validate
_ = list.LongIndexOf(0).Should().Be(-1); _ = list.LongIndexOf(0).Should().Be(-1);
_ = list.LongIndexOf(1).Should().Be(-1); _ = list.LongIndexOf(1).Should().Be(-1);
} }
[Fact] [Theory]
public void ConstructorSourceShouldInitializeList() [MemberData(nameof(RecyclableListTestData.SourceTargetDataVariants), MemberType = typeof(RecyclableListTestData))]
public void ConstructorSourceShouldInitializeList(string testCase, IEnumerable<long> testData, long itemsCount, int targetBlockSize)
{ {
// Prepare
var testData = _testData.ToArray();
// Act // Act
var list = new RecyclableList<int>(testData); using var list = new RecyclableList<long>(testData, minBlockSize: targetBlockSize);
// Validate // Validate
_ = list.Should().HaveCount(testData.Length) _ = list.LongCount.Should().Be(itemsCount);
.And.ContainInConsecutiveOrder(testData) _ = list.Should().ContainInConsecutiveOrder(testData);
.And.BeEquivalentTo(testData);
} }
} }
} }