-
Notifications
You must be signed in to change notification settings - Fork 6
/
sma_test.go
39 lines (29 loc) · 915 Bytes
/
sma_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package movavg_test
import (
. "github.com/mxmCherry/movavg"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("SMA", func() {
var _ MA = (*SMA)(nil)
It("should calculate Simple Moving Average", func() {
subject := NewSMA(3)
// zero until values added:
Expect(subject.Avg()).To(BeZero())
// load initial value
Expect(subject.Add(1)).To(Equal(float64(1)))
Expect(subject.Avg()).To(Equal(float64(1)))
// (1 + 2) / 2
Expect(subject.Add(2)).To(Equal(float64(1.5)))
Expect(subject.Avg()).To(Equal(float64(1.5)))
// (1 + 2 + 3) / 3
Expect(subject.Add(3)).To(Equal(float64(2)))
Expect(subject.Avg()).To(Equal(float64(2)))
// (2 + 3 + 4) / 3
Expect(subject.Add(4)).To(Equal(float64(3)))
Expect(subject.Avg()).To(Equal(float64(3)))
// (3 + 4 + 5) / 3
Expect(subject.Add(5)).To(Equal(float64(4)))
Expect(subject.Avg()).To(Equal(float64(4)))
})
})