示例#1
0
        public void ToPairsArray()
        {
            ToPairsArrayTest(1, 2, 3, 4);
            ToPairsArrayTest(
                new TestVertex("1"),
                new TestVertex("2"),
                new TestVertex("3"),
                new TestVertex("4"));
            ToPairsArrayTest(
                new EquatableTestVertex("1"),
                new EquatableTestVertex("2"),
                new EquatableTestVertex("3"),
                new EquatableTestVertex("4"));

            #region Local function

            void ToPairsArrayTest <TVertex>(
                TVertex vertex1,
                TVertex vertex2,
                TVertex vertex3,
                TVertex vertex4)
            {
                var distances = new Stack <double>(new[] { 123.0, 3.0, 2.0, 4.0, 5.0, 1.0 });
                var queue     = new BinaryQueue <TVertex, double>(_ => distances.Pop());

                // Empty heap
                CollectionAssert.IsEmpty(queue.ToPairsArray());

                queue.Enqueue(vertex1);
                queue.Enqueue(vertex2);
                queue.Enqueue(vertex2);
                queue.Enqueue(vertex3);
                queue.Enqueue(vertex1);

                // Array not sorted with distance
                CollectionAssert.AreEquivalent(
                    new[]
                {
                    new KeyValuePair <double, TVertex>(1.0, vertex1),
                    new KeyValuePair <double, TVertex>(5.0, vertex2),
                    new KeyValuePair <double, TVertex>(4.0, vertex2),
                    new KeyValuePair <double, TVertex>(2.0, vertex3),
                    new KeyValuePair <double, TVertex>(3.0, vertex1)
                },
                    queue.ToPairsArray());

                queue.Dequeue();
                queue.Dequeue();

                // Array not sorted with distance
                CollectionAssert.AreEquivalent(
                    new[]
                {
                    new KeyValuePair <double, TVertex>(3.0, vertex1),
                    new KeyValuePair <double, TVertex>(5.0, vertex2),
                    new KeyValuePair <double, TVertex>(4.0, vertex2)
                },
                    queue.ToPairsArray());

                queue.Dequeue();
                queue.Dequeue();
                queue.Dequeue();

                CollectionAssert.IsEmpty(queue.ToPairsArray());

                queue.Enqueue(vertex4);
                CollectionAssert.AreEqual(
                    new[] { new KeyValuePair <double, TVertex>(123.0, vertex4) },
                    queue.ToPairsArray());
            }

            #endregion
        }