-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathbreakpoint_test.go
232 lines (217 loc) · 4.21 KB
/
breakpoint_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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package breakpoint
import (
"strings"
"testing"
)
var (
valueAtTests = []struct {
breaks []Breakpoint
time float64
out float64
}{
{
[]Breakpoint{},
0,
0,
},
{
[]Breakpoint{},
0,
0,
},
{
[]Breakpoint{
{
Time: 0,
Value: 1,
},
},
0,
1,
},
{
[]Breakpoint{
{
Time: 0,
Value: 1,
},
},
2,
1,
},
{
[]Breakpoint{
{
Time: 0,
Value: 2,
},
{
Time: 1,
Value: 0,
},
},
2,
0,
},
{
[]Breakpoint{
{
Time: 1,
Value: 0,
},
{
Time: 2,
Value: 10,
},
{
Time: 3,
Value: 100,
},
},
1.5, // linear interpolation should give this result
5,
},
}
minMaxTests = []struct {
in []Breakpoint
min float64
max float64
}{
{
in: []Breakpoint{},
min: 0.0,
max: 0.0,
},
{
in: []Breakpoint{Breakpoint{0, 10}},
min: 10,
max: 10,
},
{
in: []Breakpoint{Breakpoint{0, 10}, Breakpoint{1, 5}, Breakpoint{2, 3}},
min: 3,
max: 10,
},
}
anyTests = []struct {
in Breakpoints
anyf func(Breakpoint) bool
out bool
}{
{
in: Breakpoints{Breakpoint{1, 0}, Breakpoint{2, 5}, Breakpoint{3, 10}},
anyf: func(b Breakpoint) bool { return b.Value > 5 },
out: true,
},
{
in: Breakpoints{Breakpoint{1, 0}, Breakpoint{2, 5}, Breakpoint{3, 10}},
anyf: func(b Breakpoint) bool { return b.Value > 15 },
out: false,
},
}
)
func TestBreakpoint(t *testing.T) {
// TODO: these breakpoints are invalid as long as we can't timetravel. Time should be strictly
// increasing..
input := `
3.0:1.31415
-1:1.32134
0:0
`
brk, err := ParseBreakpoints(strings.NewReader(input))
if err != nil {
t.Fatalf("Should be able to parse breakpoints: %v", err)
}
if brk[0].Time != 3.0 {
t.Fatalf("Breakpoint does not match input, expected %v, got %v", 3.0, brk[0].Time)
}
if brk[1].Time != -1 {
t.Fatal("Breakpoint does not match input")
}
if brk[2].Time != 0 {
t.Fatal("Breakpoint does not match input")
}
if brk[0].Value != 1.31415 {
t.Fatal("Breakpoint does not match input")
}
if brk[1].Value != 1.32134 {
t.Fatal("Breakpoint does not match input")
}
if brk[2].Value != 0 {
t.Fatal("Breakpoint does not match input")
}
}
// TestbreakpointStream tests various assumptions of how ticks should be handled
// We test at 10 samples per second for convenience (easier to verify correctness)
// Thus each -tick- should update our position by 0.1
func TestBreakpointStream(t *testing.T) {
input := `
0:100
10:50
20:100
`
samplerate := 10 // 10 sample per second
brks, err := ParseBreakpoints(strings.NewReader(input))
if err != nil {
t.Fatalf("Should be able to parse breakpoints: %v", err)
}
stream, err := NewBreakpointStream(brks, samplerate)
if err != nil {
t.Fatalf("Should be able to create breakpoint stream: %v", err)
}
if stream.Increment != 1.0/float64(samplerate) {
t.Fatal("Incorrect increment")
}
value := stream.Tick()
if stream.CurrentPosition != 0.1 {
t.Fatal("Incorrectly incremented stream")
}
// move to 5 seconds (50 ticks)
for i := 0; i < 50; i++ {
value = stream.Tick()
}
if value != 75 {
t.Fatalf("Expected 75 but got %v", value)
}
// move to second 15
for i := 0; i < 100; i++ {
value = stream.Tick()
}
t.Log("Should be able to get values beyond the end of the stream")
for i := 0; i < 10e5; i++ {
value = stream.Tick()
}
if value != 100 {
t.Fatalf("Value should be 100, but got %v", value)
}
}
func TestValueAt(t *testing.T) {
for _, test := range valueAtTests {
t.Run("", func(t *testing.T) {
_, res := ValueAt(test.breaks, test.time, 0)
if res != test.out {
t.Fatalf("expected %v, got %v", test.out, res)
}
})
}
}
func TestAny(t *testing.T) {
for _, test := range anyTests {
t.Run("", func(t *testing.T) {
out := test.in.Any(test.anyf)
if out != test.out {
t.Fatalf("Expected %v but got %v", test.out, out)
}
})
}
}
func TestMinMax(t *testing.T) {
for _, test := range minMaxTests {
t.Run("", func(t *testing.T) {
min, max := MinMaxValue(test.in)
if min != test.min || max != test.max {
t.Fatalf("Expected (min,max) = (%v,%v) but got (%v,%v)", test.min, test.max, min, max)
}
})
}
}