Made Add function, Added Tests. Added comments to int Tests
This commit is contained in:
parent
aad560b4fc
commit
a032e495cf
3 changed files with 71 additions and 1 deletions
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
public class FlexArrayIntTests
|
||||
{
|
||||
// Constructor tests
|
||||
[Fact]
|
||||
public void Construction_NoSize()
|
||||
{
|
||||
|
|
@ -36,6 +37,7 @@ public class FlexArrayIntTests
|
|||
array.Capacity.Should().Be(0, "negative size provided => set to 0");
|
||||
}
|
||||
|
||||
// Add tests
|
||||
[Fact]
|
||||
public void AddItem_Single()
|
||||
{
|
||||
|
|
@ -81,6 +83,7 @@ public class FlexArrayIntTests
|
|||
array.Count.Should().Be(FlexArrayInt.DefaultStartSize + 2);
|
||||
}
|
||||
|
||||
// Contains tests
|
||||
[Fact]
|
||||
public void Contains_Success()
|
||||
{
|
||||
|
|
@ -127,6 +130,7 @@ public class FlexArrayIntTests
|
|||
.Should().BeFalse("not contained in list");
|
||||
}
|
||||
|
||||
// Indexer tests
|
||||
[Fact]
|
||||
public void Indexer_Simple()
|
||||
{
|
||||
|
|
@ -176,6 +180,7 @@ public class FlexArrayIntTests
|
|||
}
|
||||
}
|
||||
|
||||
// Remove tests
|
||||
[Fact]
|
||||
public void Remove_ByIndex_Simple()
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
public class FlexArrayStringTests
|
||||
{
|
||||
// Constructor tests
|
||||
[Fact]
|
||||
public void Construction_NoSize()
|
||||
{
|
||||
|
|
@ -35,4 +36,51 @@ public class FlexArrayStringTests
|
|||
array.Count.Should().Be(0);
|
||||
array.Capacity.Should().Be(0, "negative size provided => set to 0");
|
||||
}
|
||||
|
||||
// Add tests
|
||||
[Fact]
|
||||
public void AddItem_Single()
|
||||
{
|
||||
var array = new FlexArrayString();
|
||||
|
||||
array.Add("Hello");
|
||||
|
||||
array.Count.Should().Be(1, "one item added");
|
||||
array.Capacity.Should().Be(FlexArrayString.DefaultStartSize, "capacity unchanged");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddItem_Multiple_NoGrowth()
|
||||
{
|
||||
var array = new FlexArrayString();
|
||||
|
||||
array.Add("Hello");
|
||||
array.Add("World");
|
||||
array.Add("!");
|
||||
|
||||
array.Count.Should().Be(3, "three items added");
|
||||
array.Capacity.Should().Be(FlexArrayString.DefaultStartSize, "no growth required yet");
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void AddItem_Multiple_Growth()
|
||||
{
|
||||
var array = new FlexArrayString();
|
||||
|
||||
void AddItems(int count)
|
||||
{
|
||||
for (var i = 0; i < count; i++)
|
||||
{
|
||||
// random string
|
||||
array.Add(Guid.NewGuid().ToString());
|
||||
}
|
||||
}
|
||||
|
||||
AddItems(FlexArrayString.DefaultStartSize);
|
||||
array.Capacity.Should().Be(FlexArrayString.DefaultStartSize);
|
||||
|
||||
AddItems(2);
|
||||
array.Capacity.Should().BeGreaterThan(FlexArrayString.DefaultStartSize, "capacity exceeded, had to grow");
|
||||
array.Count.Should().Be(FlexArrayString.DefaultStartSize + 2);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -27,4 +27,21 @@ public class FlexArrayString
|
|||
var size = Math.Max(0, initialSize ?? DefaultStartSize);
|
||||
_data = new string[size];
|
||||
}
|
||||
|
||||
public void Add(string input)
|
||||
{
|
||||
if (Capacity == Count)
|
||||
{
|
||||
Grow();
|
||||
}
|
||||
|
||||
_data[Count++] = input;
|
||||
}
|
||||
|
||||
private void Grow()
|
||||
{
|
||||
var newData = new string[Capacity * 2];
|
||||
Array.Copy(_data, newData, Count);
|
||||
_data = newData;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue