コード例 #1
0
        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());
        }
コード例 #2
0
        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));
        }
コード例 #3
0
        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);
        }
コード例 #4
0
        public void SameValuesAfterMultipleEnumerations()
        {
            var generator        = getGuidsGenerator();
            var cachingGenerator = CachingGenerator.Create(generator);

            var firstEnumeration  = cachingGenerator.ToArray();
            var secongEnumeration = cachingGenerator.ToArray();

            CollectionAssert.AreEqual(firstEnumeration, secongEnumeration);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
 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));
     }
 }