-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy paththreshold_test.go
105 lines (92 loc) · 3.07 KB
/
threshold_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package check
import (
"github.com/stretchr/testify/assert"
"testing"
)
var testThresholds = map[string]*Threshold{
"10": {Lower: 0, Upper: 10},
"10:": {Lower: 10, Upper: PosInf},
"~:10": {Lower: NegInf, Upper: 10},
"10:20": {Lower: 10, Upper: 20},
"@10:20": {Lower: 10, Upper: 20, Inside: true},
"-10:10": {Lower: -10, Upper: 10},
"-10.001:10.001": {Lower: -10.001, Upper: 10.001},
"": nil,
}
func TestBoundaryToString(t *testing.T) {
assert.Equal(t, "10", BoundaryToString(10))
assert.Equal(t, "10.1", BoundaryToString(10.1))
assert.Equal(t, "10.001", BoundaryToString(10.001))
}
func TestParseThreshold(t *testing.T) {
for spec, ref := range testThresholds {
th, err := ParseThreshold(spec)
if ref == nil {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Equal(t, ref, th)
assert.Equal(t, spec, th.String())
}
}
}
func TestThreshold_String(t *testing.T) {
for spec, ref := range testThresholds {
if ref != nil {
assert.Equal(t, spec, ref.String())
}
}
}
// Threshold Generate an alert if x...
// 10 < 0 or > 10, (outside the range of {0 .. 10})
// 10: < 10, (outside {10 .. ∞})
// ~:10 > 10, (outside the range of {-∞ .. 10})
// 10:20 < 10 or > 20, (outside the range of {10 .. 20})
// @10:20 ≥ 10 and ≤ 20, (inside the range of {10 .. 20})
func TestThreshold_DoesViolate(t *testing.T) {
thr, err := ParseThreshold("10")
assert.NoError(t, err)
assert.True(t, thr.DoesViolate(11))
assert.False(t, thr.DoesViolate(10))
assert.False(t, thr.DoesViolate(0))
assert.True(t, thr.DoesViolate(-1))
thr, err = ParseThreshold("10:")
assert.NoError(t, err)
assert.False(t, thr.DoesViolate(3000))
assert.False(t, thr.DoesViolate(10))
assert.True(t, thr.DoesViolate(9))
assert.True(t, thr.DoesViolate(0))
assert.True(t, thr.DoesViolate(-1))
thr, err = ParseThreshold("~:10")
assert.NoError(t, err)
assert.False(t, thr.DoesViolate(-3000))
assert.False(t, thr.DoesViolate(0))
assert.False(t, thr.DoesViolate(10))
assert.True(t, thr.DoesViolate(11))
assert.True(t, thr.DoesViolate(3000))
thr, err = ParseThreshold("10:20")
assert.NoError(t, err)
assert.False(t, thr.DoesViolate(10))
assert.False(t, thr.DoesViolate(15))
assert.False(t, thr.DoesViolate(20))
assert.True(t, thr.DoesViolate(9))
assert.True(t, thr.DoesViolate(-1))
assert.True(t, thr.DoesViolate(20.1))
assert.True(t, thr.DoesViolate(3000))
thr, err = ParseThreshold("@10:20")
assert.NoError(t, err)
assert.True(t, thr.DoesViolate(10))
assert.True(t, thr.DoesViolate(15))
assert.True(t, thr.DoesViolate(20))
assert.False(t, thr.DoesViolate(9))
assert.False(t, thr.DoesViolate(-1))
assert.False(t, thr.DoesViolate(20.1))
assert.False(t, thr.DoesViolate(3000))
}
func TestFormatFloat(t *testing.T) {
assert.Equal(t, "1000000000000", FormatFloat(1000000000000))
assert.Equal(t, "1000000000", FormatFloat(1000000000))
assert.Equal(t, "1234567890.988", FormatFloat(1234567890.9877))
assert.Equal(t, "-Inf", FormatFloat(NegInf))
assert.Equal(t, "+Inf", FormatFloat(PosInf))
}