-
Notifications
You must be signed in to change notification settings - Fork 7
/
unsafeslice_test.go
152 lines (133 loc) · 4.1 KB
/
unsafeslice_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
package unsafeslice
import (
"bytes"
"encoding/binary"
"testing"
"github.com/stretchr/testify/require"
)
func TestUnsafeSliceUint64(t *testing.T) {
w := &bytes.Buffer{}
d := []uint64{0xdead, 0xbeef, 0xb334}
binary.Write(w, binary.LittleEndian, d)
v := Uint64SliceFromByteSlice(w.Bytes())
require.Equal(t, d, v)
require.Equal(t, w.Bytes(), ByteSliceFromUint64Slice(d))
}
func TestUnsafeSliceInt64(t *testing.T) {
w := &bytes.Buffer{}
d := []int64{0xdead, 0xbeef, 0xb334}
binary.Write(w, binary.LittleEndian, d)
v := Int64SliceFromByteSlice(w.Bytes())
require.Equal(t, d, v)
require.Equal(t, w.Bytes(), ByteSliceFromInt64Slice(d))
}
func TestUnsafeSliceUint32(t *testing.T) {
w := &bytes.Buffer{}
d := []uint32{0xdead, 0xbeef, 0xb334}
binary.Write(w, binary.LittleEndian, d)
v := Uint32SliceFromByteSlice(w.Bytes())
require.Equal(t, d, v)
require.Equal(t, w.Bytes(), ByteSliceFromUint32Slice(d))
}
func TestUnsafeSliceInt32(t *testing.T) {
w := &bytes.Buffer{}
d := []int32{0xdead, 0xbeef, 0xb334}
binary.Write(w, binary.LittleEndian, d)
v := Int32SliceFromByteSlice(w.Bytes())
require.Equal(t, d, v)
require.Equal(t, w.Bytes(), ByteSliceFromInt32Slice(d))
}
func TestUnsafeSliceUint16(t *testing.T) {
w := &bytes.Buffer{}
d := []uint16{0xdead, 0xbeef, 0xb334}
binary.Write(w, binary.LittleEndian, d)
v := Uint16SliceFromByteSlice(w.Bytes())
require.Equal(t, d, v)
require.Equal(t, w.Bytes(), ByteSliceFromUint16Slice(d))
}
func TestUnsafeSliceInt16(t *testing.T) {
w := &bytes.Buffer{}
d := []int16{0xde, 0xad, 0xbe, 0xef, 0xb3, 0x34}
binary.Write(w, binary.LittleEndian, d)
v := Int16SliceFromByteSlice(w.Bytes())
require.Equal(t, d, v)
require.Equal(t, w.Bytes(), ByteSliceFromInt16Slice(d))
}
func TestUnsafeSliceUint8(t *testing.T) {
w := &bytes.Buffer{}
d := []uint8{0xde, 0xad, 0xbe, 0xef, 0xb3, 0x34}
binary.Write(w, binary.LittleEndian, d)
v := Uint8SliceFromByteSlice(w.Bytes())
require.Equal(t, d, v)
require.Equal(t, w.Bytes(), ByteSliceFromUint8Slice(d))
}
func TestUnsafeSliceInt8(t *testing.T) {
w := &bytes.Buffer{}
d := []int8{0xd, 0xe, 0xa, 0xd, 0xb, 0xe, 0xe, 0xf, 0xb, 0x3, 0x3, 0x4}
binary.Write(w, binary.LittleEndian, d)
v := Int8SliceFromByteSlice(w.Bytes())
require.Equal(t, d, v)
require.Equal(t, w.Bytes(), ByteSliceFromInt8Slice(d))
}
type Struct struct {
A uint8
B uint32
}
func makeTestStructBuffer() []byte {
w := &bytes.Buffer{}
a := &Struct{0xab, 0xdead}
b := &Struct{0xce, 0xbeef}
// Write struct values with padding
binary.Write(w, binary.LittleEndian, a.A)
w.Write([]byte{0, 0, 0})
binary.Write(w, binary.LittleEndian, a.B)
binary.Write(w, binary.LittleEndian, b.A)
w.Write([]byte{0, 0, 0})
binary.Write(w, binary.LittleEndian, b.B)
return w.Bytes()
}
func TestUnsafeSliceStruct(t *testing.T) {
var v []Struct
b := makeTestStructBuffer()
require.Nil(t, v)
StructSliceFromByteSlice(b, &v)
require.NotNil(t, v)
require.Equal(t, len(v), 2)
require.Equal(t, v[0].A, uint8(0xab))
require.Equal(t, v[0].B, uint32(0xdead))
require.Equal(t, v[1].A, uint8(0xce))
require.Equal(t, v[1].B, uint32(0xbeef))
}
func TestByteSliceFromStructSlice(t *testing.T) {
a := []Struct{
Struct{0xab, 0xdead},
Struct{0xce, 0xbeef},
}
b := ByteSliceFromStructSlice(a)
require.Equal(t, 16, len(b))
require.Equal(t, makeTestStructBuffer(), b)
require.True(t, bytes.Compare(makeTestStructBuffer(), b) == 0)
b = ByteSliceFromStructSlice([]Struct{})
require.Equal(t, len(b), 0)
}
func TestByteSliceFromString(t *testing.T) {
s := "life after 💀"
b := ByteSliceFromString(s)
require.Equal(t, s, string(b))
}
func TestStringFromByteSlice(t *testing.T) {
b := []byte("life after 💀")
s := StringFromByteSlice(b)
require.Equal(t, s, string(b))
}
func TestEmptyUnsafeSliceUint64(t *testing.T) {
require.Equal(t, []uint64(nil), Uint64SliceFromByteSlice([]byte{}))
require.Equal(t, []uint64(nil), Uint64SliceFromByteSlice(nil))
require.Equal(t, []byte(nil), ByteSliceFromUint64Slice(nil))
require.Equal(t, []byte(nil), ByteSliceFromUint64Slice([]uint64{}))
}
func TestEmptyStringFromByteSlice(t *testing.T) {
var b []byte
s := StringFromByteSlice(b)
require.Equal(t, s, string(b))
}