public void Exchange() { AtomicArray <T> ai = new AtomicArray <T>(DEFAULT_COLLECTION_SIZE); for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i) { ai.Exchange(i, one); Assert.AreEqual(one, ai.Exchange(i, zero)); Assert.AreEqual(zero, ai.Exchange(i, m10)); Assert.AreEqual(m10, ai.Exchange(i, one)); } }
public void GetReturnsLastValueLazySetAtIndex() { AtomicArray <T> ai = new AtomicArray <T>(DEFAULT_COLLECTION_SIZE); for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i) { ai.LazySet(i, one); Assert.AreEqual(one, ai[i]); ai.LazySet(i, two); Assert.AreEqual(two, ai[i]); ai.LazySet(i, m3); Assert.AreEqual(m3, ai[i]); } }
public void CompareExistingValueAndSetNewValue() { AtomicArray <T> ai = new AtomicArray <T>(DEFAULT_COLLECTION_SIZE); for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i) { ai.Exchange(i, one); Assert.IsTrue(ai.CompareAndSet(i, one, two)); Assert.IsTrue(ai.CompareAndSet(i, two, m4)); Assert.AreEqual(m4, ai[i]); Assert.IsFalse(ai.CompareAndSet(i, m5, seven)); Assert.IsFalse((seven.Equals(ai[i]))); Assert.IsTrue(ai.CompareAndSet(i, m4, seven)); Assert.AreEqual(seven, ai[i]); } }
public void CompareAndSetInMultipleThreads() { AtomicArray <T> a = new AtomicArray <T>(1); a.Exchange(0, one); Thread t = new Thread(delegate() { while (!a.CompareAndSet(0, two, three)) { Thread.Sleep(Delays.Short); } }); t.Start(); Assert.IsTrue(a.CompareAndSet(0, one, two)); t.Join(Delays.Long); Assert.IsFalse(t.IsAlive); Assert.AreEqual(a[0], three); }
public void WeakCompareAndSet() { AtomicArray <T> ai = new AtomicArray <T>(DEFAULT_COLLECTION_SIZE); for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i) { ai.Exchange(i, one); while (!ai.WeakCompareAndSet(i, one, two)) { } while (!ai.WeakCompareAndSet(i, two, m4)) { } Assert.AreEqual(m4, ai[i]); while (!ai.WeakCompareAndSet(i, m4, seven)) { } Assert.AreEqual(seven, ai[i]); } }
public void SerializeAndDeserialize() { AtomicArray <T> ai = new AtomicArray <T>(DEFAULT_COLLECTION_SIZE); for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i) { ai.Exchange(i, TestData <T> .MakeData(-i)); } MemoryStream bout = new MemoryStream(10000); BinaryFormatter formatter = new BinaryFormatter(); formatter.Serialize(bout, ai); MemoryStream bin = new MemoryStream(bout.ToArray()); BinaryFormatter formatter2 = new BinaryFormatter(); AtomicArray <T> r = (AtomicArray <T>)formatter2.Deserialize(bin); Assert.AreEqual(ai.Count, r.Count); for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i) { Assert.AreEqual(r[i], ai[i]); } }