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); }
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); }
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); }
public void LeastRecentlyUsedCacheTryGetItemThrowsIfKeyIsNull() { var cache = new LeastRecentlyUsedCache <int?, TestCacheStub>(5); TestCacheStub cacheItem; Assert.Throws <ArgumentNullException>(() => cache.TryGetItem(null, out cacheItem)); }
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); }