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

Added support for Petabytes #7

Merged
merged 1 commit into from
Oct 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/ByteSize.Tests/ArithmeticMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,20 @@ public void AddTeraBytesMethod()
Assert.Equal(4d, size.TeraBytes);
}

[Fact]
public void AddPetaBytesMethod()
{
var size = ByteSize.FromPetaBytes(2).AddPetaBytes(2);

Assert.Equal(4d * 1024 * 1024 * 1024 * 1024 * 1024 * 8, size.Bits);
Assert.Equal(4d * 1024 * 1024 * 1024 * 1024 * 1024, size.Bytes);
Assert.Equal(4d * 1024 * 1024 * 1024 * 1024, size.KiloBytes);
Assert.Equal(4d * 1024 * 1024 * 1024, size.MegaBytes);
Assert.Equal(4d * 1024 * 1024, size.GigaBytes);
Assert.Equal(4d * 1024, size.TeraBytes);
Assert.Equal(4d, size.PetaBytes);
}

[Fact]
public void SubtractMethod()
{
Expand Down
29 changes: 22 additions & 7 deletions src/ByteSize.Tests/CreatingMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,19 @@ public class CreatingMethods
public void Constructor()
{
// Arrange
double byteSize = 1099511627776;
double byteSize = 1125899906842624;

// Act
var result = new ByteSize(byteSize);

// Assert
Assert.Equal(8.796093022208e12, result.Bits);
Assert.Equal(1099511627776, result.Bytes);
Assert.Equal(1073741824, result.KiloBytes);
Assert.Equal(1048576, result.MegaBytes);
Assert.Equal(1024, result.GigaBytes);
Assert.Equal(1, result.TeraBytes);
Assert.Equal(byteSize * 8, result.Bits);
Assert.Equal(byteSize, result.Bytes);
Assert.Equal(byteSize / 1024, result.KiloBytes);
Assert.Equal(byteSize / 1024 / 1024, result.MegaBytes);
Assert.Equal(byteSize / 1024 / 1024 / 1024, result.GigaBytes);
Assert.Equal(byteSize / 1024 / 1024 / 1024 / 1024, result.TeraBytes);
Assert.Equal(1, result.PetaBytes);
}

[Fact]
Expand Down Expand Up @@ -110,5 +111,19 @@ public void FromTeraBytesMethod()
Assert.Equal(1649267441664, result.Bytes);
Assert.Equal(1.5, result.TeraBytes);
}

[Fact]
public void FromPetaBytesMethod()
{
// Arrange
double value = 1.5;

// Act
var result = ByteSize.FromPetaBytes(value);

// Assert
Assert.Equal(1688849860263936, result.Bytes);
Assert.Equal(1.5, result.PetaBytes);
}
}
}
11 changes: 11 additions & 0 deletions src/ByteSize.Tests/ParsingMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,16 @@ public void ParseTB()

Assert.Equal(expected, result);
}

[Fact]
public void ParsePB()
{
string val = "100PB";
var expected = ByteSize.FromPetaBytes(100);

var result = ByteSize.Parse(val);

Assert.Equal(expected, result);
}
}
}
13 changes: 13 additions & 0 deletions src/ByteSize.Tests/ToStringMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,19 @@ public void ReturnsTeraBytes()
Assert.Equal("10 TB", result);
}

[Fact]
public void ReturnsPetaBytes()
{
// Arrange
var b = ByteSize.FromPetaBytes(10);

// Act
var result = b.ToString("##.#### PB");

// Assert
Assert.Equal("10 PB", result);
}

[Fact]
public void ReturnsSelectedFormat()
{
Expand Down
28 changes: 28 additions & 0 deletions src/ByteSize/ByteSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,32 @@ public struct ByteSize : IComparable<ByteSize>, IEquatable<ByteSize>
public const long BytesInMegaByte = 1048576;
public const long BytesInGigaByte = 1073741824;
public const long BytesInTeraByte = 1099511627776;
public const long BytesInPetaByte = 1125899906842624;

public const string BitSymbol = "b";
public const string ByteSymbol = "B";
public const string KiloByteSymbol = "KB";
public const string MegaByteSymbol = "MB";
public const string GigaByteSymbol = "GB";
public const string TeraByteSymbol = "TB";
public const string PetaByteSymbol = "PB";

public long Bits { get; private set; }
public double Bytes { get; private set; }
public double KiloBytes { get; private set; }
public double MegaBytes { get; private set; }
public double GigaBytes { get; private set; }
public double TeraBytes { get; private set; }
public double PetaBytes { get; private set; }

public string LargestWholeNumberSymbol
{
get
{
// Absolute value is used to deal with negative values
if (Math.Abs(this.PetaBytes) >= 1)
return ByteSize.PetaByteSymbol;

if (Math.Abs(this.TeraBytes) >= 1)
return ByteSize.TeraByteSymbol;

Expand All @@ -59,6 +65,9 @@ public double LargestWholeNumberValue
get
{
// Absolute value is used to deal with negative values
if (Math.Abs(this.PetaBytes) >= 1)
return this.PetaBytes;

if (Math.Abs(this.TeraBytes) >= 1)
return this.TeraBytes;

Expand Down Expand Up @@ -89,6 +98,7 @@ public ByteSize(double byteSize)
MegaBytes = byteSize / BytesInMegaByte;
GigaBytes = byteSize / BytesInGigaByte;
TeraBytes = byteSize / BytesInTeraByte;
PetaBytes = byteSize / BytesInPetaByte;
}

public static ByteSize FromBits(long value)
Expand Down Expand Up @@ -121,6 +131,11 @@ public static ByteSize FromTeraBytes(double value)
return new ByteSize(value * BytesInTeraByte);
}

public static ByteSize FromPetaBytes(double value)
{
return new ByteSize(value * BytesInPetaByte);
}

/// <summary>
/// Converts the value of the current ByteSize object to a string.
/// The metric prefix symbol (bit, byte, kilo, mega, giga, tera) used is
Expand All @@ -140,6 +155,8 @@ public string ToString(string format)
Func<string, bool> has = s => format.IndexOf(s, StringComparison.CurrentCultureIgnoreCase) != -1;
Func<double, string> output = n => n.ToString(format, CultureInfo.InvariantCulture);

if (has("PB"))
return output(this.PetaBytes);
if (has("TB"))
return output(this.TeraBytes);
if (has("GB"))
Expand Down Expand Up @@ -223,6 +240,11 @@ public ByteSize AddTeraBytes(double value)
return this + ByteSize.FromTeraBytes(value);
}

public ByteSize AddPetaBytes(double value)
{
return this + ByteSize.FromPetaBytes(value);
}

public ByteSize Subtract(ByteSize bs)
{
return new ByteSize(this.Bytes - bs.Bytes);
Expand Down Expand Up @@ -352,6 +374,12 @@ public static bool TryParse(string s, out ByteSize result)
case "tb":
result = FromTeraBytes(number);
break;

case "PB":
case "pB":
case "pb":
result = FromPetaBytes(number);
break;
}

return true;
Expand Down