public void Exchange()
        {
            AtomicReferenceArray <T> ai = new AtomicReferenceArray <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 IndexerChokesOnOutOfRangeIndex()
        {
            AtomicReferenceArray <T> ai = new AtomicReferenceArray <T>(DEFAULT_COLLECTION_SIZE);
            T a = null;

            TestHelper.AssertException <IndexOutOfRangeException>(
                delegate { a = ai[DEFAULT_COLLECTION_SIZE]; });
            TestHelper.AssertException <IndexOutOfRangeException>(
                delegate { a = ai[-1]; });
            TestHelper.AssertException <IndexOutOfRangeException>(
                delegate { ai.Exchange(DEFAULT_COLLECTION_SIZE, zero); });
            TestHelper.AssertException <IndexOutOfRangeException>(
                delegate { ai.Exchange(-1, zero); });
            Assert.IsNull(a);
        }
        public void CompareExistingValueAndSetNewValue()
        {
            AtomicReferenceArray <T> ai = new AtomicReferenceArray <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()
        {
            AtomicReferenceArray <T> a = new AtomicReferenceArray <T>(1);

            a.Exchange(0, one);
            Thread t = ThreadManager.StartAndAssertRegistered(
                "T1", () => { while (!a.CompareAndSet(0, two, three))
                              {
                                  Thread.Sleep(Delays.Short);
                              }
                });

            Assert.IsTrue(a.CompareAndSet(0, one, two));
            ThreadManager.JoinAndVerify(Delays.Long);
            Assert.IsFalse(t.IsAlive);
            Assert.AreEqual(a[0], three);
        }
        public void WeakCompareAndSet()
        {
            AtomicReferenceArray <T> ai = new AtomicReferenceArray <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()
        {
            AtomicReferenceArray <T> atomicReferenceArray = new AtomicReferenceArray <T>(DEFAULT_COLLECTION_SIZE);

            for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i)
            {
                atomicReferenceArray.Exchange(i, TestData <T> .MakeData(-i));
            }
            MemoryStream bout = new MemoryStream(10000);

            BinaryFormatter formatter = new BinaryFormatter();

            formatter.Serialize(bout, atomicReferenceArray);

            MemoryStream             bin        = new MemoryStream(bout.ToArray());
            BinaryFormatter          formatter2 = new BinaryFormatter();
            AtomicReferenceArray <T> r          = (AtomicReferenceArray <T>)formatter2.Deserialize(bin);

            Assert.AreEqual(atomicReferenceArray.Count, r.Count);
            for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i)
            {
                Assert.AreEqual(r[i], atomicReferenceArray[i]);
            }
        }
 /// <summary>
 /// Atomically sets the element at position <paramref name="index"/> to <paramref name="newValue"/>
 /// and returns the old value.
 /// </summary>
 /// <param name="index">
 /// Ihe index
 /// </param>
 /// <param name="newValue">
 /// The new value
 /// </param>
 public T Exchange(int index, T newValue)
 {
     return(_atomicReferenceArray.Exchange(index, new ValueHolder <T>(newValue)).Value);
 }