-
Notifications
You must be signed in to change notification settings - Fork 5
/
simulated_data_test.go
287 lines (272 loc) · 8.79 KB
/
simulated_data_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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
package dastard
import (
"fmt"
"testing"
"time"
"gonum.org/v1/gonum/mat"
)
// TestTriangle checks that TriangleSource works as expected
func TestTriangle(t *testing.T) {
ts := NewTriangleSource()
config := TriangleSourceConfig{
Nchan: 4,
SampleRate: 10000.0,
Min: 100,
Max: 200,
}
ts.Configure(&config)
ts.noProcess = true
ds := DataSource(ts)
if ds.Running() {
t.Errorf("TriangleSource.Running() says true before first start.")
}
if err := Start(ds, nil, 256, 1024); err != nil {
t.Fatalf("TriangleSource could not be started")
}
if len(ts.processors) != config.Nchan {
t.Errorf("TriangleSource.Ouputs() returns %d channels, want %d", len(ts.processors), config.Nchan)
}
if ts.rowColCodes[0].rows() != 1 {
t.Errorf("have %v, expect 1", ts.rowColCodes[0].rows())
}
if ts.rowColCodes[3].rows() != 1 {
t.Errorf("have %v, expect 1", ts.rowColCodes[3].rows())
}
// Check first segment per source.
// n := int(config.Max - config.Min)
// for i, ch := range ts.output {
// segment := <-ch
// data := segment.rawData
// if len(data) != 2*n {
// t.Errorf("TriangleSource output %d is length %d, expect %d", i, len(data), 2*n)
// }
// for j := 0; j < n; j++ {
// if data[j] != config.Min+RawType(j) {
// t.Errorf("TriangleSource output %d has [%d]=%d, expect %d", i, j, data[j], int(config.Min)+j)
// }
// if data[j+n] != config.Max-RawType(j) {
// t.Errorf("TriangleSource output %d has [%d]=%d, expect %d", i, j+n, data[j+n], int(config.Max)-j)
// }
// }
// if segment.firstFrameIndex != 0 {
// t.Errorf("TriangleSource first segment, output %d gives firstFrameIndex %d, want 0", i, segment.firstFrameIndex)
// }
// }
// Check second segment per source.
// for i, ch := range ts.output {
// segment := <-ch
// data := segment.rawData
// if len(data) != 2*n {
// t.Errorf("TriangleSource output %d is length %d, expect %d", i, len(data), 2*n)
// }
// for j := 0; j < n; j++ {
// if data[j] != config.Min+RawType(j) {
// t.Errorf("TriangleSource output %d has [%d]=%d, expect %d", i, j, data[j], int(config.Min)+j)
// }
// if data[j+n] != config.Max-RawType(j) {
// t.Errorf("TriangleSource output %d has [%d]=%d, expect %d", i, j+n, data[j+n], int(config.Max)-j)
// }
// }
// if segment.firstFrameIndex != FrameIndex(2*n) {
// t.Errorf("TriangleSource second segment, ouput %d gives firstFrameIndex %d, want %d", i, segment.firstFrameIndex, 2*n)
// }
// }
// Stop, then check that Running() is correct
ds.Stop()
if ds.Running() {
t.Errorf("TriangleSource.Running() says true after stopped.")
}
// Start again
if err := Start(ds, nil, 256, 1024); err != nil {
t.Fatalf("TriangleSource could not be started, %s", err.Error())
}
if !ds.Running() {
t.Errorf("TriangleSource.Running() says false after started.")
}
if err := ts.Configure(&config); err == nil {
t.Errorf("TriangleSource can be configured with even though it's running, want error.")
}
if err := Start(ds, nil, 256, 1024); err == nil {
t.Errorf("Start(TriangleSource) was allowed when source was running, want error.")
}
time.Sleep(100 * time.Millisecond)
ds.Stop()
if ds.Running() {
t.Errorf("TriangleSource.Running() says true after stopped.")
}
// Start a third time
if err := Start(ds, nil, 256, 1024); err != nil {
t.Fatalf("TriangleSource could not be started")
}
// Check that we can alter the record length
ds.ConfigurePulseLengths(0, 0)
nsamp, npre := 500, 250
ds.ConfigurePulseLengths(nsamp, npre)
time.Sleep(5 * time.Millisecond)
dsp := ts.processors[0]
if dsp.NSamples != nsamp || dsp.NPresamples != npre {
t.Errorf("TriangleSource has (nsamp, npre)=(%d,%d), want (%d,%d)",
dsp.NSamples, dsp.NPresamples, nsamp, npre)
}
rows := 5
cols := 500
projectors := mat.NewDense(rows, cols, make([]float64, rows*cols))
basis := mat.NewDense(cols, rows, make([]float64, rows*cols))
if err := dsp.SetProjectorsBasis(projectors, basis, "test model"); err != nil {
t.Error(err)
}
if err := ts.ConfigureProjectorsBases(1, projectors, basis, "test model"); err != nil {
t.Error(err)
}
ds.Stop()
// Now configure a 0-channel source and make sure it fails
config.Nchan = 0
if err := ts.Configure(&config); err == nil {
t.Errorf("TriangleSource can be configured with 0 channels, want error.")
}
// Make sure that maxval < minval errors
config = TriangleSourceConfig{
Nchan: 4,
SampleRate: 10000.0,
Min: 300,
Max: 200,
}
if err := ts.Configure(&config); err == nil {
t.Error("TriangleSource can be configured with min>max, want error")
}
config = TriangleSourceConfig{
Nchan: 4,
SampleRate: 1.0,
Min: 100,
Max: 200,
}
if err := ts.Configure(&config); err == nil {
t.Errorf("TriangleSource can be configured with too-slow buffer %v, want error", ts.timeperbuf)
}
// Make sure that maxval == minval does not error and cycleLen is >>1.
config = TriangleSourceConfig{
Nchan: 4,
SampleRate: 10000.0,
Min: 200,
Max: 200,
}
if err := ts.Configure(&config); err != nil {
t.Error(err)
}
if ts.cycleLen != 1001 {
t.Errorf("ts.cycleLen is %v, want 1001", ts.cycleLen)
}
}
func TestSimPulse(t *testing.T) {
ps := NewSimPulseSource()
config := SimPulseSourceConfig{
Nchan: 5,
SampleRate: 150000.0,
Pedestal: 1000.0,
Amplitudes: []float64{10000.0},
Nsamp: 16000,
}
ps.Configure(&config)
ps.noProcess = true // for testing
ds := DataSource(ps)
if ds.Running() {
t.Errorf("SimPulseSource.Running() says true before first start.")
}
if err := Start(ds, nil, 123, 321); err != nil {
t.Fatalf("SimPulseSource could not be started")
}
Npresamp, Nsamples, err1 := ds.getPulseLengths()
if err1 != nil {
t.Error(err1)
}
if Npresamp != 123 || Nsamples != 321 {
t.Errorf("Npresample want 123, have %v. Nsamples want 321, have %v", Npresamp, Nsamples)
}
if len(ps.processors) != config.Nchan {
t.Errorf("SimPulseSource.Ouputs() returns %d channels, want %d", len(ps.processors), config.Nchan)
}
// // Check first segment per source.
// for i, ch := range ps.output {
// segment := <-ch
// data := segment.rawData
// if len(data) != config.Nsamp {
// t.Errorf("SimPulseSource output %d is length %d, expect %d", i, len(data), config.Nsamp)
// }
// min, max := RawType(65535), RawType(0)
// for j := 0; j < config.Nsamp; j++ {
// if data[j] < min {
// min = data[j]
// }
// if data[j] > max {
// max = data[j]
// }
// }
// if min != RawType(config.Pedestal+0.5-10) {
// t.Errorf("SimPulseSource minimum value is %d, expect %d", min, RawType(config.Pedestal+0.5))
// }
// if max <= RawType(config.Pedestal+config.Amplitude*0.4) {
// t.Errorf("SimPulseSource minimum value is %d, expect > %d", max, RawType(config.Pedestal+config.Amplitude*0.4))
// }
// if segment.firstFrameIndex != 0 {
// t.Errorf("SimPulseSource first segment, output %d gives firstFrameIndex %d, want 0", i, segment.firstFrameIndex)
// }
// }
// // Check second segment per source.
// for i, ch := range ps.output {
// segment := <-ch
// data := segment.rawData
// if len(data) != config.Nsamp {
// t.Errorf("SimPulseSource output %d is length %d, expect %d", i, len(data), config.Nsamp)
// }
// if segment.firstFrameIndex <= 0 {
// t.Errorf("SimPulseSource second segment gives firstFrameIndex %d, want %d", segment.firstFrameIndex, config.Nsamp)
// }
// }
ds.Stop()
// Check that Running() is correct
if ds.Running() {
t.Errorf("SimPulseSource.Running() says true before started.")
}
if err := Start(ds, nil, 256, 1024); err != nil {
t.Fatalf("SimPulseSource could not be started")
}
Npresamp, Nsamples, err2 := ds.getPulseLengths()
if err2 != nil {
t.Error(err2)
}
if Npresamp != 256 || Nsamples != 1024 {
t.Errorf("Npresample want 123, have %v. Nsamples want 321, have %v", Npresamp, Nsamples)
}
if !ds.Running() {
t.Errorf("SimPulseSource.Running() says false after started.")
}
if err := ps.Configure(&config); err == nil {
t.Errorf("SimPulseSource can be configured with even though it's running, want error.")
}
ds.Stop()
if ds.Running() {
t.Errorf("SimPulseSource.Running() says true after stopped.")
}
// Now configure a 0-channel source and make sure it fails
config.Nchan = 0
if err := ps.Configure(&config); err == nil {
t.Errorf("SimPulseSource can be configured with 0 channels.")
}
}
func TestErroringSource(t *testing.T) {
es := NewErroringSource()
ds := DataSource(es)
for i := 0; i < 5; i++ {
// // start the source, wait for it to end due to error, repeat
if err := Start(ds, nil, 256, 1024); err != nil {
t.Fatalf(fmt.Sprintf("Could not start ErroringSource: i=%v, err=%v", i, err))
}
es.RunDoneWait()
if ds.Running() {
t.Error("ErroringSource is running, want not running")
}
if es.nStarts != (i + 1) {
t.Errorf("have %v, want %v", es.nStarts, (i + 1))
}
}
}