## ~telemachus/algorithms

ref: 597d3c5170b0fd3982a2aa1d12e39179f2293bbf algorithms/quickselect_test.go -rw-r--r-- 1.5 KiB
597d3c51Peter Aronoff Improve the README 1 year, 2 months ago
`                                                                                `
```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```
```package algorithms_test

import (
"testing"

"git.sr.ht/~telemachus/algorithms"
)

var selectTests = map[string]struct {
given     []int
nthLowest int
expected  int
}{
"empty slice":                           {[]int{}, 0, -1},
"nthLowest > len(xs)":                   {[]int{1}, 4, -1},
"one-item slice":                        {[]int{14}, 0, 14},
"three-item slice; nthLowest at start":  {[]int{15, 16, 14}, 0, 14},
"three-item slice; nthLowest in middle": {[]int{15, 16, 14}, 1, 15},
"three-item slice; nthLowest at end":    {[]int{15, 16, 14}, 2, 16},
"ten-item slice, nthLowest in middle":   {[]int{16, 14, 15, 8, 1, 3, 5, 9, 2, 4}, 4, 5},
}

func TestLomutoQuickselect(t *testing.T) {
for msg, tc := range selectTests {
t.Run(msg, func(t *testing.T) {
actual := algorithms.QuickselectL(tc.given, 0, len(tc.given)-1, tc.nthLowest)

if tc.expected != actual {
t.Errorf("expected %d; actual %d", tc.expected, actual)
}
})
}
}

func TestHoareQuickselect(t *testing.T) {
for msg, tc := range selectTests {
t.Run(msg, func(t *testing.T) {
actual := algorithms.QuickselectH(tc.given, 0, len(tc.given)-1, tc.nthLowest)

if tc.expected != actual {
t.Errorf("expected %d; actual %d", tc.expected, actual)
}
})
}
}

func TestYourBasicQuickselect(t *testing.T) {
for msg, tc := range selectTests {
t.Run(msg, func(t *testing.T) {
actual := algorithms.QuickselectYB(tc.given, tc.nthLowest)

if tc.expected != actual {
t.Errorf("expected %d; actual %d", tc.expected, actual)
}
})
}
}
```