/// <summary>Returns a string representation of the contents of the <see cref="Cache"/> object.</summary> /// <returns>A string representation of the contents of the cache and the number of items in the cache.</returns> public override string ToString() { StringBuilder sb = new StringBuilder(); int cacheItemCount = Purge(); if (cacheItemCount > 0) { sb.AppendFormat("Total items in cache = {0:d}, contents:", cacheItemCount); // manually iterate over enumerator because our implementation of GetEnumerator() first calls Purge() which // we've already done at this point... CacheEnumerator enumerator = new CacheEnumerator(_data.GetEnumerator()); try { while (enumerator.MoveNext()) { sb.AppendFormat("\r\n{0}", enumerator.Current); } } finally { enumerator.Dispose(); } } else { sb.Append("Cache is empty."); } return(sb.ToString()); }
public void EnumeratorDisposeTest() { var head = CreateTestList(); var enumerator = new CacheEnumerator <int, int>(head); enumerator.Dispose(); bool thrown = false; try { enumerator.MoveNext(); } catch (ObjectDisposedException) { thrown = true; } Assert.IsTrue(thrown, "Exception should have thrown on MoveNext"); thrown = false; try { var current = enumerator.Current; } catch (ObjectDisposedException) { thrown = true; } Assert.IsTrue(thrown, "Exception should have thrown on Current"); }
public void EnumeratorGetCurrent() { var head = CreateTestList(); var enumerator = new CacheEnumerator <int, int>(head); var plainEnumerator = (IEnumerator)enumerator; enumerator.MoveNext(); var current = enumerator.Current; var plainCurrent = plainEnumerator.Current; Assert.AreEqual(current, plainCurrent); }
public void EnumeratorNullList() { bool thrown = false; try { var enumerator = new CacheEnumerator <int, int>(null); } catch (ArgumentException) { thrown = true; } Assert.IsTrue(thrown, "Exception should be thrown"); }
public void EnumeratorList() { var head = CreateTestList(); var enumerator = new CacheEnumerator <int, int>(head); int index = 0; while (enumerator.MoveNext()) { var current = enumerator.Current; Assert.IsNotNull(current, "Node shouldn't be null"); Assert.AreEqual(index, current.Key, "Incorrect key"); Assert.AreEqual(index, current.Value, "Incorrect value"); ++index; } }
/// <summary>Returns a string representation of the contents of the <see cref="Cache"/> object.</summary> /// <returns>A string representation of the contents of the cache and the number of items in the cache.</returns> public override string ToString() { StringBuilder sb = new StringBuilder(); int cacheItemCount = Purge(); if (cacheItemCount > 0) { sb.AppendFormat("Total items in cache = {0:d}, contents:", cacheItemCount); // manually iterate over enumerator because our implementation of GetEnumerator() first calls Purge() which // we've already done at this point... CacheEnumerator enumerator = new CacheEnumerator(_data.GetEnumerator()); try { while (enumerator.MoveNext()) { sb.AppendFormat("\r\n{0}", enumerator.Current); } } finally { enumerator.Dispose(); } } else { sb.Append("Cache is empty."); } return sb.ToString(); }