Skip to content

Commit

Permalink
Merge pull request #294 from mexx/bytesize-untyped-comparison
Browse files Browse the repository at this point in the history
provide untyped comparison for ByteSize
  • Loading branch information
MehdiK committed Jun 12, 2014
2 parents 1427e53 + 8489685 commit d91fcd7
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 1 deletion.
1 change: 1 addition & 0 deletions release_notes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
###In Development
- [#294](https://github.com/MehdiK/Humanizer/pull/294): Added support for untyped comparison of ByteSize
- [#277](https://github.com/MehdiK/Humanizer/pull/277): Added support for custom enum description attribute property names
- [#276](https://github.com/Mehdik/Humanizer/pull/276): Added Farsi ToOrdinalWords
- [#281](https://github.com/Mehdik/Humanizer/pull/281): Changed the logic for handling hyphenation and large numbers ending in twelve for English ordinal words; e.g. before "twenty first" now "twenty-first"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
public Humanizer.Bytes.ByteSize AddKilobytes(double value) { }
public Humanizer.Bytes.ByteSize AddMegabytes(double value) { }
public Humanizer.Bytes.ByteSize AddTerabytes(double value) { }
public int CompareTo(object obj) { }
public int CompareTo(Humanizer.Bytes.ByteSize other) { }
public bool Equals(object value) { }
public bool Equals(Humanizer.Bytes.ByteSize value) { }
Expand Down
48 changes: 48 additions & 0 deletions src/Humanizer.Tests/Bytes/ComparingTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Collections.Generic;
using System.Linq;
using Humanizer.Bytes;
using Xunit;
using Xunit.Extensions;

namespace Humanizer.Tests.Bytes
{
public class ComparingTests
{
[Theory]
[InlineData(13, 23, -1)]
[InlineData(23, 23, 0)]
[InlineData(45, 23, 1)]
public void CompareStrongTyped(double value, double valueToCompareWith, int expectedResult)
{
var valueSize = new ByteSize(value);
var otherSize = new ByteSize(valueToCompareWith);
var result = valueSize.CompareTo(otherSize);

Assert.Equal(expectedResult, result);
}

[Theory]
[InlineData(13, 23, -1)]
[InlineData(23, 23, 0)]
[InlineData(45, 23, 1)]
public void CompareUntyped(double value, double valueToCompareWith, int expectedResult)
{
var valueSize = new ByteSize(value);
object otherSize = new ByteSize(valueToCompareWith);
var result = valueSize.CompareTo(otherSize);

Assert.Equal(expectedResult, result);
}

[Theory]
[InlineData(new[] { "1GB", "3KB", "5MB" }, new[] { "3KB", "5MB", "1GB"})]
[InlineData(new[] { "1MB", "3KB", "5MB" }, new[] { "3KB", "1MB", "5MB"})]
public void SortList(IEnumerable<string> values, IEnumerable<string> expected)
{
var list = values.Select(ByteSize.Parse).ToList();
list.Sort();

Assert.Equal(expected.Select(ByteSize.Parse), list);
}
}
}
1 change: 1 addition & 0 deletions src/Humanizer.Tests/Humanizer.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="Bytes\ComparingTests.cs" />
<Compile Include="Bytes\CreatingTests.cs" />
<Compile Include="Bytes\ByteSizeExtensionsTests.cs" />
<Compile Include="Bytes\ParsingTests.cs" />
Expand Down
13 changes: 12 additions & 1 deletion src/Humanizer/Bytes/ByteSize.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ namespace Humanizer.Bytes
/// Represents a byte size value.
/// </summary>
#pragma warning disable 1591
public struct ByteSize : IComparable<ByteSize>, IEquatable<ByteSize>
public struct ByteSize : IComparable<ByteSize>, IEquatable<ByteSize>, IComparable
{
public static readonly ByteSize MinValue = FromBits(long.MinValue);
public static readonly ByteSize MaxValue = FromBits(long.MaxValue);
Expand Down Expand Up @@ -205,6 +205,17 @@ public override int GetHashCode()
return Bits.GetHashCode();
}

public int CompareTo(object obj)
{
if (obj == null)
return 1;

if (!(obj is ByteSize))
throw new ArgumentException("Object is not a ByteSize");

return CompareTo((ByteSize) obj);
}

public int CompareTo(ByteSize other)
{
return Bits.CompareTo(other.Bits);
Expand Down

0 comments on commit d91fcd7

Please sign in to comment.