@@ -23,28 +23,24 @@ func testFramework(t *testing.T, sortingFunction func([]int) []int) {
23
23
expected : []int {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 },
24
24
name : "Reversed Unsigned" ,
25
25
},
26
-
27
26
//Sorted slice
28
27
{
29
28
input : []int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 },
30
29
expected : []int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 },
31
30
name : "Sorted Signed" ,
32
31
},
33
-
34
32
//Reversed slice
35
33
{
36
34
input : []int {10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 , - 1 , - 2 , - 3 , - 4 , - 5 , - 6 , - 7 , - 8 , - 9 , - 10 },
37
35
expected : []int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 },
38
36
name : "Reversed Signed" ,
39
37
},
40
-
41
38
//Reversed slice, even length
42
39
{
43
40
input : []int {10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , - 1 , - 2 , - 3 , - 4 , - 5 , - 6 , - 7 , - 8 , - 9 , - 10 },
44
41
expected : []int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 },
45
42
name : "Reversed Signed #2" ,
46
43
},
47
-
48
44
//Random order with repetitions
49
45
{
50
46
input : []int {- 5 , 7 , 4 , - 2 , 6 , 5 , 8 , 3 , 2 , - 7 , - 1 , 0 , - 3 , 9 , - 6 , - 4 , 10 , 9 , 1 , - 8 , - 9 , - 10 },
@@ -98,8 +94,73 @@ func TestHeap(t *testing.T) {
98
94
testFramework (t , HeapSort )
99
95
}
100
96
97
+ func TestCount (t * testing.T ) {
98
+ testFramework (t , Count )
99
+ }
100
+
101
101
func TestQuick (t * testing.T ) {
102
- testFramework (t , QuickSort )
102
+ testCases := []struct {
103
+ input []int
104
+ expected []int
105
+ name string
106
+ }{
107
+ //Sorted slice
108
+ {
109
+ input : []int {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 },
110
+ expected : []int {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 },
111
+ name : "Sorted Unsigned" ,
112
+ },
113
+ //Reversed slice
114
+ {
115
+ input : []int {10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 },
116
+ expected : []int {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 },
117
+ name : "Reversed Unsigned" ,
118
+ },
119
+ //Sorted slice
120
+ {
121
+ input : []int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 },
122
+ expected : []int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 },
123
+ name : "Sorted Signed" ,
124
+ },
125
+ //Reversed slice
126
+ {
127
+ input : []int {10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 , - 1 , - 2 , - 3 , - 4 , - 5 , - 6 , - 7 , - 8 , - 9 , - 10 },
128
+ expected : []int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 },
129
+ name : "Reversed Signed" ,
130
+ },
131
+ //Reversed slice, even length
132
+ {
133
+ input : []int {10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , - 1 , - 2 , - 3 , - 4 , - 5 , - 6 , - 7 , - 8 , - 9 , - 10 },
134
+ expected : []int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 },
135
+ name : "Reversed Signed #2" ,
136
+ },
137
+ //Random order with repetitions
138
+ {
139
+ input : []int {- 5 , 7 , 4 , - 2 , 6 , 5 , 8 , 3 , 2 , - 7 , - 1 , 0 , - 3 , 9 , - 6 , - 4 , 10 , 9 , 1 , - 8 , - 9 , - 10 },
140
+ expected : []int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 9 , 10 },
141
+ name : "Random order Signed" ,
142
+ },
143
+ //Single-entry slice
144
+ {
145
+ input : []int {1 },
146
+ expected : []int {1 },
147
+ name : "Singleton" ,
148
+ },
149
+ {
150
+ input : []int {},
151
+ expected : []int {},
152
+ name : "Empty Slice" ,
153
+ },
154
+ }
155
+ for _ , test := range testCases {
156
+ t .Run (test .name , func (t * testing.T ) {
157
+ QuickSort (test .input )
158
+ if ! reflect .DeepEqual (test .input , test .expected ) {
159
+ t .Errorf ("test %s failed" , test .name )
160
+ t .Errorf ("actual %v expected %v" , test .input , test .expected )
161
+ }
162
+ })
163
+ }
103
164
}
104
165
105
166
func TestShell (t * testing.T ) {
@@ -122,6 +183,10 @@ func TestSelection(t *testing.T) {
122
183
testFramework (t , SelectionSort )
123
184
}
124
185
186
+ func TestComb (t * testing.T ) {
187
+ testFramework (t , combSort )
188
+ }
189
+
125
190
//END TESTS
126
191
127
192
func benchmarkFramework (b * testing.B , f func (arr []int ) []int ) {
@@ -136,23 +201,18 @@ func benchmarkFramework(b *testing.B, f func(arr []int) []int) {
136
201
//Reversed slice
137
202
{[]int {10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 },
138
203
[]int {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }, "Reversed Unsigned" },
139
-
140
204
//Sorted slice
141
205
{[]int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 },
142
206
[]int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }, "Sorted Signed" },
143
-
144
207
//Reversed slice
145
208
{[]int {10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 , - 1 , - 2 , - 3 , - 4 , - 5 , - 6 , - 7 , - 8 , - 9 , - 10 },
146
209
[]int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }, "Reversed Signed" },
147
-
148
210
//Reversed slice, even length
149
211
{[]int {10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , - 1 , - 2 , - 3 , - 4 , - 5 , - 6 , - 7 , - 8 , - 9 , - 10 },
150
212
[]int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }, "Reversed Signed #2" },
151
-
152
213
//Random order with repetitions
153
214
{[]int {- 5 , 7 , 4 , - 2 , 6 , 5 , 8 , 3 , 2 , - 7 , - 1 , 0 , - 3 , 9 , - 6 , - 4 , 10 , 9 , 1 , - 8 , - 9 , - 10 },
154
215
[]int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 9 , 10 }, "Random order Signed" },
155
-
156
216
//Empty slice
157
217
{[]int {}, []int {}, "Empty" },
158
218
//Single-entry slice
@@ -188,8 +248,45 @@ func BenchmarkHeap(b *testing.B) {
188
248
benchmarkFramework (b , HeapSort )
189
249
}
190
250
251
+ func BenchmarkCount (b * testing.B ) {
252
+ benchmarkFramework (b , Count )
253
+ }
254
+
191
255
func BenchmarkQuick (b * testing.B ) {
192
- benchmarkFramework (b , QuickSort )
256
+ var sortTests = []struct {
257
+ input []int
258
+ expected []int
259
+ name string
260
+ }{
261
+ //Sorted slice
262
+ {[]int {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 },
263
+ []int {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }, "Sorted Unsigned" },
264
+ //Reversed slice
265
+ {[]int {10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 },
266
+ []int {1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }, "Reversed Unsigned" },
267
+ //Sorted slice
268
+ {[]int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 },
269
+ []int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }, "Sorted Signed" },
270
+ //Reversed slice
271
+ {[]int {10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , 0 , - 1 , - 2 , - 3 , - 4 , - 5 , - 6 , - 7 , - 8 , - 9 , - 10 },
272
+ []int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }, "Reversed Signed" },
273
+ //Reversed slice, even length
274
+ {[]int {10 , 9 , 8 , 7 , 6 , 5 , 4 , 3 , 2 , 1 , - 1 , - 2 , - 3 , - 4 , - 5 , - 6 , - 7 , - 8 , - 9 , - 10 },
275
+ []int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 }, "Reversed Signed #2" },
276
+ //Random order with repetitions
277
+ {[]int {- 5 , 7 , 4 , - 2 , 6 , 5 , 8 , 3 , 2 , - 7 , - 1 , 0 , - 3 , 9 , - 6 , - 4 , 10 , 9 , 1 , - 8 , - 9 , - 10 },
278
+ []int {- 10 , - 9 , - 8 , - 7 , - 6 , - 5 , - 4 , - 3 , - 2 , - 1 , 0 , 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 9 , 10 }, "Random order Signed" },
279
+ //Empty slice
280
+ {[]int {}, []int {}, "Empty" },
281
+ //Single-entry slice
282
+ {[]int {1 }, []int {1 }, "Singleton" },
283
+ }
284
+ b .ResetTimer ()
285
+ for i := 0 ; i < b .N ; i ++ {
286
+ for _ , test := range sortTests {
287
+ QuickSort (test .input )
288
+ }
289
+ }
193
290
}
194
291
195
292
func BenchmarkShell (b * testing.B ) {
@@ -213,4 +310,8 @@ func BenchmarkSelection(b *testing.B) {
213
310
benchmarkFramework (b , SelectionSort )
214
311
}
215
312
313
+ func BenchmarkComb (b * testing.B ) {
314
+ benchmarkFramework (b , combSort )
315
+ }
316
+
216
317
//END BENCHMARKS
0 commit comments