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 IndexerChokesOnOutOfRangeIndex()
        {
            AtomicArray <T> ai = new AtomicArray <T>(DEFAULT_COLLECTION_SIZE);
            T a = default(T);

            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.That(a, Is.EqualTo(default(T)));
        }
        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]);
            }
        }