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

Fix ByteSize implementation in regards to IEC specification #1413

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
66 changes: 56 additions & 10 deletions src/Humanizer.Tests.Shared/Bytes/ArithmeticTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,45 +35,91 @@ public void AddKilobytes()
{
var size = ByteSize.FromKilobytes(2).AddKilobytes(2);

Assert.Equal(4 * 1000 * 8, size.Bits);
Assert.Equal(4 * 1000, size.Bytes);
Assert.Equal(4, size.Kilobytes);
}

[Fact]
public void AddKibibytes()
{
var size = ByteSize.FromKibibytes(2).AddKibibytes(2);

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

[Fact]
public void AddMegabytes()
{
var size = ByteSize.FromMegabytes(2).AddMegabytes(2);

Assert.Equal(4 * 1000 * 1000 * 8, size.Bits);
Assert.Equal(4 * 1000 * 1000, size.Bytes);
Assert.Equal(4 * 1000, size.Kilobytes);
Assert.Equal(4, size.Megabytes);
}

[Fact]
public void AddMebibytes()
{
var size = ByteSize.FromMebibytes(2).AddMebibytes(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);
Assert.Equal(4 * 1024, size.Kibibytes);
Assert.Equal(4, size.Mebibytes);
}

[Fact]
public void AddGigabytes()
{
var size = ByteSize.FromGigabytes(2).AddGigabytes(2);

Assert.Equal(4d * 1000 * 1000 * 1000 * 8, size.Bits);
Assert.Equal(4d * 1000 * 1000 * 1000, size.Bytes);
Assert.Equal(4d * 1000 * 1000, size.Kilobytes);
Assert.Equal(4d * 1000, size.Megabytes);
Assert.Equal(4d, size.Gigabytes);
}

[Fact]
public void AddGibibytes()
{
var size = ByteSize.FromGibibytes(2).AddGibibytes(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);
Assert.Equal(4d * 1024 * 1024, size.Kibibytes);
Assert.Equal(4d * 1024, size.Mebibytes);
Assert.Equal(4d, size.Gibibytes);
}

[Fact]
public void AddTerabytes()
{
var size = ByteSize.FromTerabytes(2).AddTerabytes(2);

Assert.Equal(4d * 1000 * 1000 * 1000 * 1000 * 8, size.Bits);
Assert.Equal(4d * 1000 * 1000 * 1000 * 1000, size.Bytes);
Assert.Equal(4d * 1000 * 1000 * 1000, size.Kilobytes);
Assert.Equal(4d * 1000 * 1000, size.Megabytes);
Assert.Equal(4d * 1000, size.Gigabytes);
Assert.Equal(4d, size.Terabytes);
}

[Fact]
public void AddTebibytes()
{
var size = ByteSize.FromTebibytes(2).AddTebibytes(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);
Assert.Equal(4d * 1024 * 1024 * 1024, size.Kibibytes);
Assert.Equal(4d * 1024 * 1024, size.Mebibytes);
Assert.Equal(4d * 1024, size.Gibibytes);
Assert.Equal(4d, size.Tebibytes);
}

[Fact]
Expand Down
21 changes: 11 additions & 10 deletions src/Humanizer.Tests.Shared/Bytes/ByteRateTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ public class ByteRateTests
{
[Theory]
[InlineData(400, 1, "400 B/s")]
[InlineData(4 * 1024, 1, "4 KB/s")]
[InlineData(4 * 1024 * 1024, 1, "4 MB/s")]
[InlineData(4 * 2 * 1024 * 1024, 2, "4 MB/s")]
[InlineData(4 * 1024, 0.1, "40 KB/s")]
[InlineData(15 * 60 * 1024 * 1024, 60, "15 MB/s")]
[InlineData(4 * 1000, 1, "4 KB/s")]
[InlineData(4 * 1000 * 1000, 1, "4 MB/s")]
[InlineData(4 * 2 * 1000 * 1000, 2, "4 MB/s")]
[InlineData(4 * 1000, 0.1, "40 KB/s")]
[InlineData(15 * 60 * 1000 * 1000, 60, "15 MB/s")]
public void HumanizesRates(long inputBytes, double perSeconds, string expectedValue)
{
var size = new ByteSize(inputBytes);
Expand All @@ -27,9 +27,9 @@ public void HumanizesRates(long inputBytes, double perSeconds, string expectedVa
[InlineData(10, 1, TimeUnit.Second, "10 MB/s")]
[InlineData(10, 60, TimeUnit.Minute, "10 MB/min")]
[InlineData(10, 60 * 60, TimeUnit.Hour, "10 MB/h")]
[InlineData(1, 10 * 1, TimeUnit.Second, "102.4 KB/s")]
[InlineData(1, 10 * 60, TimeUnit.Minute, "102.4 KB/min")]
[InlineData(1, 10 * 60 * 60, TimeUnit.Hour, "102.4 KB/h")]
[InlineData(1, 10 * 1, TimeUnit.Second, "100 KB/s")]
[InlineData(1, 10 * 60, TimeUnit.Minute, "100 KB/min")]
[InlineData(1, 10 * 60 * 60, TimeUnit.Hour, "100 KB/h")]
public void TimeUnitTests(long megabytes, double measurementIntervalSeconds, TimeUnit displayInterval, string expectedValue)
{
var size = ByteSize.FromMegabytes(megabytes);
Expand All @@ -42,8 +42,9 @@ public void TimeUnitTests(long megabytes, double measurementIntervalSeconds, Tim
}

[Theory]
[InlineData(19854651984, 1, TimeUnit.Second, null, "18.49 GB/s")]
[InlineData(19854651984, 1, TimeUnit.Second, "#.##", "18.49 GB/s")]
[InlineData(19854651984, 1, TimeUnit.Second, null, "19.85 GB/s")]
[InlineData(19854651984, 1, TimeUnit.Second, "#.##", "19.85 GB/s")]
[InlineData(19854651984, 1, TimeUnit.Second, "#.## GiB", "18.49 GiB/s")]
public void FormattedTimeUnitTests(long bytes, int measurementIntervalSeconds, TimeUnit displayInterval, string format, string expectedValue)
{
var size = ByteSize.FromBytes(bytes);
Expand Down
Loading