Skip to content

Commit ca59ee5

Browse files
committed
Fixed #3 when trying to output 0 bytes as human readable.
- Renamed ToSizeSuffix() to ToSizeWithSuffix()
1 parent 70b899c commit ca59ee5

File tree

4 files changed

+50
-4
lines changed

4 files changed

+50
-4
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
using NUnit.Framework;
2+
3+
namespace Comsec.SqlPrune
4+
{
5+
[TestFixture]
6+
public class Int64ExtensionsTest
7+
{
8+
[Test]
9+
public void TestToSizeWithSuffixWhenZero()
10+
{
11+
const long value = 0;
12+
13+
Assert.AreEqual("0", value.ToSizeWithSuffix());
14+
}
15+
16+
[Test]
17+
public void TestToSizeWithSuffixWhenTwoDigitsLong()
18+
{
19+
const long value = 12;
20+
21+
Assert.AreEqual("12.0 bytes", value.ToSizeWithSuffix());
22+
}
23+
24+
[Test]
25+
public void TestToSizeWithSuffixWhenThreeDigitsLong()
26+
{
27+
const long value = 354;
28+
29+
Assert.AreEqual("354.0 bytes", value.ToSizeWithSuffix());
30+
}
31+
32+
[Test]
33+
public void TestToSizeWithSuffixWhenFourDigitsLong()
34+
{
35+
const long value = 2400;
36+
37+
Assert.AreEqual("2.3 KB", value.ToSizeWithSuffix());
38+
}
39+
}
40+
}

Source/SqlPrune.Test/SqlPrune.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
<Compile Include="AutoMockingTest.cs" />
5858
<Compile Include="BakFilenameExtractorTest.cs" />
5959
<Compile Include="DateTimeExtensions.cs" />
60+
<Compile Include="Int64ExetensionsTest.cs" />
6061
<Compile Include="Models\CalendarModel.cs" />
6162
<Compile Include="Commands\PruneCommandTest.cs" />
6263
<Compile Include="Factories\BakFileListFactory.cs" />

Source/SqlPrune/Commands/PruneCommand.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,12 +237,12 @@ public override int Execute(Options options)
237237
if (totalBytes > 0)
238238
{
239239
ColorConsole.Write(ConsoleColor.DarkGreen, " Kept");
240-
Console.WriteLine(": {0,19:N0} ({1})", totalKept, totalKept.ToSizeSuffix());
240+
Console.WriteLine(": {0,19:N0} ({1})", totalKept, totalKept.ToSizeWithSuffix());
241241

242242
ColorConsole.Write(ConsoleColor.DarkRed, " Pruned");
243-
Console.WriteLine(": {0,19:N0} ({1})", totalPruned, totalPruned.ToSizeSuffix());
243+
Console.WriteLine(": {0,19:N0} ({1})", totalPruned, totalPruned.ToSizeWithSuffix());
244244

245-
Console.WriteLine(" Total: {0,19:N0} ({1})", totalBytes, totalBytes.ToSizeSuffix());
245+
Console.WriteLine(" Total: {0,19:N0} ({1})", totalBytes, totalBytes.ToSizeWithSuffix());
246246
}
247247

248248
return (int) ExitCode.Success;

Source/SqlPrune/Int64Extensions.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,13 @@ public static class Int64Extensions
1717
/// <remarks>
1818
/// JLRishe's implementation from http://stackoverflow.com/questions/14488796/does-net-provide-an-easy-way-convert-bytes-to-kb-mb-gb-etc
1919
/// </remarks>
20-
public static string ToSizeSuffix(this Int64 value)
20+
public static string ToSizeWithSuffix(this Int64 value)
2121
{
22+
if (value == 0)
23+
{
24+
return "0";
25+
}
26+
2227
var mag = (int) Math.Log(value, 1024);
2328

2429
var adjustedSize = (decimal) value/(1L << (mag*10));

0 commit comments

Comments
 (0)