예제 #1
0
        public static void Find_FilledList_NoMatchingValue_ShouldReturnNull_Test <TNode>(ILinkedList <TNode> list)
            where TNode : class, ILinkedListNode
        {
            Assert.NotNull(list);

            list.Add("Josephus Flavius");
            list.Add("Herodotus");
            list.Add("Tacitus");

            var node = list.Find("Alexander Argead");

            Assert.IsNull(node);
        }
예제 #2
0
        public static void Find_FilledList_ContainsMatchingValue_ShouldReturnNode_Test <TNode>(ILinkedList <TNode> list)
            where TNode : class, ILinkedListNode
        {
            Assert.NotNull(list);

            list.Add("Josephus Flavius");
            var targetNode = list.Add("Herodotus");

            list.Add("Tacitus");

            var foundNode = list.Find("Herodotus");

            Assert.AreSame(targetNode, foundNode);
        }
 public void Initialize()
 {
     list.Add("First");
     list.Add("Second");
     list.Add("Third");
     list.Add("Fourth");
     list.Add("Fifth");
 }
예제 #4
0
        public static void Remove_TailNode_ShouldSetPreviousAsTail_Test <TNode>(ILinkedList <TNode> list)
            where TNode : class, ILinkedListNode
        {
            Assert.NotNull(list);

            const string nodeValue = "Ray Bradbury";
            var          counter   = 0;

            list.Add($"{nodeValue}{++counter}");
            list.Add($"{nodeValue}{++counter}");
            var nodeToRemove = list.Add($"{nodeValue}{++counter}");

            list.Remove(nodeToRemove);

            CollectionAssert.AreEqual(new string[] { $"{nodeValue}1", $"{nodeValue}2" }, list.ToArray());
        }
예제 #5
0
        public static void Remove_HeadNode_ShouldSetNextAsHead_Test <TNode>(ILinkedList <TNode> list)
            where TNode : class, ILinkedListNode
        {
            Assert.NotNull(list);

            const string nodeValue = "Stanislaw Lem";
            var          counter   = 0;

            var nodeToRemove = list.Add($"{nodeValue}{++counter}");

            list.Add($"{nodeValue}{++counter}");
            list.Add($"{nodeValue}{++counter}");

            list.Remove(nodeToRemove);

            CollectionAssert.AreEqual(new string[] { $"{nodeValue}2", $"{nodeValue}3" }, list.ToArray());
        }
예제 #6
0
        public static void Remove_NodeFromNonEmptyList_ShouldRemoveNodeFromList_Test <TNode>(ILinkedList <TNode> list)
            where TNode : class, ILinkedListNode
        {
            Assert.NotNull(list);

            const string nodeValue = "Robert Heinlein";
            var          counter   = 0;

            list.Add($"{nodeValue}{++counter}");
            var nodeToRemove = list.Add($"{nodeValue}{++counter}");

            list.Add($"{nodeValue}{++counter}");

            list.Remove(nodeToRemove);

            CollectionAssert.AreEqual(new string[] { $"{nodeValue}1", $"{nodeValue}3" }, list.ToArray());
        }
 public void AddAdjacent(params INode <T>[] nodes)
 {
     foreach (var node in nodes)
     {
         adjacent.Add(node);
         Count++;
     }
 }
예제 #8
0
        public static void Value_NodeInTheList_ShouldReturnValue_Test <TNode>(ILinkedList <TNode> list)
            where TNode : class, ILinkedListNode
        {
            Assert.NotNull(list);
            const string nodeValue = "Antonin Dvorak";
            var          node      = list.Add(nodeValue);

            Assert.AreEqual(nodeValue, node.Value);
        }
예제 #9
0
        public static void Remove_OnlyNode_ListShouldBecomeEmpty_Test <TNode>(ILinkedList <TNode> list)
            where TNode : class, ILinkedListNode
        {
            Assert.NotNull(list);

            const string nodeValue = "Neil Gaiman";

            var nodeToRemove = list.Add($"{nodeValue}");

            list.Remove(nodeToRemove);

            CollectionAssert.AreEqual(Enumerable.Empty <string>(), list.ToArray());
        }
예제 #10
0
        public static void ToArray_FilledList_ShouldReturnArrayItemsInListOrder_Test <TNode>(ILinkedList <TNode> list)
            where TNode : class, ILinkedListNode
        {
            Assert.NotNull(list);

            const string nodeValue = "Roger Zelazny";
            var          counter   = 0;

            var node1 = list.Add($"{nodeValue}{++counter}");
            var node2 = list.Add($"{nodeValue}{++counter}");

            list.Add($"{nodeValue}{++counter}");
            var node4 = list.Add($"{nodeValue}{++counter}");

            list.Add($"{nodeValue}{++counter}");

            list.Remove(node2);
            list.Remove(node1);
            list.Remove(node4);

            CollectionAssert.AreEqual(new string[] { $"{nodeValue}3", $"{nodeValue}5" }, list.ToArray());
        }
        private void StepToAllNodes(TreeNode <T> treeNode)
        {
            if (treeNode != null)
            {
                StepToAllNodes(treeNode.Left);

                if (treeNode.Left == null && treeNode.Right == null)
                {
                    linkedList.Add(treeNode.Value);
                }

                StepToAllNodes(treeNode.Right);
            }
        }
예제 #12
0
        public static void Value_NodeRemovedFromList_ShouldThrow_Test <TNode>(ILinkedList <TNode> list)
            where TNode : class, ILinkedListNode
        {
            Assert.NotNull(list);
            const string nodeValue = "Antonin Dvorak";

            var node = list.Add(nodeValue);

            list.Remove(node);

            var e = Assert.Throws <InvalidOperationException>(() => { var t = node.Value; });

            Assert.AreEqual("Node is invalidated", e.Message);
        }
예제 #13
0
        public static void Remove_NodeFromAnotherList_ShouldThrow_Test <TNode>(ILinkedList <TNode> list, ILinkedList <TNode> anotherList)
            where TNode : class, ILinkedListNode
        {
            Assert.NotNull(list);

            const string nodeValue = "Harry Harrison";

            var nodeToRemove = anotherList.Add(nodeValue);

            var e = Assert.Throws <System.InvalidOperationException>(() => list.Remove(nodeToRemove));

            Assert.AreEqual("Node does not belong to this list", e.Message);

            CollectionAssert.AreEqual(Enumerable.Empty <string>(), list.ToArray());
        }
        private void StepToAllNodes(AssociativeTreeNode <T>[] array, AssociativeTreeNode <T> treeNode)
        {
            if (treeNode != null)
            {
                var leftNode = (treeNode.Left != -1) ? array[treeNode.Left] : null;
                StepToAllNodes(array, leftNode);

                if (treeNode.Left == -1 && treeNode.Right == -1)
                {
                    linkedList.Add(treeNode.Value);
                }

                var rightNode = (treeNode.Right != -1) ? array[treeNode.Right] : null;
                StepToAllNodes(array, rightNode);
            }
        }
예제 #15
0
        public static void Remove_NodeRemovedTwice_ShouldThrow_Test <TNode>(ILinkedList <TNode> list)
            where TNode : class, ILinkedListNode
        {
            Assert.NotNull(list);

            const string nodeValue = "Andre Norton";

            var nodeToRemove = list.Add($"{nodeValue}");

            list.Remove(nodeToRemove);

            var e = Assert.Throws <System.InvalidOperationException>(() => list.Remove(nodeToRemove));

            Assert.AreEqual("Node is invalidated", e.Message);

            CollectionAssert.AreEqual(Enumerable.Empty <string>(), list.ToArray());
        }
예제 #16
0
 private void AddEntry(TKey key, TValue value, ILinkedList <Entry <TKey, TValue> > bucket)
 {
     bucket.Add(new Entry <TKey, TValue>(key, value));
     Count++;
     TryResize();
 }
예제 #17
0
 public void AddTest(int[] inputArray, int value, int[] expectedArray)
 {
     SetUp(inputArray, expectedArray);
     actual.Add(value);
     Assert.AreEqual(expected, actual);
 }
예제 #18
0
        private static void TryLinkedList(ILinkedList <short> linkedList)
        {
            Console.WriteLine($"Trying {linkedList.GetType().Name}");

            var random = new Random();

            Console.WriteLine($"Display the queue: {linkedList}\n");

            for (var i = 0; i < 100; i++)
            {
                var randomValue = (short)random.Next(1000);
                Console.WriteLine($"Trying to add {randomValue} to the list.");
                linkedList.Add(randomValue);
                Console.WriteLine($"{randomValue} added to the list.");
            }

            for (short i = 0; i < 100; i++)
            {
                var op = random.Next(4); //randomly enqueue or dequeue
                try
                {
                    var randomValue = (short)random.Next(1000);
                    var randomIndex = random.Next(100);

                    switch (op)
                    {
                    case 0:    //Add
                        Console.WriteLine($"Trying to add {randomValue} to the list.");
                        linkedList.Add(randomValue);
                        Console.WriteLine($"{randomValue} added to the list.");
                        break;

                    case 1:    //Get
                        Console.WriteLine($"Trying to get...");
                        var val = linkedList.Get(random.Next(100));
                        Console.WriteLine($"get item:{val}");
                        break;

                    case 2:    //Insert
                        Console.WriteLine($"Trying to insert...");
                        linkedList.Insert(randomIndex, randomValue);
                        Console.WriteLine($"{randomValue} inserted to index:{randomIndex}.");
                        break;

                    case 3:    //Remove
                        Console.WriteLine($"Trying to remove...");
                        linkedList.Remove(randomIndex);
                        Console.WriteLine($"item removed at index:{randomIndex}.");
                        break;

                    default:
                        break;
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }

                Console.WriteLine($"Count: {linkedList.GetCount()}\n");
                Console.WriteLine($"Display the list: {linkedList}\n");
            }
        }