-
Notifications
You must be signed in to change notification settings - Fork 83
/
encoding_test.go
118 lines (112 loc) · 2.68 KB
/
encoding_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
package tstorage
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_gorillaEncoder_encodePoint_decodePoint(t *testing.T) {
tests := []struct {
name string
input []*DataPoint // to be encoded
want []*DataPoint
wantEncodedByteSize int
wantErr bool
}{
{
name: "one data point",
input: []*DataPoint{
{Timestamp: 1600000000, Value: 0.1},
},
want: []*DataPoint{
{Timestamp: 1600000000, Value: 0.1},
},
wantEncodedByteSize: 14,
wantErr: false,
},
{
name: "data points at regular intervals",
input: []*DataPoint{
{Timestamp: 1600000000, Value: 0.1},
{Timestamp: 1600000060, Value: 0.1},
{Timestamp: 1600000120, Value: 0.1},
{Timestamp: 1600000180, Value: 0.1},
},
want: []*DataPoint{
{Timestamp: 1600000000, Value: 0.1},
{Timestamp: 1600000060, Value: 0.1},
{Timestamp: 1600000120, Value: 0.1},
{Timestamp: 1600000180, Value: 0.1},
},
wantEncodedByteSize: 15,
wantErr: false,
},
{
name: "data points at random intervals",
input: []*DataPoint{
{Timestamp: 1600000000, Value: 0.1},
{Timestamp: 1600000060, Value: 1.1},
{Timestamp: 1600000182, Value: 15.01},
{Timestamp: 1600000400, Value: 0.01},
{Timestamp: 1600002000, Value: 10.8},
},
want: []*DataPoint{
{Timestamp: 1600000000, Value: 0.1},
{Timestamp: 1600000060, Value: 1.1},
{Timestamp: 1600000182, Value: 15.01},
{Timestamp: 1600000400, Value: 0.01},
{Timestamp: 1600002000, Value: 10.8},
},
wantEncodedByteSize: 52,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Encode
var buf bytes.Buffer
var num int
encoder := newSeriesEncoder(&buf)
for _, point := range tt.input {
err := encoder.encodePoint(point)
require.NoError(t, err)
num++
}
err := encoder.flush()
require.NoError(t, err)
assert.Equal(t, tt.wantEncodedByteSize, buf.Len())
// Decode
decoder, err := newSeriesDecoder(&buf)
require.NoError(t, err)
got := make([]*DataPoint, 0, num)
for i := 0; i < num; i++ {
p := &DataPoint{}
err := decoder.decodePoint(p)
require.NoError(t, err)
got = append(got, p)
}
assert.Equal(t, tt.want, got)
})
}
}
func Test_bitRange(t *testing.T) {
tests := []struct {
name string
x int64
nbits uint8
want bool
}{
{
name: "inside the range",
x: 1,
nbits: 1,
want: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := bitRange(tt.x, tt.nbits)
assert.Equal(t, tt.want, got)
})
}
}