-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathencoder_test.go
172 lines (147 loc) · 3.53 KB
/
encoder_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
// Copyright (c) Roman Atachiants and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package binary
import (
"bytes"
"encoding/json"
"testing"
"unsafe"
"github.com/stretchr/testify/assert"
)
var testMsg = msg{
Name: "Roman",
Timestamp: 1242345235,
Payload: []byte("hi"),
Ssid: []uint32{1, 2, 3},
}
type composite map[string]column
type column struct {
Varchar columnVarchar
Float64 columnFloat64
Float32 columnFloat32
}
type columnVarchar struct {
Nulls []bool
Sizes []uint32
Bytes []byte
}
type columnFloat64 struct {
Nulls []bool
Floats []float64
}
type columnFloat32 struct {
Nulls []bool
Floats []float32
}
func Test_Full(t *testing.T) {
v := composite{}
v["a"] = column{
Varchar: columnVarchar{
Nulls: []bool{false, false, false, true, false},
Sizes: []uint32{2, 2, 2, 0, 2},
Bytes: []byte{10, 10, 10, 10, 10, 10, 10, 10},
},
}
v["b"] = column{
Float64: columnFloat64{
Nulls: []bool{false, false, false, true, false},
Floats: []float64{1.1, 2.2, 3.3, 0, 4.4},
},
}
b, err := Marshal(&v)
assert.NoError(t, err)
assert.NotNil(t, b)
var o composite
err = Unmarshal(b, &o)
assert.NoError(t, err)
assert.Equal(t, v, o)
}
func newComposite() composite {
v := composite{}
v["a"] = column{
Varchar: columnVarchar{
Nulls: []bool{false, false, false, true, false, false, false, false, true, false, false, false, false, true, false},
Sizes: []uint32{2, 2, 2, 0, 2, 2, 2, 2, 0, 2, 2, 2, 2, 0, 2, 2, 2, 2, 0, 2, 2, 2, 2, 0, 2, 2, 2, 2, 0, 2},
Bytes: []byte{10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
},
}
v["b"] = column{
Float64: columnFloat64{
Nulls: []bool{false, false, false, true, false},
Floats: []float64{1.1, 2.2, 3.3, 0, 4.4},
},
}
return v
}
/*
cpu: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz
Benchmark_Binary/marshal-12 5074890 227.4 ns/op 112 B/op 2 allocs/op
Benchmark_Binary/marshal-to-12 7011523 162.3 ns/op 30 B/op 0 allocs/op
Benchmark_Binary/unmarshal-12 4224048 283.0 ns/op 72 B/op 5 allocs/op
*/
func BenchmarkBinary(b *testing.B) {
v := testMsg
enc, _ := Marshal(&v)
b.Run("marshal", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
Marshal(&v)
}
})
var buffer bytes.Buffer
b.Run("marshal-to", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
buffer.Reset()
MarshalTo(&v, &buffer)
}
})
b.Run("unmarshal", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var out msg
for n := 0; n < b.N; n++ {
Unmarshal(enc, &out)
}
})
}
func BenchmarkJSON(b *testing.B) {
v := testMsg
enc, _ := json.Marshal(&v)
b.Run("marshal", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for n := 0; n < b.N; n++ {
json.Marshal(&v)
}
})
b.Run("unmarshal", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var out msg
for n := 0; n < b.N; n++ {
json.Unmarshal(enc, &out)
}
})
}
func TestBinaryEncodeStruct(t *testing.T) {
b, err := Marshal(s0v)
assert.NoError(t, err)
assert.Equal(t, s0b, b)
}
func TestEncoderSizeOf(t *testing.T) {
var e Encoder
assert.Equal(t, 56, int(unsafe.Sizeof(e)))
}
func TestMarshalWithCustomCodec(t *testing.T) {
v := testCustom("custom codec")
b, err := Marshal(v)
assert.NoError(t, err)
assert.NotNil(t, b)
var out testCustom
err = Unmarshal(b, &out)
assert.NoError(t, err)
assert.Equal(t, v, out)
}