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]))); } } }
public void ReverseSingleSublistTest() { Func <ListNode <int>, int, int, ListNode <int> >[] functions = new Func <ListNode <int>, int, int, ListNode <int> >[] { ReverseSingleSublist.BruteForce, ReverseSingleSublist.SinglePass }; for (int i = 0; i < 10; i++) { int[] data = ArrayUtilities.CreateRandomArray(10, 0, 10); for (int start = 1; start <= 10; start++) { for (int end = start; end <= 10; end++) { ListNode <int>[] results = new ListNode <int> [functions.Length]; for (int j = 0; j < results.Length; j++) { ListNode <int> head = LinkedListUtilities.Initialize(data); results[j] = functions[j](head, start, end); Assert.IsTrue(LinkedListUtilities.AreEqual(results[0], results[j])); } } } } }
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)); }
public void ReverseTest() { Func <ListNode <int>, ListNode <int> >[] functions = new Func <ListNode <int>, ListNode <int> >[] { Reverse.BruteForce, Reverse.Build, Reverse.Traverse }; for (int i = 0; i < 10; i++) { int[] data = ArrayUtilities.CreateRandomArray(10, 0, 15); ListNode <int>[] results = new ListNode <int> [functions.Length]; for (int j = 0; j < functions.Length; j++) { results[j] = functions[j](LinkedListUtilities.Initialize(data)); Assert.IsTrue(LinkedListUtilities.AreEqual(results[0], results[j])); } } }