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

Fixed arithmetic operations and ToString() for non-english systems #5

Merged
merged 2 commits into from
Sep 27, 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
128 changes: 128 additions & 0 deletions src/ByteSize.Tests/ArithmeticMethods.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace ByteSize.Tests
{
public class ArithmeticMethods
{
[Fact]
public void AddMethod()
{
var size1 = ByteSize.FromBytes(1);
var result = size1.Add(size1);

Assert.Equal(2, result.Bytes);
Assert.Equal(16, result.Bits);
}

[Fact]
public void AddBitsMethod()
{
var size = ByteSize.FromBytes(1).AddBits(8);

Assert.Equal(2, size.Bytes);
Assert.Equal(16, size.Bits);
}

[Fact]
public void AddBytesMethod()
{
var size = ByteSize.FromBytes(1).AddBytes(1);

Assert.Equal(2, size.Bytes);
Assert.Equal(16, size.Bits);
}

[Fact]
public void AddKiloBytesMethod()
{
var size = ByteSize.FromKiloBytes(2).AddKiloBytes(2);

Assert.Equal(4 * 1024 * 8, size.Bits);
Assert.Equal(4 * 1024, size.Bytes);
Assert.Equal(4, size.KiloBytes);
}

[Fact]
public void AddMegaBytesMethod()
{
var size = ByteSize.FromMegaBytes(2).AddMegaBytes(2);

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

[Fact]
public void AddGigaBytesMethod()
{
var size = ByteSize.FromGigaBytes(2).AddGigaBytes(2);

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

[Fact]
public void AddTeraBytesMethod()
{
var size = ByteSize.FromTeraBytes(2).AddTeraBytes(2);

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

[Fact]
public void SubtractMethod()
{
var size = ByteSize.FromBytes(4).Subtract(ByteSize.FromBytes(2));

Assert.Equal(16, size.Bits);
Assert.Equal(2, size.Bytes);
}


[Fact]
public void IncrementOperator()
{
var size = ByteSize.FromBytes(2);
size++;

Assert.Equal(24, size.Bits);
Assert.Equal(3, size.Bytes);
}


[Fact]
public void MinusOperator()
{
var size = ByteSize.FromBytes(2);

size = -size;

Assert.Equal(-16, size.Bits);
Assert.Equal(-2, size.Bytes);
}

[Fact]
public void DecrementOperator()
{
var size = ByteSize.FromBytes(2);
size--;

Assert.Equal(8, size.Bits);
Assert.Equal(1, size.Bytes);
}
}
}
1 change: 1 addition & 0 deletions src/ByteSize.Tests/ByteSize.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="ArithmeticMethods.cs" />
<Compile Include="CreatingMethods.cs" />
<Compile Include="ParsingMethods.cs" />
<Compile Include="ToStringMethod.cs" />
Expand Down
23 changes: 12 additions & 11 deletions src/ByteSize/ByteSize.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Globalization;

namespace ByteSize
{
Expand Down Expand Up @@ -128,7 +129,7 @@ public static ByteSize FromTeraBytes(double value)
/// </summary>
public override string ToString()
{
return string.Format("{0} {1}", this.LargestWholeNumberValue, this.LargestWholeNumberSymbol);
return string.Format(CultureInfo.InvariantCulture, "{0} {1}", this.LargestWholeNumberValue, this.LargestWholeNumberSymbol);
}

public string ToString(string format)
Expand All @@ -137,7 +138,7 @@ public string ToString(string format)
format = "#.## " + format;

Func<string, bool> has = s => format.IndexOf(s, StringComparison.CurrentCultureIgnoreCase) != -1;
Func<double, string> output = n => n.ToString(format);
Func<double, string> output = n => n.ToString(format, CultureInfo.InvariantCulture);

if (has("TB"))
return output(this.TeraBytes);
Expand All @@ -155,7 +156,7 @@ public string ToString(string format)
if (format.IndexOf(ByteSize.BitSymbol) != -1)
return output(this.Bits);

return string.Format("{0} {1}", this.LargestWholeNumberValue.ToString(format), this.LargestWholeNumberSymbol);
return string.Format("{0} {1}", this.LargestWholeNumberValue.ToString(format, CultureInfo.InvariantCulture), this.LargestWholeNumberSymbol);
}

public override bool Equals(object value)
Expand Down Expand Up @@ -189,12 +190,12 @@ public int CompareTo(ByteSize other)

public ByteSize Add(ByteSize bs)
{
return new ByteSize(this.Bits + bs.Bits);
return new ByteSize(this.Bytes + bs.Bytes);
}

public ByteSize AddBits(long value)
{
return new ByteSize(this.Bits + value);
return this + FromBits(value);
}

public ByteSize AddBytes(double value)
Expand Down Expand Up @@ -224,27 +225,27 @@ public ByteSize AddTeraBytes(double value)

public ByteSize Subtract(ByteSize bs)
{
return new ByteSize(this.Bits - bs.Bits);
return new ByteSize(this.Bytes - bs.Bytes);
}

public static ByteSize operator +(ByteSize b1, ByteSize b2)
{
return new ByteSize(b1.Bits + b2.Bits);
return new ByteSize(b1.Bytes + b2.Bytes);
}

public static ByteSize operator ++(ByteSize b)
{
return new ByteSize(b.Bits++);
return new ByteSize(b.Bytes + 1);
}

public static ByteSize operator -(ByteSize b)
{
return new ByteSize(-b.Bits);
return new ByteSize(-b.Bytes);
}

public static ByteSize operator --(ByteSize b)
{
return new ByteSize(b.Bits--);
return new ByteSize(b.Bytes - 1);
}

public static bool operator ==(ByteSize b1, ByteSize b2)
Expand Down Expand Up @@ -311,7 +312,7 @@ public static bool TryParse(string s, out ByteSize result)

// Get the numeric part
double number;
if (!double.TryParse(numberPart, out number))
if (!double.TryParse(numberPart, NumberStyles.Float | NumberStyles.AllowThousands, NumberFormatInfo.InvariantInfo, out number))
return false;

// Get the magnitude part
Expand Down