Ejemplo n.º 1
0
        public void CannotTopEmpty()
        {
            var pq = new TestPriorityQueue <char>();

            Assert.Throws <InvalidOperationException>(() => pq.Top());            // queue empty

            pq.Add('a');
            pq.Pop();

            Assert.Throws <InvalidOperationException>(() => pq.Top());            // queue empty
        }
Ejemplo n.º 2
0
        public void CanFixedCapacity7()
        {
            // Capacity 7: complete binary tree
            const int capacity = 7;
            var       pq       = new TestPriorityQueue <char>(capacity);

            Assert.AreEqual(7, pq.Capacity);
            Assert.AreEqual(0, pq.Count);

            pq.Add('e');
            Assert.AreEqual('e', pq.Top());
            pq.Add('g');
            Assert.AreEqual('e', pq.Top());
            pq.Add('b');
            Assert.AreEqual('b', pq.Top());
            pq.Add('c');
            Assert.AreEqual('b', pq.Top());
            pq.Add('a');
            Assert.AreEqual('a', pq.Top());
            pq.Add('f');
            Assert.AreEqual('a', pq.Top());
            pq.Add('d');
            Assert.AreEqual('a', pq.Top());

            Assert.AreEqual(7, pq.Count);
            Assert.AreEqual('a', pq.Top());

            Assert.AreEqual('a', pq.AddWithOverflow('x'));             // 'a' drops out
            Assert.AreEqual('a', pq.AddWithOverflow('a'));             // 'a' never gets in...

            Assert.AreEqual(7, pq.Count);

            Assert.AreEqual('b', pq.Top());
            Assert.AreEqual('b', pq.Pop());
            Assert.AreEqual('c', pq.Pop());
            Assert.AreEqual('d', pq.Pop());
            Assert.AreEqual('e', pq.Pop());
            Assert.AreEqual('f', pq.Pop());
            Assert.AreEqual('g', pq.Pop());
            Assert.AreEqual('x', pq.Top());
            Assert.AreEqual('x', pq.Pop());

            Assert.AreEqual(0, pq.Count);

            var ex = Assert.Catch(() => pq.Pop());

            Console.WriteLine("Expected: {0}", ex.Message);
        }
Ejemplo n.º 3
0
        public void CanFixedCapacity7()
        {
            // Capacity 7: complete binary tree
            const int capacity = 7;
            var       pq       = new TestPriorityQueue <char>(capacity);

            Assert.Equal(7, pq.Capacity);
            Assert.Equal(0, pq.Count);

            pq.Add('e');
            Assert.Equal('e', pq.Top());
            pq.Add('g');
            Assert.Equal('e', pq.Top());
            pq.Add('b');
            Assert.Equal('b', pq.Top());
            pq.Add('c');
            Assert.Equal('b', pq.Top());
            pq.Add('a');
            Assert.Equal('a', pq.Top());
            pq.Add('f');
            Assert.Equal('a', pq.Top());
            pq.Add('d');
            Assert.Equal('a', pq.Top());

            Assert.Equal(7, pq.Count);
            Assert.Equal('a', pq.Top());

            Assert.Equal('a', pq.AddWithOverflow('x'));             // 'a' drops out
            Assert.Equal('a', pq.AddWithOverflow('a'));             // 'a' never gets in...

            Assert.Equal(7, pq.Count);

            Assert.Equal('b', pq.Top());
            Assert.Equal('b', pq.Pop());
            Assert.Equal('c', pq.Pop());
            Assert.Equal('d', pq.Pop());
            Assert.Equal('e', pq.Pop());
            Assert.Equal('f', pq.Pop());
            Assert.Equal('g', pq.Pop());
            Assert.Equal('x', pq.Top());
            Assert.Equal('x', pq.Pop());

            Assert.Equal(0, pq.Count);

            Assert.Throws <InvalidOperationException>(() => pq.Pop());            //    queue empty
        }
Ejemplo n.º 4
0
        public void CannotTopEmpty()
        {
            var pq = new TestPriorityQueue <char>();

            pq.Add('a');
            pq.Pop();

            var ex = Assert.Catch <InvalidOperationException>(() => pq.Top());

            Console.WriteLine("Expected: {0}", ex.Message);
        }
Ejemplo n.º 5
0
        public void CanFixedCapacity0()
        {
            const int capacity = 0;
            var       pq       = new TestPriorityQueue <char>(capacity);

            Assert.Equal(0, pq.Capacity);
            Assert.Equal(0, pq.Count);

            Assert.Throws <InvalidOperationException>(() => pq.Top());            // queue empty
            Assert.Throws <InvalidOperationException>(() => pq.Pop());            // queue empty

            // With capacity zero, all additions immediately overflow:
            Assert.Equal('z', pq.AddWithOverflow('z'));
            Assert.Equal('a', pq.AddWithOverflow('a'));
            Assert.Throws <InvalidOperationException>(() => pq.Add('b'));            // queue full
        }
Ejemplo n.º 6
0
        public void CanFixedCapacity6()
        {
            // Capacity 6: incomplete binary tree
            const int capacity = 6;
            var       pq       = new TestPriorityQueue <char>(capacity);

            Assert.Equal(6, pq.Capacity);
            Assert.Equal(0, pq.Count);

            pq.Add('e');
            Assert.Equal('e', pq.Top());
            pq.Add('b');
            Assert.Equal('b', pq.Top());
            pq.Add('c');
            Assert.Equal('b', pq.Top());
            pq.Add('a');
            Assert.Equal('a', pq.Top());
            pq.Add('f');
            Assert.Equal('a', pq.Top());
            pq.Add('d');
            Assert.Equal('a', pq.Top());

            Assert.Equal(6, pq.Count);

            Assert.Equal('a', pq.AddWithOverflow('x'));
            Assert.Equal('a', pq.AddWithOverflow('a'));

            Assert.Equal(6, pq.Count);

            Assert.Equal('b', pq.Top());
            Assert.Equal('b', pq.Pop());
            Assert.Equal('c', pq.Pop());
            Assert.Equal('d', pq.Pop());
            Assert.Equal('e', pq.Pop());
            Assert.Equal('f', pq.Pop());
            Assert.Equal('x', pq.Pop());

            Assert.Equal(0, pq.Count);

            Assert.Throws <InvalidOperationException>(() => pq.Pop());            // queueu empty
        }
Ejemplo n.º 7
0
        public void CanFixedCapacity0()
        {
            const int capacity = 0;
            var       pq       = new TestPriorityQueue <char>(capacity);

            Assert.AreEqual(0, pq.Capacity);
            Assert.AreEqual(0, pq.Count);

            var ex1 = Assert.Catch(() => pq.Top());

            Console.WriteLine("Expected: {0}", ex1.Message);

            var ex2 = Assert.Catch(() => pq.Pop());

            Console.WriteLine("Expected: {0}", ex2.Message);

            // With capacity zero, all additions immediately overflow:
            Assert.AreEqual('z', pq.AddWithOverflow('z'));
            Assert.AreEqual('a', pq.AddWithOverflow('a'));
            var ex3 = Assert.Catch(() => pq.Add('b'));

            Console.WriteLine("Expected: {0}", ex3.Message);
        }
Ejemplo n.º 8
0
        public void CanRemove()
        {
            var pq = new TestPriorityQueue <char>();

            foreach (char c in "abcdefghijklmnopqrstuvwxyz")
            {
                pq.Add(c);
            }

            Assert.IsFalse(pq.Remove('$'));             // no such item
            Assert.AreEqual(26, pq.Count);

            // Last item: easy to remove
            Assert.IsTrue(pq.Remove('z'));
            Assert.AreEqual(25, pq.Count);
            Assert.AreEqual('a', pq.Top());
            Assert.IsFalse(pq.Contains('z'));

            // Remove a bottom row item:
            Assert.IsTrue(pq.Remove('w'));
            Assert.AreEqual(24, pq.Count);
            Assert.AreEqual('a', pq.Top());
            Assert.IsFalse(pq.Contains('w'));

            // Remove an inner item:
            Assert.IsTrue(pq.Remove('e'));
            Assert.AreEqual(23, pq.Count);
            Assert.AreEqual('a', pq.Top());
            Assert.IsFalse(pq.Contains('e'));

            // Remove the root item:
            Assert.IsTrue(pq.Remove('a'));
            Assert.AreEqual(22, pq.Count);
            Assert.AreEqual('b', pq.Top());
            Assert.IsFalse(pq.Contains('a'));

            // Remove returns false if not found:
            Assert.IsFalse(pq.Remove('z'));
            Assert.IsFalse(pq.Remove('w'));
            Assert.IsFalse(pq.Remove('e'));
            Assert.IsFalse(pq.Remove('a'));

            // Pop remaining items and verify order:
            Assert.AreEqual('b', pq.Pop());
            Assert.AreEqual('c', pq.Pop());
            Assert.AreEqual('d', pq.Pop());
            Assert.AreEqual('f', pq.Pop());
            Assert.AreEqual('g', pq.Pop());
            Assert.AreEqual('h', pq.Pop());
            Assert.AreEqual('i', pq.Pop());
            Assert.AreEqual('j', pq.Pop());
            Assert.AreEqual('k', pq.Pop());
            Assert.AreEqual('l', pq.Pop());
            Assert.AreEqual('m', pq.Pop());
            Assert.AreEqual('n', pq.Pop());
            Assert.AreEqual('o', pq.Pop());
            Assert.AreEqual('p', pq.Pop());
            Assert.AreEqual('q', pq.Pop());
            Assert.AreEqual('r', pq.Pop());
            Assert.AreEqual('s', pq.Pop());
            Assert.AreEqual('t', pq.Pop());
            Assert.AreEqual('u', pq.Pop());
            Assert.AreEqual('v', pq.Pop());
            Assert.AreEqual('x', pq.Pop());
            Assert.AreEqual('y', pq.Pop());

            Assert.AreEqual(0, pq.Count);
        }