-
Notifications
You must be signed in to change notification settings - Fork 11
/
quickselect_test.go
84 lines (73 loc) · 1.75 KB
/
quickselect_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
package sturdyc_test
import (
"testing"
"time"
"github.com/creativecreature/sturdyc"
)
type testCutoffCase struct {
name string
numEntries int
percentile float64
kIndex int
}
func TestCutoff(t *testing.T) {
t.Parallel()
testCases := []testCutoffCase{
{
name: "1000 entries, 50th percentile",
numEntries: 1000,
percentile: 0.5,
kIndex: 500,
},
{
name: "1000 entries, 90th percentile",
numEntries: 1000,
percentile: 0.9,
kIndex: 900,
},
{
name: "999 entries, 16th percentile",
numEntries: 999,
percentile: 0.16,
kIndex: 159,
},
{
name: "13 entries, 2nd percentile",
numEntries: 13,
percentile: 0.02,
kIndex: 0,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
now := time.Now()
timestamps := make([]time.Time, 0, tc.numEntries)
for i := 0; i < tc.numEntries; i++ {
newTime := now.Add(time.Duration(i) * time.Second)
timestamps = append(timestamps, newTime)
}
cutoff := sturdyc.FindCutoff(timestamps, tc.percentile)
if cutoff != timestamps[tc.kIndex] {
t.Errorf("expected cutoff to be %v, got %v", timestamps[tc.kIndex], cutoff)
}
})
}
}
func TestReturnsEmptyTimeIfArgumentsAreInvalid(t *testing.T) {
t.Parallel()
values := make([]time.Time, 0, 10)
for i := 0; i < 10; i++ {
values = append(values, time.Now().Add(time.Duration(i)*time.Second))
}
if k := sturdyc.FindCutoff(values, -1); !k.IsZero() {
t.Errorf("expected zero time, got %v", k)
}
if k := sturdyc.FindCutoff(values, 1.1); !k.IsZero() {
t.Errorf("expected zero time, got %v", k)
}
if k := sturdyc.FindCutoff([]time.Time{}, 0.4); !k.IsZero() {
t.Errorf("expected zero time, got %v", k)
}
}