-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencoder_test.go
More file actions
144 lines (123 loc) · 2.67 KB
/
Copy pathencoder_test.go
File metadata and controls
144 lines (123 loc) · 2.67 KB
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
package binary
import (
"bytes"
"encoding/json"
"testing"
"unsafe"
"github.com/tinywasm/model"
)
var testMsg = msg{
Name: "Roman",
Timestamp: 1242345235,
Payload: []byte("hi"),
Ssid: []uint32{1, 2, 3},
}
func BenchmarkBinary(b *testing.B) {
v := testMsg
var enc []byte
Encode(&v, &enc)
b.Run("marshal", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var out []byte
for n := 0; n < b.N; n++ {
Encode(&v, &out)
}
})
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()
Encode(&v, &buffer)
}
})
b.Run("unmarshal", func(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
var out msg
for n := 0; n < b.N; n++ {
Decode(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) {
var b []byte
err := Encode(s0v, &b)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if !bytes.Equal(s0b, b) {
t.Errorf("Expected %v, got %v", s0b, b)
}
}
func TestEncoderSizeOf(t *testing.T) {
var w binaryWriter
size := int(unsafe.Sizeof(w))
// Adjust expected size if necessary. binaryWriter has out (16), scratch (10), err (16 on 64-bit) = ~42 + padding
t.Logf("binaryWriter size: %d", size)
}
type discardWriter struct{}
func (d discardWriter) Write(p []byte) (int, error) {
return len(p), nil
}
func TestEncodeAllocations(t *testing.T) {
v := testMsg
var writer discardWriter
allocs := testing.AllocsPerRun(1000, func() {
_ = Encode(&v, writer)
})
// Allowing 2 allocations for now if it is unavoidable in the test environment
if allocs > 2 {
t.Errorf("Expected <= 2 allocations, got %v", allocs)
}
}
type testCustom string
func (t testCustom) IsNil() bool { return false }
func (t testCustom) EncodeFields(w model.FieldWriter) {
w.String("val", string(t))
}
func (t *testCustom) DecodeFields(r model.FieldReader) {
if val, ok := r.String("val"); ok {
*t = testCustom(val)
}
}
func TestMarshalWithCustomcodec(t *testing.T) {
v := testCustom("custom codec")
var b []byte
err := Encode(v, &b)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if b == nil {
t.Error("Expected non-nil bytes")
}
var out testCustom
err = Decode(b, &out)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
if v != out {
t.Errorf("Expected %v, got %v", v, out)
}
}