Added Remove Tests and Remove Methods
This commit is contained in:
parent
1fe40ef8ab
commit
f54f8f397a
2 changed files with 209 additions and 0 deletions
|
|
@ -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");
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -55,6 +55,53 @@ public class FlexArrayString
|
|||
|
||||
return false;
|
||||
}
|
||||
|
||||
public bool Remove(string value, bool firstOnly)
|
||||
{
|
||||
var removedAny = false;
|
||||
for (var i = 0; i < Count; i++)
|
||||
{
|
||||
if (value != _data[i])
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
RemoveAt(i--);
|
||||
removedAny = true;
|
||||
if (firstOnly)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return removedAny;
|
||||
}
|
||||
|
||||
public bool RemoveAt(int index)
|
||||
{
|
||||
if (index < 0 || index >= Count)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
if (index != Count - 1)
|
||||
{
|
||||
ShiftLeft(index);
|
||||
|
||||
}
|
||||
|
||||
Count--;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
private void ShiftLeft(int fromIndex)
|
||||
{
|
||||
for (var i = fromIndex; i < Count; i++)
|
||||
{
|
||||
_data[i] = _data[i + 1];
|
||||
}
|
||||
}
|
||||
|
||||
private void Grow()
|
||||
{
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue