Skip to content

Commit

Permalink
Merge pull request #942 from longfin/feature/currency-equal
Browse files Browse the repository at this point in the history
Implement .Equals for Currency
  • Loading branch information
longfin authored Jul 31, 2020
2 parents d2a1f60 + 9cb6e3c commit 8d81300
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 1 deletion.
14 changes: 14 additions & 0 deletions Libplanet.Tests/CurrencyTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,5 +125,19 @@ public void Serializable()
Assert.Equal(currency.Hash, deserialized.Hash);
}
}

[Fact]
public void Equal()
{
var currencyA = new Currency(ticker: "GOLD", minter: AddressA);
var currencyB = new Currency(ticker: "GOLD", minter: AddressA);
var currencyC = new Currency(ticker: "GOLD", minter: AddressB);
var currencyD = new Currency(ticker: "SILVER", minter: AddressA);

Assert.Equal(currencyA, currencyA);
Assert.Equal(currencyA, currencyB);
Assert.NotEqual(currencyA, currencyC);
Assert.NotEqual(currencyA, currencyD);
}
}
}
25 changes: 24 additions & 1 deletion Libplanet/Currency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Libplanet
/// EUR (Euro), <em>not values</em> like $100 or €100.
/// </summary>
[Serializable]
public readonly struct Currency : ISerializable
public readonly struct Currency : ISerializable, IEquatable<Currency>
{
/// <summary>
/// The ticker symbol, e.g., <c>&quot;USD&quot;</c>.
Expand Down Expand Up @@ -124,6 +124,29 @@ public void GetObjectData(SerializationInfo info, StreamingContext context)
public override string ToString() =>
$"{Ticker} ({Hash})";

[Pure]
public override int GetHashCode()
{
unchecked
{
return -1545866855 + Hash.GetHashCode();
}
}

[Pure]
public override bool Equals(object? obj)
{
return obj is IEquatable<Currency> other
? other.Equals(this)
: false;
}

[Pure]
public bool Equals(Currency other)
{
return Hash.Equals(other.Hash);
}

[Pure]
private HashDigest<SHA1> GetHash()
{
Expand Down

0 comments on commit 8d81300

Please sign in to comment.