Exemplo n.º 1
0
        public void TestAddAllClear()
        {
            MinHeap <int> mh = new MinHeap <int>(intComparer, new int[] { 1, 3, 4, 0, -1, 2, 5 });

            Assert.AreEqual(mh.Count, 7);
            Assert.IsFalse(mh.IsEmpty);
            Assert.AreEqual(mh.RemoveFirst(), -1);
            Assert.AreEqual(mh.RemoveFirst(), 0);
            Assert.AreEqual(mh.RemoveFirst(), 1);
            Assert.AreEqual(mh.RemoveFirst(), 2);
            Assert.AreEqual(mh.RemoveFirst(), 3);
            Assert.AreEqual(mh.RemoveFirst(), 4);
            Assert.AreEqual(mh.RemoveFirst(), 5);
            Assert.IsTrue(mh.IsEmpty);

            mh.AddAll(new int[] { 4, 3, 2, 1, 0 });
            Assert.IsFalse(mh.IsEmpty);
            Assert.AreEqual(mh.Count, 5);
            Assert.AreEqual(mh.First, 0);

            mh.Clear();
            Assert.IsTrue(mh.IsEmpty);
            Assert.AreEqual(mh.Count, 0);
            Assert.AreEqual(mh.Capacity, 1);

            mh.AddAll(1, 2, 5, 10);
            Assert.AreEqual(mh.Count, 4);
            Assert.IsTrue(mh.Capacity >= 4);
            Assert.AreEqual(mh.RemoveFirst(), 1);
            Assert.AreEqual(mh.RemoveFirst(), 2);
            Assert.AreEqual(mh.RemoveFirst(), 5);
            Assert.AreEqual(mh.RemoveFirst(), 10);
            Assert.IsTrue(mh.IsEmpty);
        }
Exemplo n.º 2
0
        public void TestReferenceType()
        {
            MinHeap <Node> mh = new MinHeap <Node>(Node.Comparer);

            mh.Add(new Node(3));
            Assert.AreEqual(mh.First.n, 3);
            Assert.AreEqual(mh.Count, 1);
            mh.AddAll(new Node[] { new Node(1), new Node(4), new Node(0) });
            Assert.AreEqual(mh.Count, 4);
            Assert.AreEqual(mh.RemoveFirst().n, 0);
            Assert.AreEqual(mh.RemoveFirst().n, 1);
            Assert.AreEqual(mh.First.n, 3);
            Assert.AreEqual(mh.Count, 2);
            mh.Clear();
            Assert.AreEqual(mh.Count, 0);
            Assert.IsNull(mh.First);
        }