public void ReferenceEqualityComparerShouldNotUseOverriddenHash() { var t = new OverriddenHash(); var equalityComparer = new ReferenceEqualityComparer(); Assert.AreNotEqual(42, equalityComparer.GetHashCode(t)); Assert.AreEqual(equalityComparer.GetHashCode(t), RuntimeHelpers.GetHashCode(t)); }
public void It_should_return_object_GetHashCode_for_non_null_values() { var instance = new MyClass(Guid.NewGuid().ToString("D", CultureInfo.InvariantCulture)); var instanceHashCode = instance.GetHashCode(); var actual = target.GetHashCode(instance); actual.Should().Be(instanceHashCode); }
public void Equals_SameInstance_ReturnTrue() { // Setup var comparer = new ReferenceEqualityComparer <object>(); var obj = new object(); // Call & Assert Assert.IsTrue(comparer.Equals(obj, obj)); Assert.AreEqual(comparer.GetHashCode(obj), comparer.GetHashCode(obj)); }
public void TwoClassInstances_AreNotEquals() { var rec = new ReferenceEqualityComparer(); var instance = new Entity(); rec.Equals(instance, new Entity()).Should().Be.False(); rec.Equals(instance, instance).Should().Be.True(); rec.GetHashCode().Should().Not.Be.EqualTo(17); rec.GetHashCode().Should().Not.Be.EqualTo((new Entity()).GetHashCode()); }
public void Equals_OtherEqualsInstance_ReturnFalse() { // Setup var comparer = new ReferenceEqualityComparer <object>(); var objectFirst = new TestObject(); var objectSecond = new TestObject(); // Call bool equals = comparer.Equals(objectFirst, objectSecond); // Assert Assert.IsFalse(equals); Assert.IsTrue(objectFirst.Equals(objectSecond)); Assert.AreNotEqual(comparer.GetHashCode(objectFirst), comparer.GetHashCode(objectSecond)); }
public static UnrootedSymbolSet GetUnrootedSymbols(Compilation compilation) { var primaryAssembly = new WeakReference <IAssemblySymbol>(compilation.Assembly); // The dynamic type is also unrooted (i.e. doesn't point back at the compilation or source // assembly). So we have to keep track of it so we can get back from it to a project in case the // underlying compilation is GC'ed. var primaryDynamic = new WeakReference <ITypeSymbol?>( compilation.Language == LanguageNames.CSharp ? compilation.DynamicType : null); // PERF: Preallocate this array so we don't have to resize it as we're adding assembly symbols. using var _ = ArrayBuilder <(int hashcode, WeakReference <ISymbol> symbol)> .GetInstance( compilation.ExternalReferences.Length + compilation.DirectiveReferences.Length, out var secondarySymbols); foreach (var reference in compilation.References) { var symbol = compilation.GetAssemblyOrModuleSymbol(reference); if (symbol == null) { continue; } secondarySymbols.Add((ReferenceEqualityComparer.GetHashCode(symbol), new WeakReference <ISymbol>(symbol))); } // Sort all the secondary symbols by their hash. This will allow us to easily binary search for // them afterwards. Note: it is fine for multiple symbols to have the same reference hash. The // search algorithm will account for that. secondarySymbols.Sort(WeakSymbolComparer.Instance); return(new UnrootedSymbolSet(primaryAssembly, primaryDynamic, secondarySymbols.ToImmutable())); }
public void iEqualityComparer_getHashCode() { IEqualityComparer instance = new ReferenceEqualityComparer <GenericParameterHelper>(); var obj = new GenericParameterHelper(); int expected = obj.GetHashCode(); int actual = instance.GetHashCode(obj); actual.Should().Be.EqualTo(expected); }
public void generic_iEqualityComparer_getHashCode() { IEqualityComparer<GenericParameterHelper> instance = new ReferenceEqualityComparer<GenericParameterHelper>(); var obj = new GenericParameterHelper(); Int32 expected = obj.GetHashCode(); Int32 actual = instance.GetHashCode( obj ); actual.Should().Be.EqualTo( expected ); }
public void NullSafe() { var req = new ReferenceEqualityComparer<string>(); Assert.AreEqual(0, req.GetHashCode(null)); Assert.True(req.Equals(null, null)); Assert.False(req.Equals(null, "abc")); var reqo = new ReferenceEqualityComparer(); Assert.AreEqual(0, reqo.GetHashCode(null)); Assert.True(reqo.Equals(null, null)); Assert.False(reqo.Equals(null, "abc")); }
public void HashCode_Object_ReturnHashCode() { // Setup var comparer = new ReferenceEqualityComparer <object>(); var obj = new object(); // Call int code = comparer.GetHashCode(obj); // Assert Assert.AreEqual(code, obj.GetHashCode()); }
public void HashCode_ObjectHashCodeOverride_ReturnsObjectHashCode() { // Setup var comparer = new ReferenceEqualityComparer <object>(); var obj = new TestObject(); // Call int code = comparer.GetHashCode(obj); // Assert Assert.AreNotEqual(code, obj.GetHashCode()); Assert.AreEqual(code, obj.GetBaseHashCode()); }
public void GetHashCodeIgnoresHashAsIEquatable() { var referenceComparer = new ReferenceEqualityComparer(); var mock = new Mock <IEquatable <object> >(); mock.Setup(obj => obj.GetHashCode()).Returns(0); var objct = mock.Object; var hashCode = referenceComparer.GetHashCode(objct); Assert.AreNotEqual(0, hashCode); }
public void IgnoresEquivalence() { string x = new string(new []{ 'a', 'b', 'c', 'd' }); string y = new string(new []{ 'a', 'b', 'c', 'd' }); if(!Equals(x, y)) Assert.Inconclusive("Compiler has optimised x and y to one object. Change test!"); var req = new ReferenceEqualityComparer<string>(); Assert.False(req.Equals(x, y)); Assert.True(req.Equals(x, x)); var reqo = new ReferenceEqualityComparer(); Assert.False(reqo.Equals(x, y)); Assert.True(reqo.Equals(x, x)); if(req.GetHashCode(x) == req.GetHashCode(y)) Assert.Inconclusive ("Same hashcode could happen rarely, but if more than once in a blue moon, this is a bug"); if(reqo.GetHashCode(x) == req.GetHashCode(y)) Assert.Inconclusive ("Same hashcode could happen rarely, but if more than once in a blue moon, this is a bug"); if(req.GetHashCode(x) == x.GetHashCode()) Assert.Inconclusive ("Same hashcode could happen rarely, but if more than once in a blue moon, this is a bug"); Assert.AreEqual(reqo.GetHashCode(x), req.GetHashCode(x)); }
public void GetHashCode_UsesRuntimeHelpers() { ClassWithBadGetHashCodeImplementation o = new ClassWithBadGetHashCodeImplementation(); // make sure we don't call object.GetHashCode() ReferenceEqualityComparer comparer1 = ReferenceEqualityComparer.Instance; IEqualityComparer comparer2 = ReferenceEqualityComparer.Instance; IEqualityComparer <object> comparer3 = ReferenceEqualityComparer.Instance; IEqualityComparer <ClassWithBadGetHashCodeImplementation> comparer4 = ReferenceEqualityComparer.Instance; // test contravariance int runtimeHelpersHashCode = RuntimeHelpers.GetHashCode(o); Assert.Equal(runtimeHelpersHashCode, comparer1.GetHashCode(o)); Assert.Equal(runtimeHelpersHashCode, comparer2.GetHashCode(o)); Assert.Equal(runtimeHelpersHashCode, comparer3.GetHashCode(o)); Assert.Equal(runtimeHelpersHashCode, comparer4.GetHashCode(o)); }
public void GetHashcode() { Assert.That(_comparer.GetHashCode(_obj1), Is.EqualTo(_comparer.GetHashCode(_obj1))); }
public void generic_iEqualityComparer_getHashCode_null_reference() { IEqualityComparer <GenericParameterHelper> instance = new ReferenceEqualityComparer <GenericParameterHelper>(); int actual = instance.GetHashCode(null); }
int IEqualityComparer <object> .GetHashCode(object a) { return(ReferenceEqualityComparer.GetHashCode(a)); }
public void It_should_return_zero() { var actual = target.GetHashCode(null); actual.Should().Be(0); }
public void generic_iEqualityComparer_getHashCode_null_reference() { IEqualityComparer<GenericParameterHelper> instance = new ReferenceEqualityComparer<GenericParameterHelper>(); Int32 actual = instance.GetHashCode( null ); }
public sealed override int GetHashCode() { return(ReferenceEqualityComparer.GetHashCode(this)); }