예제 #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_CanAdd()
        {
            var cache = new LFUCache <object>(2);

            Assert.IsTrue(cache.TryAdd("SomeKey", new object()));
            Assert.IsTrue(cache.TryAdd("SomeKey2", new object()));
            Assert.AreEqual(2, cache.Count);
        }
예제 #3
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));
        }
예제 #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
        private void PerformanceTest_TryAdd(int cacheSize, int population, int expectedTimeMs, int rounds = 100)
        {
            var totalTime = 0.0;

            for (int j = 0; j < rounds; j++)
            {
                var cache         = new LFUCache <int>(cacheSize);
                var succcessCount = 0;
                var stopWatch     = new Stopwatch();

                stopWatch.Start();
                for (int i = 0; i < population; i++)
                {
                    if (cache.TryAdd($"Key{i}", i))
                    {
                        succcessCount++;
                    }
                }
                stopWatch.Stop();
                totalTime += stopWatch.ElapsedMilliseconds;
            }

            double time = totalTime / rounds;

            Debug.Write($"Average time = {time} [ms]");
            Assert.IsTrue(time < expectedTimeMs);
        }
예제 #6
0
        public void LFU_CanGetOrAdd3()
        {
            var cache = new LFUCache <int>(2);

            Assert.IsTrue(cache.TryAdd("MyKey1", 100));
            var result = cache.GetOrAdd("MyKey1", () =>
            {
                Assert.Fail("Should not use factory");
                return(-1);
            });

            Assert.AreEqual(100, result);
        }
예제 #7
0
        public void SmokeTest2()
        {
            var cache         = new LFUCache <int>(10);
            var succcessCount = 0;

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

            Assert.AreEqual(1, cache.Count);
            Assert.AreEqual(1000, succcessCount);
        }
예제 #8
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);
        }