feat(pkg): add Range.Contains() method

This commit is contained in:
Emmanuel BENOîT 2024-07-20 18:42:18 +02:00
parent 6d44df6216
commit 7209591e08
Signed by: Emmanuel BENOîT
SSH key fingerprint: SHA256:l7PFUUF5TCDsvYeQC9OnTNz08dFY7Fvf4Hv3neIqYpg
2 changed files with 74 additions and 0 deletions

View file

@ -2,6 +2,7 @@ package perfdata // import nocternity.net/gomonop/pkg/perfdata
import (
"fmt"
"strconv"
"strings"
)
@ -66,6 +67,29 @@ func (r *Range) String() string {
return inside + start + ":" + r.end
}
// Contains checks whether a numeric value is within the range.
func (r *Range) Contains(value float64) bool {
var inStart, inEnd bool
if r.start != "" {
startValue, err := strconv.ParseFloat(r.start, 64)
if err != nil {
panic(fmt.Sprintf("invalid performance data range start value: %v", err))
}
inStart = value < startValue
}
if r.end != "" {
endValue, err := strconv.ParseFloat(r.end, 64)
if err != nil {
panic(fmt.Sprintf("invalid performance data range end value: %v", err))
}
inEnd = value > endValue
}
return (inStart || inEnd) != r.inside
}
// A state of the range parser.
type rangeParserState int

View file

@ -69,6 +69,56 @@ func TestRangeString(t *testing.T) {
}
}
func TestRangeContains(t *testing.T) {
type Test struct {
pdr Range
value float64
result bool
}
tests := []Test{
{pdr: Range{start: "0", end: "10"}, value: 0, result: false},
{pdr: Range{start: "0", end: "10"}, value: 10, result: false},
{pdr: Range{start: "0", end: "10"}, value: -1, result: true},
{pdr: Range{start: "0", end: "10"}, value: 11, result: true},
{pdr: Range{start: "", end: "10"}, value: -1000, result: false},
{pdr: Range{start: "", end: "10"}, value: 10, result: false},
{pdr: Range{start: "", end: "10"}, value: 11, result: true},
{pdr: Range{start: "10", end: ""}, value: -1000, result: true},
{pdr: Range{start: "10", end: ""}, value: 9, result: true},
{pdr: Range{start: "10", end: ""}, value: 10, result: false},
{pdr: Range{start: "10", end: "20"}, value: 9, result: true},
{pdr: Range{start: "10", end: "20"}, value: 10, result: false},
{pdr: Range{start: "10", end: "20"}, value: 20, result: false},
{pdr: Range{start: "10", end: "20"}, value: 21, result: true},
}
// Test cases with the inside flag set and the opposite result
n := len(tests)
for i := range n {
tests = append(tests, Test{
pdr: Range{
start: tests[i].pdr.start,
end: tests[i].pdr.end,
inside: !tests[i].pdr.inside,
},
value: tests[i].value,
result: !tests[i].result,
})
}
for _, test := range tests {
t.Run(fmt.Sprintf("%v", test), func(t *testing.T) {
result := test.pdr.Contains(test.value)
assert.Equal(
t, test.result, result,
"Expected '%v', got '%v' for value '%f' and range '%s'",
test.result, result, test.value, test.pdr.String(),
)
})
}
}
func TestRangeParserOk(t *testing.T) {
type Test struct {
in string