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

WIK Fixed Bits vs Bytes issue #362

Merged
merged 1 commit into from
Dec 15, 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
133 changes: 133 additions & 0 deletions src/Humanizer.Tests/Bytes/ArithmeticTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
using Humanizer.Bytes;
using Xunit;

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

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

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

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

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

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

[Fact]
public void AddKilobytes()
{
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 AddMegabytes()
{
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 AddGigabytes()
{
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 AddTerabytes()
{
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 Subtract()
{
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);
}

[Fact]
public void PlusOperator()
{
var size1 = ByteSize.FromBytes(1);
var size2 = ByteSize.FromBytes(1);

var result = size1 + size2;

Assert.Equal(2, result.Bytes);
}
}
}
1 change: 1 addition & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Bytes\ArithmeticTests.cs" />
<Compile Include="Bytes\ByteRateTests.cs" />
<Compile Include="Bytes\ComparingTests.cs" />
<Compile Include="Bytes\CreatingTests.cs" />
Expand Down
14 changes: 7 additions & 7 deletions src/Humanizer/Bytes/ByteSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -223,12 +223,12 @@ public int CompareTo(ByteSize other)

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

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

public ByteSize AddBytes(double value)
Expand Down Expand Up @@ -258,27 +258,27 @@ public ByteSize AddTerabytes(double value)

public ByteSize Subtract(ByteSize bs)
{
return new ByteSize(Bits - bs.Bits);
return new ByteSize(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