Added out-of-sync check for the Deque enumerator; added unit test for the out-of-sync check

git-svn-id: file:///srv/devel/repo-conversion/nusu@166 d2e56fa2-650e-0410-a79f-9358c0239efd
This commit is contained in:
Markus Ewald 2009-07-14 21:52:40 +00:00
parent a56da772d4
commit 9ec6546f70
5 changed files with 65 additions and 2 deletions

View file

@ -65,6 +65,9 @@ namespace Nuclex.Support.Collections {
/// <summary>The item at the enumerator's current position</summary>
public ItemType Current {
get {
#if DEBUG
checkVersion();
#endif
if(this.currentBlock == null) {
throw new InvalidOperationException("Enumerator is not on a valid position");
}
@ -77,6 +80,10 @@ namespace Nuclex.Support.Collections {
/// <returns>True if there was a next item</returns>
public bool MoveNext() {
#if DEBUG
checkVersion();
#endif
// If we haven't reached the last block yet
if(this.currentBlockIndex < this.lastBlock) {
@ -117,6 +124,7 @@ namespace Nuclex.Support.Collections {
this.currentBlock = null;
this.currentBlockIndex = -1;
this.subIndex = this.deque.blockSize - 1;
this.expectedVersion = this.deque.version;
}
/// <summary>The item at the enumerator's current position</summary>
@ -124,6 +132,14 @@ namespace Nuclex.Support.Collections {
get { return Current; }
}
#if DEBUG
/// <summary>Ensures that the deque has not changed</summary>
private void checkVersion() {
if(this.expectedVersion != this.deque.version)
throw new InvalidOperationException("Deque has been modified");
}
#endif
/// <summary>Deque the enumerator belongs to</summary>
private Deque<ItemType> deque;
/// <summary>Size of the blocks in the deque</summary>
@ -140,6 +156,9 @@ namespace Nuclex.Support.Collections {
/// <summary>Index in the current block</summary>
private int subIndex;
/// <summary>Version the deque is expected to have</summary>
private int expectedVersion;
}
#endregion // class Enumerator
@ -303,6 +322,10 @@ namespace Nuclex.Support.Collections {
private int firstBlockStartIndex;
/// <summary>End index of data in the last block</summary>
private int lastBlockEndIndex;
#if DEBUG
/// <summary>Used to detect when enumerators go out of sync</summary>
private int version;
#endif
}