Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ToString and comparison operators to ByteRate with tests #803

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
40 changes: 40 additions & 0 deletions src/Humanizer.Tests.Shared/Bytes/ByteRateTests.cs
Expand Up @@ -77,5 +77,45 @@ public void ThowsOnUnsupportedData(TimeUnit units)
});
}

[Theory]
[InlineData(400, 10, 400, 10, 0)]
[InlineData(400, 10, 800, 20, 0)]
[InlineData(800, 20, 400, 10, 0)]
[InlineData(400, 10, 800, 10, -1)]
[InlineData(800, 10, 400, 10, 1)]
[InlineData(800, 10, 400, 20, 1)]
[InlineData(400, 20, 800, 10, -1)]
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)]
[InlineData(1024, 10, 12, 2, 0)]
[InlineData(2048, 20, 6, 1, 0)]
[InlineData(1024, 10, 12, 1, -1)]
[InlineData(2048, 10, 6, 1, 1)]
[InlineData(2048, 10, 6, 2, 1)]
[InlineData(1024, 20, 12, 1, -1)]
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);
}

}
}
Expand Up @@ -2,13 +2,18 @@
[assembly: System.Runtime.Versioning.TargetFrameworkAttribute(".NETStandard,Version=v1.0", FrameworkDisplayName="")]
namespace Humanizer.Bytes
{
public class ByteRate
public class ByteRate : System.IComparable, System.IComparable<Humanizer.Bytes.ByteRate>, System.IEquatable<Humanizer.Bytes.ByteRate>
{
public ByteRate(Humanizer.Bytes.ByteSize size, System.TimeSpan interval) { }
public System.TimeSpan Interval { get; }
public Humanizer.Bytes.ByteSize Size { get; }
public int CompareTo(Humanizer.Bytes.ByteRate other) { }
public int CompareTo(object obj) { }
public bool Equals(Humanizer.Bytes.ByteRate other) { }
public string Humanize(Humanizer.Localisation.TimeUnit timeUnit = 1) { }
public string Humanize(string format, Humanizer.Localisation.TimeUnit timeUnit = 1) { }
public override string ToString() { }
public string ToString(string format, Humanizer.Localisation.TimeUnit timeUnit = 1) { }
}
public struct ByteSize : System.IComparable, System.IComparable<Humanizer.Bytes.ByteSize>, System.IEquatable<Humanizer.Bytes.ByteSize>
{
Expand Down
55 changes: 54 additions & 1 deletion src/Humanizer/Bytes/ByteRate.cs
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,58 @@ public string Humanize(string format, TimeUnit timeUnit = TimeUnit.Second)
return new ByteSize(Size.Bytes / Interval.TotalSeconds * displayInterval.TotalSeconds)
.Humanize(format) + '/' + displayUnit;
}
/// <summary>
/// Returns the humanized string with default parameters.
/// </summary>
/// <returns></returns>
public override string ToString()
{
return Humanize();
}

/// <summary>
/// Returns a humanized string of the current rate object using the supplied parameters
/// </summary>
/// <param name="timeUnit">Unit of time to calculate rate for (defaults is per second)</param>
/// <param name="format">The string format to use for the number of bytes</param>
/// <returns></returns>
public string ToString(string format, TimeUnit timeUnit = TimeUnit.Second)
{
return Humanize(format, timeUnit);
}

/// <inheritdoc />
/// <summary>
/// Compares the current ByteRate object to another supplied ByteRate object.
/// Rates are normalized before comparing, e.g. 60Mb/Min is equal to 1024KB/sec
/// </summary>
/// <param name="other">The ByteRate object to use for the comparison</param>
/// <returns>0 if the rates are equivalent, -1 if lower than the 'other' object, 1 if higher than the 'other' object</returns>
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;
}

/// <inheritdoc />
/// <summary>
/// Checks if two ByteRate objects have the same equivalent rate
/// </summary>
/// <param name="other"></param>
/// <returns>True if rates are equivalent, otherwise false</returns>
public bool Equals(ByteRate other)
{
return CompareTo(other) == 0;
Cirzen marked this conversation as resolved.
Show resolved Hide resolved
}

/// <inheritdoc />
public int CompareTo(Object obj)
{
if (obj == null) return 1;
var cmp = (ByteRate)obj;
Cirzen marked this conversation as resolved.
Show resolved Hide resolved
return CompareTo(cmp);
}
}
}