Beispiel #1
0
        public void DeleteLotsOfMinsTest()
        {
            const int size   = 1000;
            var       random = new Random(3456);
            var       heap   = LazyParingHeap <int> .Empty;

            for (var i = 0; i < size; i++)
            {
                heap = LazyParingHeap <int> .Insert(random.Next(size), heap);
            }
            var last  = 0;
            var count = 0;

            while (!LazyParingHeap <int> .IsEmpty(heap))
            {
                var next = LazyParingHeap <int> .FindMin(heap);

                heap = LazyParingHeap <int> .DeleteMin(heap);

                Assert.IsTrue(last <= next);
                last = next;
                count++;
            }
            Assert.AreEqual(size, count);
        }
Beispiel #2
0
        private static string DumpHeap <T>(LazyParingHeap <T> .Heap node, bool showSusp) where T : IComparable <T>
        {
            var result = new StringBuilder();

            result.Append("[");
            result.Append(node.Root);
            if (!LazyParingHeap <T> .IsEmpty(node.List))
            {
                result.Append(", ");
                result.Append(DumpHeap(node.List, showSusp));
            }
            if (showSusp || node.LazyList.IsValueCreated)
            {
                if (!LazyParingHeap <T> .IsEmpty(node.LazyList.Value))
                {
                    result.Append("; ");
                    result.Append(DumpHeap(node.LazyList.Value, showSusp));
                }
            }
            else
            {
                result.Append("; susp");
            }
            result.Append("]");
            return(result.ToString());
        }
Beispiel #3
0
        public void FindMinTest()
        {
            var heap = Enumerable.Range(0, 8).Aggregate(LazyParingHeap <int> .Empty, (current, i) => LazyParingHeap <int> .Insert(i, current));
            var min  = LazyParingHeap <int> .FindMin(heap);

            Assert.AreEqual(0, min);
        }
Beispiel #4
0
        public void InsertTest3()
        {
            var heap1 = Enumerable.Range(0, 3).Aggregate(LazyParingHeap <int> .Empty, (current, i) => LazyParingHeap <int> .Insert(i, current));
            var heap  = LazyParingHeap <int> .Insert(3, heap1);

            Assert.AreEqual("[0, [3]; [1, [2]]]", DumpHeap(heap, true));
        }
Beispiel #5
0
        public void InsertTest1()
        {
            var empty = LazyParingHeap <int> .Empty;
            var heap  = LazyParingHeap <int> .Insert(0, empty);

            Assert.AreEqual("[0]", DumpHeap(heap, true));
        }
Beispiel #6
0
        public void MergeTest4()
        {
            var heap1 = Enumerable.Range(0, 3).Aggregate(LazyParingHeap <int> .Empty, (current, i) => LazyParingHeap <int> .Insert(i, current));
            var heap2 = Enumerable.Range(10, 4).Aggregate(LazyParingHeap <int> .Empty, (current, i) => LazyParingHeap <int> .Insert(i, current));
            var heap  = LazyParingHeap <int> .Merge(heap1, heap2);

            Assert.AreEqual("[0, [10, [13]; [11, [12]]]; [1, [2]]]", DumpHeap(heap, true));
        }
Beispiel #7
0
        public void MergeTest2()
        {
            var empty = LazyParingHeap <int> .Empty;
            var heap2 = Enumerable.Range(0, 8).Aggregate(LazyParingHeap <int> .Empty, (current, i) => LazyParingHeap <int> .Insert(i, current));
            var heap  = LazyParingHeap <int> .Merge(empty, heap2);

            Assert.AreSame(heap2, heap);
        }
Beispiel #8
0
        public void DeleteMinTest()
        {
            var heap = Enumerable.Range(0, 8).Aggregate(LazyParingHeap <int> .Empty, (current, i) => LazyParingHeap <int> .Insert(i, current));

            heap = LazyParingHeap <int> .DeleteMin(heap);

            Assert.AreEqual("[1; [2; [3; [4, [5; [6, [7]]]]]]]", DumpHeap(heap, true));
        }
Beispiel #9
0
        public void EmptyTest()
        {
            var empty = LazyParingHeap <int> .Empty;

            Assert.IsTrue(LazyParingHeap <int> .IsEmpty(empty));

            var heap = LazyParingHeap <int> .Insert(3, empty);

            Assert.IsFalse(LazyParingHeap <int> .IsEmpty(heap));
        }
Beispiel #10
0
        public void DeleteMinEmptyTest()
        {
            var empty = LazyParingHeap <int> .Empty;

            AssertThrows <ArgumentNullException>(() => LazyParingHeap <int> .DeleteMin(empty));
        }