示例#1
0
        public void KeyValuePair()
        {
            var splayTree = new SplayTree <int, string>();

            Assert.IsFalse(splayTree.Contains(new KeyValuePair <int, string>(5, "5")));

            splayTree.Add(4, "4");
            Assert.IsTrue(splayTree.Contains(new KeyValuePair <int, string>(4, "4")));
            Assert.IsFalse(splayTree.Contains(new KeyValuePair <int, string>(4, "5")));

            splayTree.Add(6, "6");
            Assert.IsTrue(splayTree.Contains(new KeyValuePair <int, string>(6, "6")));
            Assert.IsFalse(splayTree.Contains(new KeyValuePair <int, string>(6, "5")));
        }
示例#2
0
        public void KeyValuePair()
        {
            var splayTree = new SplayTree<int, string>();

            Assert.IsFalse(splayTree.Contains(new KeyValuePair<int, string>(5, "5")));

            splayTree.Add(4, "4");
            Assert.IsTrue(splayTree.Contains(new KeyValuePair<int, string>(4, "4")));
            Assert.IsFalse(splayTree.Contains(new KeyValuePair<int, string>(4, "5")));

            splayTree.Add(6, "6");
            Assert.IsTrue(splayTree.Contains(new KeyValuePair<int, string>(6, "6")));
            Assert.IsFalse(splayTree.Contains(new KeyValuePair<int, string>(6, "5")));
        }
        public void SplayTreeContains()
        {
            var tree = new SplayTree <int, int>()
            {
                { 1, 1 },
                { 2, 2 },
                { -1, -1 },
                { -2, -2 },
                { 3, 3 }
            };

            Assert.IsTrue(tree.Contains(new KeyValuePair <int, int>(1, 1)));
            Assert.IsFalse(tree.Contains(new KeyValuePair <int, int>(-1, 1)));
            Assert.IsFalse(tree.Contains(new KeyValuePair <int, int>(3, 2)));
        }
示例#4
0
        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Add
        ///</summary>
        public void AddTestHelper <T>()
        {
            SplayTree <double> target = new SplayTree <double>();
            double             data   = 3;

            target.Add(1);
            target.Add(2);
            target.Add(data);
            SearchResult actual = target.Contains(data);

            Assert.IsNotNull(actual.SearchPath);
        }
示例#5
0
        public void SortedElementsAfterAdding()
        {
            var tree = new SplayTree <int>();

            int elementsCount = 10000;

            //To make it more balanced
            tree.Add(50);
            tree.Add(75);
            tree.Add(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.Contains(el))
                    {
                        tree.Add(el);
                        if (tree.Root.Value != el)
                        {
                            Assert.Fail();
                        }
                    }
                    else if (tree.Root.Value != el)
                    {
                        Assert.Fail();
                    }
                    el += i;
                }
            }

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

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

            Assert.IsTrue(tree.Count == elementsCount &&
                          elementsAreSorted &&
                          count == elementsCount);
        }
示例#6
0
        public void RemoveAllExceptOne()
        {
            var tree = new SplayTree <int>();

            int elementsCount = 10000;

            //To make it more balanced
            tree.Add(50);
            tree.Add(75);
            tree.Add(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.Contains(el))
                    {
                        tree.Add(el);
                        if (tree.Root.Value != el)
                        {
                            Assert.Fail();
                        }
                    }
                    else if (tree.Root.Value != 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);
        }
示例#7
0
        public void IndexerSetTest()
        {
            var t = new SplayTree <string, string>();

            foreach (var w in _Words)
            {
                t[w.Key] = w.Value + "_Test";
                t[w.Key] = w.Value;
            }

            Assert.AreEqual(t.Count, _Words.Count);
            foreach (var w in _Words)
            {
                Assert.IsTrue(t.Contains(w));
            }
        }
示例#8
0
        public void AddKVPTest()
        {
            SplayTree <string, string> t = new SplayTree <string, string>();

            foreach (var w in _Words)
            {
                t.Add(new KeyValuePair <string, string>(w.Key, w.Value));
                Assert.IsTrue(t.Contains(w.Key, w.Value));

                Exception ex = null;

                try
                {
                    t.Add(new KeyValuePair <string, string>(w.Key, w.Value + "_Test"));
                }
                catch (Exception innerEx)
                {
                    ex = innerEx;
                }

                Assert.IsInstanceOfType(ex, typeof(ArgumentException));
            }
        }
示例#9
0
        public void CheckIfTreeIsSplayedAfterRemoval()
        {
            var tree = new SplayTree <int>();

            int elementsCount = 10000;

            //To make it more balanced
            tree.Add(50);
            tree.Add(75);
            tree.Add(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.Contains(el))
                    {
                        tree.Add(el);
                        if (tree.Root.Value != el)
                        {
                            Assert.Fail();
                        }
                    }
                    else if (tree.Root.Value != 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
                SplayTreeNode <int> curNode  = tree.Root;
                SplayTreeNode <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.Value == lastNode.Value);
                }
            }

            Assert.IsTrue(tree.Count == 1 &&
                          removedEverything &&
                          tree.Root.Value == elementsCount - 1);
        }
示例#10
0
文件: Set.cs 项目: LowerCode/Malins
 public bool Contains(T item)
 {
     return(data.Contains(item));
 }
        public void TestAllOperationsOnRandomNumbers()
        {
            int             count = 100000; //MIN IS 100
            List <int>      numbers = new List <int>(count);
            List <int>      removed = new List <int>(count);
            SplayTree <int> tree = new SplayTree <int>();
            Random          random = new Random();
            int             addCount = 0, succAddCount = 0,
                            findCount = 0, succFindCount = 0,
                            deleteCount = 0, succDeleteCount = 0,
                            compareCheck = 0;


            for (int i = 0; i < count; i++)
            {
                int number = random.Next(0, 100 * count);
                if (number % 3 == 0)
                {
                    addCount++;
                    if (tree.Add(number))
                    {
                        succAddCount++;
                        numbers.Add(number);
                        if (removed.Contains(number))
                        {
                            removed.Remove(number);
                        }
                    }
                }
                else
                if (number % 3 == 1)
                {
                    findCount++;
                    if (numbers.Count > 0)
                    {
                        Assert.IsTrue(tree.Contains(numbers[random.Next(0, numbers.Count)]));
                        //COULD BE CHANGED TO PROFIT FROM SPLAY
                        succFindCount++;
                    }
                    if (removed.Count > 0)
                    {
                        Assert.IsFalse(tree.Contains(removed[random.Next(0, removed.Count)]));
                    }
                }
                else
                if (number % 3 == 2)
                {
                    deleteCount++;
                    if (numbers.Count > 0)
                    {
                        int toDelete = numbers[random.Next(0, numbers.Count)];
                        tree.Delete(toDelete);
                        succDeleteCount++;
                        numbers.Remove(toDelete);
                        removed.Add(toDelete);
                    }
                    if (removed.Count > 0)
                    {
                        tree.Delete(removed[random.Next(0, removed.Count)]); // SHOULD DO NOTHING
                    }
                }
                if (number % (count / 100) == 0)
                {
                    compareCheck++;
                    foreach (int num in numbers)
                    {
                        Assert.IsTrue(tree.Contains(num));
                    }
                    foreach (int num in removed)
                    {
                        Assert.IsFalse(tree.Contains(num));
                    }
                    Assert.IsTrue(tree.CheckTreeStructure());
                }
            }


            String treeTraversalLevelOrder = tree.TraverseLevelOrder();

            File.WriteAllText("D:\\.NET Projects\\PlaneDepartureTracking\\PlaneDepartureTrackingMSTest\\splaytreeTestLevelOrder.txt",
                              treeTraversalLevelOrder);
            String treeTraversalInOrder = tree.TraverseInOrder();

            File.WriteAllText("D:\\.NET Projects\\PlaneDepartureTracking\\PlaneDepartureTrackingMSTest\\splaytreeTestInOrder.txt",
                              treeTraversalInOrder);
            String checkInfo = "ADD checks: " + addCount + "\r\nSuccessfully added elements: " + succAddCount +
                               "\r\nFIND checks: " + findCount + "\r\nSuccessfully found elements: " + succFindCount +
                               "\r\nDELETE checks: " + deleteCount + "\r\nSuccessfully deleted elements: " + succDeleteCount +
                               "\r\nTree-List comparisons: " + compareCheck;

            File.WriteAllText("D:\\.NET Projects\\PlaneDepartureTracking\\PlaneDepartureTrackingMSTest\\splaytreeTestStats.txt",
                              checkInfo);
        }