RecyclableArrayList.Contains fixed to not throw exception when the list is empty.
RecyclableArrayList tests sorted alphabetically, simplified & extended to use the test data & validate various combinations of item counts & block sizes.
This commit is contained in:
1 parent
070549de9e
commit
277e52f701
2 files changed
+149
-152
No files matched your search
@@ -372,7 +372,7 @@ namespace Recyclable.Collections
|
|||||||
_count = 0;
|
_count = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool Contains(T item) => _memoryBlock.Contains(item);
|
public bool Contains(T item) => Array.IndexOf(_memoryBlock, 0, _count) >= 0;
|
||||||
|
|
||||||
public void CopyTo(T[] array, int arrayIndex) => Array.Copy(_memoryBlock, 0, array, arrayIndex, _count);
|
public void CopyTo(T[] array, int arrayIndex) => Array.Copy(_memoryBlock, 0, array, arrayIndex, _count);
|
||||||
|
|
||||||
|
|||||||
@@ -167,32 +167,116 @@ namespace Recyclable.CollectionsTests
|
|||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Fact]
|
||||||
public void InsertAtTheBeginningShouldMoveItems()
|
public void ContainsShouldNotFindAnythingWhenListEmpty()
|
||||||
{
|
{
|
||||||
// Prepare
|
// Prepare
|
||||||
var testData = _testData.ToArray();
|
using var list = new RecyclableArrayList<int>();
|
||||||
var list = new RecyclableArrayList<int>();
|
|
||||||
|
// Validate
|
||||||
|
_ = list.Contains(0).Should().BeFalse();
|
||||||
|
_ = list.Contains(1).Should().BeFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(RecyclableListTestData.SourceDataVariants), MemberType = typeof(RecyclableListTestData))]
|
||||||
|
public void ContainsShouldNotFindNonExistingItems(string testCase, IEnumerable<long> testData, long itemsCount)
|
||||||
|
{
|
||||||
|
if (itemsCount == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Prepare
|
||||||
|
using var list = new RecyclableArrayList<long>(testData);
|
||||||
|
_ = testData.Any().Should().BeTrue("we need items on the list that we can look for");
|
||||||
|
|
||||||
|
// Act
|
||||||
|
foreach (var item in testData)
|
||||||
|
{
|
||||||
|
// Validate
|
||||||
|
_ = list.Contains(-item).Should().BeFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate
|
||||||
|
_ = list.Contains(0).Should().BeFalse();
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(RecyclableListTestData.SourceDataVariants), MemberType = typeof(RecyclableListTestData))]
|
||||||
|
public void CopyToShouldCopyAllItemsInTheCorrectOrder(string testCase, IEnumerable<long> testData, long itemsCount)
|
||||||
|
{
|
||||||
|
// Prepare
|
||||||
|
var list = new RecyclableArrayList<long>(testData);
|
||||||
|
long[] copiedItems = new long[itemsCount];
|
||||||
|
|
||||||
|
// Act
|
||||||
|
list.CopyTo(copiedItems, 0);
|
||||||
|
|
||||||
|
// Validate
|
||||||
|
_ = copiedItems.Length.Should().Be((int)itemsCount);
|
||||||
|
_ = copiedItems.Should().ContainInConsecutiveOrder(testData);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(RecyclableListTestData.SourceDataVariants), MemberType = typeof(RecyclableListTestData))]
|
||||||
|
public void EnumerateShouldYieldAllItemsInCorrectOrder(string testCase, IEnumerable<long> testData, long itemsCount)
|
||||||
|
{
|
||||||
|
// Prepare
|
||||||
|
var list = new RecyclableArrayList<long>(testData);
|
||||||
|
|
||||||
|
// Act
|
||||||
|
var yieldedItems = new List<long>((int)itemsCount);
|
||||||
|
foreach (var item in list)
|
||||||
|
{
|
||||||
|
yieldedItems.Add(item);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Validate
|
||||||
|
_ = yieldedItems.Count.Should().Be((int)itemsCount);
|
||||||
|
_ = yieldedItems.Should().ContainInConsecutiveOrder(testData);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(RecyclableListTestData.SourceDataVariants), MemberType = typeof(RecyclableListTestData))]
|
||||||
|
public void IndexOfShouldFindTheIndexes(string testCase, IEnumerable<long> testData, long itemsCount)
|
||||||
|
{
|
||||||
|
// Prepare
|
||||||
|
using var list = new RecyclableArrayList<long>(testData);
|
||||||
|
|
||||||
|
// Act & Validate
|
||||||
|
foreach (var index in testData)
|
||||||
|
{
|
||||||
|
var actual = list.IndexOf(index);
|
||||||
|
_ = actual.Should().Be((int)(index - 1));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(RecyclableListTestData.SourceDataVariants), MemberType = typeof(RecyclableListTestData))]
|
||||||
|
public void InsertAtTheBeginningShouldMoveItems(string testCase, IEnumerable<long> testData, long itemsCount)
|
||||||
|
{
|
||||||
|
// Prepare
|
||||||
|
var list = new RecyclableArrayList<long>();
|
||||||
|
|
||||||
// Act
|
// Act
|
||||||
foreach (var item in testData)
|
foreach (var item in testData)
|
||||||
{
|
{
|
||||||
list.Insert(0, item);
|
list.Insert(0, item);
|
||||||
_ = list.Count.Should().Be(item);
|
_ = list.Count.Should().Be((int)item);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validate
|
// Validate
|
||||||
_ = list.Capacity.Should().Be(32);
|
_ = list.Capacity.Should().BeGreaterThanOrEqualTo((int)itemsCount);
|
||||||
_ = list.Should().HaveCount(testData.Length)
|
_ = list.Count.Should().Be((int)itemsCount);
|
||||||
.And.ContainInConsecutiveOrder(testData.Reverse())
|
_ = list.Should().ContainInConsecutiveOrder(testData.Reverse());
|
||||||
.And.BeEquivalentTo(testData);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Theory]
|
||||||
public void LongIndexOfShouldReturnCorrectIndexes()
|
[MemberData(nameof(RecyclableListTestData.SourceDataVariants), MemberType = typeof(RecyclableListTestData))]
|
||||||
|
public void LongIndexOfShouldReturnCorrectIndexes(string testCase, IEnumerable<long> testData, long itemsCount)
|
||||||
{
|
{
|
||||||
// Prepare
|
// Prepare
|
||||||
var testData = _testData.ToArray();
|
var list = new RecyclableArrayList<long>(testData);
|
||||||
var list = new RecyclableArrayList<int>(testData);
|
|
||||||
|
|
||||||
// Act & Validate
|
// Act & Validate
|
||||||
foreach (var item in testData)
|
foreach (var item in testData)
|
||||||
@@ -201,27 +285,70 @@ namespace Recyclable.CollectionsTests
|
|||||||
var index = list.IndexOf(item);
|
var index = list.IndexOf(item);
|
||||||
|
|
||||||
// Validate
|
// Validate
|
||||||
_ = index.Should().Be(item - 1);
|
_ = index.Should().Be((int)(item - 1));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
[Theory]
|
||||||
public void RemoveFromTheBeginningShouldRemoveTheCorrectItem()
|
[MemberData(nameof(RecyclableListTestData.SourceDataVariants), MemberType = typeof(RecyclableListTestData))]
|
||||||
|
public void RemoveAtFromTheBeginningShouldSucceed(string testCase, IEnumerable<long> testData, long itemsCount)
|
||||||
{
|
{
|
||||||
// Prepare
|
// Prepare
|
||||||
var testData = _testData.ToArray();
|
var list = new RecyclableArrayList<long>(testData);
|
||||||
var list = new RecyclableArrayList<int>(testData);
|
|
||||||
|
|
||||||
// Act & Validate
|
// Act & Validate
|
||||||
for (var deleted = 1; deleted <= testData.LongLength; deleted++)
|
for (var deleted = 1; deleted <= itemsCount; deleted++)
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
list.RemoveAt(0);
|
||||||
|
|
||||||
|
// Validate
|
||||||
|
_ = list.Count.Should().Be((int)(itemsCount - deleted));
|
||||||
|
_ = list.Should().ContainInConsecutiveOrder(testData.Skip(deleted));
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = list.Should().BeEmpty();
|
||||||
|
_ = list.Capacity.Should().BeGreaterThanOrEqualTo((int)itemsCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(RecyclableListTestData.SourceDataVariants), MemberType = typeof(RecyclableListTestData))]
|
||||||
|
public void RemoveAtFromTheEndShouldSucceed(string testCase, IEnumerable<long> testData, long itemsCount)
|
||||||
|
{
|
||||||
|
// Prepare
|
||||||
|
var list = new RecyclableArrayList<long>(testData);
|
||||||
|
|
||||||
|
// Act & Validate
|
||||||
|
for (var deleted = 1; deleted <= itemsCount; deleted++)
|
||||||
|
{
|
||||||
|
// Act
|
||||||
|
list.RemoveAt(list.Count - 1);
|
||||||
|
|
||||||
|
// Validate
|
||||||
|
_ = list.Count.Should().Be((int)(itemsCount - deleted));
|
||||||
|
_ = list.Should().ContainInConsecutiveOrder(testData.Take((int)(itemsCount - deleted)));
|
||||||
|
}
|
||||||
|
|
||||||
|
_ = list.Should().BeEmpty();
|
||||||
|
_ = list.Capacity.Should().BeGreaterThanOrEqualTo((int)itemsCount);
|
||||||
|
}
|
||||||
|
|
||||||
|
[Theory]
|
||||||
|
[MemberData(nameof(RecyclableListTestData.SourceDataVariants), MemberType = typeof(RecyclableListTestData))]
|
||||||
|
public void RemoveFromTheBeginningShouldRemoveTheCorrectItem(string testCase, IEnumerable<long> testData, long itemsCount)
|
||||||
|
{
|
||||||
|
// Prepare
|
||||||
|
var list = new RecyclableArrayList<long>(testData);
|
||||||
|
|
||||||
|
// Act & Validate
|
||||||
|
for (var deleted = 1; deleted <= itemsCount; deleted++)
|
||||||
{
|
{
|
||||||
// Act
|
// Act
|
||||||
_ = list.Remove(deleted).Should().BeTrue();
|
_ = list.Remove(deleted).Should().BeTrue();
|
||||||
|
|
||||||
// Validate
|
// Validate
|
||||||
_ = list.Should().HaveCount(testData.Length - deleted)
|
_ = list.Count.Should().Be((int)(itemsCount - deleted));
|
||||||
.And.ContainInConsecutiveOrder(testData.Skip(deleted))
|
_ = list.Should().ContainInConsecutiveOrder(testData.Skip(deleted));
|
||||||
.And.BeEquivalentTo(testData.Skip(deleted));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,135 +371,5 @@ namespace Recyclable.CollectionsTests
|
|||||||
.And.BeEquivalentTo(testData.Take(testData.Length - deleted));
|
.And.BeEquivalentTo(testData.Take(testData.Length - deleted));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void RemoveAtFromTheBeginningShouldSucceed()
|
|
||||||
{
|
|
||||||
// Prepare
|
|
||||||
var testData = _testData.ToArray();
|
|
||||||
var list = new RecyclableArrayList<int>(testData);
|
|
||||||
|
|
||||||
// Act & Validate
|
|
||||||
for (var deleted = 1; deleted <= testData.LongLength; deleted++)
|
|
||||||
{
|
|
||||||
// Act
|
|
||||||
list.RemoveAt(0);
|
|
||||||
|
|
||||||
// Validate
|
|
||||||
_ = list.Should().HaveCount(testData.Length - deleted)
|
|
||||||
.And.ContainInConsecutiveOrder(testData.Skip(deleted))
|
|
||||||
.And.BeEquivalentTo(testData.Skip(deleted));
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = list.Should().BeEmpty();
|
|
||||||
_ = list.Capacity.Should().Be(20);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void RemoveAtFromTheEndShouldSucceed()
|
|
||||||
{
|
|
||||||
// Prepare
|
|
||||||
var testData = _testData.ToArray();
|
|
||||||
var list = new RecyclableArrayList<int>(testData);
|
|
||||||
|
|
||||||
// Act & Validate
|
|
||||||
for (var deleted = 1; deleted <= testData.LongLength; deleted++)
|
|
||||||
{
|
|
||||||
// Act
|
|
||||||
list.RemoveAt(list.Count - 1);
|
|
||||||
|
|
||||||
// Validate
|
|
||||||
_ = list.Should().HaveCount(testData.Length - deleted)
|
|
||||||
.And.ContainInConsecutiveOrder(testData.Take(testData.Length - deleted))
|
|
||||||
.And.BeEquivalentTo(testData.Take(testData.Length - deleted));
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = list.Should().BeEmpty();
|
|
||||||
_ = list.Capacity.Should().Be(20);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void EnumerateShouldYieldAllItemsInCorrectOrder()
|
|
||||||
{
|
|
||||||
// Prepare
|
|
||||||
var testData = _testData.ToArray();
|
|
||||||
var list = new RecyclableArrayList<int>(testData);
|
|
||||||
|
|
||||||
// Act
|
|
||||||
var yieldedItems = new List<int>();
|
|
||||||
foreach (var item in list)
|
|
||||||
{
|
|
||||||
yieldedItems.Add(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate
|
|
||||||
_ = yieldedItems.Should().HaveCount(testData.Length)
|
|
||||||
.And.ContainInConsecutiveOrder(testData)
|
|
||||||
.And.BeEquivalentTo(testData);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void CopyToShouldCopyAllItemsInTheCorrectOrder()
|
|
||||||
{
|
|
||||||
// Prepare
|
|
||||||
var testData = _testData.ToArray();
|
|
||||||
var list = new RecyclableArrayList<int>(testData);
|
|
||||||
int[] yieldedItems = new int[testData.Length];
|
|
||||||
|
|
||||||
// Act
|
|
||||||
list.CopyTo(yieldedItems, 0);
|
|
||||||
|
|
||||||
// Validate
|
|
||||||
_ = yieldedItems.Should().HaveCount(testData.Length)
|
|
||||||
.And.ContainInConsecutiveOrder(testData)
|
|
||||||
.And.BeEquivalentTo(testData);
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void IndexOfShouldFindTheIndexes()
|
|
||||||
{
|
|
||||||
// Prepare
|
|
||||||
var testData = _testData.ToArray();
|
|
||||||
using var list = new RecyclableArrayList<int>(testData);
|
|
||||||
|
|
||||||
// Act & Validate
|
|
||||||
foreach (var index in _testData.ToArray())
|
|
||||||
{
|
|
||||||
var actual = list.IndexOf(index);
|
|
||||||
_ = actual.Should().Be(index - 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void ContainsShouldNotFindNonExistingItems()
|
|
||||||
{
|
|
||||||
// Prepare
|
|
||||||
var testData = _testData.ToArray();
|
|
||||||
using var list = new RecyclableArrayList<int>(testData);
|
|
||||||
_ = testData.Any().Should().BeTrue("we need items on the list that we can look for");
|
|
||||||
|
|
||||||
// Act
|
|
||||||
foreach (var item in testData)
|
|
||||||
{
|
|
||||||
// Validate
|
|
||||||
_ = list.Contains(-item).Should().BeFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate
|
|
||||||
_ = list.Contains(0).Should().BeFalse();
|
|
||||||
}
|
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void ContainsShouldNotFindAnythingWhenListEmpty()
|
|
||||||
{
|
|
||||||
// Prepare
|
|
||||||
var testData = _testData.ToArray();
|
|
||||||
using var list = new RecyclableArrayList<int>(testData);
|
|
||||||
|
|
||||||
// Validate
|
|
||||||
_ = list.Contains(0).Should().BeFalse();
|
|
||||||
_ = list.Contains(1).Should().BeTrue();
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user