Skip to content

Commit

Permalink
Add ToString and comparison operators to ByteRate with tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Cirzen committed Mar 26, 2019
1 parent 81c6dbe commit 4844a70
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
42 changes: 41 additions & 1 deletion src/Humanizer.Tests.Shared/Bytes/ByteRateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public void FormattedTimeUnitTests(long bytes, int measurementIntervalSeconds, T
[InlineData(TimeUnit.Month)]
[InlineData(TimeUnit.Week)]
[InlineData(TimeUnit.Year)]
public void ThowsOnUnsupportedData(TimeUnit units)
public void ThrowsOnUnsupportedData(TimeUnit units)
{
var dummyRate = ByteSize.FromBits(1).Per(TimeSpan.FromSeconds(1));

Expand All @@ -77,5 +77,45 @@ public void ThowsOnUnsupportedData(TimeUnit units)
});
}

[Theory]
[InlineData(400, 10, 400, 10, 0)] // 40.CompareTo(40)
[InlineData(400, 10, 800, 20, 0)] // 40.CompareTo(40)
[InlineData(800, 20, 400, 10, 0)] // 40.CompareTo(40)
[InlineData(400, 10, 800, 10, -1)] // 40.CompareTo(80)
[InlineData(800, 10, 400, 10, 1)] // 80.CompareTo(40)
[InlineData(800, 10, 400, 20, 1)] // 80.CompareTo(20)
[InlineData(400, 20, 800, 10, -1)] // 20.CompareTo(80)
public void ComparisonTests_SameTypes(long leftBytes, int leftIntervalSeconds, long rightBytes, int rightIntervalSeconds, int expectedValue)
{
var leftSize = ByteSize.FromBytes(leftBytes);
var leftInterval = TimeSpan.FromSeconds(leftIntervalSeconds);
var leftByteRate = new ByteRate(leftSize, leftInterval);
var rightSize = ByteSize.FromBytes(rightBytes);
var rightInterval = TimeSpan.FromSeconds(rightIntervalSeconds);
var rightByteRate = new ByteRate(rightSize, rightInterval);

Assert.Equal(leftByteRate.CompareTo(rightByteRate), expectedValue);
}

[Theory]
[InlineData(1024, 10, 6, 1, 0)] // 1024.CompareTo(1024)
[InlineData(1024, 10, 12, 2, 0)] // 1024.CompareTo(1024)
[InlineData(2048, 20, 6, 1, 0)] // 1024.CompareTo(1024)
[InlineData(1024, 10, 12, 1, -1)] // 1024.CompareTo(2048)
[InlineData(2048, 10, 6, 1, 1)] // 2048.CompareTo(1024)
[InlineData(2048, 10, 6, 2, 1)] // 2048.CompareTo(512)
[InlineData(1024, 20, 12, 1, -1)] // 512.CompareTo(2048)
public void ComparisonTests_DifferingTypes(long leftKiloBytes, int leftIntervalSeconds, long rightMegaBytes, int rightIntervalMinutes, int expectedValue)
{
var leftSize = ByteSize.FromKilobytes(leftKiloBytes);
var leftInterval = TimeSpan.FromSeconds(leftIntervalSeconds);
var leftByteRate = new ByteRate(leftSize, leftInterval);
var rightSize = ByteSize.FromMegabytes(rightMegaBytes);
var rightInterval = TimeSpan.FromMinutes(rightIntervalMinutes);
var rightByteRate = new ByteRate(rightSize, rightInterval);

Assert.Equal(leftByteRate.CompareTo(rightByteRate), expectedValue);
}

}
}
32 changes: 31 additions & 1 deletion src/Humanizer/Bytes/ByteRate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ namespace Humanizer.Bytes
/// <summary>
/// Class to hold a ByteSize and a measurement interval, for the purpose of calculating the rate of transfer
/// </summary>
public class ByteRate
public class ByteRate : IComparable<ByteRate>, IEquatable<ByteRate>, IComparable
{
/// <summary>
/// Quantity of bytes
Expand Down Expand Up @@ -76,5 +76,35 @@ public string Humanize(string format, TimeUnit timeUnit = TimeUnit.Second)
return new ByteSize(Size.Bytes / Interval.TotalSeconds * displayInterval.TotalSeconds)
.Humanize(format) + '/' + displayUnit;
}

public override string ToString()
{
return Humanize();
}

public string ToString(string format, TimeUnit timeUnit = TimeUnit.Second)
{
return Humanize(format, timeUnit);
}

public int CompareTo(ByteRate other)
{
var left = Size.Bytes / Interval.TotalSeconds;
var right = other.Size.Bytes / other.Interval.TotalSeconds;
if (left < right) return -1;
return right < left ? 1 : 0;
}

public bool Equals(ByteRate other)
{
return CompareTo(other) == 0;
}

public int CompareTo(Object obj)
{
if (obj == null) return 1;
var cmp = (ByteRate)obj;
return CompareTo(cmp);
}
}
}

0 comments on commit 4844a70

Please sign in to comment.