-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodec_test.go
More file actions
91 lines (75 loc) · 1.93 KB
/
Copy pathcodec_test.go
File metadata and controls
91 lines (75 loc) · 1.93 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
package dbcache
import (
"testing"
"github.com/stretchr/testify/assert"
)
type codecTestStruct struct {
Name string
Value int
Tags []string
}
func testCodecRoundTrip(t *testing.T, codec Codec) {
t.Helper()
original := &codecTestStruct{
Name: "test",
Value: 42,
Tags: []string{"go", "cache"},
}
data, err := codec.Marshal(original)
assert.NoError(t, err)
assert.NotEmpty(t, data)
var decoded codecTestStruct
err = codec.Unmarshal(data, &decoded)
assert.NoError(t, err)
assert.Equal(t, original.Name, decoded.Name)
assert.Equal(t, original.Value, decoded.Value)
assert.Equal(t, original.Tags, decoded.Tags)
}
func TestJSONCodec_RoundTrip(t *testing.T) {
testCodecRoundTrip(t, JSONCodec{})
}
func TestGobCodec_RoundTrip(t *testing.T) {
testCodecRoundTrip(t, GobCodec{})
}
func TestMsgpackCodec_RoundTrip(t *testing.T) {
testCodecRoundTrip(t, MsgpackCodec{})
}
func TestJSONCodec_NilInput(t *testing.T) {
codec := JSONCodec{}
_, err := codec.Marshal(nil)
assert.NoError(t, err)
}
func TestGobCodec_NilInput(t *testing.T) {
codec := GobCodec{}
err := codec.Unmarshal([]byte{}, nil)
assert.Error(t, err)
}
func TestMsgpackCodec_NilInput(t *testing.T) {
codec := MsgpackCodec{}
_, err := codec.Marshal(nil)
assert.NoError(t, err)
}
func BenchmarkJSONCodec_Marshal(b *testing.B) {
codec := JSONCodec{}
data := &codecTestStruct{Name: "benchmark", Value: 123, Tags: []string{"a", "b"}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = codec.Marshal(data)
}
}
func BenchmarkGobCodec_Marshal(b *testing.B) {
codec := GobCodec{}
data := &codecTestStruct{Name: "benchmark", Value: 123, Tags: []string{"a", "b"}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = codec.Marshal(data)
}
}
func BenchmarkMsgpackCodec_Marshal(b *testing.B) {
codec := MsgpackCodec{}
data := &codecTestStruct{Name: "benchmark", Value: 123, Tags: []string{"a", "b"}}
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = codec.Marshal(data)
}
}