-
Notifications
You must be signed in to change notification settings - Fork 5
/
write_test.go
245 lines (226 loc) · 5.25 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
package etf
import (
"bytes"
"math"
"math/big"
"reflect"
"testing"
)
func TestWriteAtom(t *testing.T) {
c := new(Context)
test := func(in Atom, shouldFail bool) {
w := new(bytes.Buffer)
if err := c.writeAtom(w, in); err != nil {
if !shouldFail {
t.Error(in, err)
}
} else if shouldFail {
t.Error("err == nil (%v)", in)
} else if v, err := c.Read(w); err != nil {
t.Error(in, err)
} else if l := w.Len(); l != 0 {
t.Errorf("%v: buffer len %d", in, l)
} else if v != in {
t.Errorf("expected %v, got %v", in, v)
}
}
test(Atom(""), false)
test(Atom(bytes.Repeat([]byte{'a'}, math.MaxUint8)), false)
test(Atom(bytes.Repeat([]byte{'a'}, math.MaxUint8+1)), false)
test(Atom(bytes.Repeat([]byte{'a'}, math.MaxUint16)), false)
test(Atom(bytes.Repeat([]byte{'a'}, math.MaxUint16+1)), true)
}
func TestWriteBinary(t *testing.T) {
c := new(Context)
test := func(in []byte) {
w := new(bytes.Buffer)
if err := c.writeBinary(w, in); err != nil {
t.Error(in, err)
} else if v, err := c.Read(w); err != nil {
t.Error(in, err)
} else if l := w.Len(); l != 0 {
t.Errorf("%v: buffer len %d", in, l)
} else if bytes.Compare(v.([]byte), in) != 0 {
t.Errorf("expected %v, got %v", in, v)
}
}
test([]byte{})
test(bytes.Repeat([]byte{231}, 65535))
test(bytes.Repeat([]byte{123}, 65536))
}
func TestWriteBool(t *testing.T) {
c := new(Context)
test := func(in bool) {
w := new(bytes.Buffer)
if err := c.writeBool(w, in); err != nil {
t.Error(in, err)
} else if v, err := c.Read(w); err != nil {
t.Error(in, err)
} else if l := w.Len(); l != 0 {
t.Errorf("%v: buffer len %d", in, l)
} else if v != in {
t.Errorf("expected %v, got %v", in, v)
}
}
test(true)
test(false)
}
func TestWriteFloat(t *testing.T) {
c := new(Context)
test := func(in float64) {
w := new(bytes.Buffer)
if err := c.writeFloat(w, in); err != nil {
t.Error(in, err)
} else if v, err := c.Read(w); err != nil {
t.Error(in, err)
} else if l := w.Len(); l != 0 {
t.Errorf("%v: buffer len %d", in, l)
} else if v != in {
t.Errorf("expected %v, got %v", in, v)
}
}
test(0.0)
test(-12345.6789)
test(math.SmallestNonzeroFloat64)
test(math.MaxFloat64)
}
func TestWriteInt(t *testing.T) {
c := new(Context)
test := func(in int64) {
w := new(bytes.Buffer)
if err := c.writeInt(w, in); err != nil {
t.Error(in, err)
} else if v, err := c.Read(w); err != nil {
t.Error(in, err)
} else if l := w.Len(); l != 0 {
t.Errorf("%v: buffer len %d", in, l)
} else if reflect.ValueOf(v).Int() != in {
t.Errorf("expected %v, got %v", in, v)
}
}
test(0)
test(-1)
test(math.MaxInt8)
test(math.MaxInt8 + 1)
test(math.MaxInt32)
test(math.MaxInt32 + 1)
test(math.MinInt32)
test(math.MinInt32 - 1)
test(math.MinInt64)
test(math.MaxInt64)
}
func TestWriteUint(t *testing.T) {
c := new(Context)
test := func(in uint64) {
w := new(bytes.Buffer)
if err := c.writeUint(w, in); err != nil {
t.Error(in, err)
} else if v, err := c.Read(w); err != nil {
t.Error(in, err)
} else if l := w.Len(); l != 0 {
t.Errorf("%v: buffer len %d", in, l)
} else {
var uv uint64
switch v := v.(type) {
case int:
uv = uint64(v)
case int64:
uv = uint64(v)
case *big.Int:
uv = v.Uint64()
}
if uv != in {
t.Errorf("expected %v, got %v", in, v)
}
}
}
test(0)
test(math.MaxUint8)
test(math.MaxUint8 + 1)
test(math.MaxUint32)
test(math.MaxUint32 + 1)
test(math.MaxUint64)
}
func TestWritePid(t *testing.T) {
c := new(Context)
test := func(in Pid) {
w := new(bytes.Buffer)
if err := c.writePid(w, in); err != nil {
t.Error(in, err)
} else if v, err := c.Read(w); err != nil {
t.Error(in, err)
} else if l := w.Len(); l != 0 {
t.Errorf("%v: buffer len %d", in, l)
} else if v != in {
t.Errorf("expected %v, got %v", in, v)
}
}
test(Pid{Atom("omg@lol"), 38, 0, 3})
test(Pid{Atom("self@localhost"), 32, 1, 9})
}
func TestWriteString(t *testing.T) {
c := new(Context)
test := func(in string, shouldFail bool) {
w := new(bytes.Buffer)
if err := c.writeString(w, in); err != nil {
if !shouldFail {
t.Error(in, err)
}
} else if shouldFail {
t.Error("err == nil (%v)", in)
} else if v, err := c.Read(w); err != nil {
t.Error(in, err)
} else if l := w.Len(); l != 0 {
t.Errorf("%v: buffer len %d", in, l)
} else if v != in {
t.Errorf("expected %v, got %v", in, v)
}
}
test(string(bytes.Repeat([]byte{'a'}, math.MaxUint16)), false)
test("", false)
test(string(bytes.Repeat([]byte{'a'}, math.MaxUint16+1)), true)
}
func TestWriteTerm(t *testing.T) {
c := new(Context)
type s1 struct {
L []interface{}
F float64
}
type s2 struct {
Atom
S string
I int
S1 s1
B byte
}
in := s2{
Atom("lol"),
"omg",
13666,
s1{
[]interface{}{
256,
"1",
13.0,
},
13.13,
},
1,
}
w := new(bytes.Buffer)
if err := c.Write(w, in); err != nil {
t.Error(in, err)
} else {
if term, err := c.Read(w); err != nil {
t.Error(in, err)
} else if l := w.Len(); l != 0 {
t.Errorf("%v: buffer len %d", in, l)
} else if err := c.Write(w, term); err != nil {
t.Error(term, err)
} else if term, err := c.Read(w); err != nil {
t.Error(in, err)
} else if l := w.Len(); l != 0 {
t.Errorf("%v: buffer len %d", term, l)
}
}
}