Skip to content

Commit

Permalink
tests: check for values within range instead of equality; #14 (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
Preetam authored Jul 27, 2017
1 parent 4cc8cc5 commit b24eb34
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions ewma_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@ package ewma
// Copyright (c) 2013 VividCortex, Inc. All rights reserved.
// Please see the LICENSE file for applicable license terms.

import "testing"
import (
"math"
"testing"
)

const testMargin = 0.00000001

var samples = [100]float64{
4599, 5711, 4746, 4621, 5037, 4218, 4925, 4281, 5207, 5203, 5594, 5149,
Expand All @@ -17,12 +22,16 @@ var samples = [100]float64{
3197, 5139, 6101, 5279,
}

func withinMargin(a, b float64) bool {
return math.Abs(a-b) <= testMargin
}

func TestSimpleEWMA(t *testing.T) {
var e SimpleEWMA
for _, f := range samples {
e.Add(f)
}
if e.Value() != 4734.500946466118 {
if !withinMargin(e.Value(), 4734.500946466118) {
t.Errorf("e.Value() is %v, wanted %v", e.Value(), 4734.500946466118)
}
e.Set(1.0)
Expand All @@ -36,7 +45,7 @@ func TestVariableEWMA(t *testing.T) {
for _, f := range samples {
e.Add(f)
}
if e.Value() != 4734.500946466118 {
if !withinMargin(e.Value(), 4734.500946466118) {
t.Errorf("e.Value() is %v, wanted %v", e.Value(), 4734.500946466118)
}
e.Set(1.0)
Expand All @@ -50,7 +59,7 @@ func TestVariableEWMA2(t *testing.T) {
for _, f := range samples {
e.Add(f)
}
if e.Value() != 5015.397367486725 {
if !withinMargin(e.Value(), 5015.397367486725) {
t.Errorf("e.Value() is %v, wanted %v", e.Value(), 5015.397367486725)
}
}
Expand Down

0 comments on commit b24eb34

Please sign in to comment.