-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinarytransforms_test.go
More file actions
202 lines (194 loc) · 5.15 KB
/
binarytransforms_test.go
File metadata and controls
202 lines (194 loc) · 5.15 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
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
// Copyright (c) 2020 Alec Randazzo
package binarytransforms
import (
"fmt"
"reflect"
"testing"
)
func TestLittleEndianBinaryToInt64(t *testing.T) {
type args struct {
inBytes []byte
}
tests := []struct {
name string
args args
wantOutInt64 int64
wantErr bool
}{
{
name: "Testing with a random byte slice.",
args: args{inBytes: []byte{20, 21, 255}},
wantOutInt64: -60140,
wantErr: false,
},
{
name: "Testing with null byte slice.",
args: args{inBytes: []byte{}},
wantOutInt64: 0,
wantErr: true,
},
{
name: "Testing with byte slice larger than 8 bytes.",
args: args{inBytes: []byte{20, 21, 22, 23, 24, 25, 26, 27, 28, 29}},
wantOutInt64: 0,
wantErr: true,
},
{
name: "Testing with byte slice with 8 bytes.",
args: args{inBytes: []byte{1}},
wantOutInt64: 1,
wantErr: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
gotOutInt64, err := LittleEndianBinaryToInt64(tt.args.inBytes)
fmt.Println(gotOutInt64, tt.wantOutInt64)
if gotOutInt64 != tt.wantOutInt64 || (err != nil) != tt.wantErr {
t.Errorf("got = %v, want %v", gotOutInt64, tt.wantOutInt64)
}
})
}
}
func TestLittleEndianBinaryToUInt16(t *testing.T) {
type args struct {
inBytes []byte
}
tests := []struct {
name string
args args
wantOutUInt16 uint16
wantErr bool
}{
{
name: "Testing with a random byte slice.",
args: args{inBytes: []byte{246, 201}},
wantOutUInt16: 51702,
wantErr: false,
},
{
name: "Testing with null byte slice.",
args: args{inBytes: []byte{}},
wantOutUInt16: 0,
wantErr: true,
},
{
name: "Testing with byte slice larger than 8 bytes.",
args: args{inBytes: []byte{20, 20, 20, 20, 20, 20, 20, 20, 20, 20}},
wantOutUInt16: 0,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotOutUInt16, err := LittleEndianBinaryToUInt16(tt.args.inBytes); !reflect.DeepEqual(gotOutUInt16, tt.wantOutUInt16) || (err != nil) != tt.wantErr {
t.Errorf("got = %v, want %v", gotOutUInt16, tt.wantOutUInt16)
}
})
}
}
func TestLittleEndianBinaryToUInt32(t *testing.T) {
type args struct {
inBytes []byte
}
tests := []struct {
name string
args args
wantOutUInt32 uint32
wantErr bool
}{
{
name: "Testing with a random byte slice.",
args: args{inBytes: []byte{246, 201, 201, 0}},
wantOutUInt32: 13224438,
wantErr: false,
},
{
name: "Testing with null byte slice.",
args: args{inBytes: []byte{}},
wantOutUInt32: 0,
wantErr: true,
},
{
name: "Testing with byte slice larger than 8 bytes.",
args: args{inBytes: []byte{20, 20, 20, 20, 20, 20, 20, 20, 20, 20}},
wantOutUInt32: 0,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotOutUInt32, err := LittleEndianBinaryToUInt32(tt.args.inBytes); !reflect.DeepEqual(gotOutUInt32, tt.wantOutUInt32) || (err != nil) != tt.wantErr {
t.Errorf("got = %v, want %v", gotOutUInt32, tt.wantOutUInt32)
}
})
}
}
func TestLittleEndianBinaryToUInt64(t *testing.T) {
type args struct {
inBytes []byte
}
tests := []struct {
name string
args args
wantOutUInt64 uint64
wantErr bool
}{
{
name: "Testing with a random byte slice.",
args: args{inBytes: []byte{20, 21, 255}},
wantOutUInt64: 16717076,
wantErr: false,
},
{
name: "Testing with null byte slice.",
args: args{inBytes: []byte{}},
wantOutUInt64: 0,
wantErr: true,
},
{
name: "Testing with byte slice larger than 8 bytes.",
args: args{inBytes: []byte{20, 20, 20, 20, 20, 20, 20, 20, 20, 20}},
wantOutUInt64: 0,
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotOutUInt64, err := LittleEndianBinaryToUInt64(tt.args.inBytes); !reflect.DeepEqual(gotOutUInt64, tt.wantOutUInt64) || (err != nil) != tt.wantErr {
t.Errorf("got = %v, want %v", gotOutUInt64, tt.wantOutUInt64)
}
})
}
}
func TestUnicodeBytesToASCII(t *testing.T) {
type args struct {
unicodeBytes []byte
}
tests := []struct {
name string
args args
wantASCIIString string
wantErr bool
}{
{
name: "Testing with a random byte slice.",
args: args{unicodeBytes: []byte{116, 0, 101, 0, 115, 0, 116}},
wantASCIIString: "test",
wantErr: false,
},
{
name: "Testing with null byte slice.",
args: args{unicodeBytes: []byte{}},
wantASCIIString: "",
wantErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if gotASCIIString, err := UnicodeBytesToASCII(tt.args.unicodeBytes); !reflect.DeepEqual(gotASCIIString, tt.wantASCIIString) || (err != nil) != tt.wantErr {
t.Errorf("got = %v, want %v", gotASCIIString, tt.wantASCIIString)
}
})
}
}