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..20ff4d87 --- /dev/null +++ b/netmap/aggregator_test.go @@ -0,0 +1,69 @@ +package netmap + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func TestMeanAgg(t *testing.T) { + tt := []struct { + vals []float64 + res float64 + }{ + { + vals: []float64{0, 1, 3, 4, 5}, + res: 2.6, + }, + { + vals: []float64{0, 0, 0, 0}, + res: 0, + }, + { + vals: []float64{1, 1, 1, 1}, + res: 1, + }, + } + + for _, test := range tt { + a := newMeanAgg() + for _, val := range test.vals { + a.Add(val) + } + + 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()) + } +} diff --git a/netmap/json_tests/hrw_sort.json b/netmap/json_tests/hrw_sort.json index 521a852e..31283566 100644 --- a/netmap/json_tests/hrw_sort.json +++ b/netmap/json_tests/hrw_sort.json @@ -2,6 +2,7 @@ "name": "HRW ordering", "nodes": [ { + "public_key": "AsGTU2KO25PRUWal5HzInv3LLqBedankF/scMr7VpVR/", "attributes": [ { "key": "Country", @@ -18,6 +19,7 @@ ] }, { + "public_key": "AltaQMNJ8k3awpg9q8Z+CFMXHt+io4ChddgBuRw8QSN+", "attributes": [ { "key": "Country", @@ -34,6 +36,7 @@ ] }, { + "public_key": "AoV8bh005WYoMAGpucDOZ1O704ySFlypTWP+pJP1trBo", "attributes": [ { "key": "Country", @@ -50,6 +53,7 @@ ] }, { + "public_key": "ApQN0UPXMeBcCiiw+UA3XRe5EESOFbu545lEaThaZ+4L", "attributes": [ { "key": "Country", @@ -66,6 +70,7 @@ ] }, { + "public_key": "AzG1GtS58K92j6dDrGXTzu3EKJ+9GFLZLMK6lpH+mIzE", "attributes": [ { "key": "Country", @@ -82,6 +87,7 @@ ] }, { + "public_key": "AzCRU8cw/WQRPZV/f5PjxXXFSZ7dakRvzlwwEVFQdaz4", "attributes": [ { "key": "Country", @@ -94,6 +100,7 @@ ] }, { + "public_key": "As8NhDYv70g2ivJ95guRdxtDuqzONOv0nlU36xuXI2Mx", "attributes": [ { "key": "Country", @@ -110,6 +117,7 @@ ] }, { + "public_key": "Ax9daczuWTOYdrsmGrqWRkDonY86GD1OE1WUrnsOjE9e", "attributes": [ { "key": "Country", @@ -126,6 +134,7 @@ ] }, { + "public_key": "AorNy+ku9CwiGdQO/TjcGHXYmvGks6ZIS9JqVO1+KXze", "attributes": [ { "key": "Country", @@ -146,19 +155,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": [[1, 7, 5]], "placement": { "pivot": "b2JqZWN0SUQ=", - "result": [[4, 0, 7]] + "result": [[1, 7, 5]] } }, "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": [[1, 6, 0, 7, 5, 3]], "placement": { "pivot": "b2JqZWN0SUQ=", - "result": [[4, 3, 0, 7, 2, 1]] + "result": [[1, 6, 0, 7, 5, 3]] } } }