diff --git a/src/Humanizer.Tests.Shared/Bytes/ByteRateTests.cs b/src/Humanizer.Tests.Shared/Bytes/ByteRateTests.cs index e596211a2..8df310403 100644 --- a/src/Humanizer.Tests.Shared/Bytes/ByteRateTests.cs +++ b/src/Humanizer.Tests.Shared/Bytes/ByteRateTests.cs @@ -59,8 +59,6 @@ public void FormattedTimeUnitTests(long bytes, int measurementIntervalSeconds, T Assert.Equal(expectedValue, text); } - - [Theory] [InlineData(TimeUnit.Millisecond)] [InlineData(TimeUnit.Day)] @@ -77,5 +75,44 @@ 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); + } } } diff --git a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt index 791ed1eaa..109b169a5 100644 --- a/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt +++ b/src/Humanizer.Tests/ApiApprover/PublicApiApprovalTest.approve_public_api.approved.txt @@ -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, System.IEquatable { 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, System.IEquatable { diff --git a/src/Humanizer/Bytes/ByteRate.cs b/src/Humanizer/Bytes/ByteRate.cs index 67534eeca..c0d923c55 100644 --- a/src/Humanizer/Bytes/ByteRate.cs +++ b/src/Humanizer/Bytes/ByteRate.cs @@ -7,7 +7,7 @@ namespace Humanizer.Bytes /// /// Class to hold a ByteSize and a measurement interval, for the purpose of calculating the rate of transfer /// - public class ByteRate + public class ByteRate : IComparable, IEquatable, IComparable { /// /// Quantity of bytes @@ -76,5 +76,59 @@ public string Humanize(string format, TimeUnit timeUnit = TimeUnit.Second) return new ByteSize(Size.Bytes / Interval.TotalSeconds * displayInterval.TotalSeconds) .Humanize(format) + '/' + displayUnit; } + + /// + /// Returns the humanized string with default parameters. + /// + /// + public override string ToString() + { + return Humanize(); + } + + /// + /// Returns a humanized string of the current rate object using the supplied parameters + /// + /// Unit of time to calculate rate for (defaults is per second) + /// The string format to use for the number of bytes + /// + public string ToString(string format, TimeUnit timeUnit = TimeUnit.Second) + { + return Humanize(format, timeUnit); + } + + /// + /// + /// Compares the current ByteRate object to another supplied ByteRate object. + /// Rates are normalized before comparing, e.g. 60Mb/Min is equal to 1024KB/sec + /// + /// The ByteRate object to use for the comparison + /// 0 if the rates are equivalent, -1 if lower than the 'other' object, 1 if higher than the 'other' object + 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; + } + + /// + /// + /// Checks if two ByteRate objects have the same equivalent rate + /// + /// + /// True if rates are equivalent, otherwise false + public bool Equals(ByteRate other) + { + return (Size.Bytes / Interval.TotalSeconds) - (other.Size.Bytes / other.Interval.TotalSeconds) == 0; + } + + /// + public int CompareTo(Object obj) + { + if (obj == null) return 1; + var cmp = (ByteRate)obj; + return CompareTo(cmp); + } } }