forked from tunabay/go-bitarray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuilder_example_test.go
209 lines (153 loc) · 3.83 KB
/
builder_example_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
// Copyright (c) 2021 Hirotsuna Mizuno. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
package bitarray_test
import (
"bytes"
"fmt"
"io"
"github.com/tunabay/go-bitarray"
)
func ExampleBuilder() {
b := bitarray.NewBuilder()
// Add bits with various methods.
b.WriteBitsFromBytes([]byte{0b_1100_0111}, 2, 3) // 000
b.WriteByte(0xFF) // 11111111
b.WriteBit(0) // 0
b.WriteBitArray(bitarray.MustParse("111000111")) // 111000111
b.WriteByteBits([]byte{0, 1, 0, 1}) // 0101
// Build a BitArray containing the accumulated bits.
ba := b.BitArray()
fmt.Printf("% b\n", ba)
// Output:
// 00011111 11101110 00111010 1
}
func ExampleNewBuilder() {
// Create an empty builder and add bitarrays.
b1 := bitarray.NewBuilder()
b1.WriteBitArray(bitarray.MustParse("1111"))
b1.WriteBitArray(bitarray.MustParse("000"))
fmt.Printf("%b\n", b1.BitArray())
// Same as above.
b2 := bitarray.NewBuilder(
bitarray.MustParse("1111"),
bitarray.MustParse("000"),
)
fmt.Printf("%b\n", b2.BitArray())
// Output:
// 1111000
// 1111000
}
func ExampleBuilder_BitArray() {
b := bitarray.NewBuilder()
b.WriteBitArray(bitarray.MustParse("1111-00"))
b.WriteBitArray(bitarray.MustParse("1100-11"))
ba := b.BitArray()
fmt.Printf("% b\n", ba)
// Output:
// 11110011 0011
}
func ExampleBuilder_String() {
b := bitarray.NewBuilder()
b.WriteBitArray(bitarray.MustParse("1111-00"))
b.WriteBitArray(bitarray.MustParse("1100-11"))
fmt.Println(b.String())
fmt.Println(b.BitArray().String())
// Output:
// 111100110011
// 111100110011
}
func ExampleBuilder_Reset() {
b := bitarray.NewBuilder()
b.WriteBitArray(bitarray.MustParse("1111-00"))
b.WriteBitArray(bitarray.MustParse("1100-11"))
b.Reset()
b.WriteBitArray(bitarray.MustParse("111"))
fmt.Println(b)
// Output:
// 111
}
func ExampleBuilder_Len() {
b := bitarray.NewBuilder()
b.WriteBitArray(bitarray.MustParse("1111-00"))
b.WriteBitArray(bitarray.MustParse("1100-11"))
fmt.Println(b.Len())
// Output:
// 12
}
func ExampleBuilder_WriteBitsFromBytes() {
b := bitarray.NewBuilder()
src := []byte{0xF3, 0x50} // 1111-0011 0101-0000
b.WriteBitsFromBytes(src, 3, 4) // 1001
b.WriteBitsFromBytes(src, 8, 0) // (empty)
b.WriteBitsFromBytes(src, 5, 6) // 011010
b.WriteBitsFromBytes(src, 11, 3) // 100
fmt.Println(b)
// Output:
// 1001011010100
}
func ExampleBuilder_Write() {
b := bitarray.NewBuilder()
src := []byte{0xF0, 0xF0} // 1111-0000 1111-0000
n, err := b.Write(src)
if err != nil {
panic(err)
}
fmt.Println(n)
fmt.Println(b)
// Output:
// 2
// 1111000011110000
}
func ExampleBuilder_Write_copy() {
b := bitarray.NewBuilder(bitarray.MustParse("0000"))
src := bytes.NewReader([]byte{0xFF, 0x00, 0xAA})
_, err := io.Copy(b, src) // uses b.Write() via src.WriteTo()
if err != nil {
panic(err)
}
ba := b.BitArray()
fmt.Printf("% b\n", ba)
// Output:
// 00001111 11110000 00001010 1010
}
func ExampleBuilder_ReadFrom() {
b := bitarray.NewBuilder(bitarray.MustParse("0000"))
src := bytes.NewReader([]byte{0xFF, 0x00, 0xAA})
n, err := b.ReadFrom(src)
if err != nil {
panic(err)
}
fmt.Println(n)
ba := b.BitArray()
fmt.Printf("% b\n", ba)
// Output:
// 3
// 00001111 11110000 00001010 1010
}
func ExampleBuilder_WriteBit() {
b := bitarray.NewBuilder()
b.WriteBit(0)
b.WriteBit(1)
b.WriteBit(0)
b.WriteBit(1)
fmt.Println(b)
// Output:
// 0101
}
func ExampleBuilder_WriteBitArray() {
b := bitarray.NewBuilder()
b.WriteBitArray(bitarray.MustParse("111"))
b.WriteBitArray(bitarray.MustParse("0000"))
fmt.Println(b)
// Output:
// 1110000
}
func ExampleBuilder_WriteByteBits() {
b := bitarray.NewBuilder()
src := []byte{1, 0, 1, 0, 1, 0}
b.WriteByteBits(src)
fmt.Println(b)
// Output:
// 101010
}