private static void VisitCacheItems(IEnumerable <CacheKey> keysToVisit, SetAssociativeCache <CacheKey, CacheValue> cache)
 {
     foreach (var key in keysToVisit)
     {
         cache.Get(key);
     }
 }
        public void Add_Should_Update_Existing_Item_If_Key_Is_Already_In_Cache()
        {
            // Arrange
            var empNumber = new CacheKey("001");
            var empName   = new CacheValue("Steve Jobs");

            cache.Add(empNumber, empName);
            empName = new CacheValue("Bill Gates");

            // Act
            cache.Add(empNumber, empName);

            // Assert
            cache.Count.Should().Be(1);
            cache.Get(empNumber).Should().Be(empName);
        }
예제 #3
0
        public void Add_CustomNoEvictionReplacementStrategy_DoesNotEvictWhenFull()
        {
            m_cache = new SetAssociativeCache <IKeyType, string>(m_setCount, m_numberOfWays,
                                                                 (size) => new NoEvictionAssociativeCache <string>(size));

            FillCacheAndValidate();

            List <KeyValuePair <IKeyType, string> > newValues = new List <KeyValuePair <IKeyType, string> >();

            for (int i = 0; i < m_setCount; ++i)
            {
                newValues.Add(new KeyValuePair <IKeyType, string>(new KeyType(i - m_setCount), m_rand.Next().ToString()));
            }

            newValues.ForEach(pair => m_cache.Add(pair.Key, pair.Value));

            newValues.ForEach(pair => Assert.Equal(m_cache.Get(pair.Key), null));

            Assert.Equal(m_cache.Count, m_setCount * m_numberOfWays);
        }
        public void SetAssociativeCache_BasicUsage()
        {
            string myKey     = "12345";
            int    itemCount = 100;
            int    slotSize  = 100;
            SetAssociativeCache <string, string> cache = new SetAssociativeCache <string, string>(slotSize, itemCount);



            /////////////////////////////////////////
            // TEST 1). Get null value
            /////////////////////////////////////////
            string cachedValue = cache.Get(myKey);

            Assert.IsTrue(string.IsNullOrWhiteSpace(cachedValue));


            //////////////////////////////////////////////////////////////////////
            // TEST 2). Add Items and ensure that items are removed when count hit
            //////////////////////////////////////////////////////////////////////
            for (int i = 1; i < itemCount + 10; i++)
            {
                string k = "Key " + i;
                string v = "Value " + i;

                cache.Add(k, v);

                if (i >= itemCount)
                {
                    //Assert.IsTrue(cache.GetCount(k) == itemCount);
                }

                cachedValue = cache.Get(k);
                Assert.IsTrue(cachedValue == v);
            }


            //////////////////////////////////////////////////////////////////////
            // TEST 3). Add a few items and Check counts
            //////////////////////////////////////////////////////////////////////
            cache = new SetAssociativeCache <string, string>(1, 2);

            cache.Add("1", "First");
            Assert.IsTrue(cache.GetCount("1") == 1);

            cache.Add("2", "Second");
            Assert.IsTrue(cache.GetCount("2") == 2);

            cache.Add("3", "Third");
            Assert.IsTrue(cache.GetCount("3") == 2);             // NOTE: this remains the same

            cache.Add("4", "Fourth");
            Assert.IsTrue(cache.GetCount("4") == 2);             // NOTE: this remains the same



            //////////////////////////////////////////////////////////////////////
            // TEST 4). Add and Get from cache
            /////////////////////////////////////////////////////////////////////
            cache.Flush();

            cache.Add("1", "First");
            Assert.IsTrue(cache.Get("1") == "First");

            cache.Add("2", "Second");
            Assert.IsTrue(cache.Get("2") == "Second");

            cache.Add("3", "Third");
            Assert.IsTrue(cache.Get("3") == "Third");

            cache.Add("4", "Fourth");
            Assert.IsTrue(cache.Get("4") == "Fourth");

            // And finally the first 2 should now be null
            Assert.IsTrue(cache.Get("1") == null);

            Assert.IsTrue(cache.Get("2") == null);

            // Should still be count 2
            Assert.IsTrue(cache.GetCount("4") == 2);
        }
예제 #5
0
        public void SetAssociativeCache_BasicUsage()
        {
            string myKey = "12345";
            int itemCount = 100;
            int slotSize = 100;
            SetAssociativeCache<string, string> cache = new SetAssociativeCache<string, string>(slotSize, itemCount);

            /////////////////////////////////////////
            // TEST 1). Get null value
            /////////////////////////////////////////
            string cachedValue = cache.Get(myKey);
            Assert.IsTrue(string.IsNullOrWhiteSpace(cachedValue));

            //////////////////////////////////////////////////////////////////////
            // TEST 2). Add Items and ensure that items are removed when count hit
            //////////////////////////////////////////////////////////////////////
            for (int i = 1; i < itemCount + 10; i++)
            {
                string k = "Key " + i;
                string v = "Value " + i;

                cache.Add(k, v);

                if (i >= itemCount)
                {
                    //Assert.IsTrue(cache.GetCount(k) == itemCount);
                }

                cachedValue = cache.Get(k);
                Assert.IsTrue(cachedValue == v);

            }

            //////////////////////////////////////////////////////////////////////
            // TEST 3). Add a few items and Check counts
            //////////////////////////////////////////////////////////////////////
            cache = new SetAssociativeCache<string, string>(1, 2);

            cache.Add("1", "First");
            Assert.IsTrue(cache.GetCount("1") == 1);

            cache.Add("2", "Second");
            Assert.IsTrue(cache.GetCount("2") == 2);

            cache.Add("3", "Third");
            Assert.IsTrue(cache.GetCount("3") == 2); // NOTE: this remains the same

            cache.Add("4", "Fourth");
            Assert.IsTrue(cache.GetCount("4") == 2); // NOTE: this remains the same

            //////////////////////////////////////////////////////////////////////
            // TEST 4). Add and Get from cache
            /////////////////////////////////////////////////////////////////////
            cache.Flush();

            cache.Add("1", "First");
            Assert.IsTrue(cache.Get("1") == "First");

            cache.Add("2", "Second");
            Assert.IsTrue(cache.Get("2") == "Second");

            cache.Add("3", "Third");
            Assert.IsTrue(cache.Get("3") == "Third");

            cache.Add("4", "Fourth");
            Assert.IsTrue(cache.Get("4") == "Fourth");

            // And finally the first 2 should now be null
            Assert.IsTrue(cache.Get("1") == null);

            Assert.IsTrue(cache.Get("2") == null);

            // Should still be count 2
            Assert.IsTrue(cache.GetCount("4") == 2);
        }