Beispiel #1
0
        public void RemoveDupsTest()
        {
            Action <ListNode <int> >[] functions = new Action <ListNode <int> >[]
            {
                RemoveDups.Search,
                RemoveDups.Multiset
            };

            for (int i = 0; i < 10; i++)
            {
                int[] data = ArrayUtilities.CreateRandomArray(20, 0, 15);

                ListNode <int>[] nodes = new ListNode <int> [functions.Length];

                for (int j = 0; j < nodes.Length; j++)
                {
                    nodes[j] = LinkedListUtilities.Initialize(data);
                }

                for (int j = 0; j < functions.Length; j++)
                {
                    functions[j](nodes[j]);
                }

                for (int j = 1; j < functions.Length; j++)
                {
                    Assert.IsTrue(ArrayUtilities.AreEqual(LinkedListUtilities.ToArray(nodes[0]), LinkedListUtilities.ToArray(nodes[j])));
                }
            }
        }
Beispiel #2
0
        public void MergeTwoSortedListsTest()
        {
            Func <ListNode <int>, ListNode <int>, ListNode <int> >[] functions = new Func <ListNode <int>, ListNode <int>, ListNode <int> >[]
            {
                MergeTwoSortedLists.AppendAndSort,
                MergeTwoSortedLists.SinglePass
            };

            for (int i = 0; i < 10; i++)
            {
                int[] data1 = ArrayUtilities.CreateRandomArray(10, 0, 25);
                int[] data2 = ArrayUtilities.CreateRandomArray(10, 0, 25);
                Array.Sort(data1);
                Array.Sort(data2);

                int[] expected = new int[data1.Length + data2.Length];
                Array.Copy(data1, expected, data1.Length);
                Array.Copy(data2, 0, expected, data1.Length, data2.Length);
                Array.Sort(expected);

                for (int j = 0; j < functions.Length; j++)
                {
                    ListNode <int> result = functions[j](LinkedListUtilities.Initialize(data1), LinkedListUtilities.Initialize(data2));
                    int[]          actual = LinkedListUtilities.ToArray(result);

                    Assert.IsTrue(ArrayUtilities.AreEqual(expected, actual));
                }
            }
        }
        private static ListNode <int> BruteForce(ListNode <int> head, int start, int end)
        {
            int[] data = LinkedListUtilities.ToArray(head);
            for (int i = 0; i < (end - start + 1) / 2; i++)
            {
                ArrayUtilities.Swap(data, start + i - 1, end - i - 1);
            }

            return(LinkedListUtilities.Initialize(data));
        }