summaryrefslogtreecommitdiff
path: root/sort/sort_test.go
blob: 475c179cc6489bec3b341bcf7f60dc94d506745a (plain)
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package sort

import (
	"algorithms/ds"
	"fmt"
	"testing"
)

// Store results here to avoid compiler optimizations
var benchResult ds.ArrayList
var benchResultInt []int

const minLength int = 1
const maxLength int = 10000
const maxSlowLength int = 10000

var arrayListCache map[string]ds.ArrayList

type sortAlgorithm func(ds.ArrayList) ds.ArrayList
type sortAlgorithmInt func([]int) []int

func TestSelectionSort(t *testing.T) {
	for i := minLength; i <= maxSlowLength; i *= 10 {
		test(Selection, i, t)
	}
}

func TestInsertionSort(t *testing.T) {
	for i := minLength; i <= maxSlowLength; i *= 10 {
		test(Insertion, i, t)
	}
}

func TestShellSort(t *testing.T) {
	for i := minLength; i <= maxLength; i *= 10 {
		test(Shell, i, t)
	}
}

func TestMergeSort(t *testing.T) {
	for i := minLength; i <= maxLength; i *= 10 {
		test(Merge, i, t)
	}
}

func TestBottomUpMergeSort(t *testing.T) {
	t.Log("Parallel merge sort")
	for i := minLength; i <= maxLength; i *= 10 {
		test(BottomUpMerge, i, t)
	}
	test(BottomUpMerge, maxLength*2, t)
}

func TestParallelMergeSort(t *testing.T) {
	t.Log("Bottom-up merge sort")
	for i := minLength; i <= maxLength; i *= 10 {
		test(ParallelMerge, i, t)
	}
}

func TestQuickSort(t *testing.T) {
	for i := minLength; i <= maxLength; i *= 10 {
		test(Quick, i, t)
	}
}

func TestParallelQuickSort(t *testing.T) {
	for i := minLength; i <= maxLength; i *= 10 {
		test(ParallelQuick, i, t)
	}
}

func TestQuick3WaySort(t *testing.T) {
	for i := minLength; i <= maxLength; i *= 10 {
		test(Quick3Way, i, t)
	}
}

func TestShuffleSort(t *testing.T) {
	for i := 10; i <= maxLength; i *= 10 {
		testShuffle(Shuffle, i, t)
	}
}

func BenchmarkInsertionSort(b *testing.B) {
	for i := minLength; i <= maxSlowLength; i *= 10 {
		benchmark(Insertion, i, b)
	}
}

func BenchmarkSelectionSort(b *testing.B) {
	for i := minLength; i <= maxSlowLength; i *= 10 {
		benchmark(Selection, i, b)
	}
}

func BenchmarkShellSort(b *testing.B) {
	for i := minLength; i <= maxLength; i *= 10 {
		benchmark(Shell, i, b)
	}
}

func BenchmarkMergeSort(b *testing.B) {
	for i := minLength; i <= maxLength; i *= 10 {
		benchmark(Merge, i, b)
	}
}

func BenchmarkBottomUpMergeSort(b *testing.B) {
	for i := minLength; i <= maxLength; i *= 10 {
		benchmark(BottomUpMerge, i, b)
	}
}

func BenchmarkParallelMergeSort(b *testing.B) {
	for i := minLength; i <= maxLength; i *= 10 {
		benchmark(ParallelMerge, i, b)
	}
}

func BenchmarkQuickSort(b *testing.B) {
	for i := minLength; i <= maxLength; i *= 10 {
		benchmark(Quick, i, b)
	}
}

func BenchmarkParallelQuickSort(b *testing.B) {
	for i := minLength; i <= maxLength; i *= 10 {
		benchmark(ParallelQuick, i, b)
	}
}

func BenchmarkQuick3WaySort(b *testing.B) {
	for i := minLength; i <= maxLength; i *= 10 {
		benchmark(Quick3Way, i, b)
	}
}

/*
func BenchmarkShuffleSort(b *testing.B) {
	for i := minLength; i <= maxLength; i *= 10 {
		benchmark(Shuffle, i, b)
	}
}
*/

func test(sort sortAlgorithm, l int, t *testing.T) {
	cb := func(t *testing.T) {
		t.Parallel()
		a := makeRandomIntegers(l, -1)
		a = sort(a)
		if !a.Sorted() {
			t.Errorf("Array not sorted: %v", a)
		}
	}
	t.Run(fmt.Sprintf("%d", l), cb)
}

func testShuffle(sort sortAlgorithm, l int, t *testing.T) {
	cb := func(t *testing.T) {
		t.Parallel()
		a := sort(ds.NewAscendingArrayList(l))
		if a.Sorted() {
			t.Errorf("Array sorted: %v", a.FirstN(10))
		}
	}
	t.Run(fmt.Sprintf("%d", l), cb)
}

func benchmark(sort sortAlgorithm, l int, b *testing.B) {
	a := makeRandomIntegers(l, -1)
	b.Run(fmt.Sprintf("random(%d)", l), func(b *testing.B) {
		b.ResetTimer()
		for i := 0; i < b.N; i++ {
			benchResult = sort(a)
		}
	})

	a = ds.NewAscendingArrayList(l)
	b.Run(fmt.Sprintf("ascending(%d)", l), func(b *testing.B) {
		b.ResetTimer()
		for i := 0; i < b.N; i++ {
			benchResult = sort(a)
		}
	})

	a = ds.NewDescendingArrayList(l)
	b.Run(fmt.Sprintf("descending(%d)", l), func(b *testing.B) {
		b.ResetTimer()
		for i := 0; i < b.N; i++ {
			benchResult = sort(a)
		}
	})
}

func makeRandomIntegers(l, max int) ds.ArrayList {
	// Use a cache to make sure that all all sorting algos use the same
	// random sequences.
	if arrayListCache == nil {
		arrayListCache = make(map[string]ds.ArrayList)
	}

	key := fmt.Sprintf("random(%d, %d)", l, max)
	if a, ok := arrayListCache[key]; ok {
		return a
	}
	a := ds.NewRandomArrayList(l, max)
	arrayListCache[key] = a
	return a
}