/
sort_test.go
53 lines (43 loc) · 961 Bytes
/
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
package quicksort_test
import (
"github.com/droxer/quicksort"
"math/rand"
"reflect"
"testing"
"time"
)
func init() {
seed := time.Now().Unix()
rand.Seed(seed)
}
func perm(n int) (out []int) {
for _, v := range rand.Perm(n) {
out = append(out, v)
}
return
}
func TestQSort(t *testing.T) {
values := []int{9, 1, 20, 3, 6, 7}
expected := []int{1, 3, 6, 7, 9, 20}
quicksort.Sort(values)
if !reflect.DeepEqual(values, expected) {
t.Fatalf("expected %d, actual is %d", 1, values[0])
}
}
func BenchmarkQSort100(b *testing.B) {
benchmarkQSort(100, b)
}
func BenchmarkQSort1000(b *testing.B) {
benchmarkQSort(1000, b)
}
func BenchmarkQSort10000(b *testing.B) {
benchmarkQSort(10000, b)
}
func benchmarkQSort(i int, b *testing.B) {
for j := 0; j < b.N; j++ {
b.StopTimer()
values := perm(i)
b.StartTimer()
quicksort.Sort(values)
}
}