Example #1
0
        private void BasicMap()
        {
            const int Count = 1000;

            IOrderedMap <int, float> map;

            switch (args[0])
            {
            default:
                throw new ArgumentException();

            case "avl":
                map = new AVLTreeMap <int, float>();
                break;

            case "redblack":
                map = new RedBlackTreeMap <int, float>();
                break;

            case "splay":
                map = new SplayTreeMap <int, float>();
                break;
            }

            BasicMapCore(map, Count);
        }
Example #2
0
        // "You know it seemed a bit daft me having to guard him when he's a guard."
        private static void TestFramework()
        {
            TextWriter savedOutput = Console.Out;

            Console.SetOut(TextWriter.Null);

            TestBase    testBase    = new TestBase();
            UnitTestMap unitTestMap = new UnitTestMap();

            testBase.TestNoThrow(String.Empty, delegate() { testBase.TestTrue(String.Empty, delegate() { return(true); }); });
            testBase.TestThrow(String.Empty, typeof(UnitTestFailureException), delegate() { testBase.TestTrue(String.Empty, delegate() { return(false); }); });
            testBase.TestThrow(String.Empty, typeof(UnitTestFailureException), delegate() { testBase.TestTrue(String.Empty, delegate() { throw new NotImplementedException(); }); });
            testBase.TestThrow(String.Empty, typeof(UnitTestFailureException), delegate() { testBase.TestNoThrow(String.Empty, delegate() { throw new NotImplementedException(); }); });
            testBase.TestThrow(String.Empty, typeof(UnitTestFailureException), delegate() { testBase.TestThrow(String.Empty, typeof(NotImplementedException), delegate() { throw new NotImplementedException(); }); });
            testBase.TestThrow(String.Empty, typeof(UnitTestFailureException), delegate() { testBase.TestThrow(String.Empty, typeof(NotImplementedException), delegate() { throw new Exception(); }); });
            testBase.TestNoThrow(String.Empty, delegate() { unitTestMap.TestTree(String.Empty, new SplayTreeMap <int, bool>(), new UnitTestMap.Op <int, bool>[] { }, delegate() { }); });
            testBase.TestThrow(String.Empty, typeof(UnitTestFailureException), delegate() { unitTestMap.TestTree(String.Empty, new SplayTreeMap <int, bool>(), new UnitTestMap.Op <int, bool>[] { new FailOp(), }, delegate() { throw new NotImplementedException(); }); });
            testBase.TestThrow(String.Empty, typeof(UnitTestFailureException), delegate() { unitTestMap.TestTree(String.Empty, new SplayTreeMap <int, bool>(), new UnitTestMap.Op <int, bool>[] { }, delegate() { throw new NotImplementedException(); }); });

            Console.WriteLine(new Range2MapEntry(new Range(0, 0), new Range(0, 0), String.Empty).ToString());
            Console.WriteLine(new MultiRankMapEntry(0, new Range(0, 0), 0));

            SplayTreeMap <int, int> tree = new SplayTreeMap <int, int>();

            tree.Add(1, 1);
            tree.Add(2, 2);
            TestBase.Dump(tree);

            Console.SetOut(savedOutput);
        }
Example #3
0
        public void AddingElementsWithIndexer()
        {
            BinarySearchTreeMap <int, int> tree = new SplayTreeMap <int, int>();

            int elementsCount = 10000;

            //To make it more balanced
            tree[50] = 50;
            tree[75] = 75;
            tree[25] = 25;

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.ContainsKey(el))
                    {
                        tree[el] = el;
                        if (tree.Root.Key != el)
                        {
                            Assert.Fail();
                        }
                    }
                    else if (tree.Root.Key != el)
                    {
                        Assert.Fail();
                    }
                    el += i;
                }
            }

            int  last              = -1;
            int  count             = 0;
            bool elementsAreSorted = true;

            foreach (var item in tree)
            {
                if (last > item.Key)
                {
                    elementsAreSorted = false;
                }
                last = item.Key;
                count++;
            }

            Trace.WriteLine(tree.Count);

            Assert.IsTrue(tree.Count == elementsCount &&
                          elementsAreSorted &&
                          count == elementsCount);
        }
Example #4
0
        public void SortedElementsAfterAdding()
        {
            var tree = new SplayTreeMap <int, int>();

            int elementsCount = 10000;

            //To make it more balanced
            tree.Add(50, 50);
            tree.Add(75, 75);
            tree.Add(25, 25);

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.ContainsKey(el))
                    {
                        tree.Add(el, el);
                        if (tree.Root.Key != el)
                        {
                            Assert.Fail();
                        }
                    }
                    else if (tree.Root.Key != el)
                    {
                        Assert.Fail();
                    }
                    el += i;
                }
            }

            int  last              = -1;
            int  count             = 0;
            bool elementsAreSorted = true;

            foreach (var item in tree)
            {
                if (last > item.Key)
                {
                    elementsAreSorted = false;
                }
                last = item.Key;
                count++;
            }

            Assert.IsTrue(tree.Count == elementsCount &&
                          elementsAreSorted &&
                          count == elementsCount);
        }
Example #5
0
        public void CheckIfNodeIsInvalidatedAfterClearingAndAfterRemoval()
        {
            var tree = new SplayTreeMap <int, int>();

            tree.Add(3, 3);
            tree.Add(1, 1);
            tree.Add(2, 2);

            // Tree looks like this:
            //   2
            //  / \
            // 1   3

            var node1 = tree.Root.Left;
            var node2 = tree.Root;
            var node3 = tree.Root.Right;

            tree.Remove(2);
            if (node2.Left != null || node2.Right != null)
            {
                Assert.Fail("2");
            }

            tree.Remove(3);
            if (node3.Left != null || node3.Right != null)
            {
                Assert.Fail("3");
            }

            tree.Remove(1);
            if (node1.Left != null || node1.Right != null)
            {
                Assert.Fail("1");
            }

            tree.Add(3, 3);
            tree.Add(1, 1);
            tree.Add(2, 2);

            node1 = tree.Root.Left;
            node2 = tree.Root;
            node3 = tree.Root.Right;

            tree.Clear();

            Assert.IsTrue(node1.Left == null && node1.Right == null &&
                          node2.Left == null && node2.Right == null &&
                          node3.Left == null && node3.Right == null &&
                          tree.Root == null &&
                          tree.Count == 0);
        }
Example #6
0
        public void RemoveAllExceptOne()
        {
            var tree = new SplayTreeMap <int, int>();

            int elementsCount = 10000;

            //To make it more balanced
            tree.Add(50, 50);
            tree.Add(75, 75);
            tree.Add(25, 25);

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.ContainsKey(el))
                    {
                        tree.Add(el, el);
                        if (tree.Root.Key != el)
                        {
                            Assert.Fail();
                        }
                    }
                    else if (tree.Root.Key != el)
                    {
                        Assert.Fail();
                    }
                    el += i;
                }
            }

            bool removedEverything = true;

            for (int i = 0; i < elementsCount - 1; i++)
            {
                if (!tree.Remove(i))
                {
                    removedEverything = false;
                }
            }

            Assert.IsTrue(tree.Count == 1 &&
                          removedEverything &&
                          tree.Root.Value == elementsCount - 1);
        }
Example #7
0
        private void EnumerateMap()
        {
            const int Count = 1000;

            IOrderedMap <int, float> map;

            switch (args[0])
            {
            default:
                throw new ArgumentException();

            case "avl":
                map = new AVLTreeMap <int, float>();
                break;

            case "redblack":
                map = new RedBlackTreeMap <int, float>();
                break;

            case "splay":
                map = new SplayTreeMap <int, float>();
                break;
            }

            bool fast;

            switch (args[1])
            {
            default:
                throw new ArgumentException();

            case "fast":
                fast = true;
                break;

            case "robust":
                fast = false;
                break;
            }

            EnumerateCore(map, Count, fast);
        }
Example #8
0
        public void ContatinsValueBeforeAndAfterUpdatingValue()
        {
            BinarySearchTreeMap <int, int> tree = new SplayTreeMap <int, int>();

            int elementsCount = 1000;

            //To make it more balanced
            tree.Add(50, 50);
            tree.Add(75, 75);
            tree.Add(25, 25);

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.ContainsKey(el))
                    {
                        tree.Add(el, el);
                        if (tree.Root.Key != el)
                        {
                            Assert.Fail();
                        }
                    }
                    else if (tree.Root.Key != el)
                    {
                        Assert.Fail();
                    }
                    el += i;
                }
            }

            // Check if values are contained, make them negative and check again. Skip zero...
            for (int i = 1; i < elementsCount; i++)
            {
                if (!tree.ContainsValue(i))
                {
                    Assert.Fail();
                }
                if (tree.Root.Value != i)
                {
                    Assert.Fail();
                }
                tree[i] = -i;
                if (tree.Root.Value != -i)
                {
                    Assert.Fail();
                }
                if (tree.ContainsValue(i))
                {
                    Assert.Fail();
                }
                if (!tree.ContainsValue(-i))
                {
                    Assert.Fail();
                }
            }

            int  last              = -1;
            int  count             = 0;
            bool elementsAreSorted = true;

            foreach (var item in tree)
            {
                if (last > item.Key)
                {
                    elementsAreSorted = false;
                }
                last = item.Key;
                count++;
            }

            Trace.WriteLine(tree.Count);

            Assert.IsTrue(tree.Count == elementsCount &&
                          elementsAreSorted &&
                          count == elementsCount);
        }
Example #9
0
        public void UpdatingElementsWithIndexerUsingTryGetValueMethodToGetValue()
        {
            BinarySearchTreeMap <int, int> tree = new SplayTreeMap <int, int>();

            int elementsCount = 10000;

            //To make it more balanced
            tree[50] = 50;
            tree[75] = 75;
            tree[25] = 25;

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.ContainsKey(el))
                    {
                        tree.Add(el, el);
                        if (tree.Root.Key != el)
                        {
                            Assert.Fail();
                        }
                    }
                    else if (tree.Root.Key != el)
                    {
                        Assert.Fail();
                    }
                    el += i;
                }
            }

            bool removedEverything = true;

            //Removing every second number
            for (int i = 0; i < elementsCount; i += 2)
            {
                if (!tree.Remove(i))
                {
                    removedEverything = false;
                }
            }

            // Make all values negative
            for (int i = 0; i < elementsCount; i++)
            {
                int value;
                if (tree.TryGetValue(i, out value))
                {
                    if (tree.Root.Key != i)
                    {
                        Assert.Fail();
                    }
                    if (tree.Root.Value != value)
                    {
                        Assert.Fail();
                    }
                    tree[value] = -value;
                    if (tree.Root.Key != i)
                    {
                        Assert.Fail();
                    }
                    if (tree.Root.Value != -value)
                    {
                        Assert.Fail();
                    }
                }
            }


            int  last              = -1;
            int  count             = 0;
            bool elementsAreSorted = true;

            foreach (var item in tree)
            {
                if (item.Value > 0)
                {
                    Assert.Fail();
                }
                if (last > item.Key)
                {
                    elementsAreSorted = false;
                }
                last = item.Key;
                count++;
            }

            Trace.WriteLine(tree.Count);

            Assert.IsTrue(tree.Count == count &&
                          elementsAreSorted &&
                          removedEverything);
        }
Example #10
0
        public void AddingAfterRemovingAllElements()
        {
            var tree = new SplayTreeMap <int, int>();

            int elementsCount = 10000;

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.ContainsKey(el))
                    {
                        tree.Add(el, el);
                        if (tree.Root.Key != el)
                        {
                            Assert.Fail();
                        }
                    }
                    else if (tree.Root.Key != el)
                    {
                        Assert.Fail();
                    }
                    el += i;
                }
            }

            if (tree.Count != elementsCount)
            {
                Assert.Fail();
            }

            //Removing every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    tree.Remove(el);
                    el += i;
                }
            }

            if (tree.Count != 0)
            {
                Assert.Fail();
            }

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.ContainsKey(el))
                    {
                        tree.Add(el, el);
                        if (tree.Root.Key != el)
                        {
                            Assert.Fail();
                        }
                    }
                    else if (tree.Root.Key != el)
                    {
                        Assert.Fail();
                    }
                    el += i;
                }
            }

            int  last              = -1;
            int  count             = 0;
            bool elementsAreSorted = true;

            foreach (var item in tree)
            {
                if (last > item.Key)
                {
                    elementsAreSorted = false;
                }
                last = item.Key;
                count++;
            }

            Assert.IsTrue(tree.Count == elementsCount &&
                          elementsAreSorted &&
                          count == elementsCount);
        }
Example #11
0
        public void CheckIfTreeIsSplayedAfterRemoval()
        {
            var tree = new SplayTreeMap <int, int>();

            int elementsCount = 10000;

            //To make it more balanced
            tree.Add(50, 50);
            tree.Add(75, 75);
            tree.Add(25, 25);

            //Adding every seventh number, then every fifth number,
            //every third and at last all numbers
            for (int i = 7; i > 0; i -= 2)
            {
                int el = 0;
                while (el < elementsCount)
                {
                    if (!tree.ContainsKey(el))
                    {
                        tree.Add(el, el);
                        if (tree.Root.Key != el)
                        {
                            Assert.Fail();
                        }
                    }
                    else if (tree.Root.Key != el)
                    {
                        Assert.Fail();
                    }
                    el += i;
                }
            }

            bool removedEverything = true;

            for (int i = 0; i < elementsCount - 1; i++)
            {
                // Get parent of node for removal. That node have to be splayed
                SplayTreeMapNode <int, int> curNode  = tree.Root;
                SplayTreeMapNode <int, int> lastNode = null;
                while (curNode.Value != i)
                {
                    var cmp = i.CompareTo(curNode.Value);
                    if (cmp == 0)
                    {
                        break;
                    }

                    lastNode = curNode;

                    if (cmp > 0)
                    {
                        curNode = curNode.Right;
                    }
                    else
                    {
                        curNode = curNode.Left;
                    }
                }

                if (!tree.Remove(i))
                {
                    removedEverything = false;
                }
                else if (lastNode != null)
                {
                    Assert.IsTrue(tree.Root.Key == lastNode.Key &&
                                  tree.Root.Value == lastNode.Value);
                }
            }

            Assert.IsTrue(tree.Count == 1 &&
                          removedEverything &&
                          tree.Root.Value == elementsCount - 1);
        }