From 571f6ffe3cce7ecd97c7dfe649a2ce4b25beca18 Mon Sep 17 00:00:00 2001 From: Dan Kortschak Date: Fri, 21 May 2021 17:50:37 +0930 Subject: [PATCH] stat: explicitly panic when calling CDF with empty x --- stat/stat.go | 4 ++++ stat/stat_test.go | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/stat/stat.go b/stat/stat.go index 994dfc30..f17d317a 100644 --- a/stat/stat.go +++ b/stat/stat.go @@ -56,6 +56,7 @@ func Bhattacharyya(p, q []float64) float64 { // // The x data must be sorted in increasing order. If weights is nil then all // of the weights are 1. If weights is not nil, then len(x) must equal len(weights). +// CDF will panic if the length of x is zero. // // CumulantKind behaviors: // - Empirical: Returns the lowest fraction for which q is greater than or equal @@ -67,6 +68,9 @@ func CDF(q float64, c CumulantKind, x, weights []float64) float64 { if floats.HasNaN(x) { return math.NaN() } + if len(x) == 0 { + panic("stat: zero length slice") + } if !sort.Float64sAreSorted(x) { panic("x data are not sorted") } diff --git a/stat/stat_test.go b/stat/stat_test.go index 275d8492..9a29e3b8 100644 --- a/stat/stat_test.go +++ b/stat/stat_test.go @@ -1396,6 +1396,16 @@ func TestCDF(t *testing.T) { x []float64 weights []float64 }{ + { + name: "x == nil", + kind: Empirical, + x: nil, + }, + { + name: "len(x) == 0", + kind: Empirical, + x: []float64{}, + }, { name: "len(x) != len(weights)", q: 1.5,