Exemplo n.º 1
0
        public void ConstructAtomicIntegerArryWithGivenSize()
        {
            AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE);

            for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i)
            {
                Assert.AreEqual(0, ai[i]);
            }
        }
Exemplo n.º 2
0
        public void ToStringTest()
        {
            int[] a = new int[] { 17, 3, -42, 99, -7 };
            AtomicIntegerArray ai = new AtomicIntegerArray(a);

            Assert.AreEqual(ToString(a), ai.ToString());

            int[] b = new int[0];
            Assert.AreEqual("[]", new AtomicIntegerArray(b).ToString());
        }
Exemplo n.º 3
0
        public void ConstructFromExistingArray()
        {
            int[] a = new int[] { 17, 3, -42, 99, -7 };
            AtomicIntegerArray ai = new AtomicIntegerArray(a);

            Assert.AreEqual(a.Length, ai.Count);
            for (int i = 0; i < a.Length; ++i)
            {
                Assert.AreEqual(a[i], ai[i]);
            }
        }
Exemplo n.º 4
0
        public void Exchange()
        {
            AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE);

            for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i)
            {
                ai[i] = 1;
                Assert.AreEqual(1, ai.Exchange(i, 0));
                Assert.AreEqual(0, ai.Exchange(i, -10));
                Assert.AreEqual(-10, ai.Exchange(i, 1));
            }
        }
Exemplo n.º 5
0
        public void ReturnValueAndDecrement()
        {
            AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE);

            for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i)
            {
                ai[i] = 1;
                Assert.AreEqual(1, ai.ReturnValueAndDecrement(i));
                Assert.AreEqual(0, ai.ReturnValueAndDecrement(i));
                Assert.AreEqual(-1, ai.ReturnValueAndDecrement(i));
            }
        }
Exemplo n.º 6
0
        public void AddDeltaAndReturnNewValue()
        {
            AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE);

            for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i)
            {
                ai[i] = 1;
                Assert.AreEqual(3, ai.AddDeltaAndReturnNewValue(i, 2));
                Assert.AreEqual(3, ai[i]);
                Assert.AreEqual(-1, ai.AddDeltaAndReturnNewValue(i, -4));
                Assert.AreEqual(-1, ai[i]);
            }
        }
Exemplo n.º 7
0
        public void GetReturnsLastValueLazySetAtIndex()
        {
            AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE);

            for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i)
            {
                ai.LazySet(i, 1);
                Assert.AreEqual(1, ai[i]);
                ai.LazySet(i, 2);
                Assert.AreEqual(2, ai[i]);
                ai.LazySet(i, -3);
                Assert.AreEqual(-3, ai[i]);
            }
        }
Exemplo n.º 8
0
        public void CountingInMultipleThreads()
        {
            AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE);

            for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i)
            {
                ai[i] = COUNTDOWN;
            }
            Counter c1 = new Counter(ai);
            Counter c2 = new Counter(ai);

            ThreadManager.StartAndAssertRegistered("T", c1.Run, c2.Run);
            ThreadManager.JoinAndVerify();
            Assert.AreEqual(c1.counts + c2.counts, DEFAULT_COLLECTION_SIZE * COUNTDOWN);
        }
Exemplo n.º 9
0
        public void IndexerChokesOnOutOfRangeIndex()
        {
            AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE);
            int a = 0;

            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, 0); });
            TestHelper.AssertException <IndexOutOfRangeException>(
                delegate { ai.Exchange(-1, 0); });
            Assert.AreEqual(0, a);
        }
Exemplo n.º 10
0
        public void IncrementValueAndReturn()
        {
            AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE);

            for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i)
            {
                ai[i] = 1;
                Assert.AreEqual(2, ai.IncrementValueAndReturn(i));
                Assert.AreEqual(2, ai[i]);
                ai[i] = -2;
                Assert.AreEqual(-1, ai.IncrementValueAndReturn(i));
                Assert.AreEqual(0, ai.IncrementValueAndReturn(i));
                Assert.AreEqual(1, ai.IncrementValueAndReturn(i));
                Assert.AreEqual(1, ai[i]);
            }
        }
Exemplo n.º 11
0
        public void CompareExistingValueAndSetNewValue()
        {
            AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE);

            for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i)
            {
                ai[i] = 1;
                Assert.IsTrue(ai.CompareAndSet(i, 1, 2));
                Assert.IsTrue(ai.CompareAndSet(i, 2, -4));
                Assert.AreEqual(-4, ai[i]);
                Assert.IsFalse(ai.CompareAndSet(i, -5, 7));
                Assert.IsFalse(7 == ai[i]);
                Assert.IsTrue(ai.CompareAndSet(i, -4, 7));
                Assert.AreEqual(7, ai[i]);
            }
        }
Exemplo n.º 12
0
        public void CompareAndSetInMultipleThreads()
        {
            AtomicIntegerArray a = new AtomicIntegerArray(1);

            a[0] = 1;
            Thread t = ThreadManager.StartAndAssertRegistered(
                "T1", () => { while (!a.CompareAndSet(0, 2, 3))
                              {
                                  Thread.Sleep(0);
                              }
                });

            Assert.IsTrue(a.CompareAndSet(0, 1, 2));
            ThreadManager.JoinAndVerify(Delays.Long);
            Assert.IsFalse(t.IsAlive);
            Assert.AreEqual(a[0], 3);
        }
Exemplo n.º 13
0
        public void WeakCompareAndSet()
        {
            AtomicIntegerArray ai = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE);

            for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i)
            {
                ai[i] = 1;
                while (!ai.WeakCompareAndSet(i, 1, 2))
                {
                }
                while (!ai.WeakCompareAndSet(i, 2, -4))
                {
                }
                Assert.AreEqual(-4, ai[i]);
                while (!ai.WeakCompareAndSet(i, -4, 7))
                {
                }
                Assert.AreEqual(7, ai[i]);
                Assert.IsFalse(ai.WeakCompareAndSet(i, -4, 7));
            }
        }
Exemplo n.º 14
0
        public void SerializeAndDeserialize()
        {
            AtomicIntegerArray l = new AtomicIntegerArray(DEFAULT_COLLECTION_SIZE);

            for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i)
            {
                l[i] = -i;
            }

            MemoryStream bout = new MemoryStream(10000);

            BinaryFormatter formatter = new BinaryFormatter();

            formatter.Serialize(bout, l);

            MemoryStream       bin        = new MemoryStream(bout.ToArray());
            BinaryFormatter    formatter2 = new BinaryFormatter();
            AtomicIntegerArray r          = (AtomicIntegerArray)formatter2.Deserialize(bin);

            for (int i = 0; i < DEFAULT_COLLECTION_SIZE; ++i)
            {
                Assert.AreEqual(l[i], r[i]);
            }
        }
Exemplo n.º 15
0
 internal Counter(AtomicIntegerArray a)
 {
     ai = a;
 }