public void GetHashCode_TwoDictionariesHaveDifferentValue_ReturnDifferentHashCodes()
        {
            var reference = new Dictionary <string, string> {
                { "key1", "value1" }, { "key2", "value2" }
            };
            var other = new Dictionary <string, string> {
                { "key1", "value1" }, { "key2", "value3" }
            };
            var comparer = new DictionaryEqualityComparer <string, string>();

            Assert.AreNotEqual(comparer.GetHashCode(reference), comparer.GetHashCode(other));
        }
        public void GetHashCode_Null_Throws()
        {
            var comparer = new DictionaryEqualityComparer <string, string>();

            Assert.Throws <ArgumentNullException>(
                () => comparer.GetHashCode(null));
        }
Exemple #3
0
        public void Null()
        {
            Assert.True(_testComparer.Equals(null, null));
            Assert.False(_testComparer.Equals(null, new Dictionary <string, string>()));
            Assert.False(_testComparer.Equals(new Dictionary <string, string>(), null));

            Assert.AreEqual(0, _testComparer.GetHashCode(null));
        }
        public void DictionaryEqualityComparerGetHashCodeReturnsDefaultHashCodeIfDictionaryIsEmpty()
        {
            const int expectedHashCode = 23;

            var comparer = new DictionaryEqualityComparer <string, int>();

            Dictionary <string, int> dictionary = new Dictionary <string, int>();

            int result = comparer.GetHashCode(dictionary);

            Assert.AreEqual(expectedHashCode, result);
        }
        public void DictionaryEqualityComparerGetHashCodeReturnsZeroIfDictionaryIsNull()
        {
            const int expectedHashCode = 0;

            var comparer = new DictionaryEqualityComparer <string, int>();

            Dictionary <string, int> dictionary = null;

            int result = comparer.GetHashCode(dictionary);

            Assert.AreEqual(expectedHashCode, result);
        }
        public void DictionaryEqualityComparerGetHashCodeReturnsSameHashCodeIfDictionaryContainsNullValues()
        {
            var comparer = new DictionaryEqualityComparer <string, int?>();

            Dictionary <string, int?> dictionary = new Dictionary <string, int?>
            {
                { "A", 1 },
                { "B", 2 },
                { "C", 3 },
                { "D", null },
            };

            //get the HashCode 2000 times to confirm it is consistent for lifetime of dictionary
            HashSet <int> hashes = new HashSet <int>();

            for (int i = 0; i < 2000; i++)
            {
                hashes.Add(comparer.GetHashCode(dictionary));
            }

            Assert.AreEqual(1, hashes.Count);
        }
 public int GetHashCode(IEqualityComparer comparer)
 {
     return(DictionaryEqualityComparer <TKey, TValue> .GetHashCode(this, comparer));
 }
		public void GetHashCode_Null_Throws()
		{
			var comparer = new DictionaryEqualityComparer<string, string>();
			Assert.Throws<ArgumentNullException>(
				() => comparer.GetHashCode(null));
		}
		public void GetHashCode_TwoDictionariesHaveDifferentValue_ReturnDifferentHashCodes()
		{
			var reference = new Dictionary<string, string> {{"key1", "value1"}, {"key2", "value2"}};
			var other = new Dictionary<string, string> {{"key1", "value1"}, {"key2", "value3"}};
			var comparer = new DictionaryEqualityComparer<string, string>();
			Assert.AreNotEqual(comparer.GetHashCode(reference), comparer.GetHashCode(other));
		}
		public void GetHashCode_Null_Throws()
		{
			DictionaryEqualityComparer<string, string> comparer = new DictionaryEqualityComparer<string, string>();
			comparer.GetHashCode(null);
		}
		public void GetHashCode_TwoDictionariesHaveDifferentValue_ReturnDifferentHashCodes()
		{
			Dictionary<string, string> reference = new Dictionary<string, string>();
			reference.Add("key1", "value1");
			reference.Add("key2", "value2");
			Dictionary<string, string> other = new Dictionary<string, string>();
			other.Add("key1", "value1");
			other.Add("key2", "value3");
			DictionaryEqualityComparer<string, string> comparer = new DictionaryEqualityComparer<string, string>();
			Assert.AreNotEqual(comparer.GetHashCode(reference), comparer.GetHashCode(other));
		}