interp: Fix error in calculateSlopes for two (X, Y) points.
MODIFY
interp/interp.go
interp/interp.go
@@ -167,7 +167,7 @@ func findSegment(xs []float64, x float64) int {
// or len(xs) != len(ys).
func calculateSlopes(xs, ys []float64) []float64 {
n := len(xs)
- if n <= 2 {
+ if n < 2 {
panic(tooFewPoints)
}
if len(ys) != n {

MODIFY
interp/interp_test.go
interp/interp_test.go
@@ -219,10 +219,24 @@ func TestCalculateSlopesErrors(t *testing.T) {
func TestCalculateSlopes(t *testing.T) {
t.Parallel()
- got := calculateSlopes([]float64{0, 2, 3, 5}, []float64{0, 1, 1, -1})
- want := []float64{0.5, 0, -1}
- if !floats.EqualApprox(got, want, 1e-14) {
- t.Errorf("Mismatch in calculated slops: got %v, want %v", got, want)
+ for i, test := range []struct {
+ xs, ys, want []float64
+ }{
+ {
+ xs: []float64{0, 2, 3, 5},
+ ys: []float64{0, 1, 1, -1},
+ want: []float64{0.5, 0, -1},
+ },
+ {
+ xs: []float64{10, 20},
+ ys: []float64{50, 100},
+ want: []float64{5},
+ },
+ } {
+ got := calculateSlopes(test.xs, test.ys)
+ if !floats.EqualApprox(got, test.want, 1e-14) {
+ t.Errorf("Mismatch in calculated slopes in case %d: got %v, want %v", i, got, test.want)
+ }
}
}

interp: Fix error in calculateSlopes for two (X, Y) points. (#1586)