-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquicksort_test.go
100 lines (82 loc) · 1.99 KB
/
quicksort_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
package algorithms_test
import (
"testing"
"github.com/google/go-cmp/cmp"
"github.com/telemachus/algorithms"
)
var tests = map[string]struct {
orig []int
want []int
}{
"empty slice": {[]int{}, []int{}},
"one-item slice": {[]int{14}, []int{14}},
"three-item slice": {[]int{15, 16, 14}, []int{14, 15, 16}},
"four-item slice": {[]int{15, 16, 17, 14}, []int{14, 15, 16, 17}},
"five-item slice": {[]int{5, 6, 7, 4, 8}, []int{4, 5, 6, 7, 8}},
"slice containing duplicates": {[]int{2, 4, 2, 3, 1, 2}, []int{1, 2, 2, 2, 3, 4}},
"ten-item slice": {
[]int{16, 14, 15, 8, 1, 3, 5, 9, 2, 4},
[]int{1, 2, 3, 4, 5, 8, 9, 14, 15, 16},
},
"ten-item slice, reversed": {
[]int{9, 8, 7, 6, 5, 4, 3, 2, 1, 0},
[]int{0, 1, 2, 3, 4, 5, 6, 7, 8, 9},
},
}
func TestQuicksortL(t *testing.T) {
t.Parallel()
for msg, tc := range tests {
tc := tc
t.Run(msg, func(t *testing.T) {
t.Parallel()
got := make([]int, len(tc.orig))
copy(got, tc.orig)
algorithms.QuicksortL(got, 0, len(got)-1)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf(
"QuicksortL(%#v) failure (-want +got)\n%s",
tc.orig,
diff,
)
}
})
}
}
func TestQuicksortH(t *testing.T) {
t.Parallel()
for msg, tc := range tests {
tc := tc
t.Run(msg, func(t *testing.T) {
t.Parallel()
got := make([]int, len(tc.orig))
copy(got, tc.orig)
algorithms.QuicksortH(got, 0, len(got)-1)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf(
"QuicksortH(%#v) failure (-want +got)\n%s",
tc.orig,
diff,
)
}
})
}
}
func TestQuicksortYB(t *testing.T) {
t.Parallel()
for msg, tc := range tests {
tc := tc
t.Run(msg, func(t *testing.T) {
t.Parallel()
got := make([]int, len(tc.orig))
copy(got, tc.orig)
algorithms.QuicksortYB(got)
if diff := cmp.Diff(tc.want, got); diff != "" {
t.Errorf(
"QuicksortYB(%#v) failure (-want +got)\n%s",
tc.orig,
diff,
)
}
})
}
}