public void ToStringTest()
        {
            T[] a = new T[] { two, one, three, four, seven };
            AtomicReferenceArray <T> ai = new AtomicReferenceArray <T>(a);

            Assert.AreEqual(ConvertArrayToString(a), ai.ToString());
            Assert.AreEqual("[]", new AtomicReferenceArray <T>(0).ToString());
        }
        public void ConstructAtomicIntegerArryWithGivenSize()
        {
            AtomicReferenceArray <T> ai = new AtomicReferenceArray <T>(DEFAULT_COLLECTION_SIZE);

            for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i)
            {
                Assert.IsNull(ai[i]);
            }
        }
        public void ConstructFromExistingArray()
        {
            T[] a = new T[] { two, one, three, four, seven };
            AtomicReferenceArray <T> ai = new AtomicReferenceArray <T>(a);

            Assert.AreEqual(a.Length, ai.Count);
            for (int i = 0; i < a.Length; ++i)
            {
                Assert.AreEqual(a[i], ai[i]);
            }
        }
        /// <summary>
        /// Creates a new <see cref="AtomicReferenceArray{T}"/> of <paramref name="length"/>.
        /// </summary>
        /// <param name="length">
        /// the length of the array
        /// </param>
        public AtomicArray(int length)
        {
            ValueHolder <T> holder = new ValueHolder <T>();

            ValueHolder <T>[] holders = new ValueHolder <T> [length];
            for (int i = 0; i < length; i++)
            {
                holders[i] = holder;
            }
            _atomicReferenceArray = new AtomicReferenceArray <ValueHolder <T> >(holders);
        }
        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 GetReturnsLastValueSetAtIndex()
        {
            AtomicReferenceArray <T> ai = new AtomicReferenceArray <T>(DEFAULT_COLLECTION_SIZE);

            for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i)
            {
                ai[i] = one;
                Assert.AreEqual(one, ai[i]);
                ai[i] = two;
                Assert.AreEqual(two, ai[i]);
                ai[i] = m3;
                Assert.AreEqual(m3, ai[i]);
            }
        }
        public void GetReturnsLastValueLazySetAtIndex()
        {
            AtomicReferenceArray <T> ai = new AtomicReferenceArray <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 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);
        }
        /// <summary>
        /// Creates a new <see cref="AtomicReferenceArray{T}"/> with the same length as, and
        /// all elements copied from <paramref name="array"/>
        /// </summary>
        /// <param name="array">
        /// The array to copy elements from
        /// </param>
        /// <throws><see cref="ArgumentNullException"/>if array is null</throws>
        public AtomicArray(T[] array)
        {
            if (array == null)
            {
                throw new ArgumentNullException("array");
            }
            int length = array.Length;

            ValueHolder <T>[] holders = new ValueHolder <T> [length];
            for (int i = 0; i < length; i++)
            {
                holders[i] = new ValueHolder <T>(array[i]);
            }
            _atomicReferenceArray = new AtomicReferenceArray <ValueHolder <T> >(holders);
        }
        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]);
            }
        }