public void CompareAndSet() { Atomic <T> ai = new Atomic <T>(one); //CS1718 Assert.IsTrue(one == one); Assert.IsTrue(ai.CompareAndSet(one, two), "Object reference comparison 1"); Assert.IsTrue(ai.CompareAndSet(two, m4), "Object reference comparison 2"); Assert.AreEqual(m4, ai.Value); Assert.IsFalse(ai.CompareAndSet(m5, seven), "Object reference comparison 3"); Assert.IsFalse((seven.Equals(ai.Value))); Assert.IsTrue(ai.CompareAndSet(m4, seven)); Assert.AreEqual(seven, ai.Value); }
public void CompareAndSetInMultipleThreads() { Atomic <T> ai = new Atomic <T>(one); Thread t = new Thread(delegate() { while (!ai.CompareAndSet(two, three)) { Thread.Sleep(Delays.Short); } }); t.Start(); Assert.IsTrue(ai.CompareAndSet(one, two), "Value did not equal 'one' reference"); t.Join(Delays.Small); Assert.IsFalse(t.IsAlive, "Thread is still alive"); Assert.AreEqual(ai.Value, three, "Object reference not switched from 'two' to 'three'"); }
public void CompareAndSetWithNullReference() { Atomic <string> sar = new Atomic <string>(); string expected = "test"; Assert.IsTrue(sar.CompareAndSet(null, expected)); Assert.IsTrue(sar.Value.Equals(expected)); }