@@ -20,6 +20,7 @@ import (
20
20
"testing"
21
21
22
22
"github.com/stretchr/testify/assert"
23
+ "github.com/stretchr/testify/require"
23
24
)
24
25
25
26
func TestBitOperations (t * testing.T ) {
@@ -195,6 +196,50 @@ func BenchmarkGetBit(b *testing.B) {
195
196
}
196
197
}
197
198
199
+ func TestGetSetBits (t * testing.T ) {
200
+ ba := newBitArray (1000 )
201
+ buf := make ([]uint64 , 0 , 5 )
202
+
203
+ require .NoError (t , ba .SetBit (1 ))
204
+ require .NoError (t , ba .SetBit (4 ))
205
+ require .NoError (t , ba .SetBit (8 ))
206
+ require .NoError (t , ba .SetBit (63 ))
207
+ require .NoError (t , ba .SetBit (64 ))
208
+ require .NoError (t , ba .SetBit (200 ))
209
+ require .NoError (t , ba .SetBit (1000 ))
210
+
211
+ assert .Equal (t , []uint64 {1 , 4 , 8 , 63 , 64 }, ba .GetSetBits (0 , buf ))
212
+ assert .Equal (t , []uint64 {63 , 64 , 200 , 1000 }, ba .GetSetBits (10 , buf ))
213
+ assert .Equal (t , []uint64 {63 , 64 , 200 , 1000 }, ba .GetSetBits (63 , buf ))
214
+ assert .Equal (t , []uint64 {200 , 1000 }, ba .GetSetBits (128 , buf ))
215
+
216
+ require .NoError (t , ba .ClearBit (4 ))
217
+ require .NoError (t , ba .ClearBit (64 ))
218
+ assert .Equal (t , []uint64 {1 , 8 , 63 , 200 , 1000 }, ba .GetSetBits (0 , buf ))
219
+ assert .Empty (t , ba .GetSetBits (1001 , buf ))
220
+
221
+ ba .Reset ()
222
+ assert .Empty (t , ba .GetSetBits (0 , buf ))
223
+ }
224
+
225
+ func BenchmarkGetSetBits (b * testing.B ) {
226
+ numItems := uint64 (168000 )
227
+
228
+ ba := newBitArray (numItems )
229
+ for i := uint64 (0 ); i < numItems ; i ++ {
230
+ if i % 13 == 0 || i % 5 == 0 {
231
+ require .NoError (b , ba .SetBit (i ))
232
+ }
233
+ }
234
+
235
+ buf := make ([]uint64 , 0 , ba .Capacity ())
236
+
237
+ b .ResetTimer ()
238
+ for i := 0 ; i < b .N ; i ++ {
239
+ ba .GetSetBits (0 , buf )
240
+ }
241
+ }
242
+
198
243
func TestEquality (t * testing.T ) {
199
244
ba := newBitArray (s + 1 )
200
245
other := newBitArray (s + 1 )
0 commit comments