forked from TheAlgorithms/Go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsorts_test.go
138 lines (116 loc) · 2.39 KB
/
sorts_test.go
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package sorts
import (
"testing"
)
func testFramework(t *testing.T, sortingFunction func([]int) []int) {
for _, test := range sortTests {
t.Run(test.name, func(t *testing.T) {
actual := sortingFunction(test.input)
pos, sorted := compareSlices(actual, test.expected)
if !sorted {
if pos == -1 {
t.Errorf("test %s failed due to slice length changing", test.name)
}
t.Errorf("test %s failed at index %d", test.name, pos)
}
})
}
}
//BEGIN TESTS
func TestBubble(t *testing.T) {
testFramework(t, bubbleSort)
}
func TestInsertion(t *testing.T) {
testFramework(t, InsertionSort)
}
func TestMerge(t *testing.T) {
testFramework(t, Mergesort)
}
func TestHeap(t *testing.T) {
testFramework(t, HeapSort)
}
func TestQuick(t *testing.T) {
testFramework(t, QuickSort)
}
func TestShell(t *testing.T) {
testFramework(t, ShellSort)
}
func TestRadix(t *testing.T) {
testFramework(t, RadixSort)
}
// Very slow, consider commenting
// func TestSelection(t *testing.T) {
// testFramework(t, SelectionSort)
// }
/* func TestTopological(t *testing.T) {
testFramework(t, topologicalSort)
} */
//END TESTS
func BenchmarkBubble(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range sortTests {
bubbleSort(test.input)
}
}
}
func BenchmarkInsertion(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range sortTests {
InsertionSort(test.input)
}
}
}
func BenchmarkMerge(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range sortTests {
Mergesort(test.input)
}
}
}
func BenchmarkHeap(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range sortTests {
HeapSort(test.input)
}
}
}
func BenchmarkQuick(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range sortTests {
QuickSort(test.input)
}
}
}
func BenchmarkShell(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range sortTests {
ShellSort(test.input)
}
}
}
func BenchmarkRadix(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range sortTests {
RadixSort(test.input)
}
}
}
// Very Slow, consider commenting
func BenchmarkSelection(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range sortTests {
SelectionSort(test.input)
}
}
}
func compareSlices(a []int, b []int) (int, bool) {
if len(a) != len(b) {
return -1, false
}
for pos := range a {
if a[pos] != b[pos] {
return pos, false
}
}
return -1, true
}