From 163b3e19613c91b3a21304c36ba1ac56cc5c14fb Mon Sep 17 00:00:00 2001 From: AndrewDanilin Date: Tue, 12 Sep 2023 21:16:06 +0300 Subject: [PATCH] [#88] netmap: fix min aggregator bug, add tests Signed-off-by: Andrew Danilin --- netmap/aggregator.go | 12 +++++++---- netmap/aggregator_test.go | 44 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 netmap/aggregator_test.go diff --git a/netmap/aggregator.go b/netmap/aggregator.go index c018e6b1..3365fe31 100644 --- a/netmap/aggregator.go +++ b/netmap/aggregator.go @@ -23,7 +23,7 @@ type ( } minAgg struct { - min float64 + min *float64 } meanIQRAgg struct { @@ -102,13 +102,17 @@ func (a *meanAgg) Compute() float64 { } func (a *minAgg) Add(n float64) { - if a.min == 0 || n < a.min { - a.min = n + if a.min == nil || n < *a.min { + a.min = &n } } func (a *minAgg) Compute() float64 { - return a.min + if a.min == nil { + return 0 + } + + return *a.min } func (a *meanIQRAgg) Add(n float64) { diff --git a/netmap/aggregator_test.go b/netmap/aggregator_test.go new file mode 100644 index 00000000..d844803c --- /dev/null +++ b/netmap/aggregator_test.go @@ -0,0 +1,44 @@ +package netmap + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestMinAgg(t *testing.T) { + tests := []struct { + vals []float64 + res float64 + }{ + { + vals: []float64{1, 2, 3, 0, 10}, + res: 0, + }, + { + vals: []float64{10, 0, 10, 0}, + res: 0, + }, + { + vals: []float64{0, 1, 2, 3, 10}, + res: 0, + }, + { + vals: []float64{0, 0, 0, 0}, + res: 0, + }, + { + vals: []float64{10, 10, 10, 10}, + res: 10, + }, + } + + for _, test := range tests { + a := newMinAgg() + for _, val := range test.vals { + a.Add(val) + } + + require.Equal(t, test.res, a.Compute()) + } +}