Example #1
0
        public void Should_Copy_To()
        {
            //arrange
            var node1 = new MyDoublyLinkedListNode <int>(2);
            var node2 = new MyDoublyLinkedListNode <int>(4);
            var node3 = new MyDoublyLinkedListNode <int>(3);
            var node4 = new MyDoublyLinkedListNode <int>(5);

            node1.Next = node2;
            node2.Next = node3;
            node3.Next = node4;

            node2.Prev = node1;
            node3.Prev = node2;
            node4.Prev = node3;

            var list  = new MyDoublyLinkedList <int>(node1);
            var array = new int[6];

            //act
            list.CopyTo(array, 2);

            //assert
            array[0].ShouldBeEquivalentTo(0);
            array[1].ShouldBeEquivalentTo(0);
            array[2].ShouldBeEquivalentTo(2);
            array[3].ShouldBeEquivalentTo(4);
            array[4].ShouldBeEquivalentTo(3);
            array[5].ShouldBeEquivalentTo(5);
        }
Example #2
0
        public void Should_Clear()
        {
            //arrange
            var node1 = new MyDoublyLinkedListNode <int>(2);
            var node2 = new MyDoublyLinkedListNode <int>(4);
            var node3 = new MyDoublyLinkedListNode <int>(3);
            var node4 = new MyDoublyLinkedListNode <int>(5);

            node1.Next = node2;
            node2.Next = node3;
            node3.Next = node4;

            node2.Prev = node1;
            node3.Prev = node2;
            node4.Prev = node3;

            var list = new MyDoublyLinkedList <int>(node1);

            //act
            list.Clear();

            //assert
            list.Count.ShouldBeEquivalentTo(0);
            list.Head.ShouldBeEquivalentTo(list.Tail);
            list.Head.Should().BeNull();
        }
Example #3
0
        public void Should_Add_First_When_List_Length_Greater_One()
        {
            //arrange
            var node1 = new MyDoublyLinkedListNode <int>(1);
            var node2 = new MyDoublyLinkedListNode <int>(2);
            var node3 = new MyDoublyLinkedListNode <int>(3);
            var node4 = new MyDoublyLinkedListNode <int>(4);

            node1.Next = node2;
            node2.Next = node3;
            node3.Next = node4;

            node2.Prev = node1;
            node3.Prev = node2;
            node4.Prev = node3;

            var list  = new MyDoublyLinkedList <int>(node1);
            var node5 = new MyDoublyLinkedListNode <int>(5);

            //act
            list.AddFirst(node5);

            //assert
            list.Count.ShouldBeEquivalentTo(5);
            list.Head.ShouldBeEquivalentTo(node5);
            list.Tail.ShouldBeEquivalentTo(node4);
            list.Head.Next.ShouldBeEquivalentTo(node1);
            list.Head.Next.Next.ShouldBeEquivalentTo(node2);
            list.Head.Next.Next.Next.ShouldBeEquivalentTo(node3);
            list.Head.Next.Next.Next.Next.ShouldBeEquivalentTo(node4);
        }
Example #4
0
        public void Should_RemoveValue_False()
        {
            //arrange
            var node1 = new MyDoublyLinkedListNode <int>(2);
            var node2 = new MyDoublyLinkedListNode <int>(4);
            var node3 = new MyDoublyLinkedListNode <int>(3);
            var node4 = new MyDoublyLinkedListNode <int>(5);

            node1.Next = node2;
            node2.Next = node3;
            node3.Next = node4;

            node2.Prev = node1;
            node3.Prev = node2;
            node4.Prev = node3;

            var list = new MyDoublyLinkedList <int>(node1);

            //act
            var result = list.Remove(6);

            //assert
            result.ShouldBeEquivalentTo(false);
            list.Count.ShouldBeEquivalentTo(4);
            list.Head.ShouldBeEquivalentTo(node1);
            list.Head.Next.ShouldBeEquivalentTo(node2);
            list.Head.Next.Next.ShouldBeEquivalentTo(node3);
            list.Head.Next.Next.Next.ShouldBeEquivalentTo(node4);
        }
Example #5
0
        public void Should_Copy_To_Throw_If_Incorrect_ArrayIndex()
        {
            //arrange
            var node1 = new MyDoublyLinkedListNode <int>(2);
            var node2 = new MyDoublyLinkedListNode <int>(4);
            var node3 = new MyDoublyLinkedListNode <int>(3);
            var node4 = new MyDoublyLinkedListNode <int>(5);

            node1.Next = node2;
            node2.Next = node3;
            node3.Next = node4;

            node2.Prev = node1;
            node3.Prev = node2;
            node4.Prev = node3;

            var list  = new MyDoublyLinkedList <int>(node1);
            var array = new int[3];
            //act
            Action actLower  = () => list.CopyTo(array, -1);
            Action actHigher = () => list.CopyTo(array, 1);

            //assert
            actLower.ShouldThrow <ArgumentOutOfRangeException>();
            actHigher.ShouldThrow <ArgumentOutOfRangeException>();
        }
Example #6
0
        public void Should_FindLast_Check_Order()
        {
            //arrange
            var node1 = new MyDoublyLinkedListNode <int>(2);
            var node2 = new MyDoublyLinkedListNode <int>(4);
            var node3 = new MyDoublyLinkedListNode <int>(3);
            var node4 = new MyDoublyLinkedListNode <int>(1);
            var node5 = new MyDoublyLinkedListNode <int>(3);
            var node6 = new MyDoublyLinkedListNode <int>(6);

            node1.Next = node2;
            node2.Next = node3;
            node3.Next = node4;
            node4.Next = node5;
            node5.Next = node6;

            node2.Prev = node1;
            node3.Prev = node2;
            node4.Prev = node3;
            node5.Prev = node4;
            node6.Prev = node5;

            var list = new MyDoublyLinkedList <int>(node1);

            //act
            var result = list.FindLast(3);

            //assert
            result.ShouldBeEquivalentTo(node5);
        }
Example #7
0
        public void Should_RemoveNode_RemoveFirst()
        {
            //arrange
            var node1 = new MyDoublyLinkedListNode <int>(2);
            var node2 = new MyDoublyLinkedListNode <int>(4);
            var node3 = new MyDoublyLinkedListNode <int>(3);
            var node4 = new MyDoublyLinkedListNode <int>(5);

            node1.Next = node2;
            node2.Next = node3;
            node3.Next = node4;

            node2.Prev = node1;
            node3.Prev = node2;
            node4.Prev = node3;

            var list = new MyDoublyLinkedList <int>(node1);

            //act
            list.Remove(node1);

            //assert
            list.Count.ShouldBeEquivalentTo(3);
            list.Head.ShouldBeEquivalentTo(node2);
            list.Head.Next.ShouldBeEquivalentTo(node3);
            list.Head.Next.Next.ShouldBeEquivalentTo(node4);
        }
Example #8
0
        public void RemoveDups_Should_Check_One_Duplicate()
        {
            //arrange
            var list  = new MyDoublyLinkedList <int>();
            var node1 = new MyDoublyLinkedListNode <int>(1);
            var node2 = new MyDoublyLinkedListNode <int>(1);

            node1.Next = node2;
            node2.Prev = node1;
            list.Head  = node1;

            var resultList = new MyDoublyLinkedList <int>();

            resultList.Head = new MyDoublyLinkedListNode <int>(1);

            //act
            var result = _linkedList.RemoveDups(list);

            //assert
            var head           = result.Head;
            var resultListHead = resultList.Head;

            while (head != null)
            {
                head.Data.ShouldBeEquivalentTo(resultListHead.Data);
                head           = head.Next;
                resultListHead = resultListHead.Next;
            }
            resultListHead.ShouldBeEquivalentTo(null);
        }
Example #9
0
        public void Should_Add_Before_Throw_Does_Not_Have_Node()
        {
            //arrange
            var node1 = new MyDoublyLinkedListNode <int>(1);
            var node2 = new MyDoublyLinkedListNode <int>(2);
            var node3 = new MyDoublyLinkedListNode <int>(3);
            var node4 = new MyDoublyLinkedListNode <int>(4);

            node1.Next = node2;
            node2.Next = node3;
            node3.Next = node4;

            node2.Prev = node1;
            node3.Prev = node2;
            node4.Prev = node3;

            var list  = new MyDoublyLinkedList <int>(node1);
            var node5 = new MyDoublyLinkedListNode <int>(5);

            //act
            Action act = () => list.AddBefore(new MyDoublyLinkedListNode <int>(6), node5);

            //assert
            act.ShouldThrow <InvalidOperationException>();
        }
Example #10
0
        public void Should_Add_After_Check_Tail()
        {
            //arrange
            var node1 = new MyDoublyLinkedListNode <int>(1);
            var node2 = new MyDoublyLinkedListNode <int>(2);
            var node3 = new MyDoublyLinkedListNode <int>(3);
            var node4 = new MyDoublyLinkedListNode <int>(4);

            node1.Next = node2;
            node2.Next = node3;
            node3.Next = node4;

            node2.Prev = node1;
            node3.Prev = node2;
            node4.Prev = node3;

            var list  = new MyDoublyLinkedList <int>(node1);
            var node5 = new MyDoublyLinkedListNode <int>(5);

            //act
            list.AddAfter(node4, node5);

            //assert
            list.Count.ShouldBeEquivalentTo(5);
            list.Head.ShouldBeEquivalentTo(node1);
            list.Tail.ShouldBeEquivalentTo(node5);

            list.Head.Next.ShouldBeEquivalentTo(node2);
            list.Head.Next.Next.ShouldBeEquivalentTo(node3);
            list.Head.Next.Next.Next.ShouldBeEquivalentTo(node4);
            list.Head.Next.Next.Next.Next.ShouldBeEquivalentTo(node5);
        }
Example #11
0
        public void Should_RemoveNode_Length_One()
        {
            //arrange
            var node = new MyDoublyLinkedListNode <int>(2);
            var list = new MyDoublyLinkedList <int>(node);

            //act
            list.Remove(node);

            //assert
            list.Count.ShouldBeEquivalentTo(0);
            list.Head.ShouldBeEquivalentTo(list.Tail);
            list.Head.Should().BeNull();
        }
Example #12
0
        public void Should_Add_First_When_List_Is_Empty()
        {
            //arrange
            var list = new MyDoublyLinkedList <int>();

            var node = new MyDoublyLinkedListNode <int>(1);

            //act
            list.AddFirst(node);

            //assert
            list.Count.ShouldBeEquivalentTo(1);
            list.Head.ShouldBeEquivalentTo(list.Tail);
            list.Head.ShouldBeEquivalentTo(node);
        }
Example #13
0
        public void Should_Add_Before_Check_Head()
        {
            //arrange
            var node1 = new MyDoublyLinkedListNode <int>(1);

            var list  = new MyDoublyLinkedList <int>(node1);
            var node2 = new MyDoublyLinkedListNode <int>(2);

            //act
            list.AddBefore(node1, node2);

            //assert
            list.Count.ShouldBeEquivalentTo(2);
            list.Head.ShouldBeEquivalentTo(node2);
            list.Tail.ShouldBeEquivalentTo(node1);

            list.Head.Next.ShouldBeEquivalentTo(node1);
        }
Example #14
0
        public void Should_FindLast_Odd_Length()
        {
            //arrange
            var node1 = new MyDoublyLinkedListNode <int>(2);
            var node2 = new MyDoublyLinkedListNode <int>(4);
            var node3 = new MyDoublyLinkedListNode <int>(3);

            node1.Next = node2;
            node2.Next = node3;

            node2.Prev = node1;
            node3.Prev = node2;

            var list = new MyDoublyLinkedList <int>(node1);

            //act
            var result = list.FindLast(4);

            //assert
            result.ShouldBeEquivalentTo(node2);
        }
Example #15
0
        public void Should_Check_Contains_False()
        {
            //arrange
            var node1 = new MyDoublyLinkedListNode <int>(2);
            var node2 = new MyDoublyLinkedListNode <int>(4);
            var node3 = new MyDoublyLinkedListNode <int>(3);
            var node4 = new MyDoublyLinkedListNode <int>(5);

            node1.Next = node2;
            node2.Next = node3;
            node3.Next = node4;

            node2.Prev = node1;
            node3.Prev = node2;
            node4.Prev = node3;

            var list = new MyDoublyLinkedList <int>(node1);

            //act
            var result = list.Contains(7);

            //assert
            result.ShouldBeEquivalentTo(false);
        }
Example #16
0
        public void Should_FindLast_Return_Null_If_Is_Not_Found()
        {
            //arrange
            var node1 = new MyDoublyLinkedListNode <int>(1);
            var node2 = new MyDoublyLinkedListNode <int>(2);
            var node3 = new MyDoublyLinkedListNode <int>(3);
            var node4 = new MyDoublyLinkedListNode <int>(4);

            node1.Next = node2;
            node2.Next = node3;
            node3.Next = node4;

            node2.Prev = node1;
            node3.Prev = node2;
            node4.Prev = node3;

            var list = new MyDoublyLinkedList <int>(node1);

            //act
            var result = list.FindLast(7);

            //assert
            result.Should().BeNull();
        }
Example #17
0
        public void Should_Create_List_With_Chain_Nodes()
        {
            //arrange
            var node1 = new MyDoublyLinkedListNode <int>(1);
            var node2 = new MyDoublyLinkedListNode <int>(2);
            var node3 = new MyDoublyLinkedListNode <int>(3);
            var node4 = new MyDoublyLinkedListNode <int>(4);

            node1.Next = node2;
            node2.Next = node3;
            node3.Next = node4;

            node2.Prev = node1;
            node3.Prev = node2;
            node4.Prev = node3;

            //act
            var list = new MyDoublyLinkedList <int>(node1);

            //assert
            list.Head.ShouldBeEquivalentTo(node1);
            list.Tail.ShouldBeEquivalentTo(node4);
            list.Count.ShouldBeEquivalentTo(4);
        }
Example #18
0
        public void Should_RemoveNode_Throw_If_Node_Is_Not_Contained()
        {
            //arrange
            var node1 = new MyDoublyLinkedListNode <int>(2);
            var node2 = new MyDoublyLinkedListNode <int>(4);
            var node3 = new MyDoublyLinkedListNode <int>(3);
            var node4 = new MyDoublyLinkedListNode <int>(5);

            node1.Next = node2;
            node2.Next = node3;
            node3.Next = node4;

            node2.Prev = node1;
            node3.Prev = node2;
            node4.Prev = node3;

            var list = new MyDoublyLinkedList <int>(node1);

            //act
            Action act = () => list.Remove(new MyDoublyLinkedListNode <int>(2));

            //assert
            act.ShouldThrow <InvalidOperationException>();
        }
Example #19
0
        public void RemoveDups_Should_Check_True()
        {
            //arrange
            var list = new MyDoublyLinkedList <int>();

            var node1 = new MyDoublyLinkedListNode <int>(1);
            var node2 = new MyDoublyLinkedListNode <int>(3);
            var node3 = new MyDoublyLinkedListNode <int>(4);
            var node4 = new MyDoublyLinkedListNode <int>(4);
            var node5 = new MyDoublyLinkedListNode <int>(7);
            var node6 = new MyDoublyLinkedListNode <int>(-1);
            var node7 = new MyDoublyLinkedListNode <int>(1);

            node1.Next = node2;
            node2.Next = node3;
            node3.Next = node4;
            node4.Next = node5;
            node5.Next = node6;
            node6.Next = node7;

            node2.Prev = node1;
            node3.Prev = node2;
            node4.Prev = node3;
            node5.Prev = node4;
            node6.Prev = node5;
            node7.Prev = node6;

            list.Head = node1;

            var resultList = new MyDoublyLinkedList <int>();
            var node8      = new MyDoublyLinkedListNode <int>(1);
            var node9      = new MyDoublyLinkedListNode <int>(3);
            var node10     = new MyDoublyLinkedListNode <int>(4);
            var node11     = new MyDoublyLinkedListNode <int>(7);
            var node12     = new MyDoublyLinkedListNode <int>(-1);

            node8.Next  = node9;
            node9.Next  = node10;
            node10.Next = node11;
            node11.Next = node12;

            node9.Prev  = node8;
            node10.Prev = node9;
            node11.Prev = node10;
            node12.Prev = node11;

            resultList.Head = node8;

            //act
            var result = _linkedList.RemoveDups(list);

            //assert
            var head           = result.Head;
            var resultListHead = resultList.Head;

            while (head != null)
            {
                head.Data.ShouldBeEquivalentTo(resultListHead.Data);
                head           = head.Next;
                resultListHead = resultListHead.Next;
            }
            resultListHead.ShouldBeEquivalentTo(null);
        }