コード例 #1
0
        public void PeekDoesNotDequeueItem()
        {
            var queue = new LeakyQueue <int>();

            queue.Enqueue(12);
            queue.Enqueue(34);

            Assert.AreEqual(12, queue.Peek());
            Assert.AreEqual(12, queue.Peek());
        }
コード例 #2
0
        public void QueueHeadCanBeAdvanced()
        {
            var queue = new LeakyQueue <int>();

            queue.Enqueue(12);
            queue.Enqueue(34);

            Assert.AreEqual(12, queue.Peek());
            queue.AdvanceHead();
            Assert.AreEqual(34, queue.Peek());
        }
コード例 #3
0
        public void CountPropertyMatchesNumberOfContainedItems()
        {
            var queue = new LeakyQueue <int>();

            for (int index = 0; index < 42; ++index)
            {
                queue.Enqueue(index);

                Assert.AreEqual(index + 1, queue.Count);
            }
        }
コード例 #4
0
        public void CapacityCanBePassedToConstructor()
        {
            var queue = new LeakyQueue <int>(123);

            int[] oldArray = queue.Items;
            for (int index = 0; index < 123; ++index)
            {
                queue.Enqueue(index);
            }
            Assert.AreSame(oldArray, queue.Items);
        }
コード例 #5
0
        public void InternallyWrappedQueueCanBeConvertedIntoArray()
        {
            var queue = new LeakyQueue <int>();

            for (int index = 0; index < 4; ++index)
            {
                queue.Enqueue(index);
            }
            queue.Dequeue();
            queue.Dequeue();
            for (int index = 4; index < 6; ++index)
            {
                queue.Enqueue(index);
            }

            Assert.AreEqual(
                new int[] { 2, 3, 4, 5 },
                queue.ToArray()
                );
        }
コード例 #6
0
        public void QueueBecomesEmptyAfterCallingClear()
        {
            var queue = new LeakyQueue <int>();

            for (int index = 0; index < 14; ++index)
            {
                queue.Enqueue(index);
            }

            Assert.AreEqual(14, queue.Count);
            queue.Clear();
            Assert.AreEqual(0, queue.Count);
        }
コード例 #7
0
        public void QueueReturnsItemsInFifoOrder()
        {
            var queue = new LeakyQueue <int>();

            for (int index = 0; index < 15; ++index)
            {
                queue.Enqueue(index);
            }
            for (int index = 0; index < 15; ++index)
            {
                Assert.AreEqual(index, queue.Dequeue());
            }
        }
コード例 #8
0
        public void QueueCanBeConvertedIntoArray()
        {
            var queue = new LeakyQueue <int>();

            for (int index = 0; index < 4; ++index)
            {
                queue.Enqueue(index);
            }

            Assert.AreEqual(
                new int[] { 0, 1, 2, 3 },
                queue.ToArray()
                );
        }
コード例 #9
0
        public void ContainsMethodFindsNullItems()
        {
            var queue = new LeakyQueue <object>();

            Assert.IsFalse(queue.Contains(109));

            for (int index = 0; index < 12; ++index)
            {
                queue.Enqueue(new object());
            }

            Assert.IsFalse(queue.Contains(null));

            queue.Enqueue(null);

            Assert.IsTrue(queue.Contains(null));

            for (int index = 0; index < 13; ++index)
            {
                queue.Enqueue(new object());
            }

            Assert.IsTrue(queue.Contains(null));
        }
コード例 #10
0
        public void ContainsMethodFindsSearchedItem()
        {
            var queue = new LeakyQueue <int>();

            Assert.IsFalse(queue.Contains(109));

            for (int index = 0; index < 12; ++index)
            {
                queue.Enqueue(index);
            }

            Assert.IsFalse(queue.Contains(109));

            queue.Enqueue(109);

            Assert.IsTrue(queue.Contains(109));

            for (int index = 0; index < 13; ++index)
            {
                queue.Enqueue(index);
            }

            Assert.IsTrue(queue.Contains(109));
        }
コード例 #11
0
        public void QueueCanEnsureCapacityForOneMoreSlot()
        {
            var queue = new LeakyQueue <int>();

            // Make sure the queue is filled to capacity. It is a valid implementation
            // to increase capacity in advance, so we just take the initial capacity
            // and make sure to fill up to that. The queue may have increased capacity already.
            int capacity = queue.Items.Length;

            while (queue.Count < capacity)
            {
                queue.Enqueue(123);
            }

            queue.EnsureSlotAvailable();

            Assert.Greater(queue.Items.Length, queue.Count);
        }
コード例 #12
0
        public void HeadAndTailIndexCanBeQueried()
        {
            var queue = new LeakyQueue <int>();

            for (int run = 0; run < 4; ++run)
            {
                for (int index = 0; index < 16; ++index)
                {
                    queue.Enqueue(index);
                }
                for (int index = 0; index < 8; ++index)
                {
                    queue.Dequeue();
                }
            }

            // We can't make any assumptions about how the queue works, thus:
            Assert.GreaterOrEqual(queue.HeadIndex, 0);
            Assert.Less(queue.HeadIndex, queue.Items.Length);
            Assert.GreaterOrEqual(queue.TailIndex, 0);
            Assert.Less(queue.TailIndex, queue.Items.Length);
        }