Fixed an ugly bug in the IndexOf() method; achieved 100% test coverage for the deque code so far

git-svn-id: file:///srv/devel/repo-conversion/nusu@162 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2009-07-13 21:17:09 +00:00
parent 7885e86836
commit a817f52406
2 changed files with 73 additions and 4 deletions

View File

@ -122,7 +122,10 @@ namespace Nuclex.Support.Collections {
} }
} }
/// <summary>Verifies that the Insert() method works in all cases</summary> /// <summary>
/// Verifies that the Insert() method works in all cases when the deque doesn't
/// start at a block boundary
/// </summary>
[Test] [Test]
public void TestInsertNonNormalized() { public void TestInsertNonNormalized() {
for(int testedIndex = 0; testedIndex <= 96; ++testedIndex) { for(int testedIndex = 0; testedIndex <= 96; ++testedIndex) {
@ -171,7 +174,10 @@ namespace Nuclex.Support.Collections {
} }
} }
/// <summary>Verifies the the RemoveAt() method works in all cases</summary> /// <summary>
/// Verifies the the RemoveAt() method works in all cases when the deque doesn't
/// start at a block boundary
/// </summary>
[Test] [Test]
public void TestRemoveAtNonNormalized() { public void TestRemoveAtNonNormalized() {
for(int testedIndex = 0; testedIndex < 96; ++testedIndex) { for(int testedIndex = 0; testedIndex < 96; ++testedIndex) {
@ -197,6 +203,51 @@ namespace Nuclex.Support.Collections {
} }
} }
/// <summary>
/// Tests whether the RemoveAt() method keeps the state of the deque intact when
/// it has to remove a block from the left end of the deque
/// </summary>
[Test]
public void TestRemoveAtEmptiesLeftBlock() {
Deque<int> intDeque = new Deque<int>(16);
for(int item = 1; item <= 16; ++item) {
intDeque.AddLast(item);
}
intDeque.AddFirst(0);
intDeque.RemoveAt(3);
Assert.AreEqual(16, intDeque.Count);
for(int index = 0; index < 3; ++index) {
Assert.AreEqual(index, intDeque[index]);
}
for(int index = 3; index < 16; ++index) {
Assert.AreEqual(index + 1, intDeque[index]);
}
}
/// <summary>
/// Tests whether the RemoveAt() method keeps the state of the deque intact when
/// it has to remove a block from the right end of the deque
/// </summary>
[Test]
public void TestRemoveAtEmptiesRightBlock() {
Deque<int> intDeque = new Deque<int>(16);
for(int item = 0; item <= 16; ++item) {
intDeque.AddLast(item);
}
intDeque.RemoveAt(13);
Assert.AreEqual(16, intDeque.Count);
for(int index = 0; index < 13; ++index) {
Assert.AreEqual(index, intDeque[index]);
}
for(int index = 13; index < 16; ++index) {
Assert.AreEqual(index + 1, intDeque[index]);
}
}
/// <summary> /// <summary>
/// Validates that an exception is thrown if the 'First' property is accessed /// Validates that an exception is thrown if the 'First' property is accessed
/// in an empty deque /// in an empty deque
@ -284,6 +335,22 @@ namespace Nuclex.Support.Collections {
/// <summary>Tests the IndexOf() method</summary> /// <summary>Tests the IndexOf() method</summary>
[Test, TestCase(0), TestCase(16), TestCase(32), TestCase(48)] [Test, TestCase(0), TestCase(16), TestCase(32), TestCase(48)]
public void TestIndexOf(int count) { public void TestIndexOf(int count) {
Deque<int> intDeque = new Deque<int>(16);
for(int item = 0; item < count; ++item) {
intDeque.AddLast(item);
}
for(int item = 0; item < count; ++item) {
Assert.AreEqual(item, intDeque.IndexOf(item));
}
Assert.AreEqual(-1, intDeque.IndexOf(count));
}
/// <summary>
/// Tests the IndexOf() method with the deque not starting at a block boundary
/// </summary>
[Test, TestCase(0), TestCase(16), TestCase(32), TestCase(48)]
public void TestIndexOfNonNormalized(int count) {
Deque<int> intDeque = new Deque<int>(16); Deque<int> intDeque = new Deque<int>(16);
for(int item = 4; item < count; ++item) { for(int item = 4; item < count; ++item) {
intDeque.AddLast(item); intDeque.AddLast(item);
@ -293,7 +360,9 @@ namespace Nuclex.Support.Collections {
if(count > 1) { intDeque.AddFirst(1); } if(count > 1) { intDeque.AddFirst(1); }
if(count > 0) { intDeque.AddFirst(0); } if(count > 0) { intDeque.AddFirst(0); }
Assert.AreEqual(count - 1, intDeque.IndexOf(count - 1)); for(int item = 0; item < count; ++item) {
Assert.AreEqual(item, intDeque.IndexOf(item));
}
Assert.AreEqual(-1, intDeque.IndexOf(count)); Assert.AreEqual(-1, intDeque.IndexOf(count));
} }

View File

@ -110,7 +110,7 @@ namespace Nuclex.Support.Collections {
int lastBlock = this.blocks.Count - 1; int lastBlock = this.blocks.Count - 1;
for(int tempIndex = 1; tempIndex < lastBlock; ++tempIndex) { for(int tempIndex = 1; tempIndex < lastBlock; ++tempIndex) {
index = Array.IndexOf<ItemType>( index = Array.IndexOf<ItemType>(
this.blocks[1], item, 0, this.blockSize this.blocks[tempIndex], item, 0, this.blockSize
); );
if(index != -1) { if(index != -1) {
return (index - this.firstBlockStartIndex + tempIndex * this.blockSize); return (index - this.firstBlockStartIndex + tempIndex * this.blockSize);