Skip to content
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
52 changes: 52 additions & 0 deletions LibGit2Sharp.Tests/EqualityFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
using LibGit2Sharp.Tests.TestHelpers;
using Xunit;

namespace LibGit2Sharp.Tests
{
public class EqualityFixture : BaseFixture
{
[Fact]
public void EqualityHelperCanTestNullInEquals()
{
var one = new ObjectWithEquality();
var two = new ObjectWithEquality();
var three = new ObjectWithEquality(ObjectId.Zero);
var four = new ObjectWithEquality(ObjectId.Zero);

Assert.True(one.Equals(one));
Assert.True(two.Equals(two));
Assert.True(three.Equals(four));
Assert.True(four.Equals(three));
Assert.False(one.Equals(three));
Assert.False(three.Equals(one));
}

[Fact]
public void EqualityHelperCanTestNullInHashCode()
{
var one = new ObjectWithEquality();
var two = new ObjectWithEquality();
var three = new ObjectWithEquality(ObjectId.Zero);
var four = new ObjectWithEquality(ObjectId.Zero);

Assert.Equal(one.GetHashCode(), two.GetHashCode());
Assert.Equal(three.GetHashCode(), four.GetHashCode());
Assert.NotEqual(one.GetHashCode(), three.GetHashCode());
}

private class ObjectWithEquality : GitObject
{
private readonly ObjectId id;

public ObjectWithEquality(ObjectId id = null)
{
this.id = id;
}

public override ObjectId Id
{
get { return id; }
}
}
}
}
1 change: 1 addition & 0 deletions LibGit2Sharp.Tests/LibGit2Sharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="CheckoutFixture.cs" />
<Compile Include="EqualityFixture.cs" />
<Compile Include="SignatureFixture.cs" />
<Compile Include="FilterBranchFixture.cs" />
<Compile Include="RemoveFixture.cs" />
Expand Down
3 changes: 2 additions & 1 deletion LibGit2Sharp/Core/LambdaEqualityHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public int GetHashCode(T instance)
{
foreach (Func<T, object> accessor in equalityContributorAccessors)
{
hashCode = (hashCode*397) ^ accessor(instance).GetHashCode();
object item = accessor(instance);
hashCode = (hashCode*397) ^ (item != null ? item.GetHashCode() : 0);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rather than using 0 (which would level down the previous computation steps to 1), how about using a bit larger number (eg. The Java 31 or whatever...)

}
}

Expand Down