Skip to content

Commit 14f9307

Browse files
author
AndrewMorgan1
committed
extended HumanBytesExtensionsTests further for increased code coverage
1 parent d8d1bb5 commit 14f9307

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

src/ByteFlow/ByteFlow/HumanBytesExtensions.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ public static string ToHumanBytes(this long bytes, int decimalPlaces = 2)
2525
return $"0 {SizeSuffixes[0]}";
2626

2727
int mag = (int)Math.Log(bytes, 1024);
28+
if (mag >= SizeSuffixes.Length)
29+
{
30+
mag = SizeSuffixes.Length - 1; // clamp to PB
31+
}
32+
2833
double adjustedSize = bytes / Math.Pow(1024, mag);
2934

3035
return string.Format(CultureInfo.InvariantCulture,

tests/ByteFlow.Tests/HumanBytesExtensionsTests.cs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,5 +81,50 @@ public void TryParseHumanBytes_InvalidInputs_ShouldReturnFalse(string input)
8181
Assert.False(success);
8282
Assert.Equal(0, result);
8383
}
84+
85+
[Fact]
86+
public void ToHumanBytes_ShouldThrowOnNegativeInput()
87+
{
88+
Assert.Throws<ArgumentOutOfRangeException>(() => (-1L).ToHumanBytes());
89+
}
90+
91+
[Fact]
92+
public void ToHumanBytes_ShouldHandleZero()
93+
{
94+
string result = 0L.ToHumanBytes();
95+
Assert.Equal("0 B", result);
96+
}
97+
98+
[Fact]
99+
public void ToBytes_Whitespace_ShouldThrow()
100+
{
101+
Assert.Throws<ArgumentNullException>(() => " ".ToBytes());
102+
}
103+
104+
[Fact]
105+
public void ToBytes_ShouldSupportPetabytes()
106+
{
107+
string input = "1 PB";
108+
long result = input.ToBytes();
109+
110+
// 1 PB = 1024^5 bytes
111+
Assert.Equal((long)Math.Pow(1024, 5), result);
112+
}
113+
114+
[Fact]
115+
public void ToHumanBytes_ShouldHandleLongMaxValue()
116+
{
117+
string result = long.MaxValue.ToHumanBytes();
118+
Assert.Contains("PB", result); // should be expressed in petabytes
119+
}
120+
121+
[Fact]
122+
public void ToBytes_ShouldHandleVeryLargePetabytes()
123+
{
124+
string input = "8192 PB";
125+
long result = input.ToBytes();
126+
double expected = 8192 * Math.Pow(1024, 5);
127+
Assert.Equal((long)expected, result);
128+
}
84129
}
85130
}

0 commit comments

Comments
 (0)