diff --git a/ma.go b/ma.go index d3c4de6..55f4dd0 100644 --- a/ma.go +++ b/ma.go @@ -82,6 +82,11 @@ func (ma *MovingAverage) Add(values ...float64) { } } +func (ma *MovingAverage) Reset() { + ma.valPos = 0 + ma.slotsFilled = false +} + func (ma *MovingAverage) SlotsFilled() bool { return ma.slotsFilled } diff --git a/ma_concurrent.go b/ma_concurrent.go index f463250..3b5c708 100644 --- a/ma_concurrent.go +++ b/ma_concurrent.go @@ -19,6 +19,12 @@ func (c *ConcurrentMovingAverage) Add(values ...float64) { c.mux.Unlock() } +func (c *ConcurrentMovingAverage) Reset() { + c.mux.Lock() + c.ma.Reset() + c.mux.Unlock() +} + func (c *ConcurrentMovingAverage) Avg() float64 { c.mux.RLock() defer c.mux.RUnlock() diff --git a/ma_test.go b/ma_test.go index 644dbb9..ef2af10 100644 --- a/ma_test.go +++ b/ma_test.go @@ -171,3 +171,41 @@ func TestConcurrent(t *testing.T) { } wg.Wait() } + +func TestReset(t *testing.T) { + a := New(5) + a.Add(10, 20, 30, 40, 50) + if a.Count() != 5 { + t.Error("expected 5 values before reset, got", a.Count()) + } + if a.Avg() < 29.999 || a.Avg() > 30.001 { + t.Error("unexpected average before reset, got", a.Avg()) + } + + a.Reset() + + if a.Count() != 0 { + t.Error("expected 0 values after reset, got", a.Count()) + } + if a.Avg() != 0 { + t.Error("expected average 0 after reset, got", a.Avg()) + } + if len(a.Values()) != 0 { + t.Error("expected no values after reset, got", len(a.Values())) + } + if a.SlotsFilled() { + t.Error("expected slots not filled after reset") + } + + a.Add(10, 20) + + if a.Count() != 2 { + t.Error("expected 2 values after adding 2 values after reset, got", a.Count()) + } + if a.Avg() < 14.999 || a.Avg() > 15.001 { + t.Error("unexpected average after adding 2 values after reset, got", a.Avg()) + } + if a.SlotsFilled() { + t.Error("expected slots not filled after adding 2 values after reset") + } +}