예제 #1
0
        public void TestQueueLikeBehavior()
        {
            Random          random    = new Random();
            TreeQueue <int> queue     = new TreeQueue <int>(branchingFactor: 4);
            Queue <int>     reference = new Queue <int>();

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

            while (queue.Count > 0)
            {
                var expected = reference.Peek();
                Assert.Equal(expected, queue.Peek());
                Assert.Equal(expected, reference.Dequeue());
                Assert.Equal(expected, queue.Dequeue());
                queue.Validate(ValidationRules.None);

                Assert.Equal(reference, queue);
            }

            Assert.Empty(queue);
            Assert.Empty(reference);
        }
예제 #2
0
        public void TestTrimExcess()
        {
            Random          random    = new Random();
            TreeQueue <int> queue     = new TreeQueue <int>(branchingFactor: 4);
            Queue <int>     reference = new Queue <int>();

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

            for (int i = 0; i < 2; i++)
            {
                queue.Dequeue();
                reference.Dequeue();
            }

            queue.Validate(ValidationRules.None);

            // In the first call to TrimExcess, items will move
            queue.TrimExcess();
            queue.Validate(ValidationRules.RequirePacked);
            Assert.Equal(reference, queue);

            // In the second call, the list is already packed so nothing will move
            queue.TrimExcess();
            queue.Validate(ValidationRules.RequirePacked);
            Assert.Equal(reference, queue);

            TreeQueue <int> empty = new TreeQueue <int>();

            empty.Validate(ValidationRules.RequirePacked);
            empty.TrimExcess();
            empty.Validate(ValidationRules.RequirePacked);

            TreeQueue <int> single = CreateTreeQueue(Enumerable.Range(0, 1));

            single.Validate(ValidationRules.RequirePacked);
            single.TrimExcess();
            single.Validate(ValidationRules.RequirePacked);
        }
예제 #3
0
        public void TestEnqueueStaysPacked()
        {
            TreeQueue <int> queue = new TreeQueue <int>(branchingFactor: 4);

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

            queue.Validate(ValidationRules.RequirePacked);
        }
예제 #4
0
        public void TestContains()
        {
            Random random = new Random();
            var    queue  = new TreeQueue <int>(branchingFactor: 4);

            for (int i = 0; i < 2 * 4 * 4; i++)
            {
                int value = random.Next(queue.Count + 1);
                queue.Enqueue(i);

                // Use queue.Contains(i) since this is a targeted collection API test
#pragma warning disable xUnit2017 // Do not use Contains() to check if a value exists in a collection
                Assert.True(queue.Contains(i));
#pragma warning restore xUnit2017 // Do not use Contains() to check if a value exists in a collection
            }

            queue.Validate(ValidationRules.RequirePacked);
        }