Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions nodestorage/stat.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,9 @@ func calculateStatsPerObject(stats *ObjectSpaceStats) {
}

func calcMedian(sortedLengths []int) (median float64) {
if len(sortedLengths) == 0 {
return 0
}
mid := len(sortedLengths) / 2
if len(sortedLengths)%2 == 0 {
median = float64(sortedLengths[mid-1]+sortedLengths[mid]) / 2.0
Expand Down
20 changes: 20 additions & 0 deletions nodestorage/stat_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package nodestorage

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestCalcMedian(t *testing.T) {
t.Run("empty slice returns zero without panic", func(t *testing.T) {
assert.Equal(t, 0.0, calcMedian(nil))
assert.Equal(t, 0.0, calcMedian([]int{}))
})
t.Run("odd length", func(t *testing.T) {
assert.Equal(t, 3.0, calcMedian([]int{1, 2, 3, 4, 5}))
})
t.Run("even length", func(t *testing.T) {
assert.Equal(t, 2.5, calcMedian([]int{1, 2, 3, 4}))
})
}
Loading