コード例 #1
0
        public void TestQueueIsEmptyAfterDequeueingEnqueuedElements(IBlockingArrayQueue <int> queue)
        {
            queue.Clear();

            const int n = 10;

            var elements  = new List <int>();
            var producers = new List <Task>(n);

            for (var i = 0; i < n; i++)
            {
                var value = i + 1;
                elements.Add(value);
                producers.Add(new Task(() => queue.Enqueue(value)));
            }

            producers.ForEach(p => p.Start());
            producers.ForEach(p => p.Wait());

            var consumers = new List <Task>(10);

            for (var i = 0; i < n; i++)
            {
                consumers.Add(new Task(() => queue.Dequeue()));
            }

            consumers.ForEach(c => c.Start());
            consumers.ForEach(c => c.Wait());

            Assert.True(queue.IsEmpty());
        }
コード例 #2
0
        public void TestTryDequeueOnEmptyQueueFails(IBlockingArrayQueue <int> queue)
        {
            queue.Clear();

            var value = 0;

            Assert.False(queue.TryDequeue(ref value));
        }
コード例 #3
0
        public void TestTryDequeueIsSuccessfulOnNotEmptyQueue(IBlockingArrayQueue <int> queue)
        {
            queue.Clear();

            queue.Enqueue(1);
            var value = 0;

            Assert.True(queue.TryDequeue(ref value));
        }
コード例 #4
0
        public void TestTryDequeueDequeuesEnqueuedElement(IBlockingArrayQueue <int> queue)
        {
            queue.Clear();

            const int element = 1;

            queue.Enqueue(element);

            var dequeuedElement = 0;

            queue.TryDequeue(ref dequeuedElement);
            Assert.AreEqual(element, dequeuedElement);
        }
コード例 #5
0
        public void TestQueueIsNotEmptyAfterEnqueueingElements(IBlockingArrayQueue <int> queue)
        {
            queue.Clear();

            const int n = 10;

            var producers = new List <Task>(n);

            for (var i = 0; i < n; i++)
            {
                var value = i + 1;
                producers.Add(new Task(() => queue.Enqueue(value)));
            }

            producers.ForEach(p => p.Start());
            producers.ForEach(p => p.Wait());

            Assert.False(queue.IsEmpty());
        }
コード例 #6
0
        public void TestTryEnqueueOnFullQueueFails(IBlockingArrayQueue <int> queue)
        {
            queue.Clear();

            const int n = 10;

            var producers = new List <Task>(10);

            for (var i = 0; i < n; i++)
            {
                var value = i + 1;
                producers.Add(new Task(() => queue.Enqueue(value)));
            }

            producers.ForEach(p => p.Start());
            producers.ForEach(p => p.Wait());

            Assert.False(queue.TryEnqueue(11));
        }
コード例 #7
0
        public void TestClearClearesQueue(IBlockingArrayQueue <int> queue)
        {
            queue.Clear();

            const int n = 10;

            var producers = new List <Task>(10);

            for (var i = 0; i < n; i++)
            {
                var value = i + 1;
                producers.Add(new Task(() => queue.Enqueue(value)));
            }

            producers.ForEach(p => p.Start());
            producers.ForEach(p => p.Wait());

            queue.Clear();
            Assert.That(queue.IsEmpty);
        }
コード例 #8
0
        public void TestQueueContainsEnqueuedElements(IBlockingArrayQueue <int> queue)
        {
            queue.Clear();

            const int n = 10;

            var elements  = new List <int>();
            var producers = new List <Task>(n);

            for (var i = 0; i < n; i++)
            {
                var value = i + 1;
                elements.Add(value);
                producers.Add(new Task(() => queue.Enqueue(value)));
            }

            producers.ForEach(p => p.Start());
            producers.ForEach(p => p.Wait());

            while (!queue.IsEmpty())
            {
                Assert.True(elements.Contains(queue.Dequeue()));
            }
        }
コード例 #9
0
        public void TestEmptyQueueHasSize0(IBlockingArrayQueue <int> queue)
        {
            queue.Clear();

            Assert.AreEqual(0, queue.Size());
        }
コード例 #10
0
        public void TestEmptyQueueIsEmpty(IBlockingArrayQueue <int> queue)
        {
            queue.Clear();

            Assert.That(queue.IsEmpty);
        }
コード例 #11
0
        public void TestTryEnqueueIsSuccessfulOnNotFullQueue(IBlockingArrayQueue <int> queue)
        {
            queue.Clear();

            Assert.True(queue.TryEnqueue(1));
        }
コード例 #12
0
        public void TestQueueRemainsEmptyAfterSameNumberOfSimultaneousEnqueuesAndDequeues(IBlockingArrayQueue <int> queue)
        {
            queue.Clear();

            const int n = 10;

            var elements = new List <int>();
            var producersAndConsumers = new List <Task>(n);

            for (var i = 0; i < n; i++)
            {
                var value = i + 1;
                elements.Add(value);
                producersAndConsumers.Add(new Task(() => queue.Enqueue(value)));
                producersAndConsumers.Add(new Task(() => queue.Dequeue()));
            }

            producersAndConsumers.ForEach(p => p.Start());
            producersAndConsumers.ForEach(p => p.Wait());

            Assert.True(queue.IsEmpty());
        }