-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathgsv_test.go
executable file
·183 lines (156 loc) · 4.86 KB
/
gsv_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package gsv
import (
cryptoRand "crypto/rand"
"testing"
)
var visName string
var sorterMap map[string]Sorter
func init() {
test = true
sorterMap = map[string]Sorter{
"bogo": BogoSort,
"bubble": BubbleSort,
"cocktail": CocktailSort,
"comb": CombSort,
"counting": CountingSort,
"cycle": CycleSort,
"gnome": GnomeSort,
"insertion": InsertionSort,
"oddEven": OddEvenSort,
"selection": SelectionSort,
"sleep": SleepSort,
"stooge": StoogeSort,
"pancake": PancakeSort,
"quick": QuickSort,
"merge": MergeSort,
"shell": ShellSort,
"heap": HeapSort,
"radix": RadixSort,
"bitonic": BitonicSort,
}
}
// StdoutVisualizer implements the Visualizer interface for stdout output
type StdoutVisualizer struct{}
func (sv *StdoutVisualizer) Setup(name string) {
// No setup required for stdout
}
func (sv *StdoutVisualizer) AddFrame(arr []int) {
WriteStdout(arr)
}
func (sv *StdoutVisualizer) Complete() {
// No completion step required for stdout
}
func randomArray(n int, max int) []int {
arr := make([]int, n)
for i := 0; i < n; i++ {
b := make([]byte, 1)
cryptoRand.Read(b)
number := float64(b[0])
arr[i] = int(number / 255 * float64(max))
}
return arr
}
func makeVisualizer(name string) Visualizer {
if name == "gif" {
return &GifVisualizer{}
}
if name == "stdout" {
return &StdoutVisualizer{}
}
return nil
}
func runSort(visName string, arr []int, algo string, sortFunc Sorter) {
visualizer := makeVisualizer(visName)
visualizer.Setup(algo)
sortFunc(arr, visualizer.AddFrame)
visualizer.Complete()
}
func Test_GIF(t *testing.T) {
Max = 9
Count = 9
Mode = 2
runSort("gif", randomArray(Count, Max), "selection", SelectionSort)
Mode = 1
for k, v := range sorterMap {
t.Log(k)
runSort("gif", randomArray(Count, Max), k, v)
}
t.Log("finish")
}
func Test_STDOUT(t *testing.T) {
Max = 9
Count = 9
Mode = 1
for k, v := range sorterMap {
t.Log(k)
runSort("stdout", randomArray(Count, Max), k, v)
}
t.Log("finish")
}
// go test -bench=.
func Benchmark_bogo_sort(b *testing.B) { benchmarkSort("bogo", b) }
func Benchmark_bubble_sort(b *testing.B) { benchmarkSort("bubble", b) }
func Benchmark_cocktail_sort(b *testing.B) { benchmarkSort("cocktail", b) }
func Benchmark_comb_sort(b *testing.B) { benchmarkSort("comb", b) }
func Benchmark_counting_sort(b *testing.B) { benchmarkSort("counting", b) }
func Benchmark_cycle_sort(b *testing.B) { benchmarkSort("cycle", b) }
func Benchmark_gnome_sort(b *testing.B) { benchmarkSort("gnome", b) }
func Benchmark_insertion_sort(b *testing.B) { benchmarkSort("insertion", b) }
func Benchmark_oddEven_sort(b *testing.B) { benchmarkSort("oddEven", b) }
func Benchmark_selection_sort(b *testing.B) { benchmarkSort("selection", b) }
func Benchmark_sleep_sort(b *testing.B) { benchmarkSort("sleep", b) }
func Benchmark_stooge_sort(b *testing.B) { benchmarkSort("stooge", b) }
func Benchmark_pancake_sort(b *testing.B) { benchmarkSort("pancake", b) }
func Benchmark_quick_sort(b *testing.B) { benchmarkSort("quick", b) }
func Benchmark_shell_sort(b *testing.B) { benchmarkSort("shell", b) }
func Benchmark_heap_sort(b *testing.B) { benchmarkSort("heap", b) }
func Benchmark_merge_sort(b *testing.B) { benchmarkSort("merge", b) }
func Benchmark_radix_sort(b *testing.B) { benchmarkSort("radix", b) }
func Benchmark_bitonic_sort(b *testing.B) { benchmarkSort("bitonic", b) }
// WriteNop is a writer for FrameGen that does nothing.
// Ensures we only benchmark algorithms.
func WriteNop(_ []int) {}
func benchmarkSort(sort string, b *testing.B) {
arr := randomArray(Count, Max)
frameGen := FrameGen(WriteNop)
if sortFunc, found := sorterMap[sort]; found {
for n := 0; n < b.N; n++ {
sortFunc(arr, frameGen)
}
}
}
// cloneArray Clones an array so source and the result are not backed by the same slice.
func cloneArray(source []int) []int {
destination := make([]int, len(source))
copy(destination, source)
return destination
}
// TestCloneArray checks that cloneArray creates a separate copy and not a slice backed by the same array.
func TestCloneArray(t *testing.T) {
s := []int{1, 2, 3, 4, 5}
d := cloneArray(s)
if &s == &d {
t.Error("Source and Destination address should not be equal")
}
for i := range s {
if d[i] != s[i] {
t.Errorf("Expected index [%d] to be the same", i)
}
if &d[i] == &s[i] {
t.Errorf("Expected address of index [%d] to be different", i)
}
}
}
// BenchmarkConsistentArrayNoFramegen times the sort algorithms for the same array of random data
// for each algorithm without the overhead of Frame generation.
func BenchmarkConsistentArrayNoFramegen(b *testing.B) {
arr := randomArray(1000, 750)
for method, sortFn := range sorterMap {
b.Run(method, func(b *testing.B) {
for i := 0; i < b.N; i++ {
arrCopy := cloneArray(arr)
sortFn(arrCopy, nil)
}
})
}
}