-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathpacker_test.go
115 lines (99 loc) · 3.16 KB
/
packer_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
package binpacker
import (
"bytes"
"testing"
"math"
"github.com/stretchr/testify/assert"
"encoding/binary"
)
func TestPushByte(t *testing.T) {
b := new(bytes.Buffer)
p := NewPacker(binary.BigEndian, b)
p.PushByte(0x01)
assert.Equal(t, p.Error(), nil, "Has error.")
assert.Equal(t, b.Bytes(), []byte{1}, "byte error.")
}
func TestPushBytes(t *testing.T) {
b := new(bytes.Buffer)
p := NewPacker(binary.BigEndian, b)
p.PushBytes([]byte{0x01, 0x002})
assert.Equal(t, p.Error(), nil, "Has error.")
assert.Equal(t, b.Bytes(), []byte{0x01, 0x02}, "bytes error.")
}
func TestPushUint8(t *testing.T) {
b := new(bytes.Buffer)
p := NewPacker(binary.BigEndian, b)
p.PushUint8(1)
assert.Equal(t, p.Error(), nil, "Has error.")
assert.Equal(t, b.Bytes(), []byte{1}, "uint8 error.")
}
func TestPushUint16(t *testing.T) {
b := new(bytes.Buffer)
p := NewPacker(binary.BigEndian, b)
p.PushUint16(1)
assert.Equal(t, p.Error(), nil, "Has error.")
assert.Equal(t, b.Bytes(), []byte{0, 1}, "uint16 error.")
}
func TestPushInt16(t *testing.T) {
b := new(bytes.Buffer)
p := NewPacker(binary.BigEndian, b)
p.PushInt16(-1)
assert.Equal(t, p.Error(), nil, "Has error.")
assert.Equal(t, b.Bytes(), []byte{255, 255}, "uint16 error.") // -1 eq 255 255
}
func TestPushUint32(t *testing.T) {
b := new(bytes.Buffer)
p := NewPacker(binary.BigEndian, b)
p.PushUint32(1)
assert.Equal(t, p.Error(), nil, "Has error.")
assert.Equal(t, b.Bytes(), []byte{0, 0, 0, 1}, "uint32 error.")
}
func TestPushInt32(t *testing.T) {
b := new(bytes.Buffer)
p := NewPacker(binary.BigEndian, b)
p.PushInt32(-1)
assert.Equal(t, p.Error(), nil, "Has error.")
assert.Equal(t, b.Bytes(), []byte{255, 255, 255, 255}, "int32 error.")
}
func TestPushUint64(t *testing.T) {
b := new(bytes.Buffer)
p := NewPacker(binary.BigEndian, b)
p.PushUint64(1)
assert.Equal(t, p.Error(), nil, "Has error.")
assert.Equal(t, b.Bytes(), []byte{0, 0, 0, 0, 0, 0, 0, 1}, "uint64 error.")
}
func TestPushInt64(t *testing.T) {
b := new(bytes.Buffer)
p := NewPacker(binary.BigEndian, b)
p.PushInt64(-1)
assert.Equal(t, p.Error(), nil, "Has error.")
assert.Equal(t, b.Bytes(), []byte{255, 255, 255, 255, 255, 255, 255, 255}, "int64 error.")
}
func TestPushFloat32(t *testing.T) {
b := new(bytes.Buffer)
p := NewPacker(binary.BigEndian, b)
p.PushFloat32(math.SmallestNonzeroFloat32)
assert.Equal(t, p.Error(), nil, "Has error.")
assert.Equal(t, b.Bytes(), []byte{0, 0, 0, 1}, "float32 error.")
}
func TestPushFloat64(t *testing.T) {
b := new(bytes.Buffer)
p := NewPacker(binary.BigEndian, b)
p.PushFloat64(math.SmallestNonzeroFloat64)
assert.Equal(t, p.Error(), nil, "Has error.")
assert.Equal(t, b.Bytes(), []byte{0, 0, 0, 0, 0, 0, 0, 1}, "float64 error.")
}
func TestPushString(t *testing.T) {
b := new(bytes.Buffer)
p := NewPacker(binary.BigEndian, b)
p.PushString("Hi")
assert.Equal(t, p.Error(), nil, "Has error.")
assert.Equal(t, b.Bytes(), []byte{'H', 'i'}, "string error.")
}
func TestCombinedPush(t *testing.T) {
b := new(bytes.Buffer)
p := NewPacker(binary.BigEndian, b)
p.PushUint16(1).PushString("Hi")
assert.Equal(t, p.Error(), nil, "Has error.")
assert.Equal(t, b.Bytes(), []byte{0, 1, 'H', 'i'}, "combine push error.")
}