-
Notifications
You must be signed in to change notification settings - Fork 0
/
codec_test.go
171 lines (152 loc) · 3.6 KB
/
codec_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
// Copyright (c) 2019 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package raft
import (
"testing"
)
func TestJSONCodec(t *testing.T) {
type Object struct {
A bool `json:"A" xml:"A"`
}
var obj = Object{A: true}
var c = JSONCodec{}
var buf = make([]byte, 512)
var objCopy *Object
data, _ := c.Marshal(buf, &obj)
c.Unmarshal(data, &objCopy)
}
func TestXMLCodec(t *testing.T) {
type Object struct {
A bool `json:"A" xml:"A"`
}
var obj = Object{A: true}
var c = XMLCodec{}
var buf = make([]byte, 512)
var objCopy *Object
data, _ := c.Marshal(buf, &obj)
c.Unmarshal(data, &objCopy)
}
func TestBYTESCodec(t *testing.T) {
var obj = []byte{128, 8, 128, 8, 195, 245, 72, 64, 74, 216, 18, 77, 251, 33, 9, 64, 10, 72, 101, 108, 108, 111, 87, 111, 114, 108, 100, 1, 1, 255, 2, 1, 128, 1, 255}
var c = BYTESCodec{}
var buf = make([]byte, 512)
var objCopy []byte
data, _ := c.Marshal(buf, &obj)
c.Unmarshal(data, &objCopy)
}
//codeObject is a test struct
type codeObject struct {
}
//Marshal marshals the Object into buf and returns the bytes.
func (o *codeObject) Marshal(buf []byte) ([]byte, error) {
return nil, nil
}
//Unmarshal unmarshals the Object from buf and returns the number of bytes read (> 0).
func (o *codeObject) Unmarshal(data []byte) (uint64, error) {
return 0, nil
}
func TestCODECodec(t *testing.T) {
{
var obj = codeObject{}
var c = CODECodec{}
var buf = make([]byte, 512)
var objCopy codeObject
data, _ := c.Marshal(buf, &obj)
c.Unmarshal(data, &objCopy)
}
{
type Object struct {
}
var obj = Object{}
var c = CODECodec{}
var buf = make([]byte, 512)
var objCopy Object
if _, err := c.Marshal(buf, &obj); err != ErrorCODE {
t.Error(ErrorCODE)
}
if err := c.Unmarshal(nil, &objCopy); err != ErrorCODE {
t.Error(ErrorCODE)
}
}
}
type gogopbObject struct {
size int
}
func (m *gogopbObject) Size() (n int) {
return m.size
}
func (m *gogopbObject) Marshal() (dAtA []byte, err error) {
return
}
func (m *gogopbObject) MarshalTo(dAtA []byte) (int, error) {
return 0, nil
}
func (m *gogopbObject) Unmarshal(dAtA []byte) error {
return nil
}
func TestGOGOPBCodec(t *testing.T) {
{
var obj = gogopbObject{}
var c = GOGOPBCodec{}
var buf = make([]byte, 512)
var objCopy gogopbObject
data, _ := c.Marshal(buf, &obj)
c.Unmarshal(data, &objCopy)
}
{
var obj = gogopbObject{size: 128}
var c = GOGOPBCodec{}
var buf = make([]byte, 0)
var objCopy gogopbObject
data, _ := c.Marshal(buf, &obj)
c.Unmarshal(data, &objCopy)
}
{
type Object struct {
}
var obj = Object{}
var c = GOGOPBCodec{}
var buf = make([]byte, 512)
var objCopy Object
if _, err := c.Marshal(buf, &obj); err != ErrorGOGOPB {
t.Error(ErrorGOGOPB)
}
if err := c.Unmarshal(nil, &objCopy); err != ErrorGOGOPB {
t.Error(ErrorGOGOPB)
}
}
}
type msgpObject struct {
}
// MarshalMsg implements msgp.Marshaler
func (z *msgpObject) MarshalMsg(b []byte) (o []byte, err error) {
return
}
// UnmarshalMsg implements msgp.Unmarshaler
func (z *msgpObject) UnmarshalMsg(bts []byte) (o []byte, err error) {
return
}
func TestMSGPCodec(t *testing.T) {
{
var obj = msgpObject{}
var c = MSGPCodec{}
var buf = make([]byte, 512)
var objCopy msgpObject
data, _ := c.Marshal(buf, &obj)
c.Unmarshal(data, &objCopy)
}
{
type Object struct {
}
var obj = Object{}
var c = MSGPCodec{}
var buf = make([]byte, 512)
var objCopy Object
if _, err := c.Marshal(buf, &obj); err != ErrorMSGP {
t.Error(ErrorMSGP)
}
if err := c.Unmarshal(nil, &objCopy); err != ErrorMSGP {
t.Error(ErrorMSGP)
}
}
}