public void SortingTest() { Action <int[]>[] actions = new Action <int[]>[] { BubbleSort.Sort, data => BucketSort.Sort(data, SortingTests.MaxValue), data => CountingSort.Sort(data, SortingTests.MaxValue), HeapSort.Sort, InsertionSort.Sort, MergeSort.Sort, QuickSort.Sort, data => RadixSort.Sort(data, SortingTests.MaxValue), }; for (int i = 0; i < 10; i++) { for (int j = 0; j < 100; j++) { int[] data = ArrayUtilities.CreateRandomArray(j, 0, SortingTests.MaxValue); int[][] results = new int[actions.Length][]; for (int k = 0; k < actions.Length; k++) { results[k] = new int[data.Length]; Array.Copy(data, results[k], data.Length); actions[k](results[k]); Assert.IsTrue(ArrayUtilities.AreEqual(results[k], results[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]))); } } }
public void SortedMergeTest() { Action <int[], int[]>[] actions = new Action <int[], int[]>[] { SortedMerge.BruteForce, SortedMerge.SinglePass }; for (int i = 0; i < 10; i++) { int[] A = ArrayUtilities.CreateRandomArray(40, 0, 20); int[] B = ArrayUtilities.CreateRandomArray(20, 0, 20); Array.Sort(A, 0, A.Length / 2); Array.Sort(B); int[][] results = new int[actions.Length][]; for (int j = 0; j < actions.Length; j++) { results[j] = new int[A.Length]; Array.Copy(A, results[j], A.Length); int[] copyB = new int[B.Length]; Array.Copy(B, copyB, B.Length); actions[j](results[j], copyB); Assert.IsTrue(ArrayUtilities.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)); } } }
public void MultiplyIntegerTest() { Func <int[], int[], int[]>[] functions = new Func <int[], int[], int[]>[] { MultiplyInteger.Convert, MultiplyInteger.ToArray }; for (int x = -20; x <= 20; x++) { for (int y = -20; y <= 20; y++) { int[][] results = new int[functions.Length][]; for (int k = 0; k < functions.Length; k++) { results[k] = functions[k](MultiplyInteger.ToArray(x), MultiplyInteger.ToArray(y)); Assert.IsTrue(ArrayUtilities.AreEqual(results[0], results[k])); } } } }
public void DepthFirstSearchTest() { Func <Vertex[], int, bool[]>[] functions = new Func <Vertex[], int, bool[]>[] { DepthFirstSearchTestClass.RunDepthFirstSearch, DepthFirstSearchTestClass.RunBreadthFirstSearch, DepthFirstSearchTestClass.RunBellmanFord, DepthFirstSearchTestClass.RunDjikstra, DepthFirstSearchTestClass.RunFloydWarshall, }; Vertex[] vertices = new Vertex[10]; for (int i = 0; i < vertices.Length; i++) { vertices[i] = new Vertex(); } for (int i = 0; i <= vertices.Length * (vertices.Length - 1); i++) { for (int j = 0; j < vertices.Length; j++) { bool[][] results = new bool[vertices.Length][]; for (int k = 0; k < functions.Length; k++) { foreach (Vertex vertex in vertices) { vertex.Reset(); } results[k] = functions[k](vertices, j); Assert.IsTrue(ArrayUtilities.AreEqual(results[0], results[k])); } } GraphUtilities.SetRandomEdge(vertices); } }
public void ShortestPathTest() { Func <Vertex[], int[, ]>[] functions = new Func <Vertex[], int[, ]>[] { ShortestPathTestClass.RunBellmanFord, ShortestPathTestClass.RunDjikstra, ShortestPathTestClass.RunFloydWarshall, }; Vertex[] vertices = new Vertex[10]; for (int i = 0; i < vertices.Length; i++) { vertices[i] = new Vertex(); } for (int i = 0; i <= vertices.Length * (vertices.Length - 1); i++) { for (int j = 0; j < vertices.Length; j++) { int[][,] results = new int[vertices.Length][, ]; for (int k = 0; k < functions.Length; k++) { foreach (Vertex vertex in vertices) { vertex.Reset(); } results[k] = functions[k](vertices); Assert.IsTrue(ArrayUtilities.AreEqual(results[0], results[k])); } } GraphUtilities.SetRandomEdge(vertices); } }