-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsort_test.go
92 lines (69 loc) · 1.82 KB
/
sort_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
package sort
import (
"fmt"
"reflect"
"runtime"
"testing"
)
func getFunctionName(f interface{}) string {
return runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name()
}
func TestBubbleSort(t *testing.T) {
type testFunc func(nums []int)
testBody := func(f testFunc, t *testing.T) {
nums := []int{1, 4, 5, 6, 2, 9, 8, 7, 3, 5, 2, 12}
res := []int{1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 12}
f(nums)
if !reflect.DeepEqual(nums, res) {
fmt.Println(nums)
t.Error(getFunctionName(f))
}
}
testBody(bubbleSort, t)
}
func TestInsertitonSort(t *testing.T) {
nums := []int{1, 4, 5, 6, 2, 9, 8, 7, 3, 5, 2, 12}
res := []int{1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 12}
insertionSort(nums)
if !reflect.DeepEqual(nums, res) {
fmt.Println(nums)
t.Error(getFunctionName(insertionSort))
}
}
func TestQuickSort(t *testing.T) {
type testFunc func(nums []int)
testBody := func(f testFunc, t *testing.T) {
nums := []int{1, 4, 5, 6, 2, 9, 8, 7, 3, 5, 2, 12}
res := []int{1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 12}
f(nums)
if !reflect.DeepEqual(nums, res) {
fmt.Println(nums)
t.Error(getFunctionName(f))
}
}
testBody(quickSort1, t)
testBody(quickSort3, t)
}
func TestMergeSort(t *testing.T) {
type testFunc func([]int, int, int)
testBody := func(f testFunc, t *testing.T) {
nums := []int{1, 4, 5, 6, 2, 9, 8, 7, 3, 5, 2, 12}
res := []int{1, 2, 2, 3, 4, 5, 5, 6, 7, 8, 9, 12}
f(nums, 0, len(nums)-1)
if !reflect.DeepEqual(nums, res) {
fmt.Println(nums)
t.Error("mergeSort")
}
}
testBody(mergeSort, t)
testBody(mergeSortWithGuard, t)
}
func TestBucketSort(t *testing.T) {
nums := []float32{0.897, 0.565, 0.656, 0.1234, 0.665, 0.3434}
bucketSort(nums, len(nums))
res := []float32{0.1234, 0.3434, 0.565, 0.656, 0.665, 0.897}
if !reflect.DeepEqual(nums, res) {
fmt.Println(nums)
t.Error("bucketSort")
}
}