-
Notifications
You must be signed in to change notification settings - Fork 53
/
Test.java
75 lines (63 loc) · 2.41 KB
/
Test.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.example;
import java.util.*;
/**
* 排序算法测试类
*/
public class Test {
private static Bench.Function<int[], int[]> algorithm =
// Change to Bench.quickSort or Bench.mergeSort as appropriate.
Bench.insertionSort; //直接插入排序 // 需要测试的排序算法
// Bench.shellSort; //希尔排序
// Bench.selectionSort; //简单选择排序
// Bench.heapSort; //堆排序
// Bench.bubbleSort; //冒泡排序
// Bench.quickSort; //快速排序
// Bench.mergeSort; //归并排序
// Bench.radixSort; //基数排序
private static boolean check(int[] array) {
int[] reference = Arrays.copyOf(array, array.length);
int[] result;
Arrays.sort(reference);
try {
result = algorithm.apply(Arrays.copyOf(array, array.length));
} catch (Exception|StackOverflowError e) {
failed(array, reference);
System.out.println("Threw exception:");
e.printStackTrace(System.out);
return false;
}
if (!Arrays.equals(result, reference)) {
failed(array, reference);
System.out.println("Actual answer: " + show(result));
return false;
}
return true;
}
private static void failed(int[] array, int[] reference) {
System.out.println("Failed!");
System.out.println("Input array: " + show(array));
System.out.println("Expected answer: " + show(reference));
}
private static String show(int[] array) {
StringBuilder result = new StringBuilder();
result.append("{");
if (array.length > 0) result.append(array[0]);
for (int i = 1; i < array.length; i++) {
result.append(", ");
result.append(array[i]);
}
result.append("}");
return result.toString();
}
public static void main(String[] args) {
for (int size = 0; ; size++) {
System.out.printf("Testing on arrays of size %d...\n", size);
int[] sortedSample = Bench.generateSample(size, 0);
int[] partiallySortedSample = Bench.generateSample(size, 5);
int[] randomSample = Bench.generateSample(size, 100);
if (!check(sortedSample)) return;
if (!check(partiallySortedSample)) return;
if (!check(randomSample)) return;
}
}
}