Esempio n. 1
0
        public void GetHashCodeTest()
        {
            var testSamples = new List <UriFormat>
            {
                UriFormat.SafeUnescaped,
                UriFormat.Unescaped,
                UriFormat.UriEscaped
            };

            var comparer = new GenericEqualityComparer <UriFormat>((x, y) => x == y, x => x.GetHashCode());

            for (var index = 0; index < testSamples.Count; index++)
            {
                Assert.AreEqual(testSamples[index].GetHashCode(), comparer.GetHashCode(testSamples[index]));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Creates the default <see cref="EqualityComparer{T}"/>.
        /// </summary>
        /// <param name="type">The type to create the default equality comparer for.</param>
        /// <remarks>
        /// The logic in this method is replicated in vm/compile.cpp to ensure that NGen saves the right instantiations.
        /// </remarks>
        internal static object CreateDefaultEqualityComparer(Type type)
        {
            Debug.Assert(type != null && type is RuntimeType);

            object?result      = null;
            var    runtimeType = (RuntimeType)type;

            if (type == typeof(byte))
            {
                // Specialize for byte so Array.IndexOf is faster.
                result = new ByteEqualityComparer();
            }
            else if (type == typeof(string))
            {
                // Specialize for string, as EqualityComparer<string>.Default is on the startup path
                result = new GenericEqualityComparer <string>();
            }
            else if (type.IsAssignableTo(typeof(IEquatable <>).MakeGenericType(type)))
            {
                // If T implements IEquatable<T> return a GenericEqualityComparer<T>
                result = CreateInstanceForAnotherGenericParameter(
                    (RuntimeType)typeof(GenericEqualityComparer <string>),
                    runtimeType
                    );
            }
            else if (type.IsGenericType)
            {
                // Nullable does not implement IEquatable<T?> directly because that would add an extra interface call per comparison.
                // Instead, it relies on EqualityComparer<T?>.Default to specialize for nullables and do the lifted comparisons if T implements IEquatable.
                if (type.GetGenericTypeDefinition() == typeof(Nullable <>))
                {
                    result = TryCreateNullableEqualityComparer(runtimeType);
                }
            }
            else if (type.IsEnum)
            {
                // The equality comparer for enums is specialized to avoid boxing.
                result = TryCreateEnumEqualityComparer(runtimeType);
            }

            return(result
                   ?? CreateInstanceForAnotherGenericParameter(
                       (RuntimeType)typeof(ObjectEqualityComparer <object>),
                       runtimeType
                       ));
        }
Esempio n. 3
0
        public void NotEqualsTest()
        {
            var first = new List <UriFormat>
            {
                UriFormat.SafeUnescaped,
                UriFormat.Unescaped,
                UriFormat.UriEscaped
            };

            var second = new List <UriFormat>
            {
                UriFormat.Unescaped,
                UriFormat.SafeUnescaped,
                UriFormat.UriEscaped
            };

            var comparer = new GenericEqualityComparer <UriFormat>((x, y) => x == y, x => x.GetHashCode());

            var actualResult = first.SafeSequenceEqual(second, comparer);

            Assert.IsFalse(actualResult);
        }
Esempio n. 4
0
        // Equals method for the comparer itself.
        public override bool Equals(Object obj)
        {
            GenericEqualityComparer <T> comparer = obj as GenericEqualityComparer <T>;

            return(comparer != null);
        }