Added Remove Tests and Remove Methods

This commit is contained in:
MarcUs7i 2024-11-27 17:56:36 +01:00
parent 1fe40ef8ab
commit f54f8f397a
2 changed files with 209 additions and 0 deletions

View file

@ -180,4 +180,166 @@ public class FlexArrayStringTests
array[i].Should().Be(i.ToString());
}
}
// Remove tests
[Fact]
public void Remove_ByIndex_Simple()
{
var array = new FlexArrayString();
array.Add("Hello");
array.Count.Should().Be(1);
array.RemoveAt(0).Should().BeTrue("index exists");
array.Count.Should().Be(0, "count has to be reduced");
}
[Fact]
public void Remove_ByIndex_Shifting()
{
var array = new FlexArrayString();
array.Add("Goodbye");
array.Add("World");
array.Add("!");
array.Count.Should().Be(3);
array.RemoveAt(1).Should().BeTrue();
array.Count.Should().Be(2);
array[1].Should().Be("!", "items to the right have to be shifted left after removal");
array.RemoveAt(2)
.Should().BeFalse("not a valid index any longer, despite a value still being there");
}
[Fact]
public void Remove_ByIndex_FromEnd()
{
var array = new FlexArrayString();
array.Add("Hello");
array.Add("World");
array.Add("!");
array.Count.Should().Be(3);
array.RemoveAt(2).Should().BeTrue();
array.Count.Should().Be(2);
array[1].Should().Be("World");
}
[Fact]
public void Remove_ByIndex_Invalid()
{
var array = new FlexArrayString();
array.Add("This is a test!");
array.RemoveAt(1)
.Should().BeFalse("the array is big enough for this index, but no value has been set there");
array.Count.Should().Be(1, "unchanged");
array.RemoveAt(-1)
.Should().BeFalse("negative index doesn't make any sense");
}
[Fact]
public void Remove_Single_OneOccurrence()
{
var array = new FlexArrayString();
array.Add("Hello");
array.Add("corrupted");
array.Add("World!");
array.Remove("corrupted", true)
.Should().BeTrue("element was found");
array.Count.Should().Be(2, "element was removed");
}
[Fact]
public void Remove_Single_OnlyOccurrence()
{
var array = new FlexArrayString();
array.Add("Hello");
array.Remove("Hello", true)
.Should().BeTrue("element was found");
array.Count.Should().Be(0, "element was removed");
}
[Fact] public void Remove_Single_MultipleOccurrence()
{
var array = new FlexArrayString();
array.Add("Ever");
array.Add("used");
array.Add("kubernetes?");
array.Remove("kubernetes?", true)
.Should().BeTrue("element was found");
array.Count.Should().Be(2, "only one element was removed");
}
[Fact]
public void Remove_Single_NotFound()
{
var array = new FlexArrayString();
array.Add("1");
array.Add("2");
array.Add("3");
array.Remove("4", true)
.Should().BeFalse("element was not found");
array.Count.Should().Be(3, "unchanged");
}
[Fact]
public void Remove_Multiple_OneOccurrence()
{
var array = new FlexArrayString();
array.Add("1");
array.Add("2");
array.Add("3");
array.Remove("2", false)
.Should().BeTrue("element was found");
array.Count.Should().Be(2, "only one element was removed");
}
[Fact]
public void Remove_Multiple_MultipleOccurrence()
{
var array = new FlexArrayString();
array.Add("1");
array.Add("2");
array.Add("2");
array.Add("3");
array.Add("2");
array.Add("4");
array.Add("2");
array.Remove("2", false)
.Should().BeTrue("elements were found");
array.Count.Should().Be(3, "four elements were removed");
}
[Fact]
public void Remove_Multiple_OnlyOccurrence()
{
var array = new FlexArrayString();
array.Add("1");
array.Add("1");
array.Add("1");
array.Remove("1", false)
.Should().BeTrue("elements were found");
array.Count.Should().Be(0, "all elements were removed");
}
[Fact]
public void Remove_Multiple_NoOccurrence()
{
var array = new FlexArrayString();
array.Add("1");
array.Add("2");
array.Add("3");
array.Remove("4", false)
.Should().BeFalse("element was not found");
array.Count.Should().Be(3, "unchanged");
}
}