-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaverage_test.go
52 lines (48 loc) · 1.08 KB
/
average_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
40
41
42
43
44
45
46
47
48
49
50
51
52
package main
import (
"testing"
"time"
)
func newAverageTestData(t *testing.T) (ts *TimeSeries, today time.Time) {
ts = NewTimeSeries()
if err := ts.Read("testdata/average.csv"); err != nil {
t.Fatal(err)
}
today = time.Date(2018, 1, 15, 10, 0, 0, 0, time.UTC)
return
}
func TestAverage(t *testing.T) {
ts, today := newAverageTestData(t)
for lookback, expect := range map[int]float64{
1: 15,
2: 14.5,
3: 14,
4: 13.5,
5: 13,
} {
avg := average(ts, &today, lookback)
if avg != expect {
t.Errorf("lookback %d: incorrect avg %f (expected %f)",
lookback, avg, expect)
}
}
}
func TestAverages(t *testing.T) {
ts, today := newAverageTestData(t)
for days, expect := range map[[3]int]([]float64){
[3]int{1, 3, 5}: []float64{15, 14, 13},
} {
avgs := averages(ts, &today, days[:3]...)
if len(avgs) != len(expect) {
t.Errorf("incorrect length: %d (expected %d)",
len(avgs), len(expect))
continue
}
for index, value := range avgs {
if value != expect[index] {
t.Errorf("days %v: incorrect avgs %v (expected %v)",
days, avgs, expect)
}
}
}
}