Skip to content

Commit

Permalink
netmap: Fix min aggregator
Browse files Browse the repository at this point in the history
Do not return next element if `0` is being facing.
Closes #438

Signed-off-by: Pavel Karpy <[email protected]>
  • Loading branch information
carpawell committed Jul 31, 2023
1 parent fd512ad commit f03943b
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
12 changes: 8 additions & 4 deletions netmap/aggregator.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type (
}

minAgg struct {
min float64
min *float64
}

meanIQRAgg struct {
Expand Down Expand Up @@ -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) {
Expand Down
33 changes: 33 additions & 0 deletions netmap/aggregator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,36 @@ func TestMeanAgg(t *testing.T) {
require.Equal(t, test.res, a.Compute())
}
}

func TestMinAgg(t *testing.T) {
tt := []struct {
vals []float64
res float64
}{
{
vals: []float64{1, 2, 3, 4, 0, 100},
res: 0,
},
{
vals: []float64{0, 1, 3, 4, 5},
res: 0,
},
{
vals: []float64{0, 0, 0, 0},
res: 0,
},
{
vals: []float64{1, 1, 1, 1},
res: 1,
},
}

for _, test := range tt {
a := newMinAgg()
for _, val := range test.vals {
a.Add(val)
}

require.Equal(t, test.res, a.Compute())
}
}
8 changes: 4 additions & 4 deletions netmap/json_tests/hrw_sort.json
Original file line number Diff line number Diff line change
Expand Up @@ -146,19 +146,19 @@
"select 3 nodes in 3 distinct countries, same placement": {
"policy": {"replicas":[{"count":1,"selector":"Main"}],"containerBackupFactor":1,"selectors":[{"name":"Main","count":3,"clause":"DISTINCT","attribute":"Country","filter":"*"}],"filters":[],"subnetId":null},
"pivot": "Y29udGFpbmVySUQ=",
"result": [[4, 0, 7]],
"result": [[0, 2, 3]],
"placement": {
"pivot": "b2JqZWN0SUQ=",
"result": [[4, 0, 7]]
"result": [[0, 2, 3]]
}
},
"select 6 nodes in 3 distinct countries, different placement": {
"policy": {"replicas":[{"count":1,"selector":"Main"}],"containerBackupFactor":2,"selectors":[{"name":"Main","count":3,"clause":"DISTINCT","attribute":"Country","filter":"*"}],"filters":[],"subnetId":null},
"pivot": "Y29udGFpbmVySUQ=",
"result": [[4, 3, 0, 1, 7, 2]],
"result": [[0, 1, 2, 6, 3, 4]],
"placement": {
"pivot": "b2JqZWN0SUQ=",
"result": [[4, 3, 0, 7, 2, 1]]
"result": [[0, 1, 2, 6, 3, 4]]
}
}
}
Expand Down

0 comments on commit f03943b

Please sign in to comment.