-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbit_interpreter_test.go
293 lines (275 loc) · 7.24 KB
/
bit_interpreter_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
package bitstream
import (
"math"
"reflect"
"testing"
)
const (
T = true
F = false
)
func TestBits_AsByte(t *testing.T) {
tests := []struct {
name string
b Bits
want byte
}{
{"empty", Bits{}, 0},
{"4th is T", Bits{F, F, F, T}, 8},
{"8th bit T", Bits{F, F, F, F, F, F, F, F, T}, 0},
{"9th bit T", Bits{F, F, F, F, F, F, F, T}, 128},
{"8 bits T", Bits{T, T, T, T, T, T, T, T}, 255},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.AsByte(); got != tt.want {
t.Errorf("AsByte() = %v, want %v", got, tt.want)
}
})
}
}
func TestBits_AsInt(t *testing.T) {
tests := []struct {
name string
b Bits
want int
}{
{"empty", Bits{}, 0},
{"negative 1 (4-bit)", Bits{
// LSB is ON THE LEFT!!!!
// LSB is ON THE LEFT!!!!
// LSB is ON THE LEFT!!!!
T, T, T, T,
}, -1},
{"negative 1 (8 bit)", Bits{T, T, T, T, T, T, T, T}, -1},
{"negative 7 (8 bit)", Bits{T, F, F, T, T, T, T, T}, -7},
{"positive 7 (4 bit)", Bits{T, T, T, F}, 7},
{"negative 1 (1 bit)", Bits{T}, -1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.AsInt(); got != tt.want {
t.Errorf("AsInt() = %v, want %v", got, tt.want)
}
})
}
}
func TestBits_AsInt16(t *testing.T) {
tests := []struct {
name string
b Bits
want int16
}{
{"empty", Bits{}, 0},
{"negative 1 (1 bit)", Bits{T}, -1},
{"negative 1 (3 bit)", Bits{T, T, T}, -1},
{"negative 1 (16 bit)", Bits{T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T}, -1},
{"negative 1 (17 bit)", Bits{T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T}, -1},
{"0 (17 bit)", Bits{F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, T}, 0},
{"positive 2 (3 bit)", Bits{F, T, F}, 2},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.AsInt16(); got != tt.want {
t.Errorf("AsInt16() = %v, want %v", got, tt.want)
}
})
}
}
func TestBits_AsInt32(t *testing.T) {
tests := []struct {
name string
b Bits
want int32
}{
{"empty", Bits{}, 0},
{"negative 310 (10 bits)", Bits{F, T, F, T, F, F, T, T, F, T}, -310},
{"positive 1024 (12 bits)", Bits{F, F, F, F, F, F, F, F, F, F, T, F}, 1024},
{"negative 1024 (12 bits)", Bits{F, F, F, F, F, F, F, F, F, F, T, T}, -1024},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.AsInt32(); got != tt.want {
t.Errorf("AsInt32() = %v, want %v", got, tt.want)
}
})
}
}
func TestBits_AsInt64(t *testing.T) {
tests := []struct {
name string
b Bits
want int64
}{
{"2^31+3 (33 bits)", Bits{
T, T, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, T, F,
}, int64(math.Pow(2, 31) + 3)},
{"negative 2^31+3 (34 bits)", Bits{
T, F, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, T, F, T, T,
}, int64(-1 * (math.Pow(2, 31) + 3))},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.AsInt64(); got != tt.want {
t.Errorf("AsInt64() = %v, want %v", got, tt.want)
}
})
}
}
func TestBits_AsInt8(t *testing.T) {
tests := []struct {
name string
b Bits
want int8
}{
{"positive 100 (8 bits)", Bits{F, F, T, F, F, T, T, F}, 100},
{"negative 100 (8 bits)", Bits{F, F, T, T, T, F, F, T}, -100},
{"positive 8 (5 bits)", Bits{F, F, F, T, F}, 8},
{"negative 8 (5 bits)", Bits{F, F, F, T, T}, -8},
{"positive 64 (8 bits)", Bits{F, F, F, F, F, F, T, F}, 64},
{"negative 64 (7 bits)", Bits{F, F, F, F, F, F, T}, -64},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.AsInt8(); got != tt.want {
t.Errorf("AsInt8() = %v, want %v", got, tt.want)
}
})
}
}
func TestBits_AsUInt(t *testing.T) {
tests := []struct {
name string
b Bits
want uint
}{
{"2 (2 bits)", Bits{F, T}, 2},
{"70 (7 bits)", Bits{F, T, T, F, F, F, T}, 70},
{"123 (7 bits)", Bits{T, T, F, T, T, T, T}, 123},
{"256 (8 bits)", Bits{T, T, T, T, T, T, T, T}, 255},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.AsUInt(); got != tt.want {
t.Errorf("AsUInt() = %v, want %v", got, tt.want)
}
})
}
}
func TestBits_AsUInt16(t *testing.T) {
tests := []struct {
name string
b Bits
want uint16
}{
{"1945 (11 bits)", Bits{T, F, F, T, T, F, F, T, T, T, T}, 1945},
{"1945 (16 bits)", Bits{T, F, F, T, T, F, F, T, T, T, T, F, F, F, F, F}, 1945},
{"2021 (11 bits)", Bits{T, F, T, F, F, T, T, T, T, T, T}, 2021},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.AsUInt16(); got != tt.want {
t.Errorf("AsUInt16() = %v, want %v", got, tt.want)
}
})
}
}
func TestBits_AsUInt32(t *testing.T) {
tests := []struct {
name string
b Bits
want uint32
}{
{"2^31 (32 bits)", Bits{
F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, T,
}, uint32(math.Pow(2, 31))},
{"1548876 (uint32)", Bits{F, F, T, T, F, F, T, F, F, T, F, F, F, T, F, T, T, T, T, F, T, F, F, F, F, F, F, F, F, F, F, F}, 1548876},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.AsUInt32(); got != tt.want {
t.Errorf("AsUInt32() = %v, want %v", got, tt.want)
}
})
}
}
func TestBits_AsUInt64(t *testing.T) {
tests := []struct {
name string
b Bits
want uint64
}{
{"2^31 (32 bits)", Bits{
F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, T,
}, uint64(math.Pow(2, 31))},
{"2^31 (64 bits)", Bits{
F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, T,
F, F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
F, F, F, F, F, F, F, F, F, F, F, F, F, F, F,
}, uint64(math.Pow(2, 31))},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.AsUInt64(); got != tt.want {
t.Errorf("AsUInt64() = %v, want %v", got, tt.want)
}
})
}
}
func TestBits_AsUInt8(t *testing.T) {
tests := []struct {
name string
b Bits
want uint8
}{
{"8 (4 bits)", Bits{F, F, F, T}, 8},
{"8 (8 bits)", Bits{F, F, F, T, F, F, F, F}, 8},
{"17 (5 bits)", Bits{T, F, F, F, T}, 17},
{"17 (8 bits)", Bits{T, F, F, F, T, F, F, F}, 17},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.AsUInt8(); got != tt.want {
t.Errorf("AsUInt8() = %v, want %v", got, tt.want)
}
})
}
}
func TestBits_AsBytes(t *testing.T) {
tests := []struct {
name string
b Bits
want []byte
}{
{"", Bits{F, T}, []byte{2}},
{"", Bits{F, F, F, F, F, F, F, F, T}, []byte{0, 1}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.AsBytes(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("AsBytes() = %v, want %v", got, tt.want)
}
})
}
}
func TestBits_AsBool(t *testing.T) {
tests := []struct {
name string
b Bits
want bool
}{
{"single false bit", Bits{F}, F},
{"single true bit", Bits{T}, T},
{"only false bits", Bits{F, F, F, F, F, F, F, F}, F},
{"one true bit", Bits{F, F, F, F, F, F, F, F, T}, T},
{"one true bit", Bits{T, T, T, T, T}, T},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.b.AsBool(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("AsBool() = %v, want %v", got, tt.want)
}
})
}
}