Beispiel #1
0
        public static void TestInsertAfter()
        {
            var testList = new LinkedList2();

            testList.InsertAfter(null, new Node(1));
            Assert.AreEqual(1, testList.Count(), "Test TestInsertAfter failed. List count have to be equal 1");
            testList.AddInTail(new Node(3));
            testList.AddInTail(new Node(5));
            testList.InsertAfter(new Node(3), new Node(4));
            Assert.AreEqual(4, testList.Count(), "Test TestInsertAfter failed. List count have to be equal 4");
            testList.InsertAfter(new Node(1), new Node(2));
            Assert.AreEqual(5, testList.Count(), "Test TestInsertAfter failed. List count have to be equal 5");
            testList.InsertAfter(new Node(5), new Node(6));
            Assert.AreEqual(6, testList.Count(), "Test TestInsertAfter failed. List count have to be equal 6");
            testList.InsertAfter(null, new Node(0));
            Assert.AreEqual(7, testList.Count(), "Test TestInsertAfter failed. List count have to be equal 7");
            Assert.AreEqual(0, testList.head.value, "Test TestInsertAfter failed. List head.value have to be equal 0");
            Assert.AreEqual(6, testList.tail.value, "Test TestInsertAfter failed. List tail.value have to be equal 6");
            Assert.IsNull(testList.tail.next, "Test TestInsertAfter failed. List tail.next have to be equal NULL");
            Assert.IsNull(testList.head.prev, "Test TestInsertAfter failed. List head.prev have to be equal NULL");
            Assert.IsNotNull(testList.tail.prev, "Test TestInsertAfter failed. List tail.prev have to be equal NOT NULL");
            Assert.IsNotNull(testList.head.next, "Test TestInsertAfter failed. List head.next have to be equal NOT NULL");

            int  counter = -1;
            Node node    = testList.head;

            while (node != null)
            {
                counter += 1;
                Assert.AreEqual(counter, node.value, "Test TestInsertAfter failed. List sequence is not correct");
                node = node.next;
            }
        }
Beispiel #2
0
        public static void TestAddInTail()
        {
            var testList = new LinkedList2();

            Assert.AreEqual(0, testList.Count(), "Test TestAddInTail failed. Initial list count have to be equal 0");
            testList.AddInTail(new Node(1));
            Assert.AreEqual(1, testList.Count(), "Test TestAddInTail failed. List count have to be equal 1");
            testList.AddInTail(new Node(2));
            Assert.AreEqual(2, testList.Count(), "Test TestAddInTail failed. List count have to be equal 2");
            testList.AddInTail(new Node(3));
            Assert.AreEqual(3, testList.Count(), "Test TestAddInTail failed. List count have to be equal 3");
            Assert.IsNull(testList.tail.next, "Test TestAddInTail failed. List tail.next not equal NULL");
        }
Beispiel #3
0
        public void Test_Count_WhenEmpty()
        {
            var list  = new LinkedList2();
            var count = list.Count();

            Assert.AreEqual(0, count);
        }
Beispiel #4
0
        public static void TestRemove()
        {
            var testList = new LinkedList2();

            testList.AddInTail(new Node(1));
            testList.AddInTail(new Node(2));
            testList.AddInTail(new Node(3));
            testList.Remove(1);
            Assert.AreEqual(2, testList.Count(), "Test TestRemove failed. List count have to be equal 2");
            testList.Remove(3);
            Assert.AreEqual(1, testList.Count(), "Test TestRemove failed. List count have to be equal 1");
            Assert.AreSame(testList.head, testList.tail, "Test TestRemove failed. List head not equal tail");
            Assert.IsNull(testList.tail.next, "Test TestRemove failed. List tail.next not equal NULL");
            testList.Remove(2);
            Assert.AreEqual(0, testList.Count(), "Test TestRemove failed. List count have to be equal 0");
            Assert.IsNull(testList.head, "Test TestRemove failed. List not empty");
            Assert.IsNull(testList.tail, "Test TestRemove failed. List not empty");
        }
Beispiel #5
0
        public void Test_Count_WhenNonEmpty()
        {
            var list = new LinkedList2();
            var node = new Node(1);

            list.AddInTail(node);
            var count = list.Count();

            Assert.AreEqual(1, count);
        }
        public void AddInHead_in_EmptyList()
        {
            LinkedList2 testList = new LinkedList2();

            testList.AddInHead(new Node(21));

            int expected = 1;
            int actual   = testList.Count();

            Assert.AreEqual(expected, actual);
            Assert.IsNotNull(testList.Find(21));
        }
        static void TestInsertAfter()
        {
            var list = new LinkedList2();

            Console.WriteLine("insert 10");
            Console.WriteLine("count before insert = " + list.Count());
            Console.WriteLine("head before insert = " + (list.head == null ? "null" : list.head.value.ToString()));
            Console.WriteLine("tail before insert = " + (list.tail == null ? "null" : list.tail.value.ToString()));
            list.InsertAfter(null, new Node(10));
            Console.WriteLine("count after insert = " + list.Count());
            Console.WriteLine("head after insert = " + (list.head == null ? "null" : list.head.value.ToString()));
            Console.WriteLine("tail after insert = " + (list.tail == null ? "null" : list.tail.value.ToString()));

            Console.WriteLine();

            Console.WriteLine("insert 20");
            Console.WriteLine("count before insert = " + list.Count());
            Console.WriteLine("head before insert = " + (list.head == null ? "null" : list.head.value.ToString()));
            Console.WriteLine("tail before insert = " + (list.tail == null ? "null" : list.tail.value.ToString()));
            list.InsertAfter(null, new Node(20));
            Console.WriteLine("count after insert = " + list.Count());
            Console.WriteLine("head after insert = " + (list.head == null ? "null" : list.head.value.ToString()));
            Console.WriteLine("tail after insert = " + (list.tail == null ? "null" : list.tail.value.ToString()));

            Console.WriteLine();

            Console.WriteLine("insert 30 after head");
            Console.WriteLine("count before insert = " + list.Count());
            Console.WriteLine("head before insert = " + (list.head == null ? "null" : list.head.value.ToString()));
            Console.WriteLine("tail before insert = " + (list.tail == null ? "null" : list.tail.value.ToString()));
            list.InsertAfter(list.head, new Node(20));
            Console.WriteLine("count after insert = " + list.Count());
            Console.WriteLine("head after insert = " + (list.head == null ? "null" : list.head.value.ToString()));
            Console.WriteLine("tail after insert = " + (list.tail == null ? "null" : list.tail.value.ToString()));

            Console.WriteLine();

            Console.WriteLine("insert 50 after tail");
            Console.WriteLine("count before insert = " + list.Count());
            Console.WriteLine("head before insert = " + (list.head == null ? "null" : list.head.value.ToString()));
            Console.WriteLine("tail before insert = " + (list.tail == null ? "null" : list.tail.value.ToString()));
            list.InsertAfter(list.tail, new Node(50));
            Console.WriteLine("count after insert = " + list.Count());
            Console.WriteLine("head after insert = " + (list.head == null ? "null" : list.head.value.ToString()));
            Console.WriteLine("tail after insert = " + (list.tail == null ? "null" : list.tail.value.ToString()));

            Console.WriteLine();

            Console.WriteLine(new string('=', 30));
        }
Beispiel #8
0
        public void Test_Clear_WhenNonEmpty()
        {
            var list  = new LinkedList2();
            var node1 = new Node(1);

            list.AddInTail(node1);
            list.Clear();
            var count = list.Count();

            Assert.AreEqual(null, list.head);
            Assert.AreEqual(null, list.tail);
            Assert.AreEqual(0, count);
        }
Beispiel #9
0
        public static void TestClear()
        {
            var testList = new LinkedList2();

            testList.AddInTail(new Node(1));
            testList.AddInTail(new Node(2));
            testList.AddInTail(new Node(1));
            testList.AddInTail(new Node(3));
            testList.AddInTail(new Node(1));
            testList.AddInTail(new Node(4));
            testList.Clear();
            Assert.AreEqual(0, testList.Count(), "Test TestClear failed. List count not equal 0");
        }
        public void Remove_from_EmptyList()
        {
            LinkedList2 testList = new LinkedList2();
            int         expected = 0;

            testList.Remove(5);
            int actual = testList.Count();

            Assert.IsNull(testList.head);
            Assert.IsNull(testList.tail);
            Assert.AreEqual(expected, actual);
            Assert.IsNull(testList.Find(5));
        }
        static void TestClear()
        {
            Console.WriteLine("clear test");

            var list = new LinkedList2();

            list.AddInTail(new Node(20));
            list.AddInTail(new Node(24));
            list.AddInTail(new Node(10));
            list.AddInTail(new Node(15));
            list.AddInTail(new Node(77));
            list.AddInTail(new Node(24));

            Console.WriteLine("count before clear = " + list.Count());
            Console.WriteLine("head before clear = " + list.head.value);
            Console.WriteLine("tail before clear = " + list.tail.value);
            list.Clear();
            Console.WriteLine("count after clear = " + list.Count());
            Console.WriteLine("head after clear = " + (list.head == null ? "null" : list.head.value.ToString()));
            Console.WriteLine("tail after clear = " + (list.head == null ? "null" : list.tail.value.ToString()));

            Console.WriteLine(new string('=', 30));
        }
        static void TestRemoveAll()
        {
            Console.WriteLine("remove all test");

            var list = new LinkedList2();

            list.AddInTail(new Node(20));
            list.AddInTail(new Node(24));
            list.AddInTail(new Node(10));
            list.AddInTail(new Node(15));
            list.AddInTail(new Node(77));
            list.AddInTail(new Node(24));

            Console.WriteLine("delete 10");
            Console.WriteLine("count before delete = " + list.Count());
            Console.WriteLine("head before delete = " + list.head.value);
            Console.WriteLine("tail before delete = " + list.tail.value);
            list.RemoveAll(10);
            Console.WriteLine("count after delete = " + list.Count());
            Console.WriteLine("head after delete = " + list.head.value);
            Console.WriteLine("tail after delete = " + list.tail.value);

            Console.WriteLine();

            Console.WriteLine("delete 24");
            Console.WriteLine("count before delete = " + list.Count());
            Console.WriteLine("head before delete = " + list.head.value);
            Console.WriteLine("tail before delete = " + list.tail.value);
            list.RemoveAll(24);
            Console.WriteLine("count after delete = " + list.Count());
            Console.WriteLine("head after delete = " + list.head.value);
            Console.WriteLine("tail after delete = " + list.tail.value);

            Console.WriteLine();

            Console.WriteLine(new string('=', 30));
        }
        public void Remove_from_Tail()
        {
            LinkedList2 testList = new LinkedList2();

            testList.AddInTail(new Node(0));
            testList.AddInTail(new Node(1));
            testList.AddInTail(new Node(2));
            testList.AddInTail(new Node(3));
            int expected = 3;

            testList.Remove(3);
            int actual = testList.Count();

            Assert.AreEqual(expected, actual);
            Assert.IsNull(testList.Find(3));
        }
        public void AddInHead_in_NotEmptyList()
        {
            LinkedList2 testList = new LinkedList2();

            testList.AddInTail(new Node(1));
            testList.AddInTail(new Node(2));
            testList.AddInTail(new Node(3));
            testList.AddInTail(new Node(4));
            testList.AddInTail(new Node(5));
            int expected = 6;

            testList.AddInHead(new Node(19));
            int actual = testList.Count();

            Assert.AreEqual(expected, actual);
            Assert.IsNotNull(testList.Find(19));
        }
        private void RemoveAllTest(int valueToRemove, params Node[] nodes)
        {
            LinkedList2 list = GetLinkedList(nodes);

            int  nodesToRemove = nodes.Count(node => node.value == valueToRemove);
            Node expectedHead  = nodes.FirstOrDefault(node => node.value != valueToRemove);
            Node expectedTail  = nodes.LastOrDefault(node => node.value != valueToRemove);

            list.RemoveAll(valueToRemove);

            Assert.Equal(nodes.Length - nodesToRemove, list.Count());

            Assert.Same(expectedHead, list.head);
            Assert.Same(expectedTail, list.tail);

            CheckPrevAndNextCorrectness(list, nodes.Length - nodesToRemove);
        }
        public void AddNode_if_Target_non_Exists()
        {
            LinkedList2 testList = new LinkedList2();

            testList.AddInTail(new Node(1));
            testList.AddInTail(new Node(2));
            testList.AddInTail(new Node(3));
            testList.AddInTail(new Node(4));
            testList.AddInTail(new Node(5));
            int expected = 5;

            testList.InsertAfter(new Node(13), new Node(21));
            int actual = testList.Count();

            Assert.AreEqual(expected, actual);
            Assert.IsNull(testList.Find(21));
        }
        public void AddNode_in_Tail()
        {
            LinkedList2 testList = new LinkedList2();

            testList.AddInTail(new Node(1));
            testList.AddInTail(new Node(2));
            testList.AddInTail(new Node(3));
            testList.AddInTail(new Node(4));
            testList.AddInTail(new Node(5));
            int expected = 6;

            testList.InsertAfter(new Node(5), new Node(21));
            int actual = testList.Count();

            Assert.AreEqual(expected, actual);
            Assert.IsTrue(testList.Find(21).value == 21);
        }
Beispiel #18
0
        public static void TestRemoveAll()
        {
            var testList = new LinkedList2();

            testList.AddInTail(new Node(1));
            testList.AddInTail(new Node(2));
            testList.AddInTail(new Node(1));
            testList.AddInTail(new Node(3));
            testList.AddInTail(new Node(1));
            testList.RemoveAll(1);
            Assert.AreEqual(2, testList.Count(), "Test TestRemoveAll failed. List count have to be equal 2");
            Assert.AreEqual(2, testList.head.value, "Test TestRemoveAll failed. List head not equal 2");
            Assert.AreEqual(3, testList.tail.value, "Test TestRemoveAll failed. List tail not equal 3");
            Assert.IsNull(testList.tail.next, "Test TestRemoveAll failed. List tail.next not equal NULL");
            Assert.IsNull(testList.head.prev, "Test TestRemoveAll failed. List head.prev not equal NULL");
            Assert.IsNotNull(testList.head.next, "Test TestRemoveAll failed. List head.next not equal NOT NULL");
            Assert.IsNotNull(testList.tail.prev, "Test TestRemoveAll failed. List tail.prev not equal NOT NULL");
        }
        public void Remove_if_more_One_Exists()
        {
            LinkedList2 testList = new LinkedList2();

            testList.AddInTail(new Node(0));
            testList.AddInTail(new Node(1));
            testList.AddInTail(new Node(5));
            testList.AddInTail(new Node(2));
            testList.AddInTail(new Node(3));
            testList.AddInTail(new Node(4));
            testList.AddInTail(new Node(5));
            int expected = 6;

            testList.Remove(5);
            int actual = testList.Count();

            Assert.AreEqual(expected, actual);
            Assert.IsTrue(testList.FindAll(5).Count >= 1);
        }
        public void AddInHead_if_List_Is_Contain_the_Same_NodeValue()
        {
            LinkedList2 testList = new LinkedList2();

            testList.AddInTail(new Node(1));
            testList.AddInTail(new Node(4));
            testList.AddInTail(new Node(4));
            testList.AddInTail(new Node(4));
            testList.AddInTail(new Node(5));
            testList.AddInTail(new Node(4));
            testList.AddInHead(new Node(4));
            int expected = 7;
            int actual   = testList.Count();


            List <Node> resultsList = testList.FindAll(4);

            Assert.AreEqual(expected, actual);
            Assert.IsTrue(testList.FindAll(4).Count == 5);
        }
        private void RemoveTest(int valueToRemove, params Node[] nodes)
        {
            LinkedList2 list          = GetLinkedList(nodes);
            int         nodesToRemove = nodes.Count(node => node.value == valueToRemove) >= 1 ? 1 : 0;
            Node        expectedHead  = nodes.FirstOrDefault(node => node.value != valueToRemove) ??
                                        nodes.Where(node => node.value == valueToRemove).Skip(1).FirstOrDefault();
            Node expectedTail = expectedHead == null ?
                                null :
                                nodes.LastOrDefault();

            bool removeSuccess = list.Remove(valueToRemove);

            Assert.True(nodesToRemove == 0 && !removeSuccess ||
                        nodesToRemove == 1 && removeSuccess);
            Assert.Equal(nodes.Length - nodesToRemove, list.Count());
            Assert.Same(expectedHead, list.head);
            Assert.Same(expectedTail, list.tail);

            CheckPrevAndNextCorrectness(list, nodes.Length - nodesToRemove);
        }
Beispiel #22
0
        private Node GetNodeAtPosition(LinkedList2 testList, int?insertAfterPosition)
        {
            var curNode = testList.head;

            if (insertAfterPosition == null)
            {
                return(null);
            }

            if (insertAfterPosition.Value > testList.Count())
            {
                return(testList.tail);
            }

            var counter = 0;

            while (curNode != null && counter < insertAfterPosition)
            {
                curNode = curNode.next;
                counter++;
            }

            return(curNode);
        }
        static void TestRemove()
        {
            var list = new LinkedList2();

            list.AddInTail(new Node(20));
            list.AddInTail(new Node(24));
            list.AddInTail(new Node(10));
            list.AddInTail(new Node(15));
            list.AddInTail(new Node(77));
            list.AddInTail(new Node(24));

            Console.WriteLine("delete 10");
            Console.WriteLine("count before delete = " + list.Count());
            Console.WriteLine("head before delete = " + list.head.value);
            Console.WriteLine("tail before delete = " + list.tail.value);
            var result = list.Remove(10);

            Console.WriteLine("delete result = " + result);
            Console.WriteLine("count after delete = " + list.Count());
            Console.WriteLine("head after delete = " + list.head.value);
            Console.WriteLine("tail after delete = " + list.tail.value);

            Console.WriteLine();

            Console.WriteLine("delete head");
            Console.WriteLine("count before delete = " + list.Count());
            Console.WriteLine("head before delete = " + list.head.value);
            Console.WriteLine("tail before delete = " + list.tail.value);
            list.Remove(20);
            Console.WriteLine("delete result = " + result);
            Console.WriteLine("count after delete = " + list.Count());
            Console.WriteLine("head after delete = " + list.head.value);
            Console.WriteLine("tail after delete = " + list.tail.value);

            Console.WriteLine();

            Console.WriteLine("delete 24");
            Console.WriteLine("count before delete = " + list.Count());
            Console.WriteLine("head before delete = " + list.head.value);
            Console.WriteLine("tail before delete = " + list.tail.value);
            list.Remove(24);
            Console.WriteLine("delete result = " + result);
            Console.WriteLine("count after delete = " + list.Count());
            Console.WriteLine("head after delete = " + list.head.value);
            Console.WriteLine("tail after delete = " + list.tail.value);

            Console.WriteLine();

            Console.WriteLine("delete tail");
            Console.WriteLine("count before delete = " + list.Count());
            Console.WriteLine("head before delete = " + list.head.value);
            Console.WriteLine("tail before delete = " + list.tail.value);
            list.Remove(24);
            Console.WriteLine("delete result = " + result);
            Console.WriteLine("count after delete = " + list.Count());
            Console.WriteLine("head after delete = " + list.head.value);
            Console.WriteLine("tail after delete = " + list.tail.value);

            Console.WriteLine();

            Console.WriteLine("delete 15, 77");
            list.Remove(15);
            list.Remove(77);
            Console.WriteLine("count after delete = " + list.Count());
            Console.WriteLine("head after delete = " + (list.head == null ? "null" : list.head.value.ToString()));
            Console.WriteLine("tail after delete = " + (list.tail == null ? "null" : list.tail.value.ToString()));

            Console.WriteLine("delete 100");
            list.Remove(100);
            Console.WriteLine("count after delete = " + list.Count());
            Console.WriteLine("head after delete = " + (list.head == null ? "null" : list.head.value.ToString()));
            Console.WriteLine("tail after delete = " + (list.tail == null ? "null" : list.tail.value.ToString()));

            Console.WriteLine();

            Console.WriteLine(new string('=', 30));
        }
        private void CountTest(int expectedLength, params Node[] nodes)
        {
            LinkedList2 list = GetLinkedList(nodes);

            Assert.Equal(expectedLength, list.Count());
        }