Наследование: System.EventArgs
Пример #1
0
        public void TestEviction()
        {
            const int    MAX_SIZE   = 1000;
            Random       r          = new Random();
            Cache        c          = new Cache(MAX_SIZE);
            Hashtable    ht         = new Hashtable();
            Hashtable    ht_evicted = new Hashtable();
            EventHandler eh         = delegate(object o, EventArgs args) {
                Cache.EvictionArgs a = (Cache.EvictionArgs)args;
                ht_evicted[a.Key] = a.Value;
            };

            c.EvictionEvent += eh;

            int i = 0;

            for (i = 0; i < 50 * MAX_SIZE; i++)
            {
                int v = r.Next();
                ht[i] = v;
                c[i]  = v;
                int exp_size = Math.Min(i + 1, MAX_SIZE);
                Assert.AreEqual(c.Count, exp_size, "Size check");
                Assert.AreEqual(ht[i], c[i], "equivalence check");
                //Keep the zero'th element in the cache:
                object v_0 = c[0];
                Assert.IsNotNull(v_0, "0th element still in the cache");
            }
            Assert.AreEqual(c.Count, MAX_SIZE, "Full cache");
            //Now check that everything is either in the Cache or was evicted:
            IDictionaryEnumerator ide = ht.GetEnumerator();

            while (ide.MoveNext())
            {
                int    key   = (int)ide.Key;
                int    val   = (int)ide.Value;
                object c_val = c[key];
                if (!c.Contains(key))
                {
                    Assert.IsNull(c_val, "Evicted entry is null");
                    c_val = ht_evicted[key];
                    Assert.AreEqual(c_val, val, "Evicted lookup");
                }
                else
                {
                    //Not in the cache:
                    Assert.AreEqual(c_val, val, "Cache lookup");
                }
            }
            //Let's remove from the Cache and see if that worked:
            int    s0 = c.Count;
            object rv = c.Remove(0);

            Assert.AreEqual(rv, ht[0], "Removed value matches");
            Assert.IsNull(c[0], "Remove really removed");
            Assert.AreEqual(s0 - 1, c.Count, "Removed decreased size");
        }