Remove() public method

public Remove ( object key ) : object
key object
return object
コード例 #1
0
ファイル: Cache.cs プロジェクト: pstjuste/brunet
  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");

  }
コード例 #2
0
ファイル: Cache.cs プロジェクト: pstjuste/brunet
 public void TestEnumeration() {
   const int MAX_SIZE = 100;
   Random r = new Random();
   Cache c = new Cache(MAX_SIZE);
   Hashtable ht = new Hashtable();
   for(int i = 0; i < MAX_SIZE; i++) {
     int k = r.Next();
     int v = r.Next();
     ht[k] = v;
     c[k] = v;
   }
   int enum_count = 0;
   foreach(DictionaryEntry de in c) {
     Assert.IsNotNull( c[de.Key], "Enumeration");
     enum_count++;
   }
   Assert.AreEqual(enum_count, c.Count, "Enumeration count");
   //Remove a bunch at random:
   ArrayList removed = new ArrayList();
   for(int i = 0; i < MAX_SIZE / 2; i++) {
     object k = r.Next(0, MAX_SIZE);
     removed.Add( k );
     c.Remove( k );
   }
   //Make sure they are really gone:
   enum_count = 0;
   foreach(DictionaryEntry de in c) {
     Assert.IsNotNull( c[de.Key], "Enumeration after remove");
     enum_count++;
   }
   Assert.AreEqual(enum_count, c.Count, "Enumeration count after remove");
   foreach(object k in removed) {
     Assert.IsNull(c[k], "removed objects removed");
   }
   //Let's enumerate and removed:
   foreach(DictionaryEntry de in c) {
     c.Remove(de.Key);
     Assert.IsNull( c[de.Key], "Removing with enumeration");
   }
   Assert.AreEqual(0, c.Count, "Removed everything");
 }