コード例 #1
0
        public void TestEnqueue()
        {
            int value = Generator.GetInt32();

            var queue = ImmutableTreeQueue.Create <int>();

            Assert.Empty(queue);

            // Enqueue doesn't change the original queue
            queue.Enqueue(value);
            Assert.Empty(queue);

            queue = queue.Enqueue(value);
            Assert.Single(queue);
            Assert.Equal(value, queue.Peek());
            int[] expected = { value };
            int[] actual   = queue.ToArray();
            Assert.Equal(expected, actual);

            // Test through the IImmutableQueue<T> interface
            IImmutableQueue <int> immutableQueue = queue;

            immutableQueue.Enqueue(Generator.GetInt32());
            Assert.Equal(expected, immutableQueue);

            int nextValue = Generator.GetInt32();

            immutableQueue = immutableQueue.Enqueue(nextValue);
            Assert.Equal(new[] { value, nextValue }, immutableQueue);
        }
コード例 #2
0
        public void TestMultipleElementQueue()
        {
            var values = new[] { Generator.GetInt32(), Generator.GetInt32(), Generator.GetInt32() };
            var queue  = ImmutableTreeQueue.Create(values);

            Assert.Equal(values, queue);
        }
コード例 #3
0
        public void TestSingleElementQueue()
        {
            var value = Generator.GetInt32();
            var queue = ImmutableTreeQueue.Create(value);

            Assert.Equal(new[] { value }, queue);
        }
コード例 #4
0
        public void TestIEnumeratorT()
        {
            var queue = ImmutableTreeQueue.Create <int>();
            IEnumerator <int> enumerator = queue.GetEnumerator();

            Assert.Equal(0, enumerator.Current);
            Assert.False(enumerator.MoveNext());
            Assert.Equal(0, enumerator.Current);

            // Adding an item to the queue does not invalidate an enumerator
            CollectionAssert.EnumeratorNotInvalidated(queue, () => queue.Enqueue(1));

            queue      = queue.Enqueue(1);
            enumerator = queue.GetEnumerator();
            Assert.Equal(0, enumerator.Current);
            Assert.True(enumerator.MoveNext());
            Assert.Equal(1, enumerator.Current);
            Assert.False(enumerator.MoveNext());
            Assert.Equal(1, enumerator.Current);

            enumerator.Reset();
            Assert.Equal(0, enumerator.Current);
            Assert.True(enumerator.MoveNext());
            Assert.Equal(1, enumerator.Current);
            enumerator.Reset();
            Assert.Equal(0, enumerator.Current);
            Assert.True(enumerator.MoveNext());
            Assert.Equal(1, enumerator.Current);
            Assert.False(enumerator.MoveNext());
            Assert.Equal(1, enumerator.Current);
        }
コード例 #5
0
        public void TestEmptyImmutableTreeQueue()
        {
            var queue = ImmutableTreeQueue.Create <int>();

            Assert.Same(ImmutableTreeQueue <int> .Empty, queue);
            Assert.Empty(queue);
        }
コード例 #6
0
        public void TestQueueLikeBehavior()
        {
            var queue     = ImmutableTreeQueue.Create <int>();
            var reference = new Queue <int>();

            for (int i = 0; i < 2 * 4 * 4; i++)
            {
                int item = Generator.GetInt32();
                queue = queue.Enqueue(item);
                reference.Enqueue(item);
            }

            while (!queue.IsEmpty)
            {
                var expected = reference.Peek();
                Assert.Equal(expected, queue.Peek());
                Assert.Equal(expected, reference.Dequeue());

                IImmutableQueue <int> immutableQueue = queue;

                queue = queue.Dequeue(out int value);
                Assert.Equal(expected, value);
                queue.Validate(ValidationRules.None);

                Assert.Equal(reference, queue);

                // Test through the IImmutableQueue<T> interface (initialized above)
                immutableQueue = immutableQueue.Dequeue(out value);
                Assert.Equal(expected, value);
                Assert.Equal(reference, immutableQueue);
            }

            Assert.Empty(queue);
            Assert.Empty(reference);
        }
コード例 #7
0
        public void TestEnqueueStaysPacked()
        {
            var queue = ImmutableTreeQueue.Create <int>();

            for (int i = 0; i < 4 * 8 * 8; i++)
            {
                queue.Enqueue(i);
            }

            queue.Validate(ValidationRules.RequirePacked);
        }