Skip to content

Commit

Permalink
make check for distinct hashcode optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jtattermusch committed Mar 12, 2019
1 parent d305c2a commit 60a889e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -750,7 +750,8 @@ public void NaNValuesComparedBitwise()
var list2 = new RepeatedField<double> { SampleNaNs.Regular, SampleNaNs.PayloadFlipped };
var list3 = new RepeatedField<double> { SampleNaNs.Regular, SampleNaNs.SignallingFlipped };

EqualityTester.AssertInequality(list1, list2);
// All SampleNaNs have the same hashcode under certain targets (e.g. netcoreapp2.1)
EqualityTester.AssertInequality(list1, list2, checkHashcode: false);
EqualityTester.AssertEquality(list1, list3);
Assert.True(list1.Contains(SampleNaNs.SignallingFlipped));
Assert.False(list2.Contains(SampleNaNs.SignallingFlipped));
Expand Down
5 changes: 3 additions & 2 deletions csharp/src/Google.Protobuf.Test/EqualityTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,14 @@ public static void AssertEquality<T>(T first, T second) where T : IEquatable<T>
Assert.AreEqual(first.GetHashCode(), second.GetHashCode());
}

public static void AssertInequality<T>(T first, T second) where T : IEquatable<T>
public static void AssertInequality<T>(T first, T second, bool checkHashcode = true) where T : IEquatable<T>
{
Assert.IsFalse(first.Equals(second));
Assert.IsFalse(first.Equals((object) second));
// While this isn't a requirement, the chances of this test failing due to
// coincidence rather than a bug are very small.
if (first != null && second != null)
// For such rare cases, an argument can be used to disable the check.
if (checkHashcode && first != null && second != null)
{
Assert.AreNotEqual(first.GetHashCode(), second.GetHashCode());
}
Expand Down

0 comments on commit 60a889e

Please sign in to comment.