diff --git a/nodestorage/stat.go b/nodestorage/stat.go index ea4b165..24b1d36 100644 --- a/nodestorage/stat.go +++ b/nodestorage/stat.go @@ -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 diff --git a/nodestorage/stat_test.go b/nodestorage/stat_test.go new file mode 100644 index 0000000..84e05e9 --- /dev/null +++ b/nodestorage/stat_test.go @@ -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})) + }) +}