Exemple #1
0
        /// <summary>
        /// Compares the entire members of one array whith the other one.
        /// </summary>
        /// <param name="a">The array to be compared.</param>
        /// <param name="b">The array to be compared with.</param>
        /// <returns>Returns true if the two specified arrays of Objects are equal
        /// to one another. The two arrays are considered equal if both arrays
        /// contain the same number of elements, and all corresponding pairs of
        /// elements in the two arrays are equal. Two objects e1 and e2 are
        /// considered equal if (e1==null ? e2==null : e1.equals(e2)). In other
        /// words, the two arrays are equal if they contain the same elements in
        /// the same order. Also, two array references are considered equal if
        /// both are null.
        /// <para/>
        /// Note that if the type of <paramref name="T"/> is a <see cref="IDictionary{TKey, TValue}"/>,
        /// <see cref="IList{T}"/>, or <see cref="ISet{T}"/>, its values and any nested collection values
        /// will be compared for equality as well.
        /// </returns>
        public static bool Equals <T>(T[] a, T[] b)
        {
            if (object.ReferenceEquals(a, b))
            {
                return(true);
            }
            bool isValueType = typeof(T).GetTypeInfo().IsValueType;

            if (!isValueType && a == null)
            {
                return(b == null);
            }

            int length = a.Length;

            if (b.Length != length)
            {
                return(false);
            }

            for (int i = 0; i < length; i++)
            {
                T o1 = a[i];
                T o2 = b[i];
                if (!(isValueType ? o1.Equals(o2) : (o1 == null ? o2 == null : Collections.Equals(o1, o2))))
                {
                    return(false);
                }
            }

            return(true);
        }
        public void TestEqualsTypeMismatch()
        {
            var list = new List <int> {
                1, 2, 3, 4, 5
            };
            var set = new HashSet <int> {
                1, 2, 3, 4, 5
            };
            var dictionary = new Dictionary <int, int> {
                { 1, 0 }, { 2, 0 }, { 3, 0 }, { 4, 0 }, { 5, 0 }
            };
            var array = new int[] { 1, 2, 3, 4, 5 };

            Assert.IsFalse(Collections.Equals(list, set));
            Assert.IsFalse(Collections.Equals(list, dictionary));
            Assert.IsTrue(Collections.Equals(list, array)); // Types are compatible - array implements IList<T>

            Assert.IsFalse(Collections.Equals(set, dictionary));
            Assert.IsFalse(Collections.Equals(set, array));
        }
Exemple #3
0
        // TODO: When diverging from Java version of Lucene, can uncomment these to adhere to best practices when overriding the Equals method and implementing IEquatable<T>.
        ///// <summary>Overload of the == operator, it compares a
        ///// <see cref="EquatableSet{T}"/> to an <see cref="IEnumerable{T}"/>
        ///// implementation.</summary>
        ///// <param name="x">The <see cref="EquatableSet{T}"/> to compare
        ///// against <paramref name="y"/>.</param>
        ///// <param name="y">The <see cref="IEnumerable{T}"/> to compare
        ///// against <paramref name="x"/>.</param>
        ///// <returns>True if the instances are equal, false otherwise.</returns>
        //public static bool operator ==(EquatableSet<T> x, System.Collections.Generic.IEnumerable<T> y)
        //{
        //    // Call Equals.
        //    return Equals(x, y);
        //}

        ///// <summary>Overload of the == operator, it compares a
        ///// <see cref="EquatableSet{T}"/> to an <see cref="IEnumerable{T}"/>
        ///// implementation.</summary>
        ///// <param name="y">The <see cref="EquatableSet{T}"/> to compare
        ///// against <paramref name="x"/>.</param>
        ///// <param name="x">The <see cref="IEnumerable{T}"/> to compare
        ///// against <paramref name="y"/>.</param>
        ///// <returns>True if the instances are equal, false otherwise.</returns>
        //public static bool operator ==(System.Collections.Generic.IEnumerable<T> x, EquatableSet<T> y)
        //{
        //    // Call equals.
        //    return Equals(x, y);
        //}

        ///// <summary>Overload of the != operator, it compares a
        ///// <see cref="EquatableSet{T}"/> to an <see cref="IEnumerable{T}"/>
        ///// implementation.</summary>
        ///// <param name="x">The <see cref="EquatableSet{T}"/> to compare
        ///// against <paramref name="y"/>.</param>
        ///// <param name="y">The <see cref="IEnumerable{T}"/> to compare
        ///// against <paramref name="x"/>.</param>
        ///// <returns>True if the instances are not equal, false otherwise.</returns>
        //public static bool operator !=(EquatableSet<T> x, System.Collections.Generic.IEnumerable<T> y)
        //{
        //    // Return the negative of the equals operation.
        //    return !(x == y);
        //}

        ///// <summary>Overload of the != operator, it compares a
        ///// <see cref="EquatableSet{T}"/> to an <see cref="IEnumerable{T}"/>
        ///// implementation.</summary>
        ///// <param name="y">The <see cref="EquatableSet{T}"/> to compare
        ///// against <paramref name="x"/>.</param>
        ///// <param name="x">The <see cref="IEnumerable{T}"/> to compare
        ///// against <paramref name="y"/>.</param>
        ///// <returns>True if the instances are not equal, false otherwise.</returns>
        //public static bool operator !=(System.Collections.Generic.IEnumerable<T> x, EquatableSet<T> y)
        //{
        //    // Return the negative of the equals operation.
        //    return !(x == y);
        //}

        #endregion

        #region IEquatable<T> members

        /// <summary>
        /// Compares this sequence to <paramref name="other"/>, returning <c>true</c> if they
        /// are equal, <c>false</c> otherwise.
        /// <para/>
        /// The comparison takes into consideration any values in this collection and values
        /// of any nested collections, but does not take into consideration the data type.
        /// Therefore, <see cref="EquatableSet{T}"/> can equal any <see cref="ISet{T}"/>
        /// with the exact same values (in any order).
        /// </summary>
        /// <param name="other">The other object
        /// to compare against.</param>
        /// <returns><c>true</c> if the sequence in <paramref name="other"/>
        /// is the same as this one.</returns>
        public virtual bool Equals(ISet <T> other)
        {
            return(Collections.Equals(this, other));
        }
        public void TestEqualityDictionary()
        {
            var control = new Dictionary <string, IDictionary <HashMap <long, double>, string> >
            {
                { "a", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 123, 9.87 }, { 80, 88 }
                        }, "qwerty" }
                  } },
                { "z", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 456, 9.86 }, { 81, 88 }
                        }, "hexagon" }
                  } },
                { "r", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 789, 9.85 }, { 82, 88 }
                        }, "parasite" }
                  } },
                { "t", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 101, 9.84 }, { 83, 88 }
                        }, "octopus" }
                  } },
            };
            var equal = new Dictionary <string, IDictionary <HashMap <long, double>, string> >
            {
                { "a", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 123, 9.87 }, { 80, 88 }
                        }, "qwerty" }
                  } },
                { "z", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 456, 9.86 }, { 81, 88 }
                        }, "hexagon" }
                  } },
                { "r", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 789, 9.85 }, { 82, 88 }
                        }, "parasite" }
                  } },
                { "t", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 101, 9.84 }, { 83, 88 }
                        }, "octopus" }
                  } },
            };
            var equalDifferentType = new HashMap <string, IDictionary <HashMap <long, double>, string> >
            {
                { "a", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 123, 9.87 }, { 80, 88 }
                        }, "qwerty" }
                  } },
                { "z", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 456, 9.86 }, { 81, 88 }
                        }, "hexagon" }
                  } },
                { "r", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 789, 9.85 }, { 82, 88 }
                        }, "parasite" }
                  } },
                { "t", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 101, 9.84 }, { 83, 88 }
                        }, "octopus" }
                  } },
            };
            var equalDifferentOrder = new Dictionary <string, IDictionary <HashMap <long, double>, string> >
            {
                { "r", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 789, 9.85 }, { 82, 88 }
                        }, "parasite" }
                  } },
                { "t", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 101, 9.84 }, { 83, 88 }
                        }, "octopus" }
                  } },
                { "a", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 123, 9.87 }, { 80, 88 }
                        }, "qwerty" }
                  } },
                { "z", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 456, 9.86 }, { 81, 88 }
                        }, "hexagon" }
                  } },
            };

            var level1EqualLevel2EqualLevel3Unequal = new Dictionary <string, IDictionary <HashMap <long, double>, string> >
            {
                { "a", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 123, 9.87 }, { 80, 88.1 }
                        }, "qwerty" }
                  } },
                { "z", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 456, 9.86 }, { 81, 88 }
                        }, "hexagon" }
                  } },
                { "r", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 789, 9.85 }, { 82, 88 }
                        }, "parasite" }
                  } },
                { "t", new Dictionary <HashMap <long, double>, string> {
                      { new HashMap <long, double> {
                            { 101, 9.84 }, { 83, 88 }
                        }, "octopus" }
                  } },
            };

            Assert.AreEqual(Collections.GetHashCode(control), Collections.GetHashCode(control));
            Assert.IsTrue(Collections.Equals(control, control));

            Assert.AreEqual(Collections.GetHashCode(control), Collections.GetHashCode(equal));
            Assert.IsTrue(Collections.Equals(control, equal));

            Assert.AreEqual(Collections.GetHashCode(control), Collections.GetHashCode(equalDifferentType));
            Assert.IsTrue(Collections.Equals(control, equalDifferentType));

            Assert.AreEqual(Collections.GetHashCode(control), Collections.GetHashCode(equalDifferentOrder));
            Assert.IsTrue(Collections.Equals(control, equalDifferentOrder));

            Assert.AreNotEqual(Collections.GetHashCode(control), Collections.GetHashCode(level1EqualLevel2EqualLevel3Unequal));
            Assert.IsFalse(Collections.Equals(control, level1EqualLevel2EqualLevel3Unequal));
        }
        public void TestEqualitySet()
        {
            var control = new HashSet <IDictionary <string, string> >
            {
                new Dictionary <string, string> {
                    { "1", "one" }, { "2", "two" }, { "3", "three" }
                },
                new Dictionary <string, string> {
                    { "4", "four" }, { "5", "five" }, { "6", "six" }
                },
                new Dictionary <string, string> {
                    { "7", "seven" }, { "8", "eight" }, { "9", "nine" }
                },
            };
            var equal = new HashSet <IDictionary <string, string> >
            {
                new Dictionary <string, string> {
                    { "1", "one" }, { "2", "two" }, { "3", "three" }
                },
                new Dictionary <string, string> {
                    { "4", "four" }, { "5", "five" }, { "6", "six" }
                },
                new Dictionary <string, string> {
                    { "7", "seven" }, { "8", "eight" }, { "9", "nine" }
                },
            };
            var equalDifferentType = new MockHashSet <IDictionary <string, string> >
            {
                new Dictionary <string, string> {
                    { "1", "one" }, { "2", "two" }, { "3", "three" }
                },
                new Dictionary <string, string> {
                    { "4", "four" }, { "5", "five" }, { "6", "six" }
                },
                new Dictionary <string, string> {
                    { "7", "seven" }, { "8", "eight" }, { "9", "nine" }
                },
            };
            var equalDifferentOrder = new HashSet <IDictionary <string, string> >
            {
                new Dictionary <string, string> {
                    { "7", "seven" }, { "8", "eight" }, { "9", "nine" }
                },
                new Dictionary <string, string> {
                    { "1", "one" }, { "2", "two" }, { "3", "three" }
                },
                new Dictionary <string, string> {
                    { "4", "four" }, { "5", "five" }, { "6", "six" }
                },
            };
            var level1EqualLevel2Unequal = new HashSet <IDictionary <string, string> >
            {
                new Dictionary <string, string> {
                    { "1", "one" }, { "2", "two" }, { "3", "three" }
                },
                new Dictionary <string, string> {
                    { "4", "four" }, { "5", "five" }, { "6", "six" }
                },
                new Dictionary <string, string> {
                    { "7", "seven" }, { "8", "eight" }, { "9", "nine99" }
                },
            };

            Assert.AreEqual(Collections.GetHashCode(control), Collections.GetHashCode(control));
            Assert.IsTrue(Collections.Equals(control, control));

            Assert.AreEqual(Collections.GetHashCode(control), Collections.GetHashCode(equal));
            Assert.IsTrue(Collections.Equals(control, equal));

            Assert.AreEqual(Collections.GetHashCode(control), Collections.GetHashCode(equalDifferentType));
            Assert.IsTrue(Collections.Equals(control, equalDifferentType));

            // Sets are not order-sensitive
            Assert.AreEqual(Collections.GetHashCode(control), Collections.GetHashCode(equalDifferentOrder));
            Assert.IsTrue(Collections.Equals(control, equalDifferentOrder));

            Assert.AreNotEqual(Collections.GetHashCode(control), Collections.GetHashCode(level1EqualLevel2Unequal));
            Assert.IsFalse(Collections.Equals(control, level1EqualLevel2Unequal));
        }
        public void TestEqualityListSimple()
        {
            var control = new List <IList <string> >
            {
                new List <string> {
                    "one", "two", "three"
                },
                new List <string> {
                    "four", "five", "six"
                },
                new List <string> {
                    "seven", "eight", "nine"
                },
            };
            var equal = new List <IList <string> >
            {
                new List <string> {
                    "one", "two", "three"
                },
                new List <string> {
                    "four", "five", "six"
                },
                new List <string> {
                    "seven", "eight", "nine"
                },
            };
            var equalDifferentType = new IList <string>[]
            {
                new List <string> {
                    "one", "two", "three"
                },
                new List <string> {
                    "four", "five", "six"
                },
                new List <string> {
                    "seven", "eight", "nine"
                },
            };
            var equalDifferentOrder = new List <IList <string> >
            {
                new List <string> {
                    "four", "five", "six"
                },
                new List <string> {
                    "seven", "eight", "nine"
                },
                new List <string> {
                    "one", "two", "three"
                },
            };
            var level1EqualLevel2Unequal = new List <IList <string> >
            {
                new List <string> {
                    "one", "two", "three"
                },
                new List <string> {
                    "four", "five", "six"
                },
                new List <string> {
                    "seven", "eight", "nine-nine"
                },
            };

            Assert.AreEqual(Collections.GetHashCode(control), Collections.GetHashCode(control));
            Assert.IsTrue(Collections.Equals(control, control));

            Assert.AreEqual(Collections.GetHashCode(control), Collections.GetHashCode(equal));
            Assert.IsTrue(Collections.Equals(control, equal));

            Assert.AreEqual(Collections.GetHashCode(control), Collections.GetHashCode(equalDifferentType));
            Assert.IsTrue(Collections.Equals(control, equalDifferentType));

            // Lists and arrays are order - sensitive
            Assert.AreNotEqual(Collections.GetHashCode(control), Collections.GetHashCode(equalDifferentOrder));
            Assert.IsFalse(Collections.Equals(control, equalDifferentOrder));

            Assert.AreNotEqual(Collections.GetHashCode(control), Collections.GetHashCode(level1EqualLevel2Unequal));
            Assert.IsFalse(Collections.Equals(control, level1EqualLevel2Unequal));
        }