public void TestCacheStates() { var generator = getGuidsGenerator(); assertIsNotACachingState(CachingGenerator.GetCachingGeneratorState(generator)); var cachingGenerator = CachingGenerator.Create(generator); var getCachingState = (Func <CachingGenerator.State>)(() => CachingGenerator.GetCachingGeneratorState(cachingGenerator)); assertIsEmptyCachingState(getCachingState()); var firstEnumerator = cachingGenerator.GetEnumerator(); var secondEnumerator = cachingGenerator.GetEnumerator(); firstEnumerator.MoveNext(); assertIsValidCachingState(1, getCachingState()); secondEnumerator.MoveNext(); assertIsValidCachingState(1, getCachingState()); firstEnumerator.MoveNext(); assertIsValidCachingState(2, getCachingState()); firstEnumerator.Reset(); assertIsValidCachingState(2, getCachingState()); }
public void SimultaneousIteration() { var generator = getGuidsGenerator(); var cachingGenerator = CachingGenerator.Create(generator); var firstEnumerator = cachingGenerator.GetEnumerator(); var secondEnumerator = cachingGenerator.GetEnumerator(); List <Guid> lst1 = new List <Guid>(); List <Guid> lst2 = new List <Guid>(); firstEnumerator.MoveNext(); lst1.Add(firstEnumerator.Current); firstEnumerator.MoveNext(); lst1.Add(firstEnumerator.Current); secondEnumerator.MoveNext(); lst2.Add(secondEnumerator.Current); firstEnumerator.MoveNext(); lst1.Add(firstEnumerator.Current); secondEnumerator.MoveNext(); lst2.Add(secondEnumerator.Current); secondEnumerator.MoveNext(); lst2.Add(secondEnumerator.Current); CollectionAssert.AreEqual(lst1, lst2); assertIsValidCachingState(3, CachingGenerator.GetCachingGeneratorState(cachingGenerator)); }
public void NewEnumeratorAtEachRequest() { var cachingGenerator = CachingGenerator.Create(new List <string> { "qwe", "wer", "ert" }); var enumerator1 = cachingGenerator.GetEnumerator(); var enumerator2 = cachingGenerator.GetEnumerator(); Assert.AreNotSame(enumerator1, enumerator2); }
public void SameValuesAfterMultipleEnumerations() { var generator = getGuidsGenerator(); var cachingGenerator = CachingGenerator.Create(generator); var firstEnumeration = cachingGenerator.ToArray(); var secongEnumeration = cachingGenerator.ToArray(); CollectionAssert.AreEqual(firstEnumeration, secongEnumeration); }
public static void Main() { CachingGenerator cached = new CachingGenerator(); IGenerator uncached = new NonCachingGenerator(); Program.UseGenerator(cached); Program.UseGenerator(uncached); Program.CheckCache(cached, 7); Program.CheckCache(cached, 14); }
private static void CheckCache(CachingGenerator generator, int index) { if (generator.IsCached(index)) { Console.WriteLine(string.Format("{0} is cached.", index)); } else { Console.WriteLine(string.Format("{0} is not cached.", index)); } }