stats_test.go 940 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. package stats
  2. import (
  3. "errors"
  4. "math"
  5. "testing"
  6. )
  7. func TestStats_Sanitize(t *testing.T) {
  8. type testCase struct {
  9. stats Stats
  10. exp error
  11. }
  12. testCases := []testCase{
  13. {
  14. nil,
  15. nil,
  16. },
  17. {
  18. Stats{},
  19. nil,
  20. },
  21. {
  22. Stats{
  23. Val: 1.0,
  24. },
  25. nil,
  26. },
  27. {
  28. Stats{
  29. Avg: 0.1,
  30. Max: 1.0,
  31. },
  32. nil,
  33. },
  34. {
  35. Stats{
  36. Avg: math.Inf(0),
  37. Max: 1.0,
  38. },
  39. errors.New("1 errors: [avg is Inf]"),
  40. },
  41. {
  42. Stats{
  43. Avg: math.Inf(0),
  44. Max: math.NaN(),
  45. },
  46. errors.New("2 errors: [avg is Inf] [max is NaN]"),
  47. },
  48. }
  49. for _, tc := range testCases {
  50. err := tc.stats.Sanitize()
  51. if err != nil && tc.exp == nil {
  52. t.Errorf("unexpected error: %s", err)
  53. }
  54. if err == nil && tc.exp != nil {
  55. t.Errorf("expected error: %s", tc.exp)
  56. }
  57. if err != nil && tc.exp != nil && err.Error()[0] != tc.exp.Error()[0] {
  58. t.Errorf("expected error: %s; received error: %s", tc.exp, err)
  59. }
  60. }
  61. }