-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlevel03_test.go
79 lines (68 loc) · 1.59 KB
/
level03_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
package main
import (
"os"
"reflect"
"runtime"
"testing"
)
const Max = 50000
var n, r, sorted, reverse_sorted [Max]int
var sorts = []func([]int){selectionSort, bubbleSort, insertionSort}
func initAscending(a []int) {
for i := 0; i < Max; i++ {
a[i] = i + 1
}
}
func initDescending(a []int) {
for i := 0; i < Max; i++ {
a[i] = Max - i
}
}
func TestSorting(t *testing.T) {
logger.SetOutput(os.Stdout)
initAscending(sorted[:])
initDescending(reverse_sorted[:])
for _, sort := range sorts {
name := runtime.FuncForPC(reflect.ValueOf(sort).Pointer()).Name()
t.Run("Sorted List", func(t *testing.T) {
initAscending(n[:])
if n != sorted {
t.Errorf("%s failed to initialize a sorted list", name)
}
sort(n[:])
if n != sorted {
t.Errorf("%s failed to sort a sorted list", name)
}
})
t.Run("Reverse Sorted List", func(t *testing.T) {
initDescending(r[:])
if r != reverse_sorted {
t.Errorf("%s failed to initialize a reverse sorted list", name)
}
// logger.Printf("BEFORE:%v (%s)\n", r, name)
sort(r[:])
// logger.Printf("AFTER :%v (%s)\n", r, name)
if r != sorted {
t.Errorf("%s failed to sort a reverse sorted list", name)
}
})
}
}
func BenchmarkSelectionSort(b *testing.B) {
// logger.SetOutput(os.Stdout)
initAscending(sorted[:])
initDescending(reverse_sorted[:])
initAscending(n[:])
initDescending(r[:])
b.ResetTimer()
b.Run("Sorted List", func(b *testing.B) {
for i := 0; i < b.N; i++ {
selectionSort(n[:])
}
})
b.Run("Reverse Sorted List", func(b *testing.B) {
for i := 0; i < b.N; i++ {
selectionSort(r[:])
}
})
}