Skip to content

Commit 4844a70

Browse files
committed
Add ToString and comparison operators to ByteRate with tests
1 parent 81c6dbe commit 4844a70

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed

src/Humanizer.Tests.Shared/Bytes/ByteRateTests.cs

+41-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ public void FormattedTimeUnitTests(long bytes, int measurementIntervalSeconds, T
6767
[InlineData(TimeUnit.Month)]
6868
[InlineData(TimeUnit.Week)]
6969
[InlineData(TimeUnit.Year)]
70-
public void ThowsOnUnsupportedData(TimeUnit units)
70+
public void ThrowsOnUnsupportedData(TimeUnit units)
7171
{
7272
var dummyRate = ByteSize.FromBits(1).Per(TimeSpan.FromSeconds(1));
7373

@@ -77,5 +77,45 @@ public void ThowsOnUnsupportedData(TimeUnit units)
7777
});
7878
}
7979

80+
[Theory]
81+
[InlineData(400, 10, 400, 10, 0)] // 40.CompareTo(40)
82+
[InlineData(400, 10, 800, 20, 0)] // 40.CompareTo(40)
83+
[InlineData(800, 20, 400, 10, 0)] // 40.CompareTo(40)
84+
[InlineData(400, 10, 800, 10, -1)] // 40.CompareTo(80)
85+
[InlineData(800, 10, 400, 10, 1)] // 80.CompareTo(40)
86+
[InlineData(800, 10, 400, 20, 1)] // 80.CompareTo(20)
87+
[InlineData(400, 20, 800, 10, -1)] // 20.CompareTo(80)
88+
public void ComparisonTests_SameTypes(long leftBytes, int leftIntervalSeconds, long rightBytes, int rightIntervalSeconds, int expectedValue)
89+
{
90+
var leftSize = ByteSize.FromBytes(leftBytes);
91+
var leftInterval = TimeSpan.FromSeconds(leftIntervalSeconds);
92+
var leftByteRate = new ByteRate(leftSize, leftInterval);
93+
var rightSize = ByteSize.FromBytes(rightBytes);
94+
var rightInterval = TimeSpan.FromSeconds(rightIntervalSeconds);
95+
var rightByteRate = new ByteRate(rightSize, rightInterval);
96+
97+
Assert.Equal(leftByteRate.CompareTo(rightByteRate), expectedValue);
98+
}
99+
100+
[Theory]
101+
[InlineData(1024, 10, 6, 1, 0)] // 1024.CompareTo(1024)
102+
[InlineData(1024, 10, 12, 2, 0)] // 1024.CompareTo(1024)
103+
[InlineData(2048, 20, 6, 1, 0)] // 1024.CompareTo(1024)
104+
[InlineData(1024, 10, 12, 1, -1)] // 1024.CompareTo(2048)
105+
[InlineData(2048, 10, 6, 1, 1)] // 2048.CompareTo(1024)
106+
[InlineData(2048, 10, 6, 2, 1)] // 2048.CompareTo(512)
107+
[InlineData(1024, 20, 12, 1, -1)] // 512.CompareTo(2048)
108+
public void ComparisonTests_DifferingTypes(long leftKiloBytes, int leftIntervalSeconds, long rightMegaBytes, int rightIntervalMinutes, int expectedValue)
109+
{
110+
var leftSize = ByteSize.FromKilobytes(leftKiloBytes);
111+
var leftInterval = TimeSpan.FromSeconds(leftIntervalSeconds);
112+
var leftByteRate = new ByteRate(leftSize, leftInterval);
113+
var rightSize = ByteSize.FromMegabytes(rightMegaBytes);
114+
var rightInterval = TimeSpan.FromMinutes(rightIntervalMinutes);
115+
var rightByteRate = new ByteRate(rightSize, rightInterval);
116+
117+
Assert.Equal(leftByteRate.CompareTo(rightByteRate), expectedValue);
118+
}
119+
80120
}
81121
}

src/Humanizer/Bytes/ByteRate.cs

+31-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace Humanizer.Bytes
77
/// <summary>
88
/// Class to hold a ByteSize and a measurement interval, for the purpose of calculating the rate of transfer
99
/// </summary>
10-
public class ByteRate
10+
public class ByteRate : IComparable<ByteRate>, IEquatable<ByteRate>, IComparable
1111
{
1212
/// <summary>
1313
/// Quantity of bytes
@@ -76,5 +76,35 @@ public string Humanize(string format, TimeUnit timeUnit = TimeUnit.Second)
7676
return new ByteSize(Size.Bytes / Interval.TotalSeconds * displayInterval.TotalSeconds)
7777
.Humanize(format) + '/' + displayUnit;
7878
}
79+
80+
public override string ToString()
81+
{
82+
return Humanize();
83+
}
84+
85+
public string ToString(string format, TimeUnit timeUnit = TimeUnit.Second)
86+
{
87+
return Humanize(format, timeUnit);
88+
}
89+
90+
public int CompareTo(ByteRate other)
91+
{
92+
var left = Size.Bytes / Interval.TotalSeconds;
93+
var right = other.Size.Bytes / other.Interval.TotalSeconds;
94+
if (left < right) return -1;
95+
return right < left ? 1 : 0;
96+
}
97+
98+
public bool Equals(ByteRate other)
99+
{
100+
return CompareTo(other) == 0;
101+
}
102+
103+
public int CompareTo(Object obj)
104+
{
105+
if (obj == null) return 1;
106+
var cmp = (ByteRate)obj;
107+
return CompareTo(cmp);
108+
}
79109
}
80110
}

0 commit comments

Comments
 (0)