-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathwrite_test.go
323 lines (301 loc) · 7.17 KB
/
write_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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
// Copyright 2023 Sneller, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package ion
import (
"bytes"
"math/rand"
"testing"
"time"
"github.com/SnellerInc/sneller/date"
)
func TestEncodeInt(t *testing.T) {
ints := []struct {
value int64
encoded []byte
}{
{0, []byte{0x20}},
{1, []byte{0x21, 0x01}},
{-1, []byte{0x31, 0x01}},
{127, []byte{0x21, 0x7f}},
{-127, []byte{0x31, 0x7f}},
{255, []byte{0x21, 0xff}},
{-255, []byte{0x31, 0xff}},
{256, []byte{0x22, 0x01, 0x00}},
{-256, []byte{0x32, 0x01, 0x00}},
{1251, []byte{0x22, 0x04, 0xe3}},
{1103341116, []byte{0x24, 0x41, 0xc3, 0xa6, 0x3c}},
}
var b Buffer
for i := range ints {
b.Reset()
want := ints[i].encoded
b.WriteInt(ints[i].value)
got := b.Bytes()
if !bytes.Equal(got, want) {
t.Errorf("encoding %d: got %x, want %x", ints[i].value, got, want)
}
v, tail, err := ReadInt(ints[i].encoded)
if err != nil {
t.Fatal(err)
}
if v != ints[i].value {
t.Errorf("decoding %d: got %d", ints[i].value, v)
}
if len(tail) != 0 {
t.Errorf("%d bytes left over?", len(tail))
}
if s := SizeOf(ints[i].encoded); s != len(ints[i].encoded) {
t.Errorf("case %d: SizeOf(msg)=%d, len(msg)=%d", i, s, len(ints[i].encoded))
}
}
}
func TestEncodeString(t *testing.T) {
tcs := []struct {
value string
encoded []byte
}{
{"", []byte{0x80}},
{"a", []byte{0x81, 'a'}},
{"ab", []byte{0x82, 'a', 'b'}},
{"abcdefghijkl123456789", []byte{0x8e, 0x80 | 21,
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
'j', 'k', 'l',
'1', '2', '3', '4', '5', '6', '7', '8', '9',
}},
}
var b Buffer
for i := range tcs {
b.Reset()
want := tcs[i].encoded
b.WriteString(tcs[i].value)
got := b.Bytes()
if !bytes.Equal(want, got) {
t.Errorf("encoding %q: got %x, wanted %x", tcs[i].value, got, want)
}
v, tail, err := ReadString(tcs[i].encoded)
if err != nil {
t.Fatal(err)
}
if v != tcs[i].value {
t.Errorf("decoding %q: got %q", tcs[i].value, v)
}
if len(tail) != 0 {
t.Errorf("%d bytes left over?", len(tail))
}
if s := SizeOf(tcs[i].encoded); s != len(tcs[i].encoded) {
t.Errorf("case %d: SizeOf(msg)=%d, len(msg)=%d", i, s, len(tcs[i].encoded))
}
}
}
func TestShortStruct(t *testing.T) {
tcs := []struct {
expr func(b *Buffer)
out []byte
}{
{
expr: func(b *Buffer) {
b.BeginField(1)
b.WriteString("x")
},
out: []byte{0xd3, 0x81, 0x81, 'x'},
},
{
expr: func(b *Buffer) {
b.BeginField(16)
b.WriteInt(0)
},
out: []byte{0xd2, 0x90, 0x20},
},
{
expr: func(b *Buffer) {
b.BeginField(0x12)
b.WriteInt(0)
b.BeginField(0x12)
b.WriteInt(3) // ignored; duplicate
b.BeginField(0x11)
b.WriteInt(1)
b.BeginField(0x12)
b.WriteInt(5) // ignored; duplicate again
},
// fields have been re-ordered
out: []byte{0xd5, 0x91, 0x21, 0x01, 0x92, 0x20},
},
}
var b Buffer
for i := range tcs {
b.Reset()
want := tcs[i].out
b.BeginStruct(-1)
tcs[i].expr(&b)
b.EndStruct()
got := b.Bytes()
if !bytes.Equal(want, got) {
t.Errorf("case %d: wanted %x, got %x", i, want, got)
}
inner, tail := Contents(got)
if inner == nil {
t.Errorf("case %d: Contents(msg) failed", i)
}
if len(tail) != 0 {
t.Errorf("case %d: len(tail) == 0?", i)
}
if s := SizeOf(got); s != len(got) {
t.Errorf("case %d: SizeOf(msg)=%d, len(msg)=%d", i, s, len(got))
}
}
}
func TestRandomTime(t *testing.T) {
base := date.Unix(0, 0)
var b Buffer
// test many random displacements of 64-bit nanoseconds
// from the zero unix time
for i := 0; i < 10000; i++ {
b.Reset()
c := base.Add(time.Duration(rand.Uint64()))
c = c.Round(time.Duration(1000))
b.WriteTime(c)
out, _, err := ReadTime(b.Bytes())
if err != nil {
t.Fatalf("reading time %q: %s", c, err)
}
if !out.Equal(c) {
t.Fatalf("%q != %q", out, c)
}
}
}
func TestTime(t *testing.T) {
tcs := []struct {
text string
encoded []byte
}{
{
// test case from https://amzn.github.io/ion-docs/docs/binary.html#6-timestamp
text: "2000-01-01T00:00:00Z",
encoded: []byte{0x68, 0x80, 0x0F, 0xD0, 0x81, 0x81, 0x80, 0x80, 0x80},
},
{
text: time.RFC3339[:len(time.RFC3339)-5],
encoded: []byte{0x68, 0x80, 0x0F, 0xD6, 0x81, 0x82, 0x8f, 0x84, 0x85},
},
}
var b Buffer
for i := range tcs {
d, ok := date.Parse([]byte(tcs[i].text))
if !ok {
t.Fatalf("parsing %q failed", tcs[i].text)
}
b.Reset()
b.WriteTime(d)
got := b.Bytes()
if !bytes.Equal(got, tcs[i].encoded) {
t.Errorf("case %d: got %x", i, got)
t.Errorf("case %d: want %x", i, tcs[i].encoded)
}
d2, rest, err := ReadTime(got)
if err != nil {
t.Errorf("case %d: ReadDate: %s", i, err)
}
if len(rest) != 0 {
t.Errorf("case %d: %d bytes left over?", i, len(rest))
}
if !d2.Equal(d) {
t.Errorf("case %d: decoded result %s not equal to %s", i, d2, d)
}
}
}
func TestSizeOf(t *testing.T) {
tcs := []struct {
mem []byte
want int
}{
{
mem: []byte{0xee, 0x02, 0x95},
want: 280,
},
}
for i := range tcs {
if got := SizeOf(tcs[i].mem); got != tcs[i].want {
t.Errorf("case %d: got %d, want %d", i, got, tcs[i].want)
}
}
}
func TestNopPadding(t *testing.T) {
buffer := make([]byte, 32)
for padding := 1; padding < 1024*17; padding++ {
for i := 0; i < len(buffer); i++ {
buffer[i] = 0
}
header, padbytes := NopPadding(buffer, padding)
if TypeOf(buffer) != NullType {
t.Errorf("case %d: wrong Ion type %s", padding, TypeOf(buffer))
}
if SizeOf(buffer) != header+padbytes {
t.Errorf("case %d: wrong Ion object size %d, expected %d",
padding, SizeOf(buffer), header+padbytes)
}
sum := header + padbytes
if sum == padding {
continue
}
if sum > padding {
t.Errorf("case %d: header %d bytes and pad bytes %d - sum %d greater than padding",
padding, header, padbytes, sum)
}
if padding-sum > 2 {
t.Errorf("case %d: header %d bytes and pad bytes %d - sum %d too small",
padding, header, padbytes, sum)
}
}
}
func BenchmarkSizeOf(b *testing.B) {
sizes := []int{
0, 5, 12, 225, 18468,
}
var ib Buffer
for i := range sizes {
ib.WriteStringBytes(make([]byte, sizes[i]))
}
mem := ib.Bytes()
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf := mem
for len(buf) > 0 {
buf = buf[SizeOf(buf):]
}
}
}
func BenchmarkPutUvarint(b *testing.B) {
run := func(name string, sym Symbol) {
b.Run(name, func(b *testing.B) {
var ib Buffer
for i := 0; i < b.N; i++ {
ib.putuv(uint(sym))
ib.Reset()
}
})
}
run("1byte", 14)
run("2byte", 200)
run("3byte", (1<<14)+7)
}
func BenchmarkUvarint(b *testing.B) {
dst := make([]byte, 0, 10)
var ib Buffer
ib.Set(dst)
ib.putuv((1 << 21) + 1)
dst = ib.Bytes()
for i := 0; i < b.N; i++ {
_, _, _ = readuv(dst)
}
}