-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathThreshold_test.go
132 lines (117 loc) · 4.49 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package check_x
import (
"errors"
"fmt"
"math"
"testing"
)
var toString = map[Threshold]string{
Threshold{lower: 1.2, upper: 3.4, outside: true}: "1.2 : 3.4 , outside: true",
Threshold{lower: 1, upper: 3.4, outside: true}: "1 : 3.4 , outside: true",
Threshold{lower: 1.2, upper: 3, outside: true}: "1.2 : 3 , outside: true",
Threshold{lower: 1.2, upper: 3.4, outside: false}: "1.2 : 3.4 , outside: false",
Threshold{lower: -1.2, upper: -3.4, outside: false}: "-1.2 : -3.4 , outside: false",
}
func TestThreshold_String(t *testing.T) {
for th, s := range toString {
if fmt.Sprint(th) != s {
t.Errorf("Got: %s expected: %s", th, s)
}
}
}
var stringToThreshold = []struct {
input string
threshold *Threshold
err error
}{
{"-3.4", &Threshold{input: "-3.4", lower: 0, upper: -3.4, outside: true}, nil},
{" 3.4", &Threshold{input: "3.4", lower: 0, upper: 3.4, outside: true}, nil},
{"3", &Threshold{input: "3", lower: 0, upper: 3, outside: true}, nil},
{"-3", &Threshold{input: "-3", lower: 0, upper: -3, outside: true}, nil},
{"foo", nil, errors.New("")},
{"3,4", nil, errors.New("")},
{" -3.4:", &Threshold{input: "-3.4:", lower: -3.4, upper: math.MaxFloat64, outside: true}, nil},
{"3.4:", &Threshold{input: "3.4:", lower: 3.4, upper: math.MaxFloat64, outside: true}, nil},
{"-3:", &Threshold{input: "-3:", lower: -3, upper: math.MaxFloat64, outside: true}, nil},
{"3:", &Threshold{input: "3:", lower: 3, upper: math.MaxFloat64, outside: true}, nil},
{"3,1:", nil, errors.New("")},
{"~:-3.4 ", &Threshold{input: "~:-3.4", lower: minFloat64, upper: -3.4, outside: true}, nil},
{"~:3.4", &Threshold{input: "~:3.4", lower: minFloat64, upper: 3.4, outside: true}, nil},
{"~:-3", &Threshold{input: "~:-3", lower: minFloat64, upper: -3, outside: true}, nil},
{" ~:3", &Threshold{input: "~:3", lower: minFloat64, upper: 3, outside: true}, nil},
{"~:3,1", nil, errors.New("")},
{"-1.2:-3.4", nil, ErrFirstBiggerThenSecond},
{"3:2", nil, ErrFirstBiggerThenSecond},
{"1.2:3.4", &Threshold{input: "1.2:3.4", lower: 1.2, upper: 3.4, outside: true}, nil},
{"-1.2:3.4", &Threshold{input: "-1.2:3.4", lower: -1.2, upper: 3.4, outside: true}, nil},
{"-3.4:-1.2", &Threshold{input: "-3.4:-1.2", lower: -3.4, upper: -1.2, outside: true}, nil},
{"1.2:3", &Threshold{input: "1.2:3", lower: 1.2, upper: 3, outside: true}, nil},
{"1:3", &Threshold{input: "1:3", lower: 1, upper: 3, outside: true}, nil},
{"1:3.4", &Threshold{input: "1:3.4", lower: 1, upper: 3.4, outside: true}, nil},
{"1,2:3,4", nil, errors.New("")},
{"@-1.2:-3.4", nil, ErrFirstBiggerThenSecond},
{" @3:2", nil, ErrFirstBiggerThenSecond},
{"@1.2:3.4", &Threshold{input: "@1.2:3.4", lower: 1.2, upper: 3.4, outside: false}, nil},
{"@-1.2:3.4 ", &Threshold{input: "@-1.2:3.4", lower: -1.2, upper: 3.4, outside: false}, nil},
{"@-3.4:-1.2", &Threshold{input: "@-3.4:-1.2", lower: -3.4, upper: -1.2, outside: false}, nil},
{"@1.2:3", &Threshold{input: "@1.2:3", lower: 1.2, upper: 3, outside: false}, nil},
{"@1:3", &Threshold{input: "@1:3", lower: 1, upper: 3, outside: false}, nil},
{"@1:3.4", &Threshold{input: "@1:3.4", lower: 1, upper: 3.4, outside: false}, nil},
{"@1,2:3,4", nil, errors.New("")},
}
func TestNewThreshold(t *testing.T) {
for i, data := range stringToThreshold {
tGot, err := NewThreshold(data.input)
if (err == nil) != (data.err == nil) {
t.Error(i, err, data.err)
}
if (tGot != nil && data.threshold != nil) && *tGot != *data.threshold {
t.Errorf("%d - Got: %s expected: %s", i, *tGot, *data.threshold)
}
}
}
var thresholdBorders = []struct {
threshold string
value float64
expected bool
}{
{"10", -1, false},
{"10", 0, true},
{"10", 1, true},
{"10", 10, true},
{"10", 11, false},
{"10:", -1, false},
{"10:", 9, false},
{"10:", 10, true},
{"10:", 11, true},
{"~:10", 11, false},
{"~:10", 10, true},
{"~:10", 9, true},
{"~:10", -1, true},
{"10:20", -1, false},
{"10:20", 9, false},
{"10:20", 10, true},
{"10:20", 11, true},
{"10:20", 19, true},
{"10:20", 20, true},
{"10:20", 21, false},
{"@10:20", -1, true},
{"@10:20", 9, true},
{"@10:20", 10, false},
{"@10:20", 11, false},
{"@10:20", 19, false},
{"@10:20", 20, false},
{"@10:20", 21, true},
}
func TestThreshold_IsValueOK(t *testing.T) {
for i, data := range thresholdBorders {
th, err := NewThreshold(data.threshold)
if err != nil {
t.Error(i, "There should be no error ", err)
}
result := th.IsValueOK(data.value)
if result != data.expected {
t.Errorf("%d - Expected: %t, got: %t", i, result, data.expected)
}
}
}