ex-col-02-bucket-chain/BucketChain.Test/BucketTests.cs
github-classroom[bot] 593d8ebfea
Initial commit
2024-11-17 08:52:38 +00:00

96 lines
No EOL
2.4 KiB
C#

using FluentAssertions;
using Xunit;
namespace BucketChain.Test;
public sealed class BucketTests
{
private const double BucketSize = 1.2D;
[Fact]
public void Construction()
{
var bucket = new Bucket(BucketSize);
bucket.IsEmpty.Should().BeTrue();
}
[Fact]
public void Fill_Simple()
{
var bucket = new Bucket(BucketSize);
bucket.Fill(BucketSize)
.Should().Be(BucketSize, "full amount used");
bucket.IsEmpty.Should().BeFalse();
}
[Fact]
public void Fill_NotEnough()
{
const double Available = BucketSize / 2;
var bucket = new Bucket(BucketSize);
bucket.Fill(Available)
.Should().Be(Available, "everything available used");
bucket.IsEmpty.Should().BeFalse("not completely full, but not empty either");
}
[Fact]
public void Fill_MoreThanNeeded()
{
const double Available = BucketSize * 3;
var bucket = new Bucket(BucketSize);
bucket.Fill(Available)
.Should().Be(BucketSize, "can't take more than fits into bucket");
bucket.IsEmpty.Should().BeFalse();
}
[Fact]
public void Fill_InvalidAmount()
{
const double Available = -3;
var bucket = new Bucket(BucketSize);
bucket.Fill(Available)
.Should().Be(0, "cannot take anything");
bucket.IsEmpty.Should().BeTrue("bucket is still empty");
}
[Fact]
public void Empty_Simple()
{
var bucket = new Bucket(BucketSize);
bucket.Fill(BucketSize * 2);
bucket.Empty()
.Should().Be(BucketSize, "bucket was full and gets emptied completely");
bucket.IsEmpty.Should().BeTrue("has been emptied");
}
[Fact]
public void Empty_PartiallyFull()
{
var bucket = new Bucket(BucketSize);
bucket.Fill(BucketSize / 2);
bucket.Empty()
.Should().BeApproximately(BucketSize / 2,
double.Epsilon, "bucket was only partially full and gets emptied completely");
bucket.IsEmpty.Should().BeTrue("has been emptied");
}
[Fact]
public void Empty_Empty()
{
var bucket = new Bucket(BucketSize);
bucket.Empty()
.Should().Be(0, "bucket was empty");
bucket.IsEmpty.Should().BeTrue("bucket is still empty");
}
}