public void Test() {
/*
* Here are some very basic tests
*/
Hashtable ht = new Hashtable();
WeakHashtable wht = new WeakHashtable();
/*
* Check to see if it fails when working as a basic
* Hashtable.
*/
/*
* Make sure we can't add a null key
*/
bool can_add_null = true;
try {
wht[ null ] = "oh no";
}
catch(ArgumentNullException) {
can_add_null = false;
}
Assert.IsFalse(can_add_null, "Null key test");
/*
* Check to see that we can't Add() the same key twice:
*/
bool can_add_twice = true;
object key2 = "test";
try {
wht.Add(key2, "first");
wht.Add(key2, "second");
}
catch(ArgumentException) {
can_add_twice = false;
}
Assert.IsFalse(can_add_twice, "double Add");
/*
* Check to see that we can set twice:
*/
wht[key2] = "first";
wht[key2] = "second";
Assert.AreEqual("second", wht[key2], "double set");
wht.Clear();
Assert.AreEqual( wht.Count, 0 );
/*
* Put in a bunch of random keys
*/
Random r = new Random();
for(int i = 0; i < 1000; i++) {
//Use a reference type here:
object key = r.Next().ToString();
int val = r.Next();
ht[key] = val;
wht[key] = val;
}
IDictionaryEnumerator enm = ht.GetEnumerator();
while( enm.MoveNext() ) {
Assert.AreEqual( enm.Value, wht[ enm.Key ], "basic hashtable test" );
}
/*
* Let's remove all the elements we put in earlier:
*/
enm = ht.GetEnumerator();
while( enm.MoveNext() ) {
wht.Remove( enm.Key );
}
Assert.AreEqual( 0, wht.Count, "Zero count after removal");
}
#endif