forked from coder/coder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmetrics_internal_test.go
More file actions
93 lines (86 loc) · 1.53 KB
/
metrics_internal_test.go
File metadata and controls
93 lines (86 loc) · 1.53 KB
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
package metricscache
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestClosest(t *testing.T) {
t.Parallel()
testCases := []struct {
Name string
Keys []int
Input int
Expected int
NotFound bool
}{
{
Name: "Empty",
Input: 10,
NotFound: true,
},
{
Name: "Equal",
Keys: []int{1, 2, 3, 4, 5, 6, 10, 12, 15},
Input: 10,
Expected: 10,
},
{
Name: "ZeroOnly",
Keys: []int{0},
Input: 10,
Expected: 0,
},
{
Name: "NegativeOnly",
Keys: []int{-10, -5},
Input: 10,
Expected: -5,
},
{
Name: "CloseBothSides",
Keys: []int{-10, -5, 0, 5, 8, 12},
Input: 10,
Expected: 8,
},
{
Name: "CloseNoZero",
Keys: []int{-10, -5, 5, 8, 12},
Input: 0,
Expected: -5,
},
{
Name: "CloseLeft",
Keys: []int{-10, -5, 0, 5, 8, 12},
Input: 20,
Expected: 12,
},
{
Name: "CloseRight",
Keys: []int{-10, -5, 0, 5, 8, 12},
Input: -20,
Expected: -10,
},
{
Name: "ChooseZero",
Keys: []int{-10, -5, 0, 5, 8, 12},
Input: 2,
Expected: 0,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.Name, func(t *testing.T) {
t.Parallel()
m := make(map[int]int)
for _, k := range tc.Keys {
m[k] = k
}
found, _, ok := closest(m, tc.Input)
if tc.NotFound {
require.False(t, ok, "should not be found")
} else {
require.True(t, ok)
require.Equal(t, tc.Expected, found, "closest")
}
})
}
}