public void TestCachingWrapperSimple()
        {
            _cacheMisses = 0;
            var cachedSource = new CachingWrapper<int, string>(DatabaseCallMock, 0);

            Assert.AreEqual(MockedResult(0), cachedSource.Retrieve(0)); // First try should cache it (cache miss)
            Assert.AreEqual(MockedResult(0), cachedSource.Retrieve(0)); // Second try should retrieve it from cache
            Assert.AreEqual(1, _cacheMisses, "There should only be one miss!");
        }
        public void TestCachingWrapperSimple()
        {
            _cacheMisses = 0;
            var cachedSource = new CachingWrapper <int, string>(DatabaseCallMock, 0);

            Assert.AreEqual(MockedResult(0), cachedSource.Retrieve(0)); // First try should cache it (cache miss)
            Assert.AreEqual(MockedResult(0), cachedSource.Retrieve(0)); // Second try should retrieve it from cache
            Assert.AreEqual(1, _cacheMisses, "There should only be one miss!");
        }
        public void TestCachingWrapperWithCapacity()
        {
            _cacheMisses = 0;

            var cachedSource = new CachingWrapper<int, string>(DatabaseCallMock, 5);

            Assert.AreEqual(MockedResult(0), cachedSource.Retrieve(0)); // First try should cache it (cache miss)
            Assert.AreEqual(MockedResult(1), cachedSource.Retrieve(1)); // First try should cache it (cache miss)
            Assert.AreEqual(MockedResult(2), cachedSource.Retrieve(2)); // First try should cache it (cache miss)
            Assert.AreEqual(MockedResult(3), cachedSource.Retrieve(3)); // First try should cache it (cache miss)
            Assert.AreEqual(MockedResult(4), cachedSource.Retrieve(4)); // First try should cache it (cache miss)

            // Make sure 0 element is cached properly (should not increase the cache misses)
            // This should also put element 0 on top, making element 1 the last (and a candidate for removal)
            Assert.AreEqual(MockedResult(0), cachedSource.Retrieve(0), "Wrong element was returned from cache!");

            Assert.AreEqual(5, _cacheMisses, "There should be 5 cache misses so far");

            // The following statement should cause a capacity overflow which should discard the last element
            // The last element should be element 1
            Assert.AreEqual(MockedResult(5), cachedSource.Retrieve(5));

            Assert.AreEqual(6, _cacheMisses, "There should be 6 cache misses so far");

            // Since element 1 should be out of cache, a call to it should increase the misses by one.
            Assert.AreEqual(MockedResult(1), cachedSource.Retrieve(1), "Wrong element was returned from cache!");

            Assert.AreEqual(7, _cacheMisses, "There should be 7 cache misses so far");
        }
        public void TestCachingWrapperWithCapacity()
        {
            _cacheMisses = 0;

            var cachedSource = new CachingWrapper <int, string>(DatabaseCallMock, 5);

            Assert.AreEqual(MockedResult(0), cachedSource.Retrieve(0)); // First try should cache it (cache miss)
            Assert.AreEqual(MockedResult(1), cachedSource.Retrieve(1)); // First try should cache it (cache miss)
            Assert.AreEqual(MockedResult(2), cachedSource.Retrieve(2)); // First try should cache it (cache miss)
            Assert.AreEqual(MockedResult(3), cachedSource.Retrieve(3)); // First try should cache it (cache miss)
            Assert.AreEqual(MockedResult(4), cachedSource.Retrieve(4)); // First try should cache it (cache miss)

            // Make sure 0 element is cached properly (should not increase the cache misses)
            // This should also put element 0 on top, making element 1 the last (and a candidate for removal)
            Assert.AreEqual(MockedResult(0), cachedSource.Retrieve(0), "Wrong element was returned from cache!");

            Assert.AreEqual(5, _cacheMisses, "There should be 5 cache misses so far");

            // The following statement should cause a capacity overflow which should discard the last element
            // The last element should be element 1
            Assert.AreEqual(MockedResult(5), cachedSource.Retrieve(5));

            Assert.AreEqual(6, _cacheMisses, "There should be 6 cache misses so far");

            // Since element 1 should be out of cache, a call to it should increase the misses by one.
            Assert.AreEqual(MockedResult(1), cachedSource.Retrieve(1), "Wrong element was returned from cache!");

            Assert.AreEqual(7, _cacheMisses, "There should be 7 cache misses so far");
        }
        private IterationResults IterativeTest(int iterations, int capacityDivisor)
        {
            var stopwatch = new Stopwatch();

            _cacheMisses = 0;

            int capacity = (capacityDivisor == 0) ? 0 : (iterations / capacityDivisor);

            var cachedSource = new CachingWrapper <int, string>(DatabaseCallMock, capacity);

            stopwatch.Start();
            var random = new Random();

            for (int i = 0; i < iterations - 1; i++)
            {
                int num = random.Next(1, iterations);

                cachedSource.Retrieve(num);
            }
            stopwatch.Stop();

            decimal missRatio = (100 / (decimal)iterations) * _cacheMisses;

            return(new IterationResults {
                MissRatio = missRatio, Elapsed = stopwatch.ElapsedMilliseconds
            });

            //Console.WriteLine("Iterations : {0:0000}, Capacity: {1:0000} (1/{2}), Cache misses: {3:0000}, Miss ratio: {4:00.00}% - Hit ratio: {5:00.00}%, Elapsed: {6}ms", iterations, capacity, capacityDivisor, _cacheMisses, MissRatio, 100 - MissRatio, stopwatch.ElapsedMilliseconds);
        }
        private IterationResults IterativeTest(int iterations, int capacityDivisor)
        {
            var stopwatch = new Stopwatch();

            _cacheMisses = 0;

            int capacity = (capacityDivisor == 0) ? 0 : (iterations / capacityDivisor);

            var cachedSource = new CachingWrapper<int, string>(DatabaseCallMock, capacity);

            stopwatch.Start();
            var random = new Random();
            for (int i = 0; i < iterations - 1; i++)
            {
                int num = random.Next(1, iterations);

                cachedSource.Retrieve(num);
            }
            stopwatch.Stop();

            decimal missRatio = (100 / (decimal)iterations) * _cacheMisses;

            return new IterationResults {MissRatio = missRatio, Elapsed = stopwatch.ElapsedMilliseconds};

            //Console.WriteLine("Iterations : {0:0000}, Capacity: {1:0000} (1/{2}), Cache misses: {3:0000}, Miss ratio: {4:00.00}% - Hit ratio: {5:00.00}%, Elapsed: {6}ms", iterations, capacity, capacityDivisor, _cacheMisses, MissRatio, 100 - MissRatio, stopwatch.ElapsedMilliseconds);
        }