-
Notifications
You must be signed in to change notification settings - Fork 3
/
metricpoint_withoutorg_test.go
172 lines (159 loc) · 4.63 KB
/
metricpoint_withoutorg_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
package schema
import (
"math"
"reflect"
"testing"
)
func TestMetricPointMarshalWithoutOrg(t *testing.T) {
tests := []MetricPoint{
{
MKey: MKey{[16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 255}, 12345},
Time: 0,
Value: 0,
},
{
MKey: MKey{[16]byte{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, math.MaxUint32},
Time: 1234567890,
Value: 123.456789,
},
{
MKey: MKey{[16]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 0},
Time: math.MaxUint32,
Value: math.MaxFloat64,
},
}
for i, in := range tests {
// MarshalWithoutOrg with nil input
data1, err := in.MarshalWithoutOrg(nil)
if err != nil {
t.Fatalf("case %d got err %s", i, err.Error())
}
if len(data1) != 28 {
t.Fatalf("case %d did not result in 28B data packet", i)
}
// MarshalWithoutOrg with sufficient input
buf2 := make([]byte, 0, 28)
data2, err := in.MarshalWithoutOrg(buf2)
if err != nil {
t.Fatalf("case %d got err %s", i, err.Error())
}
if !reflect.DeepEqual(data1, data2[:28]) {
t.Fatalf("case %d marshaling mismatch.", i)
}
// MarshalWithoutOrg with very big input
buf3 := make([]byte, 0, 512)
data3, err := in.MarshalWithoutOrg(buf3)
if err != nil {
t.Fatalf("case %d got err %s", i, err.Error())
}
if !reflect.DeepEqual(data1, data3[:28]) {
t.Fatalf("case %d marshaling mismatch", i)
}
// MarshalWithoutOrg28 with sufficient input
buf4 := make([]byte, 0, 28)
data4, err := in.MarshalWithoutOrg28(buf4)
if err != nil {
t.Fatalf("case %d got err %s", i, err.Error())
}
if !reflect.DeepEqual(data1, data4[:28]) {
t.Fatalf("case %d marshaling mismatch.", i)
}
// MarshalWithoutOrg28 with very big input
buf5 := make([]byte, 0, 512)
data5, err := in.MarshalWithoutOrg28(buf5)
if err != nil {
t.Fatalf("case %d got err %s", i, err.Error())
}
if !reflect.DeepEqual(data1, data5[:28]) {
t.Fatalf("case %d marshaling mismatch", i)
}
// pre-existing input that should be left alone
buf6 := []byte{'f', 'o', 'o'}
data6, err := in.MarshalWithoutOrg(buf6)
if err != nil {
t.Fatalf("case %d got err %s", i, err.Error())
}
if !reflect.DeepEqual(data1, data6[3:]) {
t.Fatalf("case %d marshaling mismatch", i)
}
if string(data6[0:3]) != "foo" {
t.Fatalf("case %d pre-existing data was modified to %q", i, string(data6[0:3]))
}
// unmarshal
out := MetricPoint{}
leftover, err := out.UnmarshalWithoutOrg(data1)
if err != nil {
t.Fatalf("case %d got err %s", i, err.Error())
}
if len(leftover) != 0 {
t.Fatalf("case %d got leftover data: %v", i, leftover)
}
if out.MKey.Org != 0 {
t.Fatalf("case %d expected org 0, got %d", i, out.MKey.Org)
}
if in.MKey.Key != out.MKey.Key || in.Time != out.Time || in.Value != out.Value {
t.Fatalf("case %d data mismatch:\nexp: %v\ngot: %v", i, in, out)
}
}
}
func TestMetricPointMarshalWithoutOrgMultiple(t *testing.T) {
tests := []MetricPoint{
{
MKey: MKey{[16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 255}, math.MaxUint32},
Time: 0,
Value: 0,
},
{
MKey: MKey{[16]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 0},
Time: math.MaxUint32,
Value: math.MaxFloat64,
},
{
MKey: MKey{[16]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 255}, math.MaxUint32},
Time: 0,
Value: 0,
},
{
MKey: MKey{[16]byte{255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255}, 0},
Time: math.MaxUint32,
Value: math.MaxFloat64,
},
}
var buf []byte
for i, in := range tests {
// marshal with nil input
var err error
buf, err = in.MarshalWithoutOrg(buf)
if err != nil {
t.Fatalf("case %d got err %s", i, err.Error())
}
expLen := (i + 1) * 28
if len(buf) != expLen {
t.Fatalf("iteration %d . expected length %d, got %d", i, expLen, len(buf))
}
}
type byteCheck struct {
pos int
val uint8
comment string
}
checks := []byteCheck{
{0, 0, "byte 0 of id of first"},
{12, 12, "byte 12 of id of first"},
{27, 0, "last byte of value of first"},
{28*2 + 0, 0, "byte 0 of id of third"},
{28*2 + 12, 12, "byte 12 of id of third"},
{28*2 + 27, 0, "last byte of value of third"},
{28 + 0, 255, "byte 0 of id of second"},
{28 + 12, 255, "byte 12 of id of second"},
{28 + 27, 255, "last byte of value of second"},
{28*3 + 0, 255, "byte 0 of id of fourth"},
{28*3 + 12, 255, "byte 12 of id of fourth"},
{28*3 + 27, 255, "last byte of value of fourth"},
}
for _, check := range checks {
if buf[check.pos] != check.val {
t.Fatalf("%s: expected val %d, got %d", check.comment, check.val, buf[check.pos])
}
}
}