public void TestObjectDictionariesInequalKeys() { var d1 = new Dictionary <string, Person>() { { "k1", new Person("p1", 10) }, { "k2", new Person("p3", 20) }, { "k3", new Person("p3", 30) }, { "k4", new Person("p4", 40) }, { "k5", new Person("p5", 50) }, }; var d2 = new Dictionary <string, Person>() { { "k11", new Person("p1", 10) }, { "k2", new Person("p3", 20) }, { "k3", new Person("p3", 30) }, { "k4", new Person("p4", 40) }, { "k5", new Person("p5", 50) }, }; var comparer = new DictionaryComparer <string, Person>(); Assert.False(comparer.Equals(d1, d2)); Assert.False(comparer.Equals(d2, d1)); }
public void Equals_TwoDictionariesWhereOneHasOneEntryExtra_ShouldBeFalse() { _dictionary1["test1"] = "value1"; _dictionary1["test2"] = "value2"; _dictionary2["test1"] = "value1"; Assert.That(DictionaryComparer.Equals(_dictionary1, _dictionary2), Is.False); }
private bool Equals(HtmlTag other) { if (ReferenceEquals(null, other)) { return(false); } if (ReferenceEquals(this, other)) { return(true); } if (!string.Equals(TagName, other.TagName)) { return(false); } if (Count != other.Count) { return(false); } if (!DictionaryComparer.Equals(this, other, "class", "style")) { return(false); } if (!DictionaryComparer.Equals(Styles, other.Styles)) { return(false); } return(Classes.OrderBy(c => c).SequenceEqual(other.Classes.OrderBy(c => c)) && Contents.SequenceEqual(other.Contents)); }
/// <summary> /// Determines if the two NciUrls are Equivelent /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <returns></returns> public bool Equals(NciUrl x, NciUrl y) { DictionaryComparer <string, string> comparer = new DictionaryComparer <string, string>(); return(x.UriStem == y.UriStem && comparer.Equals(x.QueryParameters, y.QueryParameters)); }
public bool Equals(QueryString other) { if (other == null) { return(false); } return(DictionaryComparer.CheckEquality(QueryStrings, other.QueryStrings)); }
public void Equals_TwoDictionariesWhereMostEntriesAreEqualButTheUnequalEntriesAreExcludedByKey_ShouldBeTrue() { _dictionary1["test1"] = "value1"; _dictionary1["test2"] = "value2"; _dictionary2["test1"] = "w sfqffqf"; _dictionary2["test2"] = "value2"; Assert.That(DictionaryComparer.Equals(_dictionary1, _dictionary2, new[] { "test1" }), Is.True); }
public void Equals_TwoDictionariesWithEachTwoEqualEntries_ShouldBeEqual() { _dictionary1["test1"] = "value1"; _dictionary1["test2"] = "value2"; _dictionary2["test1"] = "value1"; _dictionary2["test2"] = "value2"; Assert.That(DictionaryComparer.Equals(_dictionary1, _dictionary2), Is.True); }
public void Equals_TwoDictionaryWithSameKeysButDiferentValues_ReturnFalse() { Dictionary <string, string> dic1 = new Dictionary <string, string>(); Dictionary <string, string> dic2 = new Dictionary <string, string>(); dic1.Add("a", "aa"); dic2.Add("a", "kk"); DictionaryComparer <string, string> comparer = new DictionaryComparer <string, string>(EqualityComparer <string> .Default); Assert.IsFalse(comparer.Equals(dic1, dic2)); }
/// <summary> /// Checks whether the two list are equivalent or not. /// </summary> /// <typeparam name="TModel">Type of model.</typeparam> public static bool AreListEquivalent <TModel>( IEnumerable <TModel> list1, IEnumerable <TModel> list2, Func <TModel, TModel, bool> compareFunc) where TModel : class { compareFunc.NotNull(nameof(compareFunc)); // ReSharper disable once PossibleUnintendedReferenceComparison // BER says this is OK since either both are null, or have the same reference which is // in case of our compare infra the correct definition of equal. if (list1 == list2) { return(true); } var list1List = list1 as IList <TModel> ?? list1.ToList(); var list2List = list2 as IList <TModel> ?? list2.ToList(); if (list1List.Count != list2List.Count) { return(false); } var comparer = new DictionaryComparer <TModel>(compareFunc); var counterDictionary = new Dictionary <TModel, int>(comparer); foreach (var item1 in list1List) { if (counterDictionary.ContainsKey(item1)) { counterDictionary[item1]++; } else { counterDictionary.Add(item1, 1); } } foreach (var item2 in list2List) { if (counterDictionary.ContainsKey(item2)) { counterDictionary[item2]--; } else { return(false); } } return(counterDictionary.Values.All(x => x == 0)); }
public void GetBestService_WithValidOrdersAndRestaurants_ShouldReturnResults() { // Arrange AnalysisService analysisService = new AnalysisService(); List <Restaurant> restaurants = analysisService.GetRestaurants(); Dictionary <MealType, int> orders = new Dictionary <MealType, int>() { { MealType.Others, 40 }, { MealType.Vegetarian, 5 }, { MealType.GlutenFree, 2 } }; List <Result> expectedResult = new List <Result>() { new Result() { Restaurant = new Restaurant() { Name = "Restaurant A" }, ProvidedFoods = new Dictionary <MealType, int>() { { MealType.Others, 36 }, { MealType.Vegetarian, 4 }, } }, new Result() { Restaurant = new Restaurant() { Name = "Restaurant B" }, ProvidedFoods = new Dictionary <MealType, int>() { { MealType.Others, 4 }, { MealType.Vegetarian, 1 }, { MealType.GlutenFree, 2 } } } }; // Act List <Result> results = analysisService.GetBestService(orders, restaurants); // Assert Assert.AreEqual(expectedResult.Count, results.Count, String.Format("Length of results should be {0}", expectedResult.Count)); Assert.AreEqual(expectedResult[0].Restaurant.Name, results[0].Restaurant.Name, String.Format("Name of first restaurant should be {0}", expectedResult[0].Restaurant.Name)); Assert.AreEqual(expectedResult[1].Restaurant.Name, results[1].Restaurant.Name, String.Format("Name of second restaurant should be {0}", expectedResult[1].Restaurant.Name)); DictionaryComparer <MealType, int> comparer = new DictionaryComparer <MealType, int>(); Assert.IsTrue(comparer.Equals(expectedResult[0].ProvidedFoods, results[0].ProvidedFoods), "Results should have been the same"); Assert.IsTrue(comparer.Equals(expectedResult[1].ProvidedFoods, results[1].ProvidedFoods), "Results should have been the same"); }
public void TestRefEquals() { var d1 = new Dictionary <string, Person>() { { "k1", new Person("p1", 10) }, { "k2", new Person("p2", 20) }, { "k3", new Person("p3", 30) }, { "k4", new Person("p4", 40) }, { "k5", new Person("p5", 50) }, }; var comparer = new DictionaryComparer <string, Person>(); Assert.True(comparer.Equals(d1, d1)); }
public void Equals_TwoSameDictionary_ReturnTrue() { Dictionary <string, string> dic1 = new Dictionary <string, string>(); Dictionary <string, string> dic2 = new Dictionary <string, string>(); dic1.Add("a", "aa"); dic1.Add("b", "bb"); dic1.Add("c", "cc"); dic2.Add("a", "aa"); dic2.Add("b", "bb"); dic2.Add("c", "cc"); DictionaryComparer <string, string> comparer = new DictionaryComparer <string, string>(EqualityComparer <string> .Default); Assert.IsTrue(comparer.Equals(dic1, dic2)); }
public void TestNullInequality() { var d1 = new Dictionary <string, Person>() { { "k1", new Person("p1", 10) }, { "k2", new Person("p2", 20) }, { "k3", new Person("p3", 30) }, { "k4", new Person("p4", 40) }, { "k5", new Person("p5", 50) }, }; var comparer = new DictionaryComparer <string, Person>(); Assert.False(comparer.Equals(null, d1)); Assert.False(comparer.Equals(d1, null)); }
public void DictionariesAreNotEqualBasedOnMemberCount() { var x = new Dictionary<string, object>() { { "a", 1 } }; var y = new Dictionary<string, object>() { { "a", 1 }, { "b", 2 } }; var comparer = new DictionaryComparer<string, object>(); Assert.IsFalse(comparer.Equals(x, y)); Assert.IsFalse(comparer.GetHashCode(x) == comparer.GetHashCode(y)); }
public void DictionariesAreEqual() { var x = new Dictionary<string, object>() { { "a", 1 }, { "b", 1 } }; var y = new Dictionary<string, object>() { { "a", 1 }, { "b", 1 } }; var comparer = new DictionaryComparer<string, object>(); Assert.IsTrue(comparer.Equals(x, y)); Assert.IsTrue(comparer.GetHashCode(x) == comparer.GetHashCode(y)); }
public void DictionariesAreNotEqualBasedOnMemberCount() { var x = new Dictionary <string, object>() { { "a", 1 } }; var y = new Dictionary <string, object>() { { "a", 1 }, { "b", 2 } }; var comparer = new DictionaryComparer <string, object>(); Assert.IsFalse(comparer.Equals(x, y)); Assert.IsFalse(comparer.GetHashCode(x) == comparer.GetHashCode(y)); }
public void DictionariesAreEqual() { var x = new Dictionary <string, object>() { { "a", 1 }, { "b", 1 } }; var y = new Dictionary <string, object>() { { "a", 1 }, { "b", 1 } }; var comparer = new DictionaryComparer <string, object>(); Assert.IsTrue(comparer.Equals(x, y)); Assert.IsTrue(comparer.GetHashCode(x) == comparer.GetHashCode(y)); }
//public bool NegTest1() //{ // bool retVal = true; // TestLibrary.TestFramework.BeginScenario("NegTest1: "); // try // { // // // // Add your test logic here // // // } // catch (Exception e) // { // TestLibrary.TestFramework.LogError("101", "Unexpected exception: " + e); // TestLibrary.TestFramework.LogInformation(e.StackTrace); // retVal = false; // } // return retVal; //} #endregion #endregion public static int Main() { DictionaryComparer test = new DictionaryComparer(); TestLibrary.TestFramework.BeginTestCase("DictionaryComparer"); if (test.RunTests()) { TestLibrary.TestFramework.EndTestCase(); TestLibrary.TestFramework.LogInformation("PASS"); return(100); } else { TestLibrary.TestFramework.EndTestCase(); TestLibrary.TestFramework.LogInformation("FAIL"); return(0); } }
//public bool NegTest1() //{ // bool retVal = true; // TestLibrary.TestFramework.BeginScenario("NegTest1: "); // try // { // // // // Add your test logic here // // // } // catch (Exception e) // { // TestLibrary.TestFramework.LogError("101", "Unexpected exception: " + e); // TestLibrary.TestFramework.LogInformation(e.StackTrace); // retVal = false; // } // return retVal; //} #endregion #endregion public static int Main() { DictionaryComparer test = new DictionaryComparer(); TestLibrary.TestFramework.BeginTestCase("DictionaryComparer"); if (test.RunTests()) { TestLibrary.TestFramework.EndTestCase(); TestLibrary.TestFramework.LogInformation("PASS"); return 100; } else { TestLibrary.TestFramework.EndTestCase(); TestLibrary.TestFramework.LogInformation("FAIL"); return 0; } }
public void Compare() { IDictionary <string, double> dictionaryOne = new Dictionary <string, double>() { { "he", 0 }, { "ey", 0 } }; Dictionary <string, double> dictionaryTwo = new Dictionary <string, double>() { { "he", 0 }, { "ey", 0 }, { "yg", 0 }, { "gu", 0 }, { "uy", 0 }, { "ys", 0 } }; var comparer = new DictionaryComparer(); var list = comparer.CompareDictionaries(dictionaryOne, dictionaryTwo); Assert.That(4, Is.EqualTo(list.Count)); Assert.That("yg", Is.EqualTo(list[0])); Assert.That("gu", Is.EqualTo(list[1])); Assert.That("uy", Is.EqualTo(list[2])); Assert.That("ys", Is.EqualTo(list[3])); }
public void Equals_TwoEmptyDictionaries_ShouldBeEqual() { Assert.That(DictionaryComparer.Equals(_dictionary1, _dictionary2), Is.True); }
public void TestNullEquals() { var comparer = new DictionaryComparer <string, Person>(); Assert.True(comparer.Equals(null, null)); }