Beispiel #1
0
        public void ShouldCopyTheTreeToArray()
        {
            AndersonTree <int, int> tree = new AndersonTree <int, int>();

            int[] data = new int[10];
            for (int i = 0; i < data.Length; i++)
            {
                data[i] = i * 10;
                tree.Add(data[i], data[i]);
            }

            KeyValuePair <int, int>[] copy = new KeyValuePair <int, int> [tree.Count];
            tree.CopyTo(copy, 0);

            for (int i = 0; i < data.Length; i++)
            {
                Assert.AreEqual(data[i], copy[i].Value);
            }

            KeyValuePair <int, int>[] middle_copy =
                new KeyValuePair <int, int> [tree.Count * 2];
            tree.CopyTo(middle_copy, tree.Count);

            for (int i = 0; i < data.Length; i++)
            {
                Assert.AreEqual(data[i], middle_copy[i + data.Length].Value);
            }
        }
Beispiel #2
0
        public void ShouldNotAllowDuplicates()
        {
            AndersonTree <int, int> tree = new AndersonTree <int, int>();

            tree.Add(10, 10);

            Assert.Throws <ArgumentException>(() => tree.Add(10, 10));
        }
Beispiel #3
0
        public void ShouldClearTheTree()
        {
            AndersonTree <int, int> tree = new AndersonTree <int, int>();

            tree.Add(10, 10);
            tree.Add(12, 32);
            tree.Add(56, 56);
            tree.Add(89, 89);
            Assert.AreNotEqual(0, tree.Count);
            tree.Clear();
            Assert.AreEqual(0, tree.Count);
        }
Beispiel #4
0
        public void ShouldVisitEachNodeInDescendingOrder()
        {
            AndersonTree <int, int> tree = new AndersonTree <int, int>();

            Stack <int> data = new Stack <int>(10);

            for (int i = 0; i < 10; i++)
            {
                data.Push(i * 10);
                tree.Add(i * 10, i * 10);
            }

            var visitor =
                new InOrderVisitor <int, int>(
                    new Visitor <int, int>(
                        (key, value, state) => Assert.AreEqual(data.Pop(), value)));

            tree.Accept(visitor, true);
        }
Beispiel #5
0
        public void ShouldVisitEachNodeInAscendingOrder()
        {
            AndersonTree <int, int> tree = new AndersonTree <int, int>();

            Queue <int> data = new Queue <int>(10);

            for (int i = 0; i < 10; i++)
            {
                data.Enqueue(i * 10);
                tree.Add(i * 10, i * 10);
            }

            var visitor =
                new InOrderVisitor <int, int>(
                    new Visitor <int, int>(
                        (key, value, state) => Assert.AreEqual(data.Dequeue(), value)));

            tree.Accept(visitor, false);
        }
Beispiel #6
0
        public void ShouldGetTheFirstNode()
        {
            AndersonTree <int, string> tree = new AndersonTree <int, string>();

            Assert.AreEqual(0, tree.First.Key);

            tree.Add(10, "10");
            Assert.AreEqual(10, tree.First.Key);

            tree.Add(11, "11");
            Assert.AreEqual(10, tree.First.Key);

            tree.Add(28, "28");
            Assert.AreEqual(10, tree.First.Key);

            tree.Add(5, "5");
            Assert.AreEqual(5, tree.First.Key);

            tree.Add(50, "50");
            Assert.AreEqual(5, tree.First.Key);
        }
Beispiel #7
0
        public void ShouldAcceptNullValues()
        {
            AndersonTree <string, string> tree = new AndersonTree <string, string>();

            Assert.DoesNotThrow(() => tree.Add("", null));
        }
Beispiel #8
0
        public void ShouldNotAcceptNullKeys()
        {
            AndersonTree <string, string> tree = new AndersonTree <string, string>();

            Assert.Throws <ArgumentNullException>(() => tree.Add(null, ""));
        }