@@ -2308,3 +2308,79 @@ func TestBitmapFlipMaxRangeEnd(t *testing.T) {
23082308
23092309 assert .EqualValues (t , MaxRange , bm .GetCardinality ())
23102310}
2311+
2312+ func TestIterate (t * testing.T ) {
2313+ rb := NewBitmap ()
2314+
2315+ for i := 0 ; i < 300 ; i ++ {
2316+ rb .Add (uint32 (i ))
2317+ }
2318+
2319+ var values []uint32
2320+ rb .Iterate (func (x uint32 ) bool {
2321+ values = append (values , x )
2322+ return true
2323+ })
2324+
2325+ assert .Equal (t , rb .ToArray (), values )
2326+ }
2327+
2328+ func TestIterateCompressed (t * testing.T ) {
2329+ rb := NewBitmap ()
2330+
2331+ for i := 0 ; i < 300 ; i ++ {
2332+ rb .Add (uint32 (i ))
2333+ }
2334+
2335+ rb .RunOptimize ()
2336+
2337+ var values []uint32
2338+ rb .Iterate (func (x uint32 ) bool {
2339+ values = append (values , x )
2340+ return true
2341+ })
2342+
2343+ assert .Equal (t , rb .ToArray (), values )
2344+ }
2345+
2346+ func TestIterateLargeValues (t * testing.T ) {
2347+ rb := NewBitmap ()
2348+
2349+ // This range of values ensures that all different types of containers will be used
2350+ for i := 150000 ; i < 450000 ; i ++ {
2351+ rb .Add (uint32 (i ))
2352+ }
2353+
2354+ var values []uint32
2355+ rb .Iterate (func (x uint32 ) bool {
2356+ values = append (values , x )
2357+ return true
2358+ })
2359+
2360+ assert .Equal (t , rb .ToArray (), values )
2361+ }
2362+
2363+ func TestIterateHalt (t * testing.T ) {
2364+ rb := NewBitmap ()
2365+
2366+ // This range of values ensures that all different types of containers will be used
2367+ for i := 150000 ; i < 450000 ; i ++ {
2368+ rb .Add (uint32 (i ))
2369+ }
2370+
2371+ var values []uint32
2372+ count := uint64 (0 )
2373+ stopAt := rb .GetCardinality () - 1
2374+ rb .Iterate (func (x uint32 ) bool {
2375+ values = append (values , x )
2376+ count ++
2377+ if count == stopAt {
2378+ return false
2379+ }
2380+ return true
2381+ })
2382+
2383+ expected := rb .ToArray ()
2384+ expected = expected [0 : len (expected )- 1 ]
2385+ assert .Equal (t , expected , values )
2386+ }
0 commit comments