This repository has been archived by the owner on May 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshifty_test.go
242 lines (209 loc) · 4.83 KB
/
shifty_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
package shifty
import (
"fmt"
"testing"
)
var testMap map[int]Kind
func ExampleNew() {
bits := New(Uint16)
fmt.Printf("%T size %d, max %d", bits, bits.Size(), bits.Max())
// Output: shifty.BitValue size 16, max 65535
}
func ExampleBitValue_Shift() {
bits := New(Uint8)
bits.Shift(2, 4, 32)
fmt.Printf("Value: %d", bits.Int())
// Output: Value: 38
}
func ExampleBitValue_Unshift() {
bits := New(Uint8)
bits.Shift(2, 4, 32)
bits.Unshift(32)
fmt.Printf("Value: %d", bits.Int())
// Output: Value: 6
}
func ExampleBitValue_Min() {
var bits BitValue
fmt.Printf("%d", bits.Min())
// Output: 0
}
func ExampleBitValue_Max_for8Bit() {
bits := New(Uint8)
fmt.Printf("%d", bits.Max())
// Output: 255
}
func ExampleBitValue_Max_for16Bit() {
bits := New(Uint16)
fmt.Printf("%d", bits.Max())
// Output: 65535
}
func ExampleBitValue_Max_for32bit() {
bits := New(Uint32)
fmt.Printf("%d", bits.Max())
// Output: 4294967295
}
func ExampleBitValue_Int() {
bits := New(Uint8)
bits.Shift(2, 4, 32)
fmt.Printf("%d", bits.Int())
// Output: 38
}
func ExampleBitValue_Int_mixed() {
var ints []int
for i := 0; i < 3; i++ {
bits := New(Kind(i + 1))
bits.Shift(bits.Size() << i)
ints = append(ints, bits.Int())
}
fmt.Printf("%v", ints)
// Output: [8 32 128]
}
func ExampleBitValue_Value() {
bits := New(Uint32)
bits.Shift(bits.Max())
fmt.Printf("%T", bits.Value())
// Output: *uint32
}
func ExampleBitValue_Kind() {
bits := New(Uint32)
fmt.Printf("%s", bits.Kind())
// Output: uint32
}
func ExampleBitValue_Size() {
bits := New(Uint32)
fmt.Printf("%d", bits.Size())
// Output: 32
}
func ExampleKind_Size() {
k := Uint32
fmt.Printf("%d", k.Size())
// Output: 32
}
func ExampleKind_String() {
fmt.Printf("%s", Uint32)
// Output: uint32
}
func ExampleBitValue_Positive() {
// user-defined shift values
type B uint8
const (
Bopt1 B = 1 << iota // 1
Bopt2 // 2
Bopt3 // 4
Bopt4 // 8
Bopt5 // 16
Bopt6 // 32
Bopt7 // 64
Bopt8 // 128 // go no higher (else, overflow uint8)
)
bits := New(Uint8)
bits.Shift(Bopt1, Bopt3, Bopt6)
fmt.Printf("Value contains B-options #6 (32): %t", bits.Positive(Bopt6))
// Output: Value contains B-options #6 (32): true
}
func ExampleBitValue_SetNamesMap() {
// user-defined shift values
bits := New(Uint8)
type B uint8
const (
Bopt1 B = 1 << iota // 1
Bopt2 // 2
Bopt3 // 4
Bopt4 // 8
Bopt5 // 16
Bopt6 // 32
Bopt7 // 64
Bopt8 // 128 // go no higher (else, overflow uint8)
)
// create a const->name map using
// the above const vals by way of
// incremental shifts. Note that
// we could have just as easily
// built the map manually, though
// but looping is neater and more
// succinct.
var m map[int]string = make(map[int]string, 0)
for i := 0; i < bits.Size(); i++ {
str := fmt.Sprintf("bit_option%d", i+1) // inc. label number since we start at zero
bv := 1 << i
m[bv] = str
}
// assign map to receiver
bits.SetNamesMap(m)
// now shift by string name instead
// of a constant directly :)
name := `bit_option6`
bits.Shift(name)
fmt.Printf("Value contains %s: %t (val:%d)", name, bits.Positive(name), bits.Int())
// Output: Value contains bit_option6: true (val:32)
}
func ExampleBitValue_None() {
bits := New(Uint8)
bits.Shift(8 << 1)
bits.None() // annihilate any value
fmt.Printf("%d", bits.Int())
// Output: 0
}
func ExampleBitValue_All() {
bits := New(Uint16)
bits.All() // shift EVERYTHING
fmt.Printf("%d", bits.Int())
// Output: 65535
}
func ExampleBitValue_NamesMap() {
bits := New(Uint8)
fmt.Printf("%T", bits.NamesMap()) // note this is a nil map
// Output: map[int]string
}
func TestBitValue_codecov(t *testing.T) {
var bits BitValue
bits.Kind()
bits.Int()
bits.Value()
bits.Shift(-1)
bits.Shift(8 << 8)
bits.Unshift(-1)
bits.Positive(-1)
bits.Unshift(40000000000)
if i := bits.Int(); i != 0 {
t.Errorf("%s failed: bogus value set (%d) where none should be",
t.Name(), i)
}
bits = New(Uint8)
bits.Shift(bits.Max())
bits.Shift(8 << 8)
bits.Shift(8 << 1)
bits.Positive(8 << 2)
bits.Unshift(8 << 8)
bits.Kind()
bits.Int()
bits.Value()
for _, kind := range testMap {
instance := New(kind)
size := instance.Size()
_ = kind.String()
_ = bits.Int()
for i := 0; i < size; i++ {
instance.Shift(size << i)
instance.Positive(size << i)
instance.Unshift(size << i)
instance.Shift(instance.Max())
instance.Unshift(instance.Max())
switch instance.Value().(type) {
case *uint8:
_, _ = toInt(uint8(size))
case *uint16:
_, _ = toInt(uint16(size))
case *uint32:
_, _ = toInt(uint32(size))
}
}
}
}
func init() {
testMap = map[int]Kind{
8: Uint8,
16: Uint16,
32: Uint32,
}
}