Beispiel #1
0
        public void _01_WhenRandom_Then_ListsAreMerged()
        {
            Chapter08.MyLinkedList l;
            Chapter08.MyLinkedList f;
            Random     random = new Random();
            List <int> list1  = new List <int>();

            for (int i = 0; i < 10000; i++)
            {
                list1.Add(random.Next(0, 50000));
            }
            l = CreateLinkedList(list1.OrderBy(i => i).ToArray());

            List <int> list2 = new List <int>();

            for (int i = 0; i < 10000; i++)
            {
                list2.Add(random.Next(0, 50000));
            }
            f = CreateLinkedList(list2.OrderBy(i => i).ToArray());

            var mergeSortedLists = Chapter08._01_MergeSortedLists(l, f);

            Assert.AreEqual(list1.Count + list2.Count, mergeSortedLists.Count);


            list1.AddRange(list2);
            list1.Sort();
            Chapter08.MyNode current = mergeSortedLists.Head;
            for (int i = 0; i < list1.Count; i++)
            {
                Assert.AreEqual(list1[i], current.Data);
                current = current.Next;
            }
        }
Beispiel #2
0
        public void _01_When_ListIsNull_ThenMerged()
        {
            var l = CreateLinkedList(3, 11);

            var merged = Chapter08._01_MergeSortedLists(l, null);

            Assert.AreEqual(l, merged);

            merged = Chapter08._01_MergeSortedLists(null, l);
            Assert.AreEqual(l, merged);
        }
Beispiel #3
0
        public void When_Reversed_Then_InDescendingOrder()
        {
            var values     = Enumerable.Range(15, 300).ToArray();
            var linkedList = CreateLinkedList(values);

            Chapter08._02_ReverseSinglyLinkedList(linkedList);

            Assert.AreEqual(values.Length, linkedList.Count);
            Chapter08.MyNode current = linkedList.Head;
            foreach (var val in values.OrderByDescending(i => i))
            {
                Assert.AreEqual(val, current.Data);
                current = current.Next;
            }
        }
Beispiel #4
0
        public void _01_When_SecondListLongerThanFirst_Then_SingleMergedSortedListReturned()
        {
            var l = CreateLinkedList(3, 11);
            var f = CreateLinkedList(2, 5, 7);

            var merged = Chapter08._01_MergeSortedLists(l, f);

            Assert.AreEqual(5, merged.Count);
            int[]            expected = new[] { 2, 3, 5, 7, 11 };
            Chapter08.MyNode actual   = merged.Head;
            foreach (var i in expected)
            {
                Assert.AreEqual(i, actual.Data);
                actual = actual.Next;
            }
        }
Beispiel #5
0
        public void When_KthElementDoesNotExist_Then_ListStillValid()
        {
            List <int> values = new List <int>()
            {
                1, 2, 3, 4, 5, 6, 7, 8
            };
            var linkedList = CreateLinkedList(values.ToArray());

            int k = 28;

            Chapter08._09_RemoveKthLastElement(linkedList, k);

            Assert.AreEqual(values.Count, linkedList.Count);
            Chapter08.MyNode current = linkedList.Head;
            foreach (var value in values)
            {
                Assert.AreEqual(value, current.Data);
                current = current.Next;
            }
        }