-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhlc_test.go
99 lines (79 loc) · 2.29 KB
/
hlc_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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package hlc_test
import (
gohlc "github.com/tadasv/gohlc"
"testing"
"time"
)
func syntheticNow(times []time.Time) gohlc.TimeSupplier {
ch := make(chan time.Time, len(times))
for _, v := range times {
ch <- v
}
return func() time.Time {
tt := <-ch
return tt
}
}
func checkTime(t *testing.T, res gohlc.HLCTime, expected gohlc.HLCTime) {
if res != expected {
t.Fatalf("Expected %v got %v", expected, res)
}
}
func TestHLCClock(t *testing.T) {
times := []time.Time{
time.Unix(1, 0),
time.Unix(1, 0),
time.Unix(0, 9),
time.Unix(2, 0),
time.Unix(3, 0),
time.Unix(3, 0),
time.Unix(3, 0),
time.Unix(3, 0),
time.Unix(3, 5),
time.Unix(5, 0),
time.Unix(4, 9),
time.Unix(0, 0),
}
expected := []gohlc.HLCTime{
gohlc.NewHLCTime(time.Unix(1, 0), 0),
gohlc.NewHLCTime(time.Unix(1, 0), 1),
gohlc.NewHLCTime(time.Unix(1, 0), 2),
gohlc.NewHLCTime(time.Unix(2, 0), 0),
gohlc.NewHLCTime(time.Unix(3, 0), 0),
gohlc.NewHLCTime(time.Unix(3, 0), 1),
gohlc.NewHLCTime(time.Unix(3, 0), 2),
gohlc.NewHLCTime(time.Unix(3, 0), 100),
gohlc.NewHLCTime(time.Unix(4, 4), 101),
gohlc.NewHLCTime(time.Unix(5, 0), 0),
gohlc.NewHLCTime(time.Unix(5, 0), 100),
gohlc.NewHLCTime(time.Unix(5, 0), 101),
}
// Wall clock does not move
clock := gohlc.NewHLCClock(syntheticNow(times))
tt := clock.GetTime()
checkTime(t, tt, expected[0])
tt = clock.GetTime()
checkTime(t, tt, expected[1])
tt = clock.GetTime()
checkTime(t, tt, expected[2])
// Clocked moved back
tt = clock.GetTime()
checkTime(t, tt, expected[3])
tt = clock.UpdateTime(gohlc.NewHLCTime(time.Unix(1, 2), 3))
checkTime(t, tt, expected[4])
tt = clock.UpdateTime(gohlc.NewHLCTime(time.Unix(1, 2), 3))
checkTime(t, tt, expected[5])
tt = clock.UpdateTime(gohlc.NewHLCTime(time.Unix(3, 0), 1))
checkTime(t, tt, expected[6])
tt = clock.UpdateTime(gohlc.NewHLCTime(time.Unix(3, 0), 99))
checkTime(t, tt, expected[7])
// Event with greated wall time
tt = clock.UpdateTime(gohlc.NewHLCTime(time.Unix(4, 4), 100))
checkTime(t, tt, expected[8])
tt = clock.UpdateTime(gohlc.NewHLCTime(time.Unix(4, 5), 0))
checkTime(t, tt, expected[9])
tt = clock.UpdateTime(gohlc.NewHLCTime(time.Unix(5, 0), 99))
checkTime(t, tt, expected[10])
tt = clock.UpdateTime(gohlc.NewHLCTime(time.Unix(5, 0), 50))
checkTime(t, tt, expected[11])
}