示例#1
0
        public void SmokeTest4()
        {
            var cache         = new LFUCache <int>(100);
            var succcessCount = 0;

            for (int i = 0; i < 1000; i++)
            {
                if (cache.TryAdd($"Key{i}", i))
                {
                    succcessCount++;
                }
            }
            for (int i = 0; i < 1000; i++)
            {
                if (cache.TryAdd($"Key{i}", i))
                {
                    succcessCount++;
                }
            }

            Assert.AreEqual(100, cache.Count);
            Assert.IsFalse(cache.TryGet("Key999", out var val));
            Assert.IsTrue(cache.TryGet("Key99", out var val2));
            Assert.IsFalse(cache.TryGet("Key100", out var val3));
        }
示例#2
0
        public void LFU_CanGet()
        {
            var cache = new LFUCache <object>(2);

            Assert.IsTrue(cache.TryAdd("SomeKey", new object()));
            Assert.IsTrue(cache.TryAdd("SomeKey2", new object()));
            Assert.IsTrue(cache.TryGet("SomeKey", out var val1));
            Assert.IsTrue(cache.TryGet("SomeKey2", out var val2));
            Assert.IsFalse(cache.TryGet("SomeOtherKey", out var val3));
        }
示例#3
0
        public void LFU_WhenPromoted_OtherKeysRemovedFromCache()
        {
            var cache = new LFUCache <object>(2);

            Assert.IsTrue(cache.TryAdd("SomeKey", new object()));
            Assert.IsTrue(cache.TryAdd("SomeKey2", new object()));
            Assert.IsTrue(cache.TryAdd("SomeKey", new object()));
            Assert.IsTrue(cache.TryAdd("SomeKey2", new object()));
            Assert.AreEqual(2, cache.Count);
            Assert.IsTrue(cache.TryGet("SomeKey", out var val1));
            Assert.IsTrue(cache.TryGet("SomeKey2", out var val2));

            Assert.IsFalse(cache.TryAdd("SomeOther", new object()));
            Assert.IsFalse(cache.TryAdd("SomeOther", new object()));
            Assert.IsTrue(cache.TryAdd("SomeOther", new object()));

            Assert.AreEqual(2, cache.Count);
        }
示例#4
0
        public void LFU_CanAdd_NoMoreThanSize()
        {
            var cache = new LFUCache <object>(2);

            Assert.IsTrue(cache.TryAdd("SomeKey", new object()));
            Assert.IsTrue(cache.TryAdd("SomeKey2", new object()));
            Assert.IsFalse(cache.TryAdd("SomeKey3", new object()));
            Assert.IsFalse(cache.TryAdd("SomeKey4", new object()));
            Assert.IsFalse(cache.TryGet("SomeKey4", out var val));
            Assert.AreEqual(2, cache.Count);
        }
示例#5
0
        public void Acceptance()
        {
            var lfu = new LFUCache <int, int>(2);

            lfu.Put(3, 1);
            lfu.Put(2, 1);
            lfu.Put(2, 2);    // replace key 2
            lfu.Put(4, 4);    // evicts key 3

            Assert.False(lfu.TryGet(3, out var _));
            Assert.Equal(4, lfu.Get(4));
            Assert.Equal(2, lfu.Get(2));
        }