public void Get_MovesElementToFrontOfListForEviction()
        {
            // Arrange
            var cache = new LeastRecentlyUsedCache <string>(2);

            cache.Set(KeyOne, ValueOne);
            cache.Set(KeyTwo, ValueTwo);
            cache.Get(KeyOne);
            cache.Set(KeyThree, ValueThree);

            // Act
            var result = cache.Get(KeyOne);

            // Assert
            Assert.AreEqual(ValueOne, result);
        }
        public void Get_ReturnsCachedItem()
        {
            // Arrange
            var cache = new LeastRecentlyUsedCache <string>(1);

            cache.Set(KeyOne, ValueOne);

            // Act
            var result = cache.Get(KeyOne);

            // Assert
            Assert.AreEqual(ValueOne, result);
        }
        public void Set_EvictsOldestElement()
        {
            // Arrange
            var cache = new LeastRecentlyUsedCache <string>(1);

            cache.Set(KeyOne, ValueOne);
            cache.Set(KeyTwo, ValueTwo);

            // Act
            var result = cache.Get(KeyOne);

            // Assert
            Assert.IsNull(result);
        }
示例#4
0
        public static void LRUCache()
        {
            LeastRecentlyUsedCache cache = new LeastRecentlyUsedCache(2);

            cache.Set(1, 10);
            cache.Set(5, 12);

            Assert.That(cache.Get(5) == 12);
            Assert.That(cache.Get(1) == 10);
            Assert.That(cache.Get(10) == -1);

            cache.Set(6, 14);
            Assert.That(cache.Get(5) == -1);

            LRUCache cache2 = new LRUCache(2);

            cache2.Put(2, 1);
            cache2.Put(1, 1);
            cache2.Get(2);
            cache2.Put(4, 1);
            cache2.Get(1);
            cache2.Get(2);
        }