Increased test coverage for all collection classes up to the priority queue to 100%; SetParent() is no longer 'protected internal' as internal is sufficient in this case (.NET 'protected internal' is less restrictive than 'protected' or 'internal' alone); parenting collection now unsets parent for items that are being replaced; priority queue version check for enumerators (to protected against modification of the collection) now only happens in debug mode
git-svn-id: file:///srv/devel/repo-conversion/nusu@94 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
parent
cb0355193d
commit
c43bfd47c8
|
@ -48,6 +48,9 @@
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
<Compile Include="Source\Collections\ItemEventArgs.cs" />
|
<Compile Include="Source\Collections\ItemEventArgs.cs" />
|
||||||
|
<Compile Include="Source\Collections\ItemEventArgs.Test.cs">
|
||||||
|
<DependentUpon>ItemEventArgs.cs</DependentUpon>
|
||||||
|
</Compile>
|
||||||
<Compile Include="Source\Collections\ObservableCollection.cs" />
|
<Compile Include="Source\Collections\ObservableCollection.cs" />
|
||||||
<Compile Include="Source\Collections\ObservableCollection.Test.cs">
|
<Compile Include="Source\Collections\ObservableCollection.Test.cs">
|
||||||
<DependentUpon>ObservableCollection.cs</DependentUpon>
|
<DependentUpon>ObservableCollection.cs</DependentUpon>
|
||||||
|
@ -57,8 +60,17 @@
|
||||||
<DependentUpon>PairPriorityQueue.cs</DependentUpon>
|
<DependentUpon>PairPriorityQueue.cs</DependentUpon>
|
||||||
</Compile>
|
</Compile>
|
||||||
<Compile Include="Source\Collections\Parentable.cs" />
|
<Compile Include="Source\Collections\Parentable.cs" />
|
||||||
|
<Compile Include="Source\Collections\Parentable.Test.cs">
|
||||||
|
<DependentUpon>Parentable.cs</DependentUpon>
|
||||||
|
</Compile>
|
||||||
<Compile Include="Source\Collections\ParentingCollection.cs" />
|
<Compile Include="Source\Collections\ParentingCollection.cs" />
|
||||||
|
<Compile Include="Source\Collections\ParentingCollection.Test.cs">
|
||||||
|
<DependentUpon>ParentingCollection.cs</DependentUpon>
|
||||||
|
</Compile>
|
||||||
<Compile Include="Source\Collections\PriorityItemPair.cs" />
|
<Compile Include="Source\Collections\PriorityItemPair.cs" />
|
||||||
|
<Compile Include="Source\Collections\PriorityItemPair.Test.cs">
|
||||||
|
<DependentUpon>PriorityItemPair.cs</DependentUpon>
|
||||||
|
</Compile>
|
||||||
<Compile Include="Source\Collections\PriorityQueue.cs" />
|
<Compile Include="Source\Collections\PriorityQueue.cs" />
|
||||||
<Compile Include="Source\Collections\PriorityQueue.Test.cs">
|
<Compile Include="Source\Collections\PriorityQueue.Test.cs">
|
||||||
<DependentUpon>PriorityQueue.cs</DependentUpon>
|
<DependentUpon>PriorityQueue.cs</DependentUpon>
|
||||||
|
|
57
Source/Collections/ItemEventArgs.Test.cs
Normal file
57
Source/Collections/ItemEventArgs.Test.cs
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
#region CPL License
|
||||||
|
/*
|
||||||
|
Nuclex Framework
|
||||||
|
Copyright (C) 2002-2008 Nuclex Development Labs
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the IBM Common Public License as
|
||||||
|
published by the IBM Corporation; either version 1.0 of the
|
||||||
|
License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
IBM Common Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the IBM Common Public
|
||||||
|
License along with this library
|
||||||
|
*/
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
#if UNITTEST
|
||||||
|
|
||||||
|
using NUnit.Framework;
|
||||||
|
using NMock2;
|
||||||
|
|
||||||
|
namespace Nuclex.Support.Collections {
|
||||||
|
|
||||||
|
/// <summary>Unit Test for the item event argument container</summary>
|
||||||
|
[TestFixture]
|
||||||
|
public class ItemEventArgsTest {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests whether an integer argument can be stored in the argument container
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestIntegerArgument() {
|
||||||
|
ItemEventArgs<int> test = new ItemEventArgs<int>(12345);
|
||||||
|
Assert.AreEqual(12345, test.Item);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests whether a string argument can be stored in the argument container
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestStringArgument() {
|
||||||
|
ItemEventArgs<string> test = new ItemEventArgs<string>("hello world");
|
||||||
|
Assert.AreEqual("hello world", test.Item);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Nuclex.Support.Collections
|
||||||
|
|
||||||
|
#endif // UNITTEST
|
|
@ -118,6 +118,49 @@ namespace Nuclex.Support.Collections {
|
||||||
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>Tests whether items in the collection can be replaced</summary>
|
||||||
|
[Test]
|
||||||
|
public void TestItemReplacement() {
|
||||||
|
Expect.Exactly(3).On(this.mockedSubscriber).
|
||||||
|
Method("ItemAdded").
|
||||||
|
WithAnyArguments();
|
||||||
|
|
||||||
|
this.observedCollection.Add(1);
|
||||||
|
this.observedCollection.Add(2);
|
||||||
|
this.observedCollection.Add(3);
|
||||||
|
|
||||||
|
Expect.Once.On(this.mockedSubscriber).
|
||||||
|
Method("ItemRemoved").
|
||||||
|
WithAnyArguments();
|
||||||
|
|
||||||
|
Expect.Once.On(this.mockedSubscriber).
|
||||||
|
Method("ItemAdded").
|
||||||
|
WithAnyArguments();
|
||||||
|
|
||||||
|
// Replace the middle item with something else
|
||||||
|
this.observedCollection[1] = 4;
|
||||||
|
|
||||||
|
Assert.AreEqual(
|
||||||
|
1,
|
||||||
|
this.observedCollection.IndexOf(4)
|
||||||
|
);
|
||||||
|
|
||||||
|
this.mockery.VerifyAllExpectationsHaveBeenMet();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Tests whether the ItemRemoved event is fired</summary>
|
||||||
|
[Test]
|
||||||
|
public void TestListConstructor() {
|
||||||
|
int[] integers = new int[] { 12, 34, 56, 78 };
|
||||||
|
|
||||||
|
ObservableCollection<int> testCollection = new ObservableCollection<int>(integers);
|
||||||
|
|
||||||
|
CollectionAssert.AreEqual(
|
||||||
|
integers,
|
||||||
|
testCollection
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>Mock object factory</summary>
|
/// <summary>Mock object factory</summary>
|
||||||
private Mockery mockery;
|
private Mockery mockery;
|
||||||
/// <summary>The mocked observable collection subscriber</summary>
|
/// <summary>The mocked observable collection subscriber</summary>
|
||||||
|
|
|
@ -34,8 +34,7 @@ namespace Nuclex.Support.Collections {
|
||||||
/// <summary>Tests to ensure the count property is properly updated</summary>
|
/// <summary>Tests to ensure the count property is properly updated</summary>
|
||||||
[Test]
|
[Test]
|
||||||
public void TestCount() {
|
public void TestCount() {
|
||||||
PairPriorityQueue<float, string> testQueue =
|
PairPriorityQueue<float, string> testQueue = new PairPriorityQueue<float, string>();
|
||||||
new PairPriorityQueue<float, string>();
|
|
||||||
|
|
||||||
Assert.AreEqual(0, testQueue.Count);
|
Assert.AreEqual(0, testQueue.Count);
|
||||||
testQueue.Enqueue(12.34f, "a");
|
testQueue.Enqueue(12.34f, "a");
|
||||||
|
@ -53,8 +52,7 @@ namespace Nuclex.Support.Collections {
|
||||||
/// <summary>Tests to ensure that the priority collection actually sorts items</summary>
|
/// <summary>Tests to ensure that the priority collection actually sorts items</summary>
|
||||||
[Test]
|
[Test]
|
||||||
public void TestOrdering() {
|
public void TestOrdering() {
|
||||||
PairPriorityQueue<float, string> testQueue =
|
PairPriorityQueue<float, string> testQueue = new PairPriorityQueue<float, string>();
|
||||||
new PairPriorityQueue<float, string>();
|
|
||||||
|
|
||||||
testQueue.Enqueue(1.0f, "a");
|
testQueue.Enqueue(1.0f, "a");
|
||||||
testQueue.Enqueue(9.0f, "i");
|
testQueue.Enqueue(9.0f, "i");
|
||||||
|
@ -77,6 +75,75 @@ namespace Nuclex.Support.Collections {
|
||||||
Assert.AreEqual("a", testQueue.Dequeue().Item);
|
Assert.AreEqual("a", testQueue.Dequeue().Item);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// <summary>Tests to ensure that the priority collection's Peek() method works</summary>
|
||||||
|
[Test]
|
||||||
|
public void TestPeek() {
|
||||||
|
PairPriorityQueue<float, string> testQueue = new PairPriorityQueue<float, string>();
|
||||||
|
|
||||||
|
testQueue.Enqueue(1.0f, "a");
|
||||||
|
testQueue.Enqueue(2.0f, "b");
|
||||||
|
testQueue.Enqueue(0.0f, "c");
|
||||||
|
|
||||||
|
Assert.AreEqual("b", testQueue.Peek().Item);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Tests whether the priority collection can copy itself into an array</summary>
|
||||||
|
[Test]
|
||||||
|
public void TestCopyTo() {
|
||||||
|
PairPriorityQueue<float, string> testQueue = new PairPriorityQueue<float, string>();
|
||||||
|
|
||||||
|
testQueue.Enqueue(1.0f, "a");
|
||||||
|
testQueue.Enqueue(9.0f, "i");
|
||||||
|
testQueue.Enqueue(2.0f, "b");
|
||||||
|
testQueue.Enqueue(8.0f, "h");
|
||||||
|
testQueue.Enqueue(3.0f, "c");
|
||||||
|
testQueue.Enqueue(7.0f, "g");
|
||||||
|
testQueue.Enqueue(4.0f, "d");
|
||||||
|
testQueue.Enqueue(6.0f, "f");
|
||||||
|
testQueue.Enqueue(5.0f, "e");
|
||||||
|
|
||||||
|
PriorityItemPair<float, string>[] itemArray = new PriorityItemPair<float, string>[9];
|
||||||
|
testQueue.CopyTo(itemArray, 0);
|
||||||
|
|
||||||
|
CollectionAssert.AreEquivalent(testQueue, itemArray);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests whether the priority collection provides a synchronization root
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestSyncRoot() {
|
||||||
|
PairPriorityQueue<int, int> testQueue = new PairPriorityQueue<int, int>();
|
||||||
|
|
||||||
|
// If IsSynchronized returns true, SyncRoot is allowed to be null
|
||||||
|
if(!testQueue.IsSynchronized) {
|
||||||
|
lock(testQueue.SyncRoot) {
|
||||||
|
testQueue.Clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests whether the priority collection provides a working type-safe enumerator
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestEnumerator() {
|
||||||
|
PairPriorityQueue<float, string> testQueue = new PairPriorityQueue<float, string>();
|
||||||
|
|
||||||
|
testQueue.Enqueue(1.0f, "a");
|
||||||
|
testQueue.Enqueue(2.0f, "b");
|
||||||
|
testQueue.Enqueue(0.0f, "c");
|
||||||
|
|
||||||
|
List<PriorityItemPair<float, string>> testList =
|
||||||
|
new List<PriorityItemPair<float,string>>();
|
||||||
|
|
||||||
|
foreach(PriorityItemPair<float, string> entry in testQueue) {
|
||||||
|
testList.Add(entry);
|
||||||
|
}
|
||||||
|
|
||||||
|
CollectionAssert.AreEquivalent(testQueue, testList);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Nuclex.Support.Collections
|
} // namespace Nuclex.Support.Collections
|
||||||
|
|
102
Source/Collections/Parentable.Test.cs
Normal file
102
Source/Collections/Parentable.Test.cs
Normal file
|
@ -0,0 +1,102 @@
|
||||||
|
#region CPL License
|
||||||
|
/*
|
||||||
|
Nuclex Framework
|
||||||
|
Copyright (C) 2002-2008 Nuclex Development Labs
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the IBM Common Public License as
|
||||||
|
published by the IBM Corporation; either version 1.0 of the
|
||||||
|
License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
IBM Common Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the IBM Common Public
|
||||||
|
License along with this library
|
||||||
|
*/
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
#if UNITTEST
|
||||||
|
|
||||||
|
using NUnit.Framework;
|
||||||
|
using NMock2;
|
||||||
|
|
||||||
|
namespace Nuclex.Support.Collections {
|
||||||
|
|
||||||
|
/// <summary>Unit Test for the Parentable class</summary>
|
||||||
|
[TestFixture]
|
||||||
|
public class ParentableTest {
|
||||||
|
|
||||||
|
#region class TestParentable
|
||||||
|
|
||||||
|
/// <summary>Parentable object that can be the child of an int</summary>
|
||||||
|
private class TestParentable : Parentable<int> {
|
||||||
|
|
||||||
|
/// <summary>Initializes a new instance of the parentable test class</summary>
|
||||||
|
public TestParentable() { }
|
||||||
|
|
||||||
|
/// <summary>The parent object that owns this instance</summary>
|
||||||
|
public int GetParent() {
|
||||||
|
return base.Parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Invoked whenever the instance's owner changes</summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// When items are parented for the first time, the oldParent argument will
|
||||||
|
/// be null. Also, if the element is removed from the collection, the
|
||||||
|
/// current parent will be null.
|
||||||
|
/// </remarks>
|
||||||
|
/// <param name="oldParent">Previous owner of the instance</param>
|
||||||
|
protected override void OnParentChanged(int oldParent) {
|
||||||
|
this.parentChangedCalled = true;
|
||||||
|
|
||||||
|
base.OnParentChanged(oldParent); // to satisfy NCover :-/
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Whether the OnParentChanged method has been called</summary>
|
||||||
|
public bool ParentChangedCalled {
|
||||||
|
get { return this.parentChangedCalled; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Whether the OnParentChanged method has been called</summary>
|
||||||
|
private bool parentChangedCalled;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion // class TestParentable
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests whether a parent can be assigned and then retrieved from
|
||||||
|
/// the parentable object
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestParentAssignment() {
|
||||||
|
TestParentable testParentable = new TestParentable();
|
||||||
|
|
||||||
|
testParentable.SetParent(12345);
|
||||||
|
Assert.AreEqual(12345, testParentable.GetParent());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests whether a parent can be assigned and then retrieved from
|
||||||
|
/// the parentable object
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestParentChangedNotification() {
|
||||||
|
TestParentable testParentable = new TestParentable();
|
||||||
|
|
||||||
|
testParentable.SetParent(12345);
|
||||||
|
|
||||||
|
Assert.IsTrue(testParentable.ParentChangedCalled);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Nuclex.Support.Collections
|
||||||
|
|
||||||
|
#endif // UNITTEST
|
|
@ -42,7 +42,7 @@ namespace Nuclex.Support.Collections {
|
||||||
protected virtual void OnParentChanged(ParentType oldParent) { }
|
protected virtual void OnParentChanged(ParentType oldParent) { }
|
||||||
|
|
||||||
/// <summary>Assigns a new parent to this instance</summary>
|
/// <summary>Assigns a new parent to this instance</summary>
|
||||||
protected internal void SetParent(ParentType parent) {
|
internal void SetParent(ParentType parent) {
|
||||||
ParentType oldParent = this.parent;
|
ParentType oldParent = this.parent;
|
||||||
this.parent = parent;
|
this.parent = parent;
|
||||||
|
|
||||||
|
|
191
Source/Collections/ParentingCollection.Test.cs
Normal file
191
Source/Collections/ParentingCollection.Test.cs
Normal file
|
@ -0,0 +1,191 @@
|
||||||
|
#region CPL License
|
||||||
|
/*
|
||||||
|
Nuclex Framework
|
||||||
|
Copyright (C) 2002-2008 Nuclex Development Labs
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the IBM Common Public License as
|
||||||
|
published by the IBM Corporation; either version 1.0 of the
|
||||||
|
License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
IBM Common Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the IBM Common Public
|
||||||
|
License along with this library
|
||||||
|
*/
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
#if UNITTEST
|
||||||
|
|
||||||
|
using NUnit.Framework;
|
||||||
|
using NMock2;
|
||||||
|
|
||||||
|
namespace Nuclex.Support.Collections {
|
||||||
|
|
||||||
|
/// <summary>Unit Test for the Parenting Collection class</summary>
|
||||||
|
[TestFixture]
|
||||||
|
public class ParentingCollectionTest {
|
||||||
|
|
||||||
|
#region class TestParentable
|
||||||
|
|
||||||
|
/// <summary>Parentable object that can be the child of an int</summary>
|
||||||
|
private class TestParentable : Parentable<int>, IDisposable {
|
||||||
|
|
||||||
|
/// <summary>Initializes a new instance of the parentable test class</summary>
|
||||||
|
public TestParentable() { }
|
||||||
|
|
||||||
|
/// <summary>The parent object that owns this instance</summary>
|
||||||
|
public int GetParent() {
|
||||||
|
return base.Parent;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Immediately releases all resources owned by the item</summary>
|
||||||
|
public void Dispose() {
|
||||||
|
this.disposeCalled = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Whether Dispose() has been called on this item</summary>
|
||||||
|
public bool DisposeCalled {
|
||||||
|
get { return this.disposeCalled; }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Whether Dispose() has been called on this item</summary>
|
||||||
|
private bool disposeCalled;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion // class TestParentable
|
||||||
|
|
||||||
|
#region class TestParentingCollection
|
||||||
|
|
||||||
|
/// <summary>Parentable object that can be the child of an int</summary>
|
||||||
|
private class TestParentingCollection : ParentingCollection<int, TestParentable> {
|
||||||
|
|
||||||
|
/// <summary>Changes the parent of the collection</summary>
|
||||||
|
/// <param name="parent">New parent to assign to the collection</param>
|
||||||
|
public void SetParent(int parent) {
|
||||||
|
base.Reparent(parent);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Disposes all items contained in the collection</summary>
|
||||||
|
public new void DisposeItems() {
|
||||||
|
base.DisposeItems();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion // class TestParentingCollection
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests whether the parenting collection propagates its parent to an item that
|
||||||
|
/// is added to the collection after the collection's aprent is already assigned
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestPropagatePreassignedParent() {
|
||||||
|
TestParentingCollection testCollection = new TestParentingCollection();
|
||||||
|
TestParentable testParentable = new TestParentable();
|
||||||
|
|
||||||
|
testCollection.SetParent(54321);
|
||||||
|
testCollection.Add(testParentable);
|
||||||
|
|
||||||
|
Assert.AreEqual(54321, testParentable.GetParent());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests whether the parenting collection propagates a new parent to all items
|
||||||
|
/// contained in it when its parent is changed
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestPropagateParentChange() {
|
||||||
|
TestParentingCollection testCollection = new TestParentingCollection();
|
||||||
|
TestParentable testParentable = new TestParentable();
|
||||||
|
|
||||||
|
testCollection.Add(testParentable);
|
||||||
|
testCollection.SetParent(54321);
|
||||||
|
|
||||||
|
Assert.AreEqual(54321, testParentable.GetParent());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests whether the parenting collection propagates its parent to an item that
|
||||||
|
/// is added to the collection after the collection's aprent is already assigned
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestPropagateParentOnReplace() {
|
||||||
|
TestParentingCollection testCollection = new TestParentingCollection();
|
||||||
|
TestParentable testParentable1 = new TestParentable();
|
||||||
|
TestParentable testParentable2 = new TestParentable();
|
||||||
|
|
||||||
|
testCollection.SetParent(54321);
|
||||||
|
testCollection.Add(testParentable1);
|
||||||
|
testCollection[0] = testParentable2;
|
||||||
|
|
||||||
|
Assert.AreEqual(0, testParentable1.GetParent());
|
||||||
|
Assert.AreEqual(54321, testParentable2.GetParent());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests whether the parenting collection unsets the parent when an item is removed
|
||||||
|
/// from the collection
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestUnsetParentOnRemoveItem() {
|
||||||
|
TestParentingCollection testCollection = new TestParentingCollection();
|
||||||
|
TestParentable testParentable = new TestParentable();
|
||||||
|
|
||||||
|
testCollection.Add(testParentable);
|
||||||
|
testCollection.SetParent(54321);
|
||||||
|
|
||||||
|
Assert.AreEqual(54321, testParentable.GetParent());
|
||||||
|
|
||||||
|
testCollection.RemoveAt(0);
|
||||||
|
|
||||||
|
Assert.AreEqual(0, testParentable.GetParent());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests whether the parenting collection unsets the parent when all item are
|
||||||
|
/// removed from the collection by clearing it
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestUnsetParentOnClear() {
|
||||||
|
TestParentingCollection testCollection = new TestParentingCollection();
|
||||||
|
TestParentable testParentable = new TestParentable();
|
||||||
|
|
||||||
|
testCollection.Add(testParentable);
|
||||||
|
testCollection.SetParent(54321);
|
||||||
|
|
||||||
|
Assert.AreEqual(54321, testParentable.GetParent());
|
||||||
|
|
||||||
|
testCollection.Clear();
|
||||||
|
|
||||||
|
Assert.AreEqual(0, testParentable.GetParent());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Tests whether the parenting collection calls Dispose() on all contained items
|
||||||
|
/// that implement IDisposable when its DisposeItems() method is called
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestDisposeItems() {
|
||||||
|
TestParentingCollection testCollection = new TestParentingCollection();
|
||||||
|
TestParentable testParentable = new TestParentable();
|
||||||
|
|
||||||
|
testCollection.Add(testParentable);
|
||||||
|
|
||||||
|
testCollection.DisposeItems();
|
||||||
|
|
||||||
|
Assert.IsTrue(testParentable.DisposeCalled);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Nuclex.Support.Collections
|
||||||
|
|
||||||
|
#endif // UNITTEST
|
|
@ -73,6 +73,7 @@ namespace Nuclex.Support.Collections {
|
||||||
/// <param name="index">Index of the element that was assigned</param>
|
/// <param name="index">Index of the element that was assigned</param>
|
||||||
/// <param name="item">New item</param>
|
/// <param name="item">New item</param>
|
||||||
protected override void SetItem(int index, ItemType item) {
|
protected override void SetItem(int index, ItemType item) {
|
||||||
|
base[index].SetParent(default(ParentType));
|
||||||
base.SetItem(index, item);
|
base.SetItem(index, item);
|
||||||
item.SetParent(this.parent);
|
item.SetParent(this.parent);
|
||||||
}
|
}
|
||||||
|
|
101
Source/Collections/PriorityItemPair.Test.cs
Normal file
101
Source/Collections/PriorityItemPair.Test.cs
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
#region CPL License
|
||||||
|
/*
|
||||||
|
Nuclex Framework
|
||||||
|
Copyright (C) 2002-2008 Nuclex Development Labs
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the IBM Common Public License as
|
||||||
|
published by the IBM Corporation; either version 1.0 of the
|
||||||
|
License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
IBM Common Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the IBM Common Public
|
||||||
|
License along with this library
|
||||||
|
*/
|
||||||
|
#endregion
|
||||||
|
|
||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
|
#if UNITTEST
|
||||||
|
|
||||||
|
using NUnit.Framework;
|
||||||
|
using NMock2;
|
||||||
|
|
||||||
|
namespace Nuclex.Support.Collections {
|
||||||
|
|
||||||
|
/// <summary>Unit Test for the Priority/Item pair class</summary>
|
||||||
|
[TestFixture]
|
||||||
|
public class PriorityItemPairTest {
|
||||||
|
|
||||||
|
#region class ToStringNullReturner
|
||||||
|
|
||||||
|
/// <summary>Test class in which ToString() can return null</summary>
|
||||||
|
private class ToStringNullReturner {
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Returns a System.String that represents the current System.Object
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>A System.String that represents the current System.Object</returns>
|
||||||
|
public override string ToString() { return null; }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endregion // class ToStringNullReturner
|
||||||
|
|
||||||
|
/// <summary>Tests whether the pair's default constructor works</summary>
|
||||||
|
[Test]
|
||||||
|
public void TestDefaultConstructor() {
|
||||||
|
new PriorityItemPair<int, string>();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Tests whether the priority can be retrieved from the pair</summary>
|
||||||
|
[Test]
|
||||||
|
public void TestPriorityRetrieval() {
|
||||||
|
PriorityItemPair<int, string> testPair = new PriorityItemPair<int, string>(
|
||||||
|
12345, "hello world"
|
||||||
|
);
|
||||||
|
|
||||||
|
Assert.AreEqual(12345, testPair.Priority);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Tests whether the item can be retrieved from the pair</summary>
|
||||||
|
[Test]
|
||||||
|
public void TestItemRetrieval() {
|
||||||
|
PriorityItemPair<int, string> testPair = new PriorityItemPair<int, string>(
|
||||||
|
12345, "hello world"
|
||||||
|
);
|
||||||
|
|
||||||
|
Assert.AreEqual("hello world", testPair.Item);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Tests whether the ToString() methods works with valid strings</summary>
|
||||||
|
[Test]
|
||||||
|
public void TestToStringWithValidStrings() {
|
||||||
|
PriorityItemPair<string, string> testPair = new PriorityItemPair<string, string>(
|
||||||
|
"hello", "world"
|
||||||
|
);
|
||||||
|
|
||||||
|
Assert.AreEqual("[hello, world]", testPair.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>Tests whether the ToString() methods works with null strings</summary>
|
||||||
|
[Test]
|
||||||
|
public void TestToStringWithNullStrings() {
|
||||||
|
PriorityItemPair<ToStringNullReturner, ToStringNullReturner> testPair =
|
||||||
|
new PriorityItemPair<ToStringNullReturner, ToStringNullReturner>(
|
||||||
|
new ToStringNullReturner(), new ToStringNullReturner()
|
||||||
|
);
|
||||||
|
|
||||||
|
Assert.AreEqual("[, ]", testPair.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace Nuclex.Support.Collections
|
||||||
|
|
||||||
|
#endif // UNITTEST
|
|
@ -31,6 +31,8 @@ namespace Nuclex.Support.Collections {
|
||||||
[TestFixture]
|
[TestFixture]
|
||||||
public class PriorityQueueTest {
|
public class PriorityQueueTest {
|
||||||
|
|
||||||
|
#region class FloatComparer
|
||||||
|
|
||||||
/// <summary>Comparer for two floating point values</summary>
|
/// <summary>Comparer for two floating point values</summary>
|
||||||
private class FloatComparer : IComparer<float> {
|
private class FloatComparer : IComparer<float> {
|
||||||
|
|
||||||
|
@ -47,6 +49,8 @@ namespace Nuclex.Support.Collections {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#endregion // class FloatComparer
|
||||||
|
|
||||||
/// <summary>Tests to ensure the count property is properly updated</summary>
|
/// <summary>Tests to ensure the count property is properly updated</summary>
|
||||||
[Test]
|
[Test]
|
||||||
public void TestCount() {
|
public void TestCount() {
|
||||||
|
@ -91,6 +95,56 @@ namespace Nuclex.Support.Collections {
|
||||||
Assert.AreEqual(1.0f, testQueue.Dequeue());
|
Assert.AreEqual(1.0f, testQueue.Dequeue());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
|
/// <summary>
|
||||||
|
/// Tests whether the priority queue's enumerators are invalidated when the queue's
|
||||||
|
/// contents are modified
|
||||||
|
/// </summary>
|
||||||
|
[Test, ExpectedException(typeof(InvalidOperationException))]
|
||||||
|
public void TestEnumeratorInvalidationOnModify() {
|
||||||
|
PriorityQueue<int> testQueue = new PriorityQueue<int>();
|
||||||
|
IEnumerator<int> testQueueEnumerator = testQueue.GetEnumerator();
|
||||||
|
|
||||||
|
testQueue.Enqueue(123);
|
||||||
|
|
||||||
|
testQueueEnumerator.MoveNext();
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that an exception is thrown when Peek() is called on an empty queue
|
||||||
|
/// </summary>
|
||||||
|
[Test, ExpectedException(typeof(InvalidOperationException))]
|
||||||
|
public void TestPeekEmptyQueue() {
|
||||||
|
PriorityQueue<int> testQueue = new PriorityQueue<int>();
|
||||||
|
testQueue.Peek();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that an exception is thrown when Dequeue() is called on an empty queue
|
||||||
|
/// </summary>
|
||||||
|
[Test, ExpectedException(typeof(InvalidOperationException))]
|
||||||
|
public void TestDequeueEmptyQueue() {
|
||||||
|
PriorityQueue<int> testQueue = new PriorityQueue<int>();
|
||||||
|
testQueue.Dequeue();
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Verifies that the priority queue can handle large amounts of data
|
||||||
|
/// </summary>
|
||||||
|
[Test]
|
||||||
|
public void TestLargeQueue() {
|
||||||
|
PriorityQueue<int> testQueue = new PriorityQueue<int>();
|
||||||
|
List<int> testList = new List<int>();
|
||||||
|
|
||||||
|
for(int index = 0; index < 1000; ++index) {
|
||||||
|
testQueue.Enqueue(index * 2);
|
||||||
|
testList.Add(index * 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
CollectionAssert.AreEquivalent(testList, testQueue);
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Nuclex.Support.Collections
|
} // namespace Nuclex.Support.Collections
|
||||||
|
|
|
@ -41,27 +41,32 @@ namespace Nuclex.Support.Collections {
|
||||||
|
|
||||||
/// <summary>Resets the enumerator to its initial state</summary>
|
/// <summary>Resets the enumerator to its initial state</summary>
|
||||||
public void Reset() {
|
public void Reset() {
|
||||||
index = -1;
|
this.index = -1;
|
||||||
version = priorityQueue.version;
|
#if DEBUG
|
||||||
|
this.expectedVersion = priorityQueue.version;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>The current item being enumerated</summary>
|
/// <summary>The current item being enumerated</summary>
|
||||||
ItemType IEnumerator<ItemType>.Current {
|
ItemType IEnumerator<ItemType>.Current {
|
||||||
get {
|
get {
|
||||||
|
#if DEBUG
|
||||||
checkVersion();
|
checkVersion();
|
||||||
return priorityQueue.heap[index];
|
#endif
|
||||||
|
return this.priorityQueue.heap[index];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Moves to the next item in the priority queue</summary>
|
/// <summary>Moves to the next item in the priority queue</summary>
|
||||||
/// <returns>True if a next item was found, false if the end has been reached</returns>
|
/// <returns>True if a next item was found, false if the end has been reached</returns>
|
||||||
public bool MoveNext() {
|
public bool MoveNext() {
|
||||||
|
#if DEBUG
|
||||||
checkVersion();
|
checkVersion();
|
||||||
|
#endif
|
||||||
if(index + 1 == priorityQueue.count)
|
if(this.index + 1 == this.priorityQueue.count)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
++index;
|
++this.index;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
@ -69,17 +74,21 @@ namespace Nuclex.Support.Collections {
|
||||||
/// <summary>Releases all resources used by the enumerator</summary>
|
/// <summary>Releases all resources used by the enumerator</summary>
|
||||||
public void Dispose() { }
|
public void Dispose() { }
|
||||||
|
|
||||||
|
#if DEBUG
|
||||||
/// <summary>Ensures that the priority queue has not changed</summary>
|
/// <summary>Ensures that the priority queue has not changed</summary>
|
||||||
private void checkVersion() {
|
private void checkVersion() {
|
||||||
if(version != priorityQueue.version)
|
if(this.expectedVersion != this.priorityQueue.version)
|
||||||
throw new InvalidOperationException("Priority queue has been modified");
|
throw new InvalidOperationException("Priority queue has been modified");
|
||||||
}
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
/// <summary>The current item being enumerated</summary>
|
/// <summary>The current item being enumerated</summary>
|
||||||
object IEnumerator.Current {
|
object IEnumerator.Current {
|
||||||
get {
|
get {
|
||||||
|
#if DEBUG
|
||||||
checkVersion();
|
checkVersion();
|
||||||
return priorityQueue.heap[index];
|
#endif
|
||||||
|
return this.priorityQueue.heap[index];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -87,8 +96,10 @@ namespace Nuclex.Support.Collections {
|
||||||
private int index;
|
private int index;
|
||||||
/// <summary>The priority queue whose items this instance enumerates</summary>
|
/// <summary>The priority queue whose items this instance enumerates</summary>
|
||||||
private PriorityQueue<ItemType> priorityQueue;
|
private PriorityQueue<ItemType> priorityQueue;
|
||||||
|
#if DEBUG
|
||||||
/// <summary>Expected version of the priority queue</summary>
|
/// <summary>Expected version of the priority queue</summary>
|
||||||
private int version;
|
private int expectedVersion;
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -128,9 +139,9 @@ namespace Nuclex.Support.Collections {
|
||||||
ItemType result = this.heap[0];
|
ItemType result = this.heap[0];
|
||||||
--this.count;
|
--this.count;
|
||||||
trickleDown(0, this.heap[this.count]);
|
trickleDown(0, this.heap[this.count]);
|
||||||
|
#if DEBUG
|
||||||
++this.version;
|
++this.version;
|
||||||
|
#endif
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -142,13 +153,17 @@ namespace Nuclex.Support.Collections {
|
||||||
|
|
||||||
++this.count;
|
++this.count;
|
||||||
bubbleUp(this.count - 1, item);
|
bubbleUp(this.count - 1, item);
|
||||||
|
#if DEBUG
|
||||||
++this.version;
|
++this.version;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>Removes all items from the priority queue</summary>
|
/// <summary>Removes all items from the priority queue</summary>
|
||||||
public void Clear() {
|
public void Clear() {
|
||||||
this.count = 0;
|
this.count = 0;
|
||||||
|
#if DEBUG
|
||||||
++this.version;
|
++this.version;
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -258,10 +273,12 @@ namespace Nuclex.Support.Collections {
|
||||||
private int count;
|
private int count;
|
||||||
/// <summary>Available space in the priority queue</summary>
|
/// <summary>Available space in the priority queue</summary>
|
||||||
private int capacity;
|
private int capacity;
|
||||||
/// <summary>Incremented whenever the priority queue is modified</summary>
|
|
||||||
private int version;
|
|
||||||
/// <summary>Tree containing the items in the priority queue</summary>
|
/// <summary>Tree containing the items in the priority queue</summary>
|
||||||
private ItemType[] heap;
|
private ItemType[] heap;
|
||||||
|
#if DEBUG
|
||||||
|
/// <summary>Incremented whenever the priority queue is modified</summary>
|
||||||
|
private int version;
|
||||||
|
#endif
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user