public void AddRemoveTest()
        {
            var cache = new WeakCache <string, TestClass>(false, value => value.Text);

            TestClass item = new TestClass("1");

            cache.Add(item);
            Assert.AreEqual(1, cache.Count);

            item = new TestClass("2");
            cache.Add(item);
            Assert.AreEqual(2, cache.Count);
            Assert.AreEqual(item, cache[item.Text, false]);

            ICache <string, TestClass> icache = cache;

            Assert.AreEqual(item, icache[item.Text, false]);
            Assert.AreEqual(null, icache["3", false]);

            cache.Remove(item);
            Assert.AreEqual(1, cache.Count);

            cache.Clear();
            Assert.AreEqual(0, cache.Count);
        }
Exemple #2
0
        public void Cache_should_be_clear_on_demand()
        {
            ICache cache = new WeakCache();

            for (int i = 0; i < 5; i++)
            {
                cache[i] = i;
            }

            Assert.That(cache[0], Is.Not.Null);
            Assert.That(cache[4], Is.Not.Null);
            cache.Clear();
            Assert.That(cache[0], Is.Null);
            Assert.That(cache[4], Is.Null);
        }