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

Add reference check to HashAddress.Equals #64

Closed
wants to merge 1 commit into from
Closed
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
22 changes: 19 additions & 3 deletions HydraScript.Lib/BackEnd/Addresses/HashAddress.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,36 @@ namespace HydraScript.Lib.BackEnd.Addresses;
public class HashAddress : IAddress
{
private readonly int _seed;

public IAddress Next { get; set; }

public HashAddress(int seed) =>
_seed = seed;

public bool Equals(IAddress other)
{
if (other is HashAddress hashed)
return _seed == hashed._seed;
if (other is HashAddress _)
return Equals(other);

return false;
}

public override bool Equals(object obj)
{
if (!ReferenceEquals(this, obj))
return false;

if (obj.GetType() != GetType())
return false;

return Equals((HashAddress)obj);
}

protected bool Equals(HashAddress other)
{
return _seed == other._seed;
}

public override int GetHashCode()
{
var i1 = _seed ^ 17;
Expand Down
35 changes: 35 additions & 0 deletions HydraScript.Tests/Unit/BackEnd/HashAddressTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using HydraScript.Lib.BackEnd.Addresses;
using Xunit;

namespace HydraScript.Tests.Unit.BackEnd;

public class HashAddressTests
{
[Fact]
public void EqualsReturnsFalseForTwoDifferentObjectsWithSameSeed()
{
const int seed = 1;

var addressOne = new HashAddress(seed);
var addressTwo = new HashAddress(seed);

Assert.NotEqual(addressOne, addressTwo);
}

[Fact]
public void EqualsReturnsTrueForTwoSameOjectsWithSameSeed()
{
var address = new HashAddress(1);

Assert.Equal(address, address);
}

[Fact]
public void EqualsReturnsFalseForTwoObjectsWithDifferentSeed()
{
var addressOne = new HashAddress(0);
var addressTwo = new HashAddress(1);

Assert.NotEqual(addressOne, addressTwo);
}
}
Loading