예제 #1
0
        static void Main(string[] args)
        {
            //var list2 = new LoopSingleLinkList<int>();
            //NodeWithLink<int> n = null;
            //var list2 = new LoopSingleLinkList<int>(1, 2, 3);
            //ShowList(list2);

            checkValueType();
            Console.WriteLine("\n");
            checkReferenceType();
            Console.WriteLine("\n");
            checkReferenceTypeWithNull();
            Console.WriteLine("\n");

            Console.WriteLine("Пустая коллекция:");
            var list = new LoopSingleLinkList <int>();

            ShowList(list);

            Console.WriteLine("\n");
            Console.WriteLine("Коллекция с одного элемента:");
            list.AddFirst(1);
            ShowList(list);

            Console.ReadKey();
        }
예제 #2
0
        public static void checkReferenceTypeWithNull()
        {
            Console.WriteLine("Проверка reference type c null\n");
            var list = new LoopSingleLinkList <Person>();

            try
            {
                list.AddLast(new Person("Serhii", "Yanchuk", "*****@*****.**"));
                Person serhii = null;
                list.AddLast(serhii);
                list.AddLast(new Person("Serhii", "Yanchuk", "*****@*****.**"));
                list.AddLast(new Person("Serhii", "Yanchuk", "*****@*****.**"));
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Исключение: {ex.Message}");
                Console.WriteLine($"Метод: {ex.TargetSite}");
                Console.WriteLine($"Трассировка стека: {ex.StackTrace}");
            }

            Console.WriteLine($"First: {list.First?.Value} \nLast: {list.Last?.Value}");
            Console.WriteLine($"Длина: {list.Length}");
            ShowList(list);

            NodeWithLink <Person> node = list.Find(null);

            Console.WriteLine($"\nНайден null value: {node != null}");

            list.Remove(null);
            Console.WriteLine("\nУдалено узел с null value");
            ShowList(list);
        }
예제 #3
0
        public static void SaveList <T>(object sender)
        {
            LoopSingleLinkList <T> collection = sender as LoopSingleLinkList <T>;

            try
            {
                using (StreamWriter sw = new StreamWriter(sender.GetHashCode().ToString() + ".txt", false, System.Text.Encoding.Default))
                {
                    if (collection.First != null) // список пуст
                    {
                        NodeWithLink <T> currentNode = collection.First;
                        do
                        {
                            if (currentNode.Value != null) // значение в узле равно null
                            {
                                sw.WriteLine(currentNode.Value);
                            }
                            currentNode = currentNode.Next;
                        } while (currentNode != collection.First); // перебор циклического списка
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Исключение: {ex.Message}");
                Console.WriteLine($"Метод: {ex.TargetSite}");
                Console.WriteLine($"Трассировка стека: {ex.StackTrace}");
            }
        }
예제 #4
0
        public void Remove_RemoveNotExistNodeFromList_TrueReturned()
        {
            // Arrange
            LoopSingleLinkList <int> list = new LoopSingleLinkList <int>(1, 2, 3, 4);
            // Act
            bool isDeleted = list.Remove(0);

            // Assert
            Assert.False(isDeleted);
        }
예제 #5
0
        public void Contains_ContainsNotExistNodeWithSpecificValue_FalseReturned()
        {
            // Arrange
            LoopSingleLinkList <int> list = new LoopSingleLinkList <int>(1, 2, 3, 4, 5);
            // Act
            bool isFound = list.Contains(0);

            // Assert
            Assert.False(isFound);
        }
예제 #6
0
        public void FindLast_FindLastNotExistNodeWithSpecificValueType_NullNodeReturned()
        {
            // Arrange
            LoopSingleLinkList <int> list = new LoopSingleLinkList <int>(1, 2, 3, 4, 5);
            // Act
            NodeWithLink <int> found = list.FindLast(0);

            // Assert
            Assert.Null(found);
        }
        public void AddFirst_NullNodeAddFirstInList_ArgumentNullExceptionReturned()
        {
            // Arrange
            LoopSingleLinkList <T> list  = new LoopSingleLinkList <T>();
            NodeWithLink <T>       first = null;

            // Act
            // Assert
            Assert.Throws <ArgumentNullException>(() => list.AddFirst(first));
        }
예제 #8
0
        public void AddFirst_NodeAddFirstInList_LengthUp1()
        {
            // Arrange
            LoopSingleLinkList <int> list  = new LoopSingleLinkList <int>();
            NodeWithLink <int>       first = new NodeWithLink <int>(1);

            // Act
            list.AddFirst(first);
            // Assert
            Assert.Equal(1, list.Length);
        }
        public void AddFirst_NodeAddFirstInList_LengthUp1()
        {
            // Arrange
            LoopSingleLinkList <T> list  = new LoopSingleLinkList <T>();
            NodeWithLink <T>       first = CreateSampleNode();

            // Act
            list.AddFirst(first);
            // Assert
            Assert.Equal(1, list.Length);
        }
예제 #10
0
        public void Clear_RemoveAllNodesFromList_NodesDeleted()
        {
            // Arrange
            LoopSingleLinkList <int> list = new LoopSingleLinkList <int>(1, 2, 3, 4, 5);

            // Act
            list.Clear();
            // Assert
            Assert.Equal(0, list.Length);
            Assert.Null(list.First);
            Assert.Null(list.Last);
        }
예제 #11
0
        public void Remove_RemoveExistNodeFromListWithOneNode_TrueReturned()
        {
            // Arrange
            LoopSingleLinkList <int> list = new LoopSingleLinkList <int>(1);
            // Act
            bool isDeleted = list.Remove(1);

            // Assert
            Assert.True(isDeleted);
            Assert.Null(list.First);
            Assert.Null(list.Last);
        }
예제 #12
0
        public void AddFirst_NodeWithReferenceTypeElementAddFirstInEmptyList_NodeAdded()
        {
            // Arrange
            LoopSingleLinkList <Person> list  = new LoopSingleLinkList <Person>();
            NodeWithLink <Person>       first = new NodeWithLink <Person>(new Person("s1", "y1"));

            // Act
            list.AddFirst(first);
            // Assert
            Assert.Same(first, list.First);
            Assert.Same(first, list.Last);
        }
예제 #13
0
        public void AddLast_NodeWithValueTypeElementAddLastInEmptyList_NodeAdded()
        {
            // Arrange
            LoopSingleLinkList <int> list = new LoopSingleLinkList <int>();
            NodeWithLink <int>       last = new NodeWithLink <int>(1);

            // Act
            list.AddLast(last);
            // Assert
            Assert.Same(last, list.First);
            Assert.Same(last, list.Last);
        }
        public void AddLast_NodeAddLastInEmptyList_NodeAdded()
        {
            // Arrange
            LoopSingleLinkList <T> list = new LoopSingleLinkList <T>();
            NodeWithLink <T>       last = CreateSampleNode();

            // Act
            list.AddLast(last);
            // Assert
            Assert.Same(last, list.First);
            Assert.Same(last, list.Last);
        }
        public void Remove_RemoveNotExistNodeFromList_FalseReturned()
        {
            // Arrange
            NodeWithLink <T>[]     sequence = CreateSequenceOfFiveNodes();
            LoopSingleLinkList <T> list     = new LoopSingleLinkList <T>(sequence);
            T removeValue = CreateSampleNode().Value;
            // Act
            bool isDeleted = list.Remove(removeValue);

            // Assert
            Assert.False(isDeleted);
        }
        public void Contains_ContainsNotExistNodeWithSpecificValue_TrueReturned()
        {
            // Arrange
            NodeWithLink <T>[]     sequence = CreateSequenceOfFiveNodes();
            LoopSingleLinkList <T> list     = new LoopSingleLinkList <T>(sequence);
            T notExistValue = CreateSampleNode().Value;
            // Act
            bool isFound = list.Contains(notExistValue);

            // Assert
            Assert.False(isFound);
        }
        public void Find_FindExistNodeWithSpecificValue_NotNullNodeReturned()
        {
            // Arrange
            NodeWithLink <T>[]     sequence = CreateSequenceOfFiveNodes();
            LoopSingleLinkList <T> list     = new LoopSingleLinkList <T>(sequence);
            T existValue = sequence[1].Value;
            // Act
            NodeWithLink <T> found = list.Find(existValue);

            // Assert
            Assert.NotNull(found);
        }
        public void FindLast_FindLastNotExistNodeWithSpecificValue_NullNodeReturned()
        {
            // Arrange
            NodeWithLink <T>[]     sequence = CreateSequenceOfFiveNodes();
            LoopSingleLinkList <T> list     = new LoopSingleLinkList <T>(sequence);
            T notExistValue = CreateSampleNode().Value;
            // Act
            NodeWithLink <T> found = list.FindLast(notExistValue);

            // Assert
            Assert.Null(found);
        }
        public void AddAfter_AddAfterWithAnyNullArgumentsNode_ArgumentNullExceptionReturned()
        {
            // Arrange
            NodeWithLink <T>       node1 = CreateSampleNode();
            LoopSingleLinkList <T> list  = new LoopSingleLinkList <T>(node1);
            NodeWithLink <T>       node2 = null;

            // Act
            // Assert
            Assert.Throws <ArgumentNullException>(() => list.AddAfter(node1, node2));
            Assert.Throws <ArgumentNullException>(() => list.AddAfter(node2, node1));
        }
예제 #20
0
        public void Find_FindExistNodeWithNullValue_NotNullNodeReturned()
        {
            // Arrange
            NodeWithLink <Person>       node1 = new NodeWithLink <Person>(new Person("s1", "y1"));
            NodeWithLink <Person>       node2 = new NodeWithLink <Person>(null);
            NodeWithLink <Person>       node3 = new NodeWithLink <Person>(new Person("s3", "y3"));
            LoopSingleLinkList <Person> list  = new LoopSingleLinkList <Person>(node1, node2, node3);
            // Act
            NodeWithLink <Person> found = list.Find(null);

            // Assert
            Assert.NotNull(found);
        }
        public void Clear_RemoveAllNodesFromList_NodesDeleted()
        {
            // Arrange
            NodeWithLink <T>[]     sequence = CreateSequenceOfFiveNodes();
            LoopSingleLinkList <T> list     = new LoopSingleLinkList <T>(sequence);

            // Act
            list.Clear();
            // Assert
            Assert.Equal(0, list.Length);
            Assert.Null(list.First);
            Assert.Null(list.Last);
        }
        public void Remove_RemoveExistNodeFromListWithOneNode_TrueReturned()
        {
            // Arrange
            NodeWithLink <T>       first = CreateSampleNode();
            LoopSingleLinkList <T> list  = new LoopSingleLinkList <T>(first);
            // Act
            bool isDeleted = list.Remove(first.Value);

            // Assert
            Assert.True(isDeleted);
            Assert.Null(list.First);
            Assert.Null(list.Last);
        }
        public void AddAfter_NodeAddAfterInList_LengthUp1()
        {
            // Arrange
            NodeWithLink <T>[]     sequence = CreateSequenceOfFiveNodes();
            LoopSingleLinkList <T> list     = new LoopSingleLinkList <T>(sequence);
            NodeWithLink <T>       node1    = sequence[0];
            NodeWithLink <T>       node0    = CreateSampleNode();

            // Act
            list.AddAfter(node1, node0);
            // Assert
            Assert.Equal(sequence.Length + 1, list.Length);
        }
예제 #24
0
        public void AddLast_NodeWithReferenceTypeElementAddLastInNotEmptyList_NodeAdded()
        {
            // Arrange
            LoopSingleLinkList <Person> list    = new LoopSingleLinkList <Person>(new Person("s1", "y1"), new Person("s2", "y2"));
            NodeWithLink <Person>       last    = list.Last;
            NodeWithLink <Person>       newLast = new NodeWithLink <Person>(new Person("s3", "y3"));

            // Act
            list.AddLast(newLast);
            // Assert
            Assert.Same(newLast, list.Last);
            Assert.Same(newLast, last.Next);
            Assert.Same(list.First, newLast?.Next);
        }
예제 #25
0
        public void AddFirst_NodeWithValueTypeElementAddFirstInNotEmptyList_NodeAdded()
        {
            // Arrange
            LoopSingleLinkList <int> list     = new LoopSingleLinkList <int>(1, 2);
            NodeWithLink <int>       first    = list.First;
            NodeWithLink <int>       newFirst = new NodeWithLink <int>(3);

            // Act
            list.AddFirst(newFirst);
            // Assert
            Assert.Same(newFirst, list.First);
            Assert.Same(newFirst, list.Last?.Next);
            Assert.Same(first, newFirst?.Next);
        }
        public void GetEnumerator_ForeachSequenceWithNullNode_ArgumentNullExceptionReturned()
        {
            // Arrange
            NodeWithLink <T>[]     sequence = CreateSequenceOfFiveNodes();
            LoopSingleLinkList <T> list     = new LoopSingleLinkList <T>(sequence);

            list.First.Next = null;
            // Act/Assert
            Assert.Throws <ArgumentNullException>(() => {
                foreach (var temp in list)
                {
                }
            });
        }
        public void GetEnumerator_ForeachWithCorrectSequence_CorrectIterations()
        {
            // Arrange
            NodeWithLink <T>[]     sequence = CreateSequenceOfFiveNodes();
            LoopSingleLinkList <T> list     = new LoopSingleLinkList <T>(sequence);
            int i = 0;

            // Act/Assert
            foreach (var temp in list)
            {
                Assert.Equal(sequence[i].Value, temp);
                i++;
            }
        }
예제 #28
0
        public void FindLast_FindLastNotExistNodeWithSpecificReferenceType_NullNodeReturned()
        {
            // Arrange
            NodeWithLink <Person>       node1 = new NodeWithLink <Person>(new Person("s1", "y1"));
            NodeWithLink <Person>       node2 = new NodeWithLink <Person>(new Person("s2", "y2"));
            NodeWithLink <Person>       node3 = new NodeWithLink <Person>(new Person("s3", "y3"));
            LoopSingleLinkList <Person> list  = new LoopSingleLinkList <Person>(node1, node2, node3);
            Person temp = new Person("s4", "y4");
            // Act
            NodeWithLink <Person> found = list.FindLast(temp);

            // Assert
            Assert.Null(found);
        }
예제 #29
0
        public void AddAfter_NodeAddAfterInList_LengthUp1()
        {
            // Arrange
            NodeWithLink <int>       node1 = new NodeWithLink <int>(1);
            NodeWithLink <int>       node2 = new NodeWithLink <int>(2);
            NodeWithLink <int>       node3 = new NodeWithLink <int>(3);
            LoopSingleLinkList <int> list  = new LoopSingleLinkList <int>(node1, node2, node3);
            NodeWithLink <int>       node4 = new NodeWithLink <int>(4);

            // Act
            list.AddAfter(node1, node4);
            // Assert
            Assert.Equal(4, list.Length);
        }
예제 #30
0
        public void Remove_RemoveExistMiddleNodeFromList_LengthDown1()
        {
            // Arrange
            NodeWithLink <int>       node1 = new NodeWithLink <int>(1);
            NodeWithLink <int>       node2 = new NodeWithLink <int>(2);
            NodeWithLink <int>       node3 = new NodeWithLink <int>(3);
            NodeWithLink <int>       node4 = new NodeWithLink <int>(4);
            LoopSingleLinkList <int> list  = new LoopSingleLinkList <int>(node1, node2, node3, node4);

            // Act
            list.Remove(3);
            // Assert
            Assert.Equal(3, list.Length);
        }