summaryrefslogtreecommitdiff
path: root/sort/sort_test.go
blob: ee49893bf36c32d6bb00d97c03c94d4b0a101bf4 (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
211
212
213
214
215
216
217
package sort

import (
	"fmt"
	"testing"

	"codeberg.org/snonux/algorithms/ds"
)

// Store here so that Go isn't optimizing the benchmark away.
var benchResult interface{}

const minLength int = 1
const maxLength int = 1000000
const factor int = 100
const maxSlowLength int = 100000

type sortAlgorithm[V ds.Number] func(ds.ArrayList[V]) ds.ArrayList[V]

func TestSleepSort(t *testing.T) {
	a := ds.NewRandomArrayList[int](10, 10)
	a = Sleep(a)
	if !a.Sorted() {
		t.Errorf("Array not sorted: %v", a)
	}
}

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

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

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

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

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

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

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

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

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

func TestShuffleSort(t *testing.T) {
	for i := 10; i <= maxLength; i *= factor {
		testShuffleSort(Shuffle[int], i, t)
	}
}

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

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

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

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

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

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

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

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

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

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

// shortMaxLength caps the largest input size exercised under `go test -short`
// (used by `make verify`, which runs under the race detector where the
// million-element cases would be far too slow). The full range still runs in a
// plain `make test`.
const shortMaxLength int = 10000

func test[V ds.Number](sort sortAlgorithm[V], l int, t *testing.T) {
	cb := func(t *testing.T) {
		if testing.Short() && l > shortMaxLength {
			t.Skipf("skipping size %d in -short mode", l)
		}
		t.Parallel()
		a := ds.NewRandomArrayList[V](l, -1)
		a = sort(a)
		if !a.Sorted() {
			t.Errorf("Array not sorted: %v", a)
		}
	}
	t.Run(fmt.Sprintf("%d", l), cb)
}

func testShuffleSort[V ds.Number](sort sortAlgorithm[V], l int, t *testing.T) {
	cb := func(t *testing.T) {
		if testing.Short() && l > shortMaxLength {
			t.Skipf("skipping size %d in -short mode", l)
		}
		t.Parallel()
		a := sort(ds.NewAscendingArrayList[V](l))
		if a.Sorted() {
			t.Errorf("Array sorted: %v", a.FirstN(10))
		}
	}
	t.Run(fmt.Sprintf("%d", l), cb)
}

func benchmark[V ds.Number](sort sortAlgorithm[V], l int, b *testing.B) {
	a := ds.NewRandomArrayList[V](l, -1)
	aux := ds.NewArrayList[V](l)

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

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

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