Commit 60a889e0 authored by Jan Tattermusch's avatar Jan Tattermusch

make check for distinct hashcode optional

parent d305c2ac
...@@ -750,7 +750,8 @@ namespace Google.Protobuf.Collections ...@@ -750,7 +750,8 @@ namespace Google.Protobuf.Collections
var list2 = new RepeatedField<double> { SampleNaNs.Regular, SampleNaNs.PayloadFlipped }; var list2 = new RepeatedField<double> { SampleNaNs.Regular, SampleNaNs.PayloadFlipped };
var list3 = new RepeatedField<double> { SampleNaNs.Regular, SampleNaNs.SignallingFlipped }; 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); EqualityTester.AssertEquality(list1, list3);
Assert.True(list1.Contains(SampleNaNs.SignallingFlipped)); Assert.True(list1.Contains(SampleNaNs.SignallingFlipped));
Assert.False(list2.Contains(SampleNaNs.SignallingFlipped)); Assert.False(list2.Contains(SampleNaNs.SignallingFlipped));
......
...@@ -49,13 +49,14 @@ namespace Google.Protobuf ...@@ -49,13 +49,14 @@ namespace Google.Protobuf
Assert.AreEqual(first.GetHashCode(), second.GetHashCode()); 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(second));
Assert.IsFalse(first.Equals((object) second)); Assert.IsFalse(first.Equals((object) second));
// While this isn't a requirement, the chances of this test failing due to // While this isn't a requirement, the chances of this test failing due to
// coincidence rather than a bug are very small. // 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()); Assert.AreNotEqual(first.GetHashCode(), second.GetHashCode());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment