84 lines
2.3 KiB
C#
84 lines
2.3 KiB
C#
namespace SantaClausInc.Test;
|
|
|
|
public sealed class ListTests
|
|
{
|
|
[Fact]
|
|
public void Construction()
|
|
{
|
|
var list = new List<int>();
|
|
|
|
list.Count.Should().Be(0, "no values added so far");
|
|
list.Capacity.Should().Be(List<int>.InitialCapacity, "capacity is >0 right away");
|
|
}
|
|
|
|
[Fact]
|
|
public void Add_Single()
|
|
{
|
|
const long Value = 44L;
|
|
var list = new List<long>();
|
|
|
|
list.Add(Value);
|
|
|
|
list.Count.Should().Be(1, "one item added");
|
|
list.Capacity.Should().Be(List<long>.InitialCapacity, "did not have to grow yet");
|
|
list[0].Should()
|
|
.NotBe(default(long), "valid index")
|
|
.And.Be(Value, "value added to list is stored within and can be accessed");
|
|
}
|
|
|
|
[Fact]
|
|
public void Add_Multiple()
|
|
{
|
|
const string Value1 = "foo";
|
|
const string? Value2 = null;
|
|
const string Value3 = "bar";
|
|
var list = new List<string?>();
|
|
|
|
list.Add(Value1);
|
|
list.Add(Value2);
|
|
list.Add(Value3);
|
|
|
|
list.Count.Should().Be(3, "three items added");
|
|
list.Capacity.Should().Be(List<long>.InitialCapacity, "did not have to grow yet");
|
|
(list[0], list[1], list[2]).Should().Be((Value1, Value2, Value3),
|
|
"values added are stored in the order in which they have been added");
|
|
}
|
|
|
|
[Fact]
|
|
public void Add_TriggerGrowth()
|
|
{
|
|
const int Times = 100;
|
|
var startDate = DateOnly.FromDateTime(DateTime.Now);
|
|
var date = startDate;
|
|
var list = new List<DateOnly>();
|
|
|
|
for (var i = 0; i < Times; i++)
|
|
{
|
|
list.Add(date);
|
|
date = date.AddDays(1);
|
|
}
|
|
|
|
list.Count.Should().Be(Times);
|
|
list[Times - 1].Should().Be(startDate.AddDays(Times - 1));
|
|
list.Capacity.Should().Be(108, "grows by 1.5 every time the capacity is exceeded");
|
|
}
|
|
|
|
[Fact]
|
|
public void Indexer_Simple()
|
|
{
|
|
const float Value = 12.34F;
|
|
var list = new List<float>();
|
|
list.Add(Value);
|
|
|
|
list[0].Should().Be(Value, "accessing value with proper index");
|
|
}
|
|
|
|
[Fact]
|
|
public void Indexer_Invalid()
|
|
{
|
|
var list = new List<float>();
|
|
list.Add(12.34F);
|
|
|
|
list[10].Should().Be(default(float), "invalid index used");
|
|
}
|
|
}
|