-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbinmani_test.go
121 lines (101 loc) · 3.05 KB
/
binmani_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
package binmani
import "testing"
func TestGetMask(t *testing.T) {
got := GetMask(4, 1)
if got != 0b00010000 {
t.Errorf("GetMask(4, 1) = %016b, want 0b00010000", got)
}
got = GetMask(2, 3)
if got != 0b00011100 {
t.Errorf("GetMask(2, 3) = %016b, want 0b00011100", got)
}
got = GetMask(0, 8)
if got != 0b11111111 {
t.Errorf("GetMask(0, 8) = %016b, want 0b11111111", got)
}
got = GetMask(3, 8)
if got != 0b0000011111111000 {
t.Errorf("GetMask(3, 8) = %016b, want 0b0000011111111000", got)
}
}
func TestReadFrom(t *testing.T) {
in := uint16(0b0101011101100110)
got := ReadFrom(in, 5, 1)
if got != 0b00000001 {
t.Errorf("ReadFrom(%016b, 5, 1) = %016b, want 0b00000001", in, got)
}
got = ReadFrom(in, 5, 3)
if got != 0b00000011 {
t.Errorf("ReadFrom(%016b, 5, 3) = %016b, want 0b00000011", in, got)
}
got = ReadFrom(in, 0, 16)
if got != in {
t.Errorf("ReadFrom(%016b, 0, 16) = %016b, want %016b", in, got, in)
}
}
func TestWriteTo(t *testing.T) {
in := uint16(0b0101011101100110)
got := WriteTo(in, 7, 1, 0)
if got != in {
t.Errorf("WriteTo(%016b, 7, 1, 0) = %016b, want %016b", in, got, in)
}
got = WriteTo(in, 7, 1, 1)
if got != 0b0101011111100110 {
t.Errorf("WriteTo(%016b, 7, 1, 1) = %016b, want 0b0101011111100110", in, got)
}
got = WriteTo(in, 7, 3, 0b00000101)
if got != 0b0101011011100110 {
t.Errorf("WriteTo(%016b, 7, 3, 0b00000101) = %016b, want 0b0101011011100110", in, got)
}
}
func TestBytesToBits(t *testing.T) {
in := []byte{}
expect := []uint8{}
got := BytesToBits(in)
if !arrsEqual(*got, expect) {
t.Errorf("BytesToBits(%v) = %v, want %v", in, *got, expect)
}
in = []byte{0x61, 0x62, 0x63, 0x64}
expect = []uint8{0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0}
got = BytesToBits(in)
if !arrsEqual(*got, expect) {
t.Errorf("BytesToBits(%v) = %v, want %v", in, *got, expect)
}
}
func TestBitsToBytes(t *testing.T) {
in := []uint8{}
expect := []byte{}
got := BitsToBytes(in, true)
if !arrsEqual(*got, expect) {
t.Errorf("BitsToBytes(%v, true) = %v, want %v", in, *got, expect)
}
in = []uint8{0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0}
expect = []byte{0x61, 0x62, 0x63, 0x64}
got = BitsToBytes(in, true)
if !arrsEqual(*got, expect) {
t.Errorf("BitsToBytes(%v, true) = %v, want %v", in, *got, expect)
}
in = []uint8{0, 1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1}
expect = []byte{0x61, 0x62, 0x63, 0x64}
got = BitsToBytes(in, false)
if !arrsEqual(*got, expect) {
t.Errorf("BitsToBytes(%v, false) = %v, want %v", in, *got, expect)
}
in = []uint8{1, 1, 0, 0, 0, 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 1, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 1, 0, 0}
expect = []byte{0x61, 0x62, 0x63, 0x64}
got = BitsToBytes(in, true)
if !arrsEqual(*got, expect) {
t.Errorf("BitsToBytes(%v, true) = %v, want %v", in, *got, expect)
}
}
func arrsEqual(a, b []byte) bool {
if len(a) != len(b) {
return false
}
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}