Exemple #1
0
        protected HeapNode Pop()
        {
            HeapNode node = this.Heap.Pop();

            Assert.IsTrue(this.IsValidHeap());
            return(node);
        }
        public void TestPushThrowsOnAlreadyPushedNode()
        {
            HeapNode node = new HeapNode(this.RandomValue());

            this.Push(node);

            Assert.Throws <InvalidOperationException>(() => this.Heap.Push(node));
        }
        public void TestDebugPopThrowsOnCorruptedHeap()
        {
            HeapNode node1 = new HeapNode(1);
            HeapNode node2 = new HeapNode(2);

            this.Push(node1);
            this.Push(node2);

            node1.Value = 3;

            Assert.Throws <InvalidOperationException>(() => this.Heap.Pop());
        }
        public void TestHeapAutomaticallyResizes()
        {
            for (int i = 0; i < 1000; i++)
            {
                this.Push(new HeapNode(i));
                Assert.AreEqual(i + 1, this.Heap.Count);
            }

            for (int i = 0; i < 1000; i++)
            {
                HeapNode node = this.Pop();
                Assert.AreEqual(i, node.Value);
            }
        }
Exemple #5
0
        public void TestContains()
        {
            HeapNode node1 = new HeapNode(this.RandomValue());
            HeapNode node2 = new HeapNode(this.RandomValue());

            // A newly created heap contains no elements.
            Assert.IsFalse(this.Heap.Contains(node1));

            // Once a node is pushed to the heap, it's contained in it.
            this.Push(node1);
            Assert.IsTrue(this.Heap.Contains(node1));

            this.Push(node2);
            Assert.IsTrue(this.Heap.Contains(node1));
            Assert.IsTrue(this.Heap.Contains(node2));
        }
Exemple #6
0
        public void TestRemove()
        {
            int num = this.Rng.Next(1, 10);

            HeapNode[] nodes = new HeapNode[num];
            for (int i = 0; i < num; i++)
            {
                nodes[i] = new HeapNode(this.RandomValue());
                this.Push(nodes[i]);
            }

            HeapNode node = nodes[this.Rng.Next(0, num)];

            this.Heap.Remove(node);
            Assert.IsFalse(this.Heap.Contains(node));
            Assert.AreEqual(num - 1, this.Heap.Count);
            Assert.IsTrue(this.IsValidHeap());
        }
Exemple #7
0
        public void TestPeek()
        {
            int      value = this.RandomValue();
            HeapNode node1 = new HeapNode(value);

            this.Heap.Push(node1);
            Assert.AreEqual(node1, this.Heap.Peek());

            HeapNode node2 = new HeapNode(value - 1);

            this.Heap.Push(node2);
            Assert.AreEqual(node2, this.Heap.Peek());

            HeapNode node3 = new HeapNode(value + 1);

            this.Heap.Push(node3);
            Assert.AreEqual(node2, this.Heap.Peek());
        }
Exemple #8
0
        public void TestHeapify()
        {
            int num = this.Rng.Next(1, 10);

            HeapNode[] nodes = new HeapNode[num];
            int        min   = int.MaxValue;

            for (int i = 0; i < num; i++)
            {
                nodes[i] = new HeapNode(this.RandomValue());
                this.Push(nodes[i]);
                if (nodes[i].Value < min)
                {
                    min = (int)nodes[i].Value;
                }
            }

            HeapNode node = nodes[this.Rng.Next(0, num)];

            node.Value = min - 1;

            this.Heap.Heapify(node);
            Assert.AreEqual(node, this.Pop());
        }
Exemple #9
0
 protected void Push(HeapNode node)
 {
     this.Heap.Push(node);
     Assert.IsTrue(this.IsValidHeap());
 }
        public void TestRemoveThrowsOnNodeNotInHeap()
        {
            HeapNode node = new HeapNode(this.RandomValue());

            Assert.Throws <InvalidOperationException>(() => this.Heap.Remove(node));
        }