-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperation_test.go
366 lines (345 loc) · 7.92 KB
/
operation_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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package bitwiser
import (
"testing"
)
func TestSimpleBitwiseOp_And(t *testing.T) {
op := NewBitOperation()
//
inOut := [][]byte{
{0x10, 0x10, 0x10},
{0x11, 0x10, 0x10},
}
//
for k, v := range inOut {
if op.And(v[0], v[1]) != v[2] {
t.Errorf("Test#%d: %s with error", k, "op.And")
}
}
}
func TestSimpleBitwiseOp_Or(t *testing.T) {
op := NewBitOperation()
//
inOut := [][]byte{
{0x10, 0x10, 0x10},
{0x11, 0x10, 0x11},
}
//
for k, v := range inOut {
if op.Or(v[0], v[1]) != v[2] {
t.Errorf("Test#%d: %s with error", k, "op.Or")
}
}
}
func TestSimpleBitwiseOp_Xor(t *testing.T) {
op := NewBitOperation()
//
inOut := [][]byte{
{0x10, 0x10, 0x00},
{0x11, 0x10, 0x01},
}
//
for k, v := range inOut {
if op.Xor(v[0], v[1]) != v[2] {
t.Errorf("Test#%d: %s with error", k, "op.Xor")
}
}
}
func TestSimpleBitwiseOp_Not(t *testing.T) {
op := NewBitOperation()
//
inOut := [][]byte{
{0x10, 0xEF},
{0x11, 0xEE},
}
//
for k, v := range inOut {
if op.Not(v[0]) != v[1] {
t.Errorf("Test#%d: %s with error", k, "op.Not")
}
}
}
func TestSimpleBitwiseOp_ShiftLeft(t *testing.T) {
op := NewBitOperation()
//
inOut := []struct {
a byte
b uint
c byte
}{
{0x10, 2, 0x40}, //0001 0000 --> (2) --> 0100 0000
{0x11, 1, 0x22}, //0001 0001 --> (1) --> 0010 0010
{0x11, 10, 0x00}, //0001 0001 --> (1) --> 0000 0000
}
//
for k, v := range inOut {
if op.ShiftLeft(v.a, v.b) != v.c {
t.Errorf("Test#%d: %s with error", k, "op.ShiftLeft")
}
}
}
func TestSimpleBitwiseOp_ShiftRight(t *testing.T) {
op := NewBitOperation()
//
inOut := []struct {
a byte
b uint
c byte
}{
{0x10, 2, 0x04}, //0001 0000 --> (2) --> 0000 0100
{0x11, 1, 0x08}, //0001 0001 --> (1) --> 0000 1000
}
//
for k, v := range inOut {
if op.ShiftRight(v.a, v.b) != v.c {
t.Errorf("Test#%d: %s with error", k, "op.ShiftRight")
}
}
}
func TestBytewiseOp_And(t *testing.T) {
op := NewByteOperation()
//
inOut := []struct {
a Bytes
b Bytes
c Bytes
}{
{
Bytes{[]byte{0x10}, 1},
Bytes{[]byte{0x10}, 1},
Bytes{[]byte{0x10}, 1}},
{
Bytes{[]byte{0x11}, 1},
Bytes{[]byte{0x10}, 1},
Bytes{[]byte{0x10}, 1}},
}
//
for k, v := range inOut {
//
r, e := op.And(v.a, v.b)
if e != nil {
t.Errorf("Test#%d: %s with error %s", k, "op.And", e.Error())
continue
}
//
if r.ToInt() != v.c.ToInt() {
t.Errorf("Test#%d: %s with error. Expected: 0x%x. Result: 0x%x", k, "op.And", v.c.b, r.b)
} else {
t.Logf("Test#%d: %s with success. Expected: 0x%x. Result: 0x%x", k, "op.And", v.c.b, r.b)
}
}
}
func TestBytewiseOp_Or(t *testing.T) {
op := NewByteOperation()
//
inOut := []struct {
a Bytes
b Bytes
c Bytes
}{
{
Bytes{[]byte{0x10}, 1},
Bytes{[]byte{0x10}, 1},
Bytes{[]byte{0x10}, 1}},
{
Bytes{[]byte{0x11}, 1},
Bytes{[]byte{0x10}, 1},
Bytes{[]byte{0x11}, 1}},
}
//
for k, v := range inOut {
//
r, e := op.Or(v.a, v.b)
if e != nil {
t.Errorf("Test#%d: %s with error %s", k, "op.Or", e.Error())
continue
}
//
if r.ToInt() != v.c.ToInt() {
t.Errorf("Test#%d: %s with error. Expected: 0x%x. Result: 0x%x", k, "op.Or", v.c.b, r.b)
} else {
t.Logf("Test#%d: %s with success. Expected: 0x%x. Result: 0x%x", k, "op.Or", v.c.b, r.b)
}
}
}
func TestBytewiseOp_Xor(t *testing.T) {
op := NewByteOperation()
//
inOut := []struct {
a Bytes
b Bytes
c Bytes
}{
{
Bytes{[]byte{0x10}, 1},
Bytes{[]byte{0x10}, 1},
Bytes{[]byte{0x00}, 1}},
{
Bytes{[]byte{0x11}, 1},
Bytes{[]byte{0x10}, 1},
Bytes{[]byte{0x01}, 1}},
}
//
for k, v := range inOut {
//
r, e := op.Xor(v.a, v.b)
if e != nil {
t.Errorf("Test#%d: %s with error %s", k, "op.Xor", e.Error())
continue
}
//
if r.ToInt() != v.c.ToInt() {
t.Errorf("Test#%d: %s with error. Expected: 0x%x. Result: 0x%x", k, "op.Xor", v.c.b, r.b)
} else {
t.Logf("Test#%d: %s with success. Expected: 0x%x. Result: 0x%x", k, "op.Xor", v.c.b, r.b)
}
}
}
func TestBytewiseOp_Not(t *testing.T) {
op := NewByteOperation()
//
inOut := []struct {
a Bytes
c Bytes
}{
{
Bytes{[]byte{0x10}, 1},
Bytes{[]byte{0xEF}, 1}},
{
Bytes{[]byte{0x11}, 1},
Bytes{[]byte{0xEE}, 1}},
}
//
for k, v := range inOut {
//
r, e := op.Not(v.a)
if e != nil {
t.Errorf("Test#%d: %s with error %s", k, "op.Not", e.Error())
continue
}
//
if r.ToInt() != v.c.ToInt() {
t.Errorf("Test#%d: %s with error. Expected: 0x%x. Result: 0x%x", k, "op.Not", v.c.b, r.b)
} else {
t.Logf("Test#%d: %s with success. Expected: 0x%x. Result: 0x%x", k, "op.Not", v.c.b, r.b)
}
}
}
func TestBytewiseOp_ShiftLeft(t *testing.T) {
op := NewByteOperation()
//
inOut := []struct {
a Bytes
b uint
c Bytes
}{
{
Bytes{[]byte{0x01}, 1}, 1,
Bytes{[]byte{0x02}, 1}}, //0000 0001 --> (1) --> 0000 0010
{
Bytes{[]byte{0x00,0xF1}, 2}, 1,
Bytes{[]byte{0x01, 0xe2}, 2}}, //0000 0000 1111 0001 --> (1) --> 0000 0001 1110 0010
{
Bytes{[]byte{0xf0,0xf9}, 2}, 1,
Bytes{[]byte{0xe1, 0xf2}, 2}}, //1111 0000 1111 1001 --> (1) --> 1110 0001 1111 0010
{
Bytes{[]byte{0x2d,0xe9,0x2f,0xfe}, 4}, 2,
Bytes{[]byte{0xb7,0xa4,0xbf,0xf8}, 4}}, //0x2DE92FFE --> (2) --> 0xB7A4BFF8
{
Bytes{[]byte{0xff,0xe9,0x2f,0xfe}, 4}, 3,
Bytes{[]byte{0xff,0x49,0x7f,0xf0}, 4}}, //0xffE92FFE --> (2) --> 0xFF497FF0
}
//
for k, v := range inOut {
//
r, e := op.ShiftLeft(v.a, v.b)
if e != nil {
t.Errorf("Test#%d: %s with error %s", k, "op.ShiftLeft", e.Error())
continue
}
//
if r.ToInt() != v.c.ToInt() {
t.Errorf("Test#%d: %s with error. Expected: 0x%x. Result: 0x%x", k, "op.ShiftLeft", v.c.b, r.b)
} else {
t.Logf("Test#%d: %s with success. Expected: 0x%x. Result: 0x%x", k, "op.ShiftLeft", v.c.b, r.b)
}
}
}
func TestBytewiseOp_ShiftRight(t *testing.T) {
op := NewByteOperation()
//
inOut := []struct {
a Bytes
b uint
c Bytes
}{
{
Bytes{[]byte{0x01}, 1}, 1,
Bytes{[]byte{0x00}, 1}}, //0000 0001 --> (1) --> 0000 0010
{
Bytes{[]byte{0x00,0xF1}, 2}, 1,
Bytes{[]byte{0x00, 0x78}, 2}}, //0000 0000 1111 0001 --> (1) --> 0000 0001 1110 0010
{
Bytes{[]byte{0xf0,0xf9}, 2}, 1,
Bytes{[]byte{0x78, 0x7c}, 2}}, //1111 0000 1111 1001 --> (1) --> 1110 0001 1111 0010
{
Bytes{[]byte{0x2d,0xe9,0x2f,0xfe}, 4}, 2,
Bytes{[]byte{0x0b,0x7a,0x4b,0xff}, 4}}, //0x2DE92FFE --> (2) --> 0xB7A4BFF
{
Bytes{[]byte{0xff,0xe9,0x2f,0xfe}, 4}, 3,
Bytes{[]byte{0x1f,0xfd,0x25,0xff}, 4}}, //0xffE92FFE --> (3) --> 0x1FFD25FF
}
//
for k, v := range inOut {
//
r, e := op.ShiftRight(v.a, v.b)
if e != nil {
t.Errorf("Test#%d: %s with error %s", k, "op.ShiftRight", e.Error())
continue
}
//
if r.ToInt() != v.c.ToInt() {
t.Errorf("Test#%d: %s with error. Expected: 0x%x. Result: 0x%x", k, "op.ShiftRight", v.c.b, r.b)
} else {
t.Logf("Test#%d: %s with success. Expected: 0x%x. Result: 0x%x", k, "op.ShiftRight", v.c.b, r.b)
}
}
}
func TestParseFromBits(t *testing.T) {
//
inOut := []struct {
a string
b int
c error
}{
{"011100", 28, nil},
{"0 1110 1010 1101 1001", 60121, nil},
{"0 1110 1010 1101 1001a", -1, ErrBadFormat},
{"01-110 1110 1010 1101 1001", -1, ErrBadFormat},
{"01 110 1110 1010 1101 1001", 977625, nil}, //auto-fix for 0000 1110 1110 1010 1101 1001
}
//
for k, v := range inOut {
//
b, err := ParseFromBits(v.a)
if v.c == nil && err != nil {
t.Errorf("%s", err.Error())
continue
}
if v.c != nil {
if err == nil {
t.Errorf("Expected an error: %s, but not occured", v.c.Error())
continue
} else if v.c.Error() != err.Error(){
t.Errorf("Expected an error: %s, but occured %s", v.c.Error(), err.Error())
continue
}
t.Logf("Test#%d: %s with success (negative). Expected: %s. Result: %s", k, "ParseFromBits", v.c, err.Error())
} else {
if b.ToInt() != v.b {
t.Errorf("Number inválid. Expected %d. Result: %d", v.b, b.ToInt())
} else {
t.Logf("Test#%d: %s with success. Expected: 0x%x. Result: 0x%x", k, "ParseFromBits", v.b, b.ToInt())
}
}
}
}