Fixed error in cumprod code. Added test for cumprod

This commit is contained in:
btracey
2013-05-17 00:13:35 -07:00
parent b2c4f68fd6
commit 2f29c868b1
2 changed files with 14 additions and 1 deletions

View File

@@ -115,7 +115,7 @@ func CumProd(receiver, s []float64) []float64 {
}
receiver[0] = s[0]
for i := 1; i < len(s); i++ {
receiver[i] = receiver[i-1] + s[i]
receiver[i] = receiver[i-1] * s[i]
}
return receiver
}

View File

@@ -115,3 +115,16 @@ func TestCumSum(t *testing.T) {
t.Errorf("Wrong cumsum returned with non-nil receiver. Expected %v, returned %v", truth, val)
}
}
func TestCumProd(t *testing.T) {
s := []float64{3, 4, 1, 7, 5}
val := CumProd(nil, s)
truth := []float64{3, 12, 12, 84, 420}
if !Eq(val, truth, 1E-15) {
t.Errorf("Wrong cumprod returned with nil receiver. Expected %v, returned %v", truth, val)
}
val = CumProd(val, s)
if !Eq(val, truth, 1E-15) {
t.Errorf("Wrong cumprod returned with non-nil receiver. Expected %v, returned %v", truth, val)
}
}