public void GetHashCode_Null_Throws()
        {
            var comparer = new DictionaryEqualityComparer <string, string>();

            Assert.Throws <ArgumentNullException>(
                () => comparer.GetHashCode(null));
        }
        internal ReadOnlyDictionary(IDictionary <TKey, TValue> dictionary, DictionaryEqualityComparer <TKey, TValue> structuralEqualityComparer, IFormatProvider toStringFormatProvider)
#pragma warning restore CS8714 // The type cannot be used as type parameter in the generic type or method. Nullability of type argument doesn't match 'notnull' constraint.
        {
            this.dictionary = dictionary ?? throw new ArgumentNullException(nameof(dictionary));
            this.structuralEqualityComparer = structuralEqualityComparer ?? throw new ArgumentNullException(nameof(structuralEqualityComparer));
            this.toStringFormatProvider     = toStringFormatProvider ?? throw new ArgumentNullException(nameof(toStringFormatProvider));
        }
        public void DictionaryEqualityComparerReturnsTrueIfFirstAndSecondAreBothNull()
        {
            var comparer = new DictionaryEqualityComparer <string, int>(EqualityComparer <int> .Default);

            IDictionary <string, int> first  = null;
            IDictionary <string, int> second = null;

            bool result = comparer.Equals(first, second);

            Assert.IsTrue(result);
        }
        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 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 static bool ArePermutations_CountingFrequencies(string a, string b)
        {
            if (a.Length != b.Length)
            {
                return(false);
            }

            var aCharCounts = GetCharacterFrequencies(a);
            var bCharCounts = GetCharacterFrequencies(b);

            var dictEqualityComparer = new DictionaryEqualityComparer <char, int>();

            return(dictEqualityComparer.Equals(aCharCounts, bCharCounts));
        }
        public void DictionaryEqualityComparerReturnsFalseIfFirstIsShorterLengthThanSecond()
        {
            var comparer = new DictionaryEqualityComparer <string, int>(EqualityComparer <int> .Default);

            IDictionary <string, int> first  = new Dictionary <string, int>();
            IDictionary <string, int> second = new Dictionary <string, int>
            {
                { "A", 1 },
                { "B", 2 },
                { "C", 3 },
                { "D", 4 },
            };

            bool result = comparer.Equals(first, second);

            Assert.IsFalse(result);
        }
        public void DictionaryEqualityComparerReturnsTrueIfDictionariesAreSameReference()
        {
            var comparer = new DictionaryEqualityComparer <string, int>(EqualityComparer <int> .Default);

            IDictionary <string, int> first = new Dictionary <string, int>
            {
                { "A", 1 },
                { "B", 2 },
                { "C", 3 },
                { "D", 4 },
            };
            IDictionary <string, int> second = first;

            bool result = comparer.Equals(first, second);

            Assert.IsTrue(result);
        }
        public void DictionaryEqualityComaprerReturnsFalseIfSecondDictionaryIsNull()
        {
            var comparer = new DictionaryEqualityComparer <string, int>(EqualityComparer <int> .Default);

            IDictionary <string, int> first = new Dictionary <string, int>
            {
                { "A", 1 },
                { "B", 2 },
                { "C", 3 },
                { "D", 4 },
            };
            IDictionary <string, int> second = null;

            bool result = comparer.Equals(first, second);

            Assert.IsFalse(result);
        }
Exemplo n.º 11
0
        public bool Test()
        {
            if (_expectedResult.Length != _actualResult.Length)
            {
                return(false);
            }
            DictionaryEqualityComparer <K, V> comparer = new DictionaryEqualityComparer <K, V>();

            for (int i = 0; i != _expectedResult.Length; ++i)
            {
                if (!comparer.Equals(_expectedResult[i], _actualResult[i]))
                {
                    return(false);
                }
            }
            return(true);
        }
        public bool Equals(TestBodyValuesV1Request request, TestBodyValuesV1Response response)
        {
            Argument.NotNull(request, nameof(request));
            Argument.NotNull(response, nameof(response));

            var collectionEqualityComparer = new CollectionEqualityComparer();
            var dictionaryEqualityComparer = new DictionaryEqualityComparer();

            bool equals = true;

            if ((request.Boolean == response.Boolean &&
                 request.Byte == response.Byte &&
                 request.Char == response.Char &&
                 request.DateTimeOffset == response.DateTimeOffset &&
                 request.Decimal == response.Decimal &&
                 request.Double == response.Double &&
                 request.Enum == response.Enum &&
                 request.Int16 == response.Int16 &&
                 request.Int32 == response.Int32 &&
                 request.Int64 == response.Int64 &&
                 request.SignedByte == response.SignedByte &&
                 request.Single == response.Single &&
                 request.String == response.String &&
                 request.TimeSpan == response.TimeSpan &&
                 request.UnsignedInt16 == response.UnsignedInt16 &&
                 request.UnsignedInt32 == response.UnsignedInt32 &&
                 request.UnsignedInt64 == response.UnsignedInt64 &&
                 request.Uri == response.Uri &&
                 request.Uuid == response.Uuid) == false)
            {
                equals = false;
            }

            else if ((collectionEqualityComparer.CollectionEquals(request.Array, response.Array) &&
                      collectionEqualityComparer.CollectionEquals(request.Bytes, response.Bytes) &&
                      dictionaryEqualityComparer.DictionaryEquals(request.Dictionary, response.Dictionary)) == false)
            {
                equals = false;
            }

            return(equals);
        }
        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 void DictionaryEqualityComparerReturnsTrueIfFirstAndSecondDictionaryKeyValuePairsAreTheSameButDifferentOrder()
        {
            var comparer = new DictionaryEqualityComparer <string, int>(EqualityComparer <int> .Default);

            IDictionary <string, int> first = new Dictionary <string, int>
            {
                { "A", 1 },
                { "B", 2 },
                { "C", 3 },
                { "D", 4 },
            };
            IDictionary <string, int> second = new Dictionary <string, int>
            {
                { "D", 4 },
                { "B", 2 },
                { "C", 3 },
                { "A", 1 }
            };

            bool result = comparer.Equals(first, second);

            Assert.IsTrue(result);
        }
        public void DictionaryEqualityComparerReturnsFalseIfFirstAndSecondValuesAreDifferent()
        {
            var comparer = new DictionaryEqualityComparer <string, int>(EqualityComparer <int> .Default);

            IDictionary <string, int> first = new Dictionary <string, int>
            {
                { "A", 1 },
                { "B", 2 },
                { "C", 3 },
                { "D", 4 },
            };
            IDictionary <string, int> second = new Dictionary <string, int>
            {
                { "A", 1 },
                { "B", 2 },
                { "C", 3 },
                { "D", 5 },
            };

            bool result = comparer.Equals(first, second);

            Assert.IsFalse(result);
        }
		public void GetHashCode_Null_Throws()
		{
			var comparer = new DictionaryEqualityComparer<string, string>();
			Assert.Throws<ArgumentNullException>(
				() => comparer.GetHashCode(null));
		}
		public void SetUp()
		{
			_comparer = new DictionaryEqualityComparer<string, string>();
			_x = new Dictionary<string, string>();
			_y = new Dictionary<string, string>();
		}
		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 SetUp()
 {
     _comparer = new DictionaryEqualityComparer <string, string>();
     _x        = new Dictionary <string, string>();
     _y        = new Dictionary <string, string>();
 }
        public void DictionaryEqualityComparerInitialises()
        {
            var comparer = new DictionaryEqualityComparer <string, int>();

            Assert.IsNotNull(comparer);
        }
Exemplo n.º 21
0
 internal static IEqualityComparer <IReadOnlyDictionary <TKey, TValue> > Dictionary <TKey, TValue>(IEqualityComparer <TValue> valueComparer = null)
 {
     return(DictionaryEqualityComparer <TKey, TValue> .Get(valueComparer));
 }
		public void GetHashCode_Null_Throws()
		{
			DictionaryEqualityComparer<string, string> comparer = new DictionaryEqualityComparer<string, string>();
			comparer.GetHashCode(null);
		}
 public int GetHashCode(IEqualityComparer comparer)
 {
     return(DictionaryEqualityComparer <TKey, TValue> .GetHashCode(this, comparer));
 }
        public void DictionaryEqualityComparerInitialisesWithSpecifiedValueComparer()
        {
            var comparer = new DictionaryEqualityComparer <string, int>(EqualityComparer <int> .Default);

            Assert.IsNotNull(comparer);
        }
 public bool Equals(object other, IEqualityComparer comparer)
 {
     return(DictionaryEqualityComparer <TKey, TValue> .Equals(this, other, comparer));
 }
		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));
		}