-
Notifications
You must be signed in to change notification settings - Fork 0
/
Reader_test.go
179 lines (143 loc) · 3.82 KB
/
Reader_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
package binary
import (
"bytes"
"fmt"
"testing"
)
func getReader(p []byte) *Reader {
return &Reader{
off: 0,
n: len(p),
buf: p,
}
}
func TestReader_ReadNumber(t *testing.T) {
wrongSize := 9
normalSize := 4
checkingNum := int32(0x7FFFFFFF)
numBytes := []byte{0xFF, 0xFF, 0xFF, 0x7F}
bs := getReader(numBytes)
_, err := bs.ReadNumber(wrongSize)
if err == nil {
t.Errorf("There must be an error with n = %d", wrongSize)
bs.off = 0
return
}
num, err := bs.ReadNumber(normalSize)
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if int32(num) != checkingNum {
t.Errorf("Wrong number. Expected %x, got %x", checkingNum, num)
}
}
func TestReader_ReadByte(t *testing.T) {
checkingNum := byte(0xFF)
numBytes := []byte{0xFF}
bs := getReader(numBytes)
num, err := bs.ReadByte()
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
return
}
if num != checkingNum {
t.Errorf("Wrong number. Expected %x, got %x", checkingNum, num)
}
}
func TestReader_ReadSignedShort(t *testing.T) {
checkingNum := int16(0x7FAA)
numBytes := []byte{0xAA, 0x7F}
bs := getReader(numBytes)
num, err := bs.ReadSignedShort()
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if num != checkingNum {
t.Errorf("Wrong number. Expected %x, got %x", checkingNum, num)
}
}
func TestReader_ReadUnsignedShort(t *testing.T) {
checkingNum := uint16(0xFFAA)
numBytes := []byte{0xAA, 0xFF}
bs := getReader(numBytes)
num, err := bs.ReadUnsignedShort()
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if num != checkingNum {
t.Errorf("Wrong number. Expected %x, got %x", checkingNum, num)
}
}
func TestReader_ReadSignedInt32(t *testing.T) {
checkingNum := int32(0x7FAABBCC)
numBytes := []byte{0xCC, 0xBB, 0xAA, 0x7F}
bs := getReader(numBytes)
num, err := bs.ReadSignedInt32()
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if num != checkingNum {
t.Errorf("Wrong number. Expected %x, got %x", checkingNum, num)
}
}
func TestReader_ReadUnsignedInt32(t *testing.T) {
checkingNum := uint32(0xFFAABBCC)
numBytes := []byte{0xCC, 0xBB, 0xAA, 0xFF}
bs := getReader(numBytes)
num, err := bs.ReadUnsignedInt32()
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if num != checkingNum {
t.Errorf("Wrong number. Expected %x, got %x", checkingNum, num)
}
}
func TestReader_ReadSignedLong(t *testing.T) {
checkingNum := int64(0x7FAABBCCDDEEFF11)
numBytes := []byte{0x11, 0xFF, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0x7F}
bs := getReader(numBytes)
num, err := bs.ReadSignedLong()
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if num != checkingNum {
t.Errorf("Wrong number. Expected %x, got %x", checkingNum, num)
}
}
func TestReader_ReadUnsignedLong(t *testing.T) {
checkingNum := uint64(0xFFAABBCCDDEE1122)
numBytes := []byte{0x22, 0x11, 0xEE, 0xDD, 0xCC, 0xBB, 0xAA, 0xFF}
bs := getReader(numBytes)
num, err := bs.ReadUnsignedLong()
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if num != checkingNum {
t.Errorf("Wrong number. Expected %x, got %x", checkingNum, num)
}
}
func TestReader_ReadByteArray(t *testing.T) {
byteAr := []byte{0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF}
byteLen := byte(len(byteAr))
wrongByteLen := byteLen + 1
bs := getReader([]byte{wrongByteLen})
bs.n += int(byteLen)
bs.buf = append(bs.buf, byteAr...)
n, ar, err := bs.ReadByteArray()
if err == nil {
fmt.Println(n, ar)
t.Errorf("There must be an error with wrong byte array size")
bs.off = 0
}
bs.buf[0] = byteLen
n, ar, err = bs.ReadByteArray()
if err != nil {
t.Fatalf("Unexpected error: %s", err.Error())
}
if n != byteLen {
t.Errorf("Wrong size array. Expected %d, got %d", byteLen, n)
}
if !bytes.Equal(byteAr, ar) {
t.Errorf("Wrong byte array. Expected %s, got %s", string(byteAr), string(ar))
}
}