Exemplo n.º 1
0
        public void TryPopRight_IsAtomic()
        {
            //Arrange
            const int    initialCount = 5000000;
            const double stopAt       = initialCount * 0.9;

            int popCount = 0;
            var deque    = new ConcurrentDeque <int>();

            for (int i = 0; i < initialCount; i++)
            {
                deque.PushRight(i);
            }

            Action popRight = () =>
            {
                while (popCount <= stopAt)
                {
                    int i;
                    Assert.True(deque.TryPopRight(out i));
                    Interlocked.Increment(ref popCount);
                }
            };

            //Act
            popRight.RunInParallel(ThreadCount);

            //Assert
            var  expectedCount = initialCount - popCount;
            long expectedSum   = Enumerable.Range(0, expectedCount).LongSum();

            VerifyState(deque, expectedCount, expectedSum);
        }
Exemplo n.º 2
0
        public void WithEnumerable_MaintainsPointersIntegrity(int[] collection)
        {
            var deque = new ConcurrentDeque <int>(collection);

            Assert.Equal(collection, deque.TraverseLeftRight().Select(n => n._value));
            Assert.Equal(collection.Reverse(), deque.TraverseRightLeft().Select(n => n._value));
        }
Exemplo n.º 3
0
        public void WithIEnumerableCopiesCollection(Int32[] collection)
        {
            Int32[] array = { 1, 2, 3, 4 };
            var     deque = new ConcurrentDeque <Int32>(array);

            Assert.AreEqual(array, deque);
        }
Exemplo n.º 4
0
        public void WithIEnumerable_CopiesCollection(int[] collection)
        {
            int[] array = { 1, 2, 3, 4 };
            var   deque = new ConcurrentDeque <int>(array);

            Assert.Equal(array, deque);
        }
Exemplo n.º 5
0
        public void EnumeratorDoesNotIncludeConcurrentModifications()
        {
            //Arrange
            var   arr   = new[] { 1, 2, 3 };
            var   deque = new ConcurrentDeque <Int32>(arr);
            Int32 item;

            //Act
            var iterator = deque.GetEnumerator();

            iterator.MoveNext();

            deque.TryPopLeft(out item);
            deque.TryPopLeft(out item);
            deque.PushLeft(6);

            deque.TryPopRight(out item);
            deque.PushRight(6);

            //Assert
            Assert.AreEqual(1, iterator.Current);

            Assert.True(iterator.MoveNext());
            Assert.AreEqual(2, iterator.Current);

            Assert.True(iterator.MoveNext());
            Assert.AreEqual(3, iterator.Current);

            Assert.False(iterator.MoveNext());
        }
Exemplo n.º 6
0
        public void PushLeft_IsAtomic()
        {
            //Arrange
            long pushCount = 0;
            long sum       = 0;
            bool cancelled = false;

            var deque = new ConcurrentDeque <int>();

            //keep adding items to the deque
            Action pushLeft = () =>
            {
                Random rnd = new Random();
                while (!cancelled)
                {
                    int val = rnd.Next(1, 11);
                    deque.PushLeft(val);
                    Interlocked.Increment(ref pushCount);
                    Interlocked.Add(ref sum, val);
                }
            };

            //Act
            pushLeft.RunInParallel(() => cancelled = true, ThreadCount, RunningTime);

            //Assert
            VerifyState(deque, pushCount, sum);
        }
Exemplo n.º 7
0
        private static void ExecuteOp(ConcurrentDeque <int> deque, int op)
        {
            int item;

            switch (op)
            {
            case PopLeft:
                deque.TryPopLeft(out item);
                break;

            case PopRight:
                deque.TryPopRight(out item);
                break;

            case PushLeft:
                deque.PushLeft(10);
                break;

            case PushRight:
                deque.PushRight(10);
                break;

            default:
                throw new InvalidOperationException();
            }
        }
Exemplo n.º 8
0
        public void PushRight_AppendsNode_ToEmptyDeque()
        {
            var deque = new ConcurrentDeque <int>();

            deque.PushRight(1);

            Assert.Equal(new[] { 1 }, deque);
        }
Exemplo n.º 9
0
        public void PushRight_AppendsNode_ToNonEmptyList()
        {
            var deque = new ConcurrentDeque <int>(new[] { 1, 2, 3 });

            deque.PushRight(4);

            Assert.Equal(new[] { 1, 2, 3, 4 }, deque);
        }
Exemplo n.º 10
0
        public void PushLeftAppendsNodeToNonEmptyList()
        {
            var deque = new ConcurrentDeque <Int32>(new[] { 1, 2, 3 });

            deque.PushLeft(0);

            Assert.AreEqual(new[] { 0, 1, 2, 3 }, deque);
        }
Exemplo n.º 11
0
        public void PushLeftAppendsNodeToEmptyDeque()
        {
            var deque = new ConcurrentDeque <Int32>();

            deque.PushLeft(1);

            Assert.AreEqual(new[] { 1 }, deque);
        }
Exemplo n.º 12
0
        public void TryPeekLeft_Fails_IfDequeIsEmpty()
        {
            var deque = new ConcurrentDeque <int>();

            int item;

            Assert.False(deque.TryPeekLeft(out item));
        }
Exemplo n.º 13
0
        public void PushLeft_AppendsNode_ToNonEmptyList()
        {
            var deque = new ConcurrentDeque <int>(new[] { 1, 2, 3 });

            deque.PushLeft(0);

            Assert.Equal(new[] { 0, 1, 2, 3 }, deque);
        }
Exemplo n.º 14
0
        public void TryPeekLeftFailsIfDequeIsEmpty()
        {
            var deque = new ConcurrentDeque <Int32>();

            Int32 item;

            Assert.False(deque.TryPeekLeft(out item));
        }
Exemplo n.º 15
0
        public void ToArrayReturnsSnapshot()
        {
            Int32[] array = { 0, 1, 2 };
            var     deque = new ConcurrentDeque <Int32>(array);

            var snapshot = deque.ToArray();

            Assert.AreEqual(array, snapshot);
        }
Exemplo n.º 16
0
        /// <summary>
        /// Constructs a new debugger view object for the provided collection object.
        /// </summary>
        /// <param name="deque">A collection to browse in the debugger.</param>
        public ConcurrentDequeDebugView(ConcurrentDeque <T> deque)
        {
            if (deque == null)
            {
                throw new ArgumentNullException("deque");
            }

            _deque = deque;
        }
Exemplo n.º 17
0
        public void TryPeekLeftInspectsTheLeftmostItem()
        {
            var deque = new ConcurrentDeque <Int32>(new[] { 1, 2, 3 });

            Int32 item;

            Assert.True(deque.TryPeekLeft(out item));
            Assert.AreEqual(1, item);
            Assert.Contains(1, deque);
        }
Exemplo n.º 18
0
        public void IsStable_ByDefault()
        {
            //Act
            var deque = new ConcurrentDeque <int>();

            //Assert
            var anchor = deque._anchor;

            Assert.Equal(ConcurrentDeque <int> .DequeStatus.Stable, anchor._status);
        }
Exemplo n.º 19
0
        public void TryPeekRight_Inspects_TheRightmostItem()
        {
            var deque = new ConcurrentDeque <int>(new[] { 1, 2, 3 });

            int item;

            Assert.True(deque.TryPeekRight(out item));
            Assert.Equal(3, item);
            Assert.Contains(3, deque);
        }
Exemplo n.º 20
0
        public void TryPopRight_FailsOnEmptyDeque()
        {
            //Arrange
            var deque = new ConcurrentDeque <int>();

            //Act & Assert
            int item;

            Assert.False(deque.TryPopRight(out item));
            Assert.Equal(item, default(int));
        }
Exemplo n.º 21
0
        public void PointersAreNull_ByDefault()
        {
            //Act
            var deque = new ConcurrentDeque <int>();

            //Assert
            var anchor = deque._anchor;

            Assert.Null(anchor._left);
            Assert.Null(anchor._right);
        }
Exemplo n.º 22
0
        public void TryPopLeftFailsOnEmptyDeque()
        {
            //Arrange
            var deque = new ConcurrentDeque <Int32>();

            //Act & Assert
            Int32 item;

            Assert.False(deque.TryPopLeft(out item));
            Assert.AreEqual(item, default(Int32));
        }
Exemplo n.º 23
0
        private void VerifyState(ConcurrentDeque <int> deque, long expectedCount, long expectedSum)
        {
            //Assert
            Assert.Equal(expectedCount, deque.Count);
            Assert.Equal(expectedSum, deque.LongSum());

            //test internal state
            //traverse the deque in both directions
            Assert.Equal(expectedCount, deque.TraverseLeftRight().LongCount());
            Assert.Equal(expectedCount, deque.TraverseRightLeft().LongCount());
        }
Exemplo n.º 24
0
        public void ClearRemovesAllItems()
        {
            //Arrange
            var deque = new ConcurrentDeque <Int32>(new[] { 1, 2, 3, 4 });

            //Act
            deque.Clear();

            //Assert
            Assert.True(deque.IsEmpty);
        }
Exemplo n.º 25
0
        public void TryAdd_AppendsValue_ToTheRight()
        {
            //Act
            IProducerConsumerCollection <int> deque = new ConcurrentDeque <int>();

            deque.TryAdd(2);
            deque.TryAdd(3);

            //Assert
            Assert.Equal(new[] { 2, 3 }, deque);
        }
Exemplo n.º 26
0
        public void CollectionCopyToCopiesItems()
        {
            Int32[] array = { 0, 1, 2 };
            var     deque = new ConcurrentDeque <Int32>(array);

            var copy = new Int32[3];

            ((ICollection)deque).CopyTo(copy, 0);

            Assert.AreEqual(array, copy);
        }
Exemplo n.º 27
0
        public void TryPopRight_ReturnsTheRightmostItem()
        {
            //Arrange
            var deque = new ConcurrentDeque <int>(new[] { 1, 3, 5 });

            //Act & Assert
            int item;

            Assert.True(deque.TryPopRight(out item));
            Assert.Equal(item, 5);
            Assert.Equal(new[] { 1, 3 }, deque);
        }
Exemplo n.º 28
0
        public void ToArray_ReturnsSnapshot()
        {
            //Arrange
            int[] array = { 0, 1, 2 };
            var   deque = new ConcurrentDeque <int>(array);

            //Act
            var snapshot = deque.ToArray();

            //Assert
            Assert.Equal(array, snapshot);
        }
Exemplo n.º 29
0
        public void TryPopLeftReturnsTheLeftmostItem()
        {
            //Arrange
            var deque = new ConcurrentDeque <Int32>(new[] { 1, 3, 5 });

            //Act & Assert
            Int32 item;

            Assert.True(deque.TryPopLeft(out item));
            Assert.AreEqual(item, 1);
            Assert.AreEqual(new[] { 3, 5 }, deque);
        }
Exemplo n.º 30
0
        public void TryPopLeftReturnsTheLastRemainingItem()
        {
            //Arrange
            var deque = new ConcurrentDeque <Int32>();

            deque.PushRight(1);

            //Act & Assert
            Int32 item;

            Assert.True(deque.TryPopLeft(out item));
            Assert.AreEqual(item, 1);
            Assert.AreEqual(0, deque.Count);
        }
 internal WorkerThread(int index, ThreadTaskManager manager, Action threadStart)
 {
     this.manager = manager;
     thread = new Thread(ThreadExecutionLoop);
     thread.IsBackground = true;
     taskData = new ConcurrentDeque<TaskEntry>();
     this.threadStart = threadStart;
     UpdateIndex(index);
     thread.Start();
 }
Exemplo n.º 32
0
 internal WorkerThread(int index, ThreadTaskManager manager, Action<object> threadStart, object initializationInformation)
 {
     this.manager = manager;
     thread = new Thread(ThreadExecutionLoop);
     thread.IsBackground = true;
     taskData = new ConcurrentDeque<TaskEntry>();
     this.threadStart = threadStart;
     this.initializationInformation = initializationInformation;
     UpdateIndex(index);
     //#if WINDOWS
     //                ResourcePool.addThreadID(thread.ManagedThreadId);
     //#endif
     thread.Start();
 }