From 147acaf98d044779fe405b7d35d23ffadd36aa49 Mon Sep 17 00:00:00 2001 From: John Stewien Date: Tue, 17 Dec 2024 17:03:56 +1030 Subject: [PATCH] #35 Added more unit tests to try to repro the issue --- .../ConcurrentObservableDictionaryTests.cs | 91 ++++++++++++++++++- .../KeyTestClasses.cs | 32 ++++++- 2 files changed, 121 insertions(+), 2 deletions(-) diff --git a/ExamplesAndTests/Swordfish.NET.UnitTestV3/ConcurrentObservableDictionaryTests.cs b/ExamplesAndTests/Swordfish.NET.UnitTestV3/ConcurrentObservableDictionaryTests.cs index d474be4..5071760 100644 --- a/ExamplesAndTests/Swordfish.NET.UnitTestV3/ConcurrentObservableDictionaryTests.cs +++ b/ExamplesAndTests/Swordfish.NET.UnitTestV3/ConcurrentObservableDictionaryTests.cs @@ -563,6 +563,32 @@ public void TestIndexOfReferenceTypeInViews() } } + [TestMethod] + public void AddRemoveRangeValueTypeTest() + { + IEnumerable> GetIEnumerable() + { + for (int i = 0; i < 10; ++i) + { + yield return new KeyValuePair(i.ToString(), i.ToString()); + } + } + + // Don't need to convert to array because value type + var itemsToAdd = GetIEnumerable(); + var dictionary1 = new ConcurrentObservableDictionary(); + dictionary1.AddRange(itemsToAdd); + + Assert.IsTrue(dictionary1.Count == itemsToAdd.Count(), "Count doesn't match number of items added"); + + foreach (var item in itemsToAdd) + { + Assert.IsTrue(dictionary1.Remove(item.Key), "Problem removing item"); + } + + Assert.IsTrue(dictionary1.Count == 0, "Not all items removed"); + } + [TestMethod] public void AddRemoveRangeReferenceTypeTest() { @@ -574,6 +600,7 @@ IEnumerable> GetIEnumerable() } } + // Need to convert to array so we have the same objects being used var itemsToAdd = GetIEnumerable().ToArray(); var dictionary1 = new ConcurrentObservableDictionary(); dictionary1.AddRange(itemsToAdd); @@ -588,8 +615,34 @@ IEnumerable> GetIEnumerable() Assert.IsTrue(dictionary1.Count == 0, "Not all items removed"); } + [TestMethod] + public void AddRemoveRangeReferenceTypeWithOverrideTest() + { + IEnumerable> GetIEnumerable() + { + for (int i = 0; i < 10; ++i) + { + yield return new KeyValuePair(new KeyStringWithOverride(i.ToString()), i.ToString()); + } + } + + // Don't convert to array, testing the use of reference types that override Equals and GetHashCode + var itemsToAdd = GetIEnumerable(); + var dictionary1 = new ConcurrentObservableDictionary(); + dictionary1.AddRange(itemsToAdd); + + Assert.IsTrue(dictionary1.Count == itemsToAdd.Count(), "Count doesn't match number of items added"); + + foreach (var item in itemsToAdd) + { + Assert.IsTrue(dictionary1.Remove(item.Key), "Problem removing item"); + } + + Assert.IsTrue(dictionary1.Count == 0, "Not all items removed"); + } + /// - /// Same as above test, but uses Add() instead of AddRange() + /// Uses Add() instead of AddRange() /// [TestMethod] public void AddRemoveReferenceTypeTest() @@ -622,5 +675,41 @@ IEnumerable> GetIEnumerable() Assert.IsTrue(dictionary1.Count == 0, "Not all items removed"); } + + /// + /// Uses Add() instead of AddRange() + /// + [TestMethod] + public void AddRemoveReferenceTypeWithOverrideTest() + { + // There was an issue with ConcurrentObservableDictionary.AddMany throwing an + // exception when passed an IEnumerable. + + IEnumerable> GetIEnumerable() + { + for (int i = 0; i < 10; ++i) + { + yield return new KeyValuePair(new KeyStringWithOverride(i.ToString()), i.ToString()); + } + } + + // Don't convert to array, testing the use of reference types that override Equals and GetHashCode + var itemsToAdd = GetIEnumerable(); + var dictionary1 = new ConcurrentObservableDictionary(); + // Use Add() instead of AddRange() + foreach (var item in itemsToAdd) + { + dictionary1.Add(item); + } + + Assert.IsTrue(dictionary1.Count == itemsToAdd.Count(), "Count doesn't match number of items added"); + + foreach (var item in itemsToAdd) + { + Assert.IsTrue(dictionary1.Remove(item.Key), "Problem removing item"); + } + + Assert.IsTrue(dictionary1.Count == 0, "Not all items removed"); + } } } diff --git a/ExamplesAndTests/Swordfish.NET.UnitTestV3/KeyTestClasses.cs b/ExamplesAndTests/Swordfish.NET.UnitTestV3/KeyTestClasses.cs index 045f35f..786e8f6 100644 --- a/ExamplesAndTests/Swordfish.NET.UnitTestV3/KeyTestClasses.cs +++ b/ExamplesAndTests/Swordfish.NET.UnitTestV3/KeyTestClasses.cs @@ -16,6 +16,37 @@ public KeyStringTest(string keyValue) public string KeyValue { get; } } + /// + /// A test class for creating objects to be used as Keys in Dictionary tests. + /// + public class KeyStringWithOverride + { + public KeyStringWithOverride(string keyValue) + { + KeyValue = keyValue; + } + + public override int GetHashCode() + { + return KeyValue.GetHashCode(); + } + + public override bool Equals(object obj) + { + if (obj is KeyStringWithOverride ex) + { + return KeyValue.Equals(ex.KeyValue); + } + else + { + return false; + } + + } + + public string KeyValue { get; } + } + /// /// A test class for creating objects to be used as Keys in Dictionary tests. /// @@ -27,5 +58,4 @@ public KeyIntTest(int keyValue) } public int KeyValue { get; } } - }