Exemplo n.º 1
0
        public void LeastRecentlyUsedCacheInsertMovesItemToMostRecentlyUsedIfItAlreadyExistsInCache()
        {
            var cache = new LeastRecentlyUsedCache <int, TestCacheStub>(5);

            cache.Insert(1, new TestCacheStub {
                Id = 1, Value = 1.1
            });
            cache.Insert(2, new TestCacheStub {
                Id = 2, Value = 1.2
            });
            cache.Insert(3, new TestCacheStub {
                Id = 3, Value = 1.3
            });
            cache.Insert(4, new TestCacheStub {
                Id = 4, Value = 1.4
            });
            cache.Insert(5, new TestCacheStub {
                Id = 5, Value = 1.5
            });
            //should move 1 to most recently used
            cache.Insert(1, new TestCacheStub {
                Id = 1, Value = 1.1
            });
            //should force removal of LRU
            cache.Insert(6, new TestCacheStub {
                Id = 6, Value = 1.6
            });

            TestCacheStub value;
            bool          result1 = cache.TryGetItem(1, out value);
            bool          result2 = cache.TryGetItem(2, out value);

            Assert.IsTrue(result1);
            Assert.IsFalse(result2);
        }
Exemplo n.º 2
0
        public void LeastRecentlyUsedCacheInsertUpdatesCacheWithMostRecentInstanceOfItemIfItAlreadyExistsInCache()
        {
            const double expectedValue = 5.5;

            var cache = new LeastRecentlyUsedCache <int, TestCacheStub>(5);

            cache.Insert(1, new TestCacheStub {
                Id = 1, Value = 1.1
            });
            cache.Insert(2, new TestCacheStub {
                Id = 2, Value = 1.2
            });
            cache.Insert(3, new TestCacheStub {
                Id = 3, Value = 1.3
            });
            cache.Insert(4, new TestCacheStub {
                Id = 4, Value = 1.4
            });
            cache.Insert(5, new TestCacheStub {
                Id = 5, Value = 1.5
            });
            //should move 1 to most recently used
            cache.Insert(1, new TestCacheStub {
                Id = 1, Value = 5.5
            });

            TestCacheStub item;

            cache.TryGetItem(1, out item);

            Assert.AreEqual(expectedValue, item.Value);
        }
Exemplo n.º 3
0
        public void LeastRecentlyUsedCacheRemovesLeastRecentlyUsedItemWhenCapacityReached()
        {
            var cache = new LeastRecentlyUsedCache <int, TestCacheStub>(5);

            cache.Insert(1, new TestCacheStub {
                Id = 1, Value = 1.1
            });
            cache.Insert(2, new TestCacheStub {
                Id = 2, Value = 1.2
            });
            cache.Insert(3, new TestCacheStub {
                Id = 3, Value = 1.3
            });
            cache.Insert(4, new TestCacheStub {
                Id = 4, Value = 1.4
            });
            cache.Insert(5, new TestCacheStub {
                Id = 5, Value = 1.5
            });
            cache.Insert(6, new TestCacheStub {
                Id = 6, Value = 1.6
            });

            TestCacheStub value;
            bool          result = cache.TryGetItem(1, out value);

            Assert.IsFalse(result);
        }
Exemplo n.º 4
0
        public void LeastRecentlyUsedCacheTryGetItemThrowsIfKeyIsNull()
        {
            var cache = new LeastRecentlyUsedCache <int?, TestCacheStub>(5);

            TestCacheStub cacheItem;

            Assert.Throws <ArgumentNullException>(() => cache.TryGetItem(null, out cacheItem));
        }
Exemplo n.º 5
0
        public void LeastRecentlyUsedCacheTryGetItemReturnsFalseIfItemIsInCache()
        {
            var cache     = new LeastRecentlyUsedCache <int, TestCacheStub>(5);
            var cacheItem = new TestCacheStub {
                Id = 1, Value = 1.1
            };

            cache.Insert(cacheItem.Id, cacheItem);

            TestCacheStub value;
            bool          result = cache.TryGetItem(2, out value);

            Assert.IsFalse(result);
        }