示例#1
0
文件: Arrays.cs 项目: sycct/lucenenet
        public static int GetHashCode <T>(T[] a)
        {
            if (a == null)
            {
                return(0);
            }

            int  result      = 1;
            bool isValueType = typeof(T).GetTypeInfo().IsValueType;

            foreach (var item in a)
            {
                result = 31 * result + (item == null ? 0 :
                                        // LUCENENET specific: if this is a reference type, pass to
                                        // Collections.GetHashCode() in case we have an array of collections
                                        (isValueType ? item.GetHashCode() : Collections.GetHashCode(item)));
            }

            return(result);
        }
示例#2
0
 /// <summary>
 /// Returns the hash code value for this list.
 /// <para/>
 /// The hash code determination takes into consideration any values in
 /// this collection and values of any nested collections, but does not
 /// take into consideration the data type. Therefore, the hash codes will
 /// be exactly the same for this <see cref="EquatableSet{T}"/> and another
 /// <see cref="ISet{T}"/> with the same values (in any order).
 /// </summary>
 /// <returns>the hash code value for this list</returns>
 public override int GetHashCode()
 {
     return(Collections.GetHashCode(this));
 }
示例#3
0
        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));
        }
示例#4
0
        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));
        }
示例#5
0
        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));
        }