Skip to content

Commit

Permalink
#35 Added more unit tests to try to repro the issue
Browse files Browse the repository at this point in the history
  • Loading branch information
stewienj committed Dec 17, 2024
1 parent 55d82e1 commit 147acaf
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,32 @@ public void TestIndexOfReferenceTypeInViews()
}
}

[TestMethod]
public void AddRemoveRangeValueTypeTest()
{
IEnumerable<KeyValuePair<string, string>> GetIEnumerable()
{
for (int i = 0; i < 10; ++i)
{
yield return new KeyValuePair<string, string>(i.ToString(), i.ToString());
}
}

// Don't need to convert to array because value type
var itemsToAdd = GetIEnumerable();
var dictionary1 = new ConcurrentObservableDictionary<string, string>();
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()
{
Expand All @@ -574,6 +600,7 @@ IEnumerable<KeyValuePair<KeyStringTest, string>> GetIEnumerable()
}
}

// Need to convert to array so we have the same objects being used
var itemsToAdd = GetIEnumerable().ToArray();
var dictionary1 = new ConcurrentObservableDictionary<KeyStringTest, string>();
dictionary1.AddRange(itemsToAdd);
Expand All @@ -588,8 +615,34 @@ IEnumerable<KeyValuePair<KeyStringTest, string>> GetIEnumerable()
Assert.IsTrue(dictionary1.Count == 0, "Not all items removed");
}

[TestMethod]
public void AddRemoveRangeReferenceTypeWithOverrideTest()
{
IEnumerable<KeyValuePair<KeyStringWithOverride, string>> GetIEnumerable()
{
for (int i = 0; i < 10; ++i)
{
yield return new KeyValuePair<KeyStringWithOverride, string>(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<KeyStringWithOverride, string>();
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");
}

/// <summary>
/// Same as above test, but uses Add() instead of AddRange()
/// Uses Add() instead of AddRange()
/// </summary>
[TestMethod]
public void AddRemoveReferenceTypeTest()
Expand Down Expand Up @@ -622,5 +675,41 @@ IEnumerable<KeyValuePair<KeyStringTest, string>> GetIEnumerable()

Assert.IsTrue(dictionary1.Count == 0, "Not all items removed");
}

/// <summary>
/// Uses Add() instead of AddRange()
/// </summary>
[TestMethod]
public void AddRemoveReferenceTypeWithOverrideTest()
{
// There was an issue with ConcurrentObservableDictionary.AddMany throwing an
// exception when passed an IEnumerable.

IEnumerable<KeyValuePair<KeyStringWithOverride, string>> GetIEnumerable()
{
for (int i = 0; i < 10; ++i)
{
yield return new KeyValuePair<KeyStringWithOverride, string>(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<KeyStringWithOverride, string>();
// 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");
}
}
}
32 changes: 31 additions & 1 deletion ExamplesAndTests/Swordfish.NET.UnitTestV3/KeyTestClasses.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,37 @@ public KeyStringTest(string keyValue)
public string KeyValue { get; }
}

/// <summary>
/// A test class for creating objects to be used as Keys in Dictionary tests.
/// </summary>
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; }
}

/// <summary>
/// A test class for creating objects to be used as Keys in Dictionary tests.
/// </summary>
Expand All @@ -27,5 +58,4 @@ public KeyIntTest(int keyValue)
}
public int KeyValue { get; }
}

}

0 comments on commit 147acaf

Please sign in to comment.