-
Notifications
You must be signed in to change notification settings - Fork 0
/
be_test.go
73 lines (64 loc) · 2.37 KB
/
be_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
package bytebits
import (
"bytes"
"testing"
"encoding/hex"
)
type rotTest struct{ s []byte; n, d int; z []byte }
var rotTests = []rotTest{
// Zero-byte slices
{ []byte{}, 0, 0, []byte{}},
{ []byte{}, 0, 1, []byte{0}},
{ []byte{}, 1, 0, []byte{}},
{ []byte{}, 1, 5, []byte{0,0,0,0,0}},
{ []byte{}, -1, 0, []byte{}},
{ []byte{}, 7, 0, []byte{}},
{ []byte{}, -7, 0, []byte{}},
{ []byte{}, 8, 0, []byte{}},
{ []byte{}, -8, 0, []byte{}},
{ []byte{}, 15, 0, []byte{}},
{ []byte{}, -15, 0, []byte{}},
// One-byte slices
{ []byte{0xac}, 0, 0, []byte{0xac}},
{ []byte{0xac}, 0, 1, []byte{0xac}},
{ []byte{0xac}, 1, 0, []byte{0x59}},
{ []byte{0xac}, 1, 5, []byte{0x59,0,0,0,0}},
{ []byte{0xac}, -1, 0, []byte{0x56}},
{ []byte{0xac}, 7, 0, []byte{0x56}},
{ []byte{0xac}, -7, 0, []byte{0x59}},
{ []byte{0xac}, 8, 0, []byte{0xac}},
{ []byte{0xac}, -8, 0, []byte{0xac}},
{ []byte{0xac}, 15, 0, []byte{0x56}},
{ []byte{0xac}, -15, 0, []byte{0x59}},
// 4-byte slices
{ []byte{0xde,0xad,0xbe,0xef}, 0, 0, []byte{0xde,0xad,0xbe,0xef} },
{ []byte{0xde,0xad,0xbe,0xef}, 0, 4, []byte{0xde,0xad,0xbe,0xef} },
{ []byte{0xde,0xad,0xbe,0xef}, 0, 5, []byte{0xde,0xad,0xbe,0xef,0} },
{ []byte{0xde,0xad,0xbe,0xef}, 1, 0, []byte{0xbd,0x5b,0x7d,0xdf} },
{ []byte{0xde,0xad,0xbe,0xef}, -1, 0, []byte{0xef,0x56,0xdf,0x77} },
{ []byte{0xde,0xad,0xbe,0xef}, 4, 0, []byte{0xea,0xdb,0xee,0xfd} },
{ []byte{0xde,0xad,0xbe,0xef}, -4, 0, []byte{0xfd,0xea,0xdb,0xee} },
{ []byte{0xde,0xad,0xbe,0xef}, 8, 0, []byte{0xad,0xbe,0xef,0xde} },
{ []byte{0xde,0xad,0xbe,0xef}, -8, 0, []byte{0xef,0xde,0xad,0xbe} },
{ []byte{0xde,0xad,0xbe,0xef}, 9, 0, []byte{0x5b,0x7d,0xdf,0xbd} },
{ []byte{0xde,0xad,0xbe,0xef}, -9, 0, []byte{0x77,0xef,0x56,0xdf} },
{ []byte{0xde,0xad,0xbe,0xef}, 12, 0, []byte{0xdb,0xee,0xfd,0xea} },
{ []byte{0xde,0xad,0xbe,0xef}, -12, 0, []byte{0xee,0xfd,0xea,0xdb} },
{ []byte{0xde,0xad,0xbe,0xef}, 15, 0, []byte{0xdf,0x77,0xef,0x56} },
{ []byte{0xde,0xad,0xbe,0xef}, -15, 0, []byte{0x7d,0xdf,0xbd,0x5b} },
}
func TestRotateLeft(t *testing.T) {
for i, rt := range(rotTests) {
var d []byte
if rt.d > 0 {
d = make([]byte, rt.d)
}
d = BigEndian.RotateLeft(d, rt.s, rt.n)
if bytes.Compare(d, rt.z) != 0 {
t.Errorf("Rotate test %v failed %v << %v: %v != %v",
i, hex.EncodeToString(rt.s), rt.n,
hex.EncodeToString(d),
hex.EncodeToString(rt.z))
}
}
}