-
Notifications
You must be signed in to change notification settings - Fork 175
Expand file tree
/
Copy pathbinaryutil_test.go
More file actions
142 lines (134 loc) · 4.22 KB
/
Copy pathbinaryutil_test.go
File metadata and controls
142 lines (134 loc) · 4.22 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
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
package binaryutil
import (
"bytes"
"encoding/binary"
"reflect"
"testing"
"unsafe"
)
func TestNativeByteOrder(t *testing.T) {
// See https://stackoverflow.com/a/53286786
var natEnd binary.ByteOrder
canary := [2]byte{}
*(*uint16)(unsafe.Pointer(&canary[0])) = uint16(0xABCD)
switch canary {
case [2]byte{0xCD, 0xAB}:
natEnd = binary.LittleEndian
case [2]byte{0xAB, 0xCD}:
natEnd = binary.BigEndian
default:
t.Fatalf("unsupported \"mixed\" native endianness")
}
tests := []struct {
name string
expectedv interface{}
marshal func(v interface{}) []byte
unmarshal func(b []byte) interface{}
reference func(v interface{}, b []byte)
}{
{
name: "Uint16",
expectedv: uint16(0x1234),
marshal: func(v interface{}) []byte { return NativeEndian.PutUint16(v.(uint16)) },
unmarshal: func(b []byte) interface{} { return NativeEndian.Uint16(b) },
reference: func(v interface{}, b []byte) { natEnd.PutUint16(b, v.(uint16)) },
},
{
name: "Uint32",
expectedv: uint32(0x12345678),
marshal: func(v interface{}) []byte { return NativeEndian.PutUint32(v.(uint32)) },
unmarshal: func(b []byte) interface{} { return NativeEndian.Uint32(b) },
reference: func(v interface{}, b []byte) { natEnd.PutUint32(b, v.(uint32)) },
},
{
name: "Uint64",
expectedv: uint64(0x1234567801020304),
marshal: func(v interface{}) []byte { return NativeEndian.PutUint64(v.(uint64)) },
unmarshal: func(b []byte) interface{} { return NativeEndian.Uint64(b) },
reference: func(v interface{}, b []byte) { natEnd.PutUint64(b, v.(uint64)) },
},
}
for _, tt := range tests {
expectedb := make([]byte, reflect.TypeOf(tt.expectedv).Size())
tt.reference(tt.expectedv, expectedb)
actualb := tt.marshal(tt.expectedv)
if !bytes.Equal(actualb, expectedb) {
t.Errorf("NativeEndian.Put%s failure, expected: %#v, got: %#v", tt.name, expectedb, actualb)
}
actualv := tt.unmarshal(actualb)
if !reflect.DeepEqual(tt.expectedv, actualv) {
t.Errorf("NativeEndian.%s failure, expected: %#v, got: %#v", tt.name, tt.expectedv, actualv)
}
}
}
func TestBigEndian(t *testing.T) {
tests := []struct {
name string
expected []byte
expectedv interface{}
actual []byte
unmarshal func(b []byte) interface{}
}{
{
name: "Uint16",
expected: []byte{0x12, 0x34},
expectedv: uint16(0x1234),
actual: BigEndian.PutUint16(0x1234),
unmarshal: func(b []byte) interface{} { return BigEndian.Uint16(b) },
},
{
name: "Uint32",
expected: []byte{0x12, 0x34, 0x56, 0x78},
expectedv: uint32(0x12345678),
actual: BigEndian.PutUint32(0x12345678),
unmarshal: func(b []byte) interface{} { return BigEndian.Uint32(b) },
},
{
name: "Uint64",
expected: []byte{0x12, 0x34, 0x56, 0x78, 0x01, 0x02, 0x03, 0x04},
expectedv: uint64(0x1234567801020304),
actual: BigEndian.PutUint64(0x1234567801020304),
unmarshal: func(b []byte) interface{} { return BigEndian.Uint64(b) },
},
}
for _, tt := range tests {
if bytes.Compare(tt.actual, tt.expected) != 0 {
t.Errorf("BigEndian.Put%s failure, expected: %#v, got: %#v", tt.name, tt.expected, tt.actual)
}
if actual := tt.unmarshal(tt.actual); !reflect.DeepEqual(actual, tt.expectedv) {
t.Errorf("BigEndian.%s failure, expected: %#v, got: %#v", tt.name, tt.expectedv, actual)
}
}
}
func TestOtherTypes(t *testing.T) {
tests := []struct {
name string
expected []byte
expectedv interface{}
actual []byte
unmarshal func(b []byte) interface{}
}{
{
name: "Int32",
expected: []byte{0x78, 0x56, 0x34, 0x12},
expectedv: int32(0x12345678),
actual: PutInt32(0x12345678),
unmarshal: func(b []byte) interface{} { return Int32(b) },
},
{
name: "String",
expected: []byte{0x74, 0x65, 0x73, 0x74},
expectedv: "test",
actual: PutString("test"),
unmarshal: func(b []byte) interface{} { return String(b) },
},
}
for _, tt := range tests {
if bytes.Compare(tt.actual, tt.expected) != 0 {
t.Errorf("Put%s failure, expected: %#v, got: %#v", tt.name, tt.expected, tt.actual)
}
if actual := tt.unmarshal(tt.actual); !reflect.DeepEqual(actual, tt.expectedv) {
t.Errorf("%s failure, expected: %#v, got: %#v", tt.name, tt.expectedv, actual)
}
}
}