-
Notifications
You must be signed in to change notification settings - Fork 2
/
rebaser_test.go
217 lines (181 loc) · 5.78 KB
/
rebaser_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
package distil
import (
"math"
"math/rand"
"testing"
btrdb "github.com/SoftwareDefinedBuildings/btrdb-go"
)
const NANO int64 = 1000000000
const FREQ int64 = 120
const SPACING_INT64 int64 = NANO / FREQ
func sliceToChan(s []btrdb.StandardValue) chan btrdb.StandardValue {
var rv chan btrdb.StandardValue = make(chan btrdb.StandardValue)
go func () {
for _, sv := range s {
rv <- sv
}
close(rv)
}()
return rv
}
func chanToSlice(svs chan btrdb.StandardValue) []btrdb.StandardValue {
var rv = make([]btrdb.StandardValue, 0, 8)
for sv := range svs {
rv = append(rv, sv)
}
return rv
}
func floatEquals(x float64, y float64) bool {
return math.Abs(x - y) <= 1e-10 * math.Max(math.Abs(x), math.Abs(y))
}
func makeAligned(ssec, esec int64) []btrdb.StandardValue {
var s []btrdb.StandardValue = make([]btrdb.StandardValue, 0, (esec - ssec) * 120)
for second := int64(ssec); second < esec; second++ {
for i := int64(0); i < 120; i++ {
t := (second * 1000000000) + (i * SPACING_INT64)
v := float64(i)
sv := btrdb.StandardValue{ Time: t, Value: v }
s = append(s, sv)
}
}
return s
}
func TestFullyAligned(t *testing.T) {
s := makeAligned(0, 3)
var c chan btrdb.StandardValue = sliceToChan(s)
var pnr Rebaser = RebasePadSnap(FREQ)
var oc chan btrdb.StandardValue = pnr.Process(0 * NANO, 3 * NANO, c)
var os []btrdb.StandardValue = chanToSlice(oc)
if len(os) != len(s) {
t.Fatalf("Output has %d points (expected %d)", len(os), len(s))
}
for i := range os {
if os[i].Time != s[i].Time || !floatEquals(os[i].Value, s[i].Value) {
t.Logf("Got (%d, %f) at entry %d; expected (%d, %f)", os[i].Time, os[i].Value, i, s[i].Time, s[i].Value)
t.Fail()
}
}
}
func TestExtraSpace(t *testing.T) {
s := makeAligned(2, 5)
var c chan btrdb.StandardValue = sliceToChan(s)
var pnr Rebaser = RebasePadSnap(FREQ)
var oc chan btrdb.StandardValue = pnr.Process(1 * NANO, 6 * NANO, c)
var os []btrdb.StandardValue = chanToSlice(oc)
var explen int64 = int64(len(s)) + 2 * FREQ
if int64(len(os)) != explen {
t.Fatalf("Output has %d points (expected %d)", len(os), explen)
}
var i int64
for i = 0; i < FREQ; i++ {
expt := (1 * NANO) + i * SPACING_INT64
if os[i].Time != expt || !math.IsNaN(os[i].Value) {
t.Logf("Got (%d, %f) at entry %d; expected (%d, NaN)", os[i].Time, os[i].Value, i, expt)
t.Fail()
}
}
for i = 0; i < FREQ; i++ {
expt := (5 * NANO) + i * SPACING_INT64
j := i + 4 * FREQ
if os[j].Time != expt || !math.IsNaN(os[j].Value) {
t.Logf("Got (%d, %f) at entry %d; expected (%d, NaN)", os[j].Time, os[j].Value, j, expt)
t.Fail()
}
}
for j := range s {
i := int64(j) + FREQ
if os[i].Time != s[j].Time || !floatEquals(os[i].Value, s[j].Value) {
t.Logf("Got (%d, %f) at entry %d; expected (%d, %f)", os[i].Time, os[i].Value, j, s[j].Time, s[j].Value)
t.Fail()
}
}
}
func TestExtraSpaceJitter(t *testing.T) {
s := makeAligned(2, 5)
var ns []btrdb.StandardValue = make([]btrdb.StandardValue, len(s))
copy(ns, s)
half_space := ((SPACING_INT64 - 2) / 2)
for k := range ns {
ns[k].Time += (rand.Int63n(SPACING_INT64 - 2) - half_space)
}
var c chan btrdb.StandardValue = sliceToChan(ns)
var pnr Rebaser = RebasePadSnap(FREQ)
var oc chan btrdb.StandardValue = pnr.Process(1 * NANO, 6 * NANO, c)
var os []btrdb.StandardValue = chanToSlice(oc)
var explen int64 = int64(len(s)) + 2 * FREQ
if int64(len(os)) != explen {
t.Fatalf("Output has %d points (expected %d)", len(os), explen)
}
var i int64
for i = 0; i < FREQ; i++ {
expt := (1 * NANO) + i * SPACING_INT64
if os[i].Time != expt || !math.IsNaN(os[i].Value) {
t.Logf("Got (%d, %f) at entry %d; expected (%d, NaN)", os[i].Time, os[i].Value, i, expt)
t.Fail()
}
}
for i = 0; i < FREQ; i++ {
expt := (5 * NANO) + i * SPACING_INT64
j := i + 4 * FREQ
if os[j].Time != expt || !math.IsNaN(os[j].Value) {
t.Logf("Got (%d, %f) at entry %d; expected (%d, NaN)", os[j].Time, os[j].Value, j, expt)
t.Fail()
}
}
for j := range s {
i := int64(j) + FREQ
if os[i].Time != s[j].Time || !floatEquals(os[i].Value, s[j].Value) {
t.Logf("Got (%d, %f) at entry %d; expected (%d, %f)", os[i].Time, os[i].Value, j, s[j].Time, s[j].Value)
t.Fail()
}
}
}
func TestExtraSpaceMissingJitter(t *testing.T) {
s := makeAligned(2, 5)
var ns []btrdb.StandardValue = make([]btrdb.StandardValue, len(s) >> 1)
for m := range ns {
ns[m] = s[m << 1]
}
half_space := ((SPACING_INT64 - 2) / 2)
for k := range ns {
ns[k].Time += (rand.Int63n(SPACING_INT64 - 2) - half_space)
}
var c chan btrdb.StandardValue = sliceToChan(ns)
var pnr Rebaser = RebasePadSnap(FREQ)
var oc chan btrdb.StandardValue = pnr.Process(1 * NANO, 6 * NANO, c)
var os []btrdb.StandardValue = chanToSlice(oc)
var explen int64 = int64(len(s)) + 2 * FREQ
if int64(len(os)) != explen {
t.Fatalf("Output has %d points (expected %d)", len(os), explen)
}
var i int64
for i = 0; i < FREQ; i++ {
expt := (1 * NANO) + i * SPACING_INT64
if os[i].Time != expt || !math.IsNaN(os[i].Value) {
t.Logf("Got (%d, %f) at entry %d; expected (%d, NaN)", os[i].Time, os[i].Value, i, expt)
t.Fail()
}
}
for i = 0; i < FREQ; i++ {
expt := (5 * NANO) + i * SPACING_INT64
j := i + 4 * FREQ
if os[j].Time != expt || !math.IsNaN(os[j].Value) {
t.Logf("Got (%d, %f) at entry %d; expected (%d, NaN)", os[j].Time, os[j].Value, j, expt)
t.Fail()
}
}
for j := range s {
i := int64(j) + FREQ
if (i & 0x1) == 0 {
if os[i].Time != s[j].Time || !floatEquals(os[i].Value, s[j].Value) {
t.Logf("Got (%d, %f) at entry %d; expected (%d, %f)", os[i].Time, os[i].Value, j, s[j].Time, s[j].Value)
t.Fail()
}
} else {
if os[i].Time != s[j].Time || !math.IsNaN(os[i].Value) {
t.Logf("Got (%d, %f) at entry %d; expected (%d, NaN)", os[i].Time, os[i].Value, j, s[j].Time)
t.Fail()
}
}
}
}