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 generic_iEqualityComparer_equals_with_null_and_null() { var instance = new ReferenceEqualityComparer <GenericParameterHelper>(); bool actual = instance.Equals(null, null); actual.Should().Be.True(); }
public void generic_iEqualityComparer_equals_with_null_and_null() { var instance = new ReferenceEqualityComparer<GenericParameterHelper>(); bool actual = instance.Equals( null, null ); actual.Should().Be.True(); }
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 ReferenceEqualityComparerClassEqualsMethodReturnsTrueWhenTheSameTimeOfDayObjectIsPassedToBothParameters() { TimeOfDay testParam = _rnd.NextTimeOfDay(); ReferenceEqualityComparer testObject = ReferenceEqualityComparer.Default; bool testOutput = testObject.Equals(testParam, testParam); Assert.IsTrue(testOutput); }
internal static bool IsItemContainedInAllSourceItems <TItemSource>( IViewModel selectionVM, TItemSource sourceItem ) { IEnumerable <TItemSource> allSourceItems = GetAllSourceItems <TItemSource>(selectionVM); return(allSourceItems.Contains(sourceItem, ReferenceEqualityComparer <TItemSource> .CreateSmartComparer())); }
public void iEqualityComparer_equals_with_null_and_instance() { IEqualityComparer instance = new ReferenceEqualityComparer <GenericParameterHelper>(); var y = new GenericParameterHelper(); bool actual = instance.Equals(null, y); actual.Should().Be.False(); }
public NameFixupGraph() { ReferenceEqualityComparer <object> singleton = ReferenceEqualityComparer <object> .Singleton; this._dependenciesByChildObject = new Dictionary <object, NameFixupToken>(singleton); this._dependenciesByName = new Dictionary <string, FrugalObjectList <NameFixupToken> >(StringComparer.Ordinal); this._dependenciesByParentObject = new Dictionary <object, FrugalObjectList <NameFixupToken> >(singleton); this._resolvedTokensPendingProcessing = new Queue <NameFixupToken>(); this._uninitializedObjectsAtParseEnd = new System.Xaml.Context.HashSet <object>(singleton); }
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 InstanceProperty_ReturnsSingleton() { ReferenceEqualityComparer comparer1 = ReferenceEqualityComparer.Instance; Assert.NotNull(comparer1); ReferenceEqualityComparer comparer2 = ReferenceEqualityComparer.Instance; Assert.Same(comparer1, comparer2); }
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 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 iEqualityComparer_equals_with_equal_instances() { IEqualityComparer instance = new ReferenceEqualityComparer <GenericParameterHelper>(); var x = new GenericParameterHelper(); var y = x; bool actual = instance.Equals(x, y); actual.Should().Be.True(); }
public void generic_iEqualityComparer_equals_with_equal_instances() { var instance = new ReferenceEqualityComparer<GenericParameterHelper>(); var x = new GenericParameterHelper(); var y = x; bool actual = instance.Equals( x, y ); actual.Should().Be.True(); }
public void AnObjectIsEqualToItself() { var referenceComparer = new ReferenceEqualityComparer(); var obj1 = new object(); var obj2 = obj1; var consideredEqual = referenceComparer.Equals(obj1, obj2); Assert.IsTrue(consideredEqual); }
public WrapperCollectionAccessorBehavior( bool shouldCacheSourceCollection, IEqualityComparer <TItemSource> reusabilitySourceComparer = null ) { _shouldCacheSourceCollection = shouldCacheSourceCollection; _reusabilitySourceComparer = reusabilitySourceComparer ?? ReferenceEqualityComparer <TItemSource> .CreateSmartComparer(); }
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 static void Equals___Should_return_true___When_both_objects_are_the_same_reference() { // Arrange var item = Some.Dummies <byte>().ToArray(); var systemUnderTest = new ReferenceEqualityComparer <byte[]>(); // Act var actual = systemUnderTest.Equals(item, item); // Assert actual.Should().BeTrue(); }
/// <summary> /// Constructs a caching reference resolver. /// </summary> /// <param name="maxAge">The time a reference should be cached.</param> /// <param name="maxCapacity">The maximum number of references to cache. If the limit is reached, the oldest references are evicted.</param> /// <param name="equalityComparer">An equality comparer to be used to determine equality of referenced objects.</param> public CachingReferenceResolver(TimeSpan maxAge, int maxCapacity, IEqualityComparer <object> equalityComparer) { if (equalityComparer == null) { equalityComparer = new ReferenceEqualityComparer(); } this.references = new Dictionary <object, string>(equalityComparer); this.evictionQueue = new Queue <CacheItem>(); this.maxCapacity = maxCapacity; this.maxAge = maxAge; }
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 ReferenceEqualityComparerClassEqualsMethodReturnsFalseWhenTwoDifferentButEqualTimeOfDayObjectsArePassedAsParameters() { TimeOfDay testParam0 = _rnd.NextTimeOfDay(); TimeOfDay testParam1 = new TimeOfDay(testParam0.AbsoluteSeconds); ReferenceEqualityComparer testObject = ReferenceEqualityComparer.Default; bool testOutput = testObject.Equals(testParam0, testParam1); Assert.IsFalse(testOutput); // Just to double-check that the parameters do compare equal! Assert.AreEqual(testParam0, testParam1); }
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 static void Equals___Should_return_true___When_both_objects_are_null() { // Arrange object item1 = null; object item2 = null; var systemUnderTest = new ReferenceEqualityComparer <object>(); // Act var actual = systemUnderTest.Equals(item1, item2); // Assert actual.Should().BeTrue(); }
public static void Equals___Should_return_false___When_objects_are_not_the_same_reference_and_equal() { // Arrange var item1 = new Version(1, 2, 3, 4); var item2 = new Version(1, 2, 3, 4); var systemUnderTest = new ReferenceEqualityComparer <object>(); // Act var actual = systemUnderTest.Equals(item1, item2); // Assert actual.Should().BeFalse(); }
public void TestCase03_CompareNullableValueTypes() { ReferenceEqualityComparer <DateTime?> comparer = new ReferenceEqualityComparer <DateTime?>(); /* Situation 1: Comparing two variables that point to the same instance (note: a nullable value-type is still a value-type) */ DateTime?dummyA = new DateTime?(new DateTime(2000, 5, 10)); DateTime?dummyB = dummyA; bool result = comparer.Equals(dummyA, dummyB); Assert.IsFalse(result); /* Situation 2: Comparing two variables that point to different instances with different contents */ dummyA = new DateTime?(new DateTime(2000, 5, 10)); dummyB = new DateTime?(new DateTime(2000, 6, 9)); result = comparer.Equals(dummyA, dummyB); Assert.IsFalse(result); /* Situation 3: Comparing two variables that point to different instances with the same content */ dummyA = new DateTime?(new DateTime(2000, 5, 10)); dummyB = new DateTime?(new DateTime(2000, 5, 10)); result = comparer.Equals(dummyA, dummyB); Assert.IsFalse(result); /* Situation 4: Comparing an instance with its 'clone' */ DateTime testValue = new DateTime(2000, 5, 10); dummyA = new DateTime?(testValue); dummyB = new DateTime?(testValue); result = comparer.Equals(dummyA, dummyB); Assert.IsFalse(result); /* Situation 5: Comparing an instance with a null-value */ dummyA = new DateTime?(new DateTime(2000, 5, 10)); dummyB = null; result = comparer.Equals(dummyA, dummyB); Assert.IsFalse(result); /* Situation 6: Comparing a null-value with an instance */ dummyA = null; dummyB = new DateTime?(new DateTime(2000, 5, 10)); result = comparer.Equals(dummyA, dummyB); Assert.IsFalse(result); /* Situation 6: Comparing two null-values */ dummyA = null; dummyB = null; result = comparer.Equals(dummyA, dummyB); Assert.IsTrue(result); }
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 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 DifferentObjectsEqualAsIEquatableAreNotConsideredEqual() { var referenceComparer = new ReferenceEqualityComparer(); var mock1 = new Mock <IEquatable <object> >(); mock1.Setup(obj => obj.Equals(It.IsAny <object>())).Returns(true); var mock2 = new Mock <IEquatable <object> >(); mock2.Setup(obj => obj.Equals(It.IsAny <object>())).Returns(true); var obj1 = mock1.Object; var obj2 = mock2.Object; var consideredEqual = referenceComparer.Equals(obj1, obj2); Assert.IsFalse(consideredEqual); }
public void TestCase02_CompareValueTypes() { ReferenceEqualityComparer <DateTime> comparer = new ReferenceEqualityComparer <DateTime>(); /* Situation 1: Comparing two variables that point to different instances with different contents */ DateTime dummyA = new DateTime(2000, 5, 10); DateTime dummyB = new DateTime(2000, 6, 9); bool result = comparer.Equals(dummyA, dummyB); Assert.IsFalse(result); /* Situation 2: Comparing two variables that point to different instances with the same content */ dummyA = new DateTime(2000, 5, 10); dummyB = new DateTime(2000, 5, 10); result = comparer.Equals(dummyA, dummyB); Assert.IsFalse(result); }
private Dictionary <TItemSource, TItemVM> GetCache(IBehaviorContext context) { Dictionary <TItemSource, TItemVM> cache; if (!context.FieldValues.TryGetValue(_cache, out cache)) { // Reference equality because: // (1) If the source objects are reloaded in a new NHibernate session, // the are equal to the old object and thus item VMs would be mistakenly // reused. // (2) If two items of the all source items collection are equal, only one // view model would be created for both. cache = new Dictionary <TItemSource, TItemVM>(ReferenceEqualityComparer <TItemSource> .CreateSmartComparer()); context.FieldValues.SetValue(_cache, cache); } return(cache); }
public static void Equals___Should_return_false___When_one_object_is_null_and_the_other_is_not_null() { // Arrange var item1a = new Version(); Version item1b = null; Version item2a = null; var item2b = new Version(); var systemUnderTest = new ReferenceEqualityComparer <Version>(); // Act var actual1 = systemUnderTest.Equals(item1a, item1b); var actual2 = systemUnderTest.Equals(item2a, item2b); // Assert actual1.Should().BeFalse(); actual2.Should().BeFalse(); }
/// <summary> /// Clones the current data service context data /// </summary> /// <returns>A clone of the current data service context data</returns> public DataServiceContextData Clone() { var clone = new DataServiceContextData(this.ContextType, this.MaxProtocolVersion); clone.nextChangeOrder = this.nextChangeOrder; if (this.BaseUri != null) { clone.BaseUri = new Uri(this.BaseUri.OriginalString, UriKind.RelativeOrAbsolute); } clone.AddAndUpdateResponsePreference = this.AddAndUpdateResponsePreference; clone.MergeOption = this.MergeOption; clone.ResolveEntitySet = this.ResolveEntitySet; clone.ResolveName = this.ResolveName; clone.ResolveType = this.ResolveType; clone.UsePostTunneling = this.UsePostTunneling; clone.HttpStack = this.HttpStack; var mapping = new Dictionary<EntityDescriptorData, EntityDescriptorData>(ReferenceEqualityComparer.Create<EntityDescriptorData>()); foreach (var entityDescriptor in this.entityDatas) { var clonedDescriptor = entityDescriptor.Clone(clone); mapping[entityDescriptor] = clonedDescriptor; clone.entityDatas.Add(clonedDescriptor); } foreach (var linkDescriptor in this.linkDatas) { EntityDescriptorData clonedSource = null; EntityDescriptorData clonedTarget = null; mapping.TryGetValue(linkDescriptor.SourceDescriptor, out clonedSource); if (linkDescriptor.TargetDescriptor != null) { mapping.TryGetValue(linkDescriptor.TargetDescriptor, out clonedTarget); } var clonedDescriptor = linkDescriptor.Clone(clonedSource, clonedTarget); clone.linkDatas.Add(clonedDescriptor); } return clone; }
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 iEqualityComparer_equals_with_null_and_instance() { IEqualityComparer instance = new ReferenceEqualityComparer<GenericParameterHelper>(); var y = new GenericParameterHelper(); bool actual = instance.Equals( null, y ); actual.Should().Be.False(); }
public void referenceEqualityComparer_ctor() { ReferenceEqualityComparer<GenericParameterHelper> instance = new ReferenceEqualityComparer<GenericParameterHelper>(); }
public void generic_iEqualityComparer_getHashCode_null_reference() { IEqualityComparer<GenericParameterHelper> instance = new ReferenceEqualityComparer<GenericParameterHelper>(); Int32 actual = instance.GetHashCode( null ); }