Removed overridable change notifications from MultiDictionary - accurately sending these would involve considerable overhead; added unit tests for all main interface methods of the MultiDictionary
git-svn-id: file:///srv/devel/repo-conversion/nusu@261 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
b37c4a757c
commit
df169e376a
4 changed files with 663 additions and 79 deletions
|
@ -119,8 +119,6 @@ namespace Nuclex.Support.Collections {
|
|||
if(values.Count == 0) {
|
||||
this.typedDictionary.Remove(itemToRemove.Key);
|
||||
}
|
||||
|
||||
OnRemoved(itemToRemove);
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
|
|
@ -206,6 +206,184 @@ namespace Nuclex.Support.Collections {
|
|||
Assert.IsFalse(dictionary.ContainsKey(20));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the key collection can be retrieved from the dictionary
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void KeyCollectionCanBeRetrieved() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
dictionary.Add(10, "ten");
|
||||
dictionary.Add(10, "zehn");
|
||||
|
||||
ICollection<int> keys = dictionary.Keys;
|
||||
Assert.IsNotNull(keys);
|
||||
Assert.AreEqual(1, keys.Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the key collection can be retrieved from the dictionary
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void ValueCollectionCanBeRetrieved() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
dictionary.Add(10, "ten");
|
||||
dictionary.Add(10, "zehn");
|
||||
dictionary.Add(20, "twenty");
|
||||
|
||||
ICollection<string> values = dictionary.Values;
|
||||
Assert.IsNotNull(values);
|
||||
Assert.AreEqual(3, values.Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that TryGetValue() returns false and doesn't throw if a key
|
||||
/// is not found in the collection
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void TryGetValueReturnsFalseOnMissingKey() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
ICollection<string> values;
|
||||
Assert.IsFalse(dictionary.TryGetValue(123, out values));
|
||||
}
|
||||
|
||||
/// <summary>Verifies that keys can be looked up via TryGetValue()</summary>
|
||||
[Test]
|
||||
public void TryGetValueCanLookUpValues() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
dictionary.Add(10, "ten");
|
||||
dictionary.Add(10, "zehn");
|
||||
ICollection<string> values;
|
||||
Assert.IsTrue(dictionary.TryGetValue(10, out values));
|
||||
Assert.AreEqual(2, values.Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that assigning null to a key deletes all the values stored
|
||||
/// under it
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void AssigningNullToKeyRemovesAllValues() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
dictionary.Add(10, "ten");
|
||||
dictionary.Add(10, "zehn");
|
||||
dictionary.Add(20, "twenty");
|
||||
|
||||
Assert.AreEqual(3, dictionary.Count);
|
||||
dictionary[10] = null;
|
||||
Assert.AreEqual(1, dictionary.Count);
|
||||
Assert.IsFalse(dictionary.ContainsKey(10));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that assigning null to a key deletes all the values stored
|
||||
/// under it
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void ValueListCanBeAssignedToNewKey() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
dictionary[3] = new List<string>() { "three", "drei" };
|
||||
|
||||
Assert.AreEqual(2, dictionary.Count);
|
||||
Assert.IsTrue(dictionary.Contains(new KeyValuePair<int, string>(3, "three")));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that assigning null to a key deletes all the values stored
|
||||
/// under it
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void ValueListCanOverwriteExistingKey() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
dictionary.Add(10, "dix");
|
||||
|
||||
Assert.AreEqual(1, dictionary.Count);
|
||||
|
||||
dictionary[10] = new List<string>() { "ten", "zehn" };
|
||||
|
||||
Assert.AreEqual(2, dictionary.Count);
|
||||
Assert.IsFalse(dictionary.Contains(new KeyValuePair<int, string>(10, "dix")));
|
||||
Assert.IsTrue(dictionary.Contains(new KeyValuePair<int, string>(10, "ten")));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that nothing bad happens when a key is removed from the dictionary
|
||||
/// that it doesn't contain
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void NonExistingKeyCanBeRemoved() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
Assert.AreEqual(0, dictionary.RemoveKey(123));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the remove method returns the number of values that have
|
||||
/// been removed from the dictionary
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void RemoveReturnsNumberOfValuesRemoved() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
dictionary.Add(10, "ten");
|
||||
dictionary.Add(10, "zehn");
|
||||
Assert.AreEqual(2, dictionary.RemoveKey(10));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the dictionary becomes empty after clearing it
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void DictionaryIsEmptyAfterClear() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
dictionary.Add(10, "ten");
|
||||
dictionary.Add(10, "zehn");
|
||||
dictionary.Add(20, "twenty");
|
||||
Assert.AreEqual(3, dictionary.Count);
|
||||
dictionary.Clear();
|
||||
Assert.AreEqual(0, dictionary.Count);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that non-existing values can be removed from the dictionary
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void NonExistingValueCanBeRemoved() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
Assert.IsFalse(dictionary.Remove(123, "test"));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that nothing bad happens when the last value under a key is removed
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void LastValueOfKeyCanBeRemoved() {
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
dictionary.Add(123, "test");
|
||||
dictionary.Remove(123, "test");
|
||||
Assert.AreEqual(0, dictionary.CountValues(123));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Verifies that the dictionary can be copied into an array
|
||||
/// </summary>
|
||||
[Test]
|
||||
public void DictionaryCanBeCopiedIntoArray() {
|
||||
var expected = new List<KeyValuePair<int, string>>() {
|
||||
new KeyValuePair<int, string>(1, "one"),
|
||||
new KeyValuePair<int, string>(1, "eins"),
|
||||
new KeyValuePair<int, string>(2, "two"),
|
||||
new KeyValuePair<int, string>(2, "zwei")
|
||||
};
|
||||
|
||||
var dictionary = new MultiDictionary<int, string>();
|
||||
foreach(KeyValuePair<int, string> entry in expected) {
|
||||
dictionary.Add(entry.Key, entry.Value);
|
||||
}
|
||||
|
||||
var actual = new KeyValuePair<int, string>[4];
|
||||
dictionary.CopyTo(actual, 0);
|
||||
|
||||
CollectionAssert.AreEquivalent(expected, actual);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
} // namespace Nuclex.Support.Collections
|
||||
|
|
|
@ -328,43 +328,17 @@ namespace Nuclex.Support.Collections {
|
|||
get { return this.typedDictionary[key]; }
|
||||
set {
|
||||
if(value == null) {
|
||||
this.typedDictionary.Remove(key);
|
||||
}
|
||||
|
||||
ICollection<TValue> currentValues;
|
||||
if(this.typedDictionary.TryGetValue(key, out currentValues)) {
|
||||
ValueList currentValueList = (ValueList)currentValues;
|
||||
|
||||
int index = 0;
|
||||
foreach(TValue addedValue in value) {
|
||||
if(index < currentValueList.Count) {
|
||||
TValue original = currentValueList[index];
|
||||
currentValueList[index] = addedValue;
|
||||
OnReplaced(
|
||||
new KeyValuePair<TKey, TValue>(key, original),
|
||||
new KeyValuePair<TKey, TValue>(key, addedValue)
|
||||
);
|
||||
} else {
|
||||
currentValueList.Add(addedValue);
|
||||
OnAdded(new KeyValuePair<TKey, TValue>(key, addedValue));
|
||||
}
|
||||
++index;
|
||||
}
|
||||
|
||||
int count = currentValueList.Count;
|
||||
while(count > index) {
|
||||
--count;
|
||||
TValue removedValue = currentValueList[count];
|
||||
currentValueList.RemoveAt(count);
|
||||
OnRemoved(new KeyValuePair<TKey, TValue>(key, removedValue));
|
||||
}
|
||||
RemoveKey(key);
|
||||
} else {
|
||||
currentValues = new ValueList(this);
|
||||
this.typedDictionary.Add(key, currentValues);
|
||||
|
||||
ICollection<TValue> currentValues;
|
||||
if(this.typedDictionary.TryGetValue(key, out currentValues)) {
|
||||
currentValues.Clear();
|
||||
} else {
|
||||
currentValues = new ValueList(this);
|
||||
this.typedDictionary.Add(key, currentValues);
|
||||
}
|
||||
foreach(TValue addedValue in value) {
|
||||
currentValues.Add(addedValue);
|
||||
OnAdded(new KeyValuePair<TKey, TValue>(key, addedValue));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -381,7 +355,6 @@ namespace Nuclex.Support.Collections {
|
|||
}
|
||||
|
||||
values.Add(value);
|
||||
OnAdded(new KeyValuePair<TKey, TValue>(key, value));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
|
@ -397,12 +370,9 @@ namespace Nuclex.Support.Collections {
|
|||
ICollection<TValue> values;
|
||||
if(this.typedDictionary.TryGetValue(key, out values)) {
|
||||
values.Remove(value);
|
||||
|
||||
if(values.Count == 0) {
|
||||
this.typedDictionary.Remove(key);
|
||||
}
|
||||
|
||||
OnRemoved(new KeyValuePair<TKey, TValue>(key, value));
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
|
@ -418,10 +388,6 @@ namespace Nuclex.Support.Collections {
|
|||
if(this.typedDictionary.TryGetValue(key, out values)) {
|
||||
this.count -= values.Count;
|
||||
this.typedDictionary.Remove(key);
|
||||
|
||||
foreach(TValue value in values) {
|
||||
OnRemoved(new KeyValuePair<TKey, TValue>(key, value));
|
||||
}
|
||||
return values.Count;
|
||||
} else {
|
||||
return 0;
|
||||
|
@ -430,33 +396,10 @@ namespace Nuclex.Support.Collections {
|
|||
|
||||
/// <summary>Removes all items from the Dictionary</summary>
|
||||
public void Clear() {
|
||||
OnClearing();
|
||||
this.typedDictionary.Clear();
|
||||
this.count = 0;
|
||||
OnCleared();
|
||||
}
|
||||
|
||||
/// <summary>Fires the 'ItemAdded' event</summary>
|
||||
/// <param name="item">Item that has been added to the collection</param>
|
||||
protected virtual void OnAdded(KeyValuePair<TKey, TValue> item) { }
|
||||
|
||||
/// <summary>Fires the 'ItemRemoved' event</summary>
|
||||
/// <param name="item">Item that has been removed from the collection</param>
|
||||
protected virtual void OnRemoved(KeyValuePair<TKey, TValue> item) { }
|
||||
|
||||
/// <summary>Fires the 'ItemReplaced' event</summary>
|
||||
/// <param name="oldItem">Item that was replaced in the collection</param>
|
||||
/// <param name="newItem">Item that the original was replaced by</param>
|
||||
protected virtual void OnReplaced(
|
||||
KeyValuePair<TKey, TValue> oldItem, KeyValuePair<TKey, TValue> newItem
|
||||
) { }
|
||||
|
||||
/// <summary>Fires the 'Clearing' event</summary>
|
||||
protected virtual void OnClearing() { }
|
||||
|
||||
/// <summary>Fires the 'Cleared' event</summary>
|
||||
protected virtual void OnCleared() { }
|
||||
|
||||
/// <summary>The wrapped Dictionary under its type-safe interface</summary>
|
||||
private IDictionary<TKey, ICollection<TValue>> typedDictionary;
|
||||
/// <summary>The wrapped Dictionary under its object interface</summary>
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue