public void TestCAS() {
Random r = new Random();
System.Collections.Hashtable ht = new System.Collections.Hashtable();
for(int i = 0; i < 128; i++) {
byte[] key_buf = new byte[ r.Next(1024) ];
r.NextBytes(key_buf);
MemBlock key = MemBlock.Reference(key_buf);
byte[] val_buf = new byte[ r.Next(1024) ];
r.NextBytes(val_buf);
MemBlock val = MemBlock.Reference(val_buf);
MemBlock old_v = CompareSwap(key, val, null);
ht[key] = val;
Assert.IsNull(old_v, "old value is null");
//Try it again, make sure it doesn't work:
Assert.IsNotNull(CompareSwap(key, val, null), "old value is not null");
//Try it again with a different value:
MemBlock other_v = MemBlock.Concat(key, val);
Assert.AreEqual(val, CompareSwap(key, other_v, other_v), "update failed");
MemBlock current = Read(key);
Assert.AreEqual(val, current, "still not updated");
Assert.AreNotEqual(other_v, current, "make sure update didn't work");
//Now do a real update:
Assert.AreEqual(val, CompareSwap(key, other_v, val), "first update");
Assert.AreEqual(other_v, Read(key), "update worked");
ht[key] = other_v;
}
foreach(System.Collections.DictionaryEntry de in ht) {
MemBlock recall_v = Read((MemBlock)de.Key);
Assert.AreEqual(recall_v, de.Value, "check recall");
}
}
#endif