a go package that implements the efficient, accurate, and stable calculation of online statistical quantities.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

46 lines
928B

  1. package ostat
  2. import (
  3. "math"
  4. "testing"
  5. )
  6. func TestInsert(t *testing.T) {
  7. os := NewOnlineStat()
  8. v := []float64{4, 7, 13, 16}
  9. for _, i := range v {
  10. os.Push(i)
  11. }
  12. if os.Min != 4 {
  13. t.Errorf("incorrectly calculated min")
  14. }
  15. if os.Max != 16 {
  16. t.Errorf("incorrectly calculated max")
  17. }
  18. if m, _ := os.Mean(); m != 10.0 {
  19. t.Errorf("incorrect mean")
  20. }
  21. if variance, _ := os.Variance(); variance != 30.0 {
  22. t.Errorf("incorrect variance: %f", variance)
  23. }
  24. if stdev, _ := os.StdDev(); stdev != math.Sqrt(30.0) {
  25. t.Errorf("incorrect stdev: %f", stdev)
  26. }
  27. }
  28. func TestEmpty(t *testing.T) {
  29. os := NewOnlineStat()
  30. _, err := os.Mean()
  31. if err == nil {
  32. t.Errorf("failure to notify the running stat was empty")
  33. }
  34. _, err = os.Variance()
  35. if err == nil {
  36. t.Errorf("failure to notify the running stat was empty")
  37. }
  38. _, err = os.StdDev()
  39. if err == nil {
  40. t.Errorf("failure to notify the running stat was empty")
  41. }
  42. }