2020-09-28 17:18:36 +00:00
|
|
|
package bitmapdb_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2020-10-02 08:16:21 +00:00
|
|
|
"github.com/RoaringBitmap/roaring"
|
2021-05-20 18:25:53 +00:00
|
|
|
"github.com/ledgerwatch/erigon/ethdb/bitmapdb"
|
2020-09-28 17:18:36 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-10-19 13:11:01 +00:00
|
|
|
func TestCutLeft(t *testing.T) {
|
|
|
|
bm := roaring.New()
|
|
|
|
for j := 0; j < 10_000; j += 20 {
|
|
|
|
bm.AddRange(uint64(j), uint64(j+10))
|
|
|
|
}
|
|
|
|
N := uint64(1024)
|
|
|
|
for bm.GetCardinality() > 0 {
|
|
|
|
lft := bitmapdb.CutLeft(bm, N)
|
|
|
|
lftSz := lft.GetSerializedSizeInBytes()
|
|
|
|
if bm.GetCardinality() > 0 {
|
|
|
|
require.True(t, lftSz > N-256 && lftSz < N+256)
|
|
|
|
} else {
|
|
|
|
require.True(t, lft.GetSerializedSizeInBytes() > 0)
|
|
|
|
require.True(t, lftSz < N+256)
|
2020-09-28 17:18:36 +00:00
|
|
|
}
|
2020-10-19 13:11:01 +00:00
|
|
|
}
|
2020-10-02 03:54:11 +00:00
|
|
|
|
2020-10-19 13:11:01 +00:00
|
|
|
bm = roaring.New()
|
|
|
|
for j := 0; j < 10_000; j += 20 {
|
|
|
|
bm.AddRange(uint64(j), uint64(j+10))
|
|
|
|
}
|
|
|
|
N = uint64(2048)
|
|
|
|
for bm.GetCardinality() > 0 {
|
|
|
|
lft := bitmapdb.CutLeft(bm, N)
|
|
|
|
lftSz := lft.GetSerializedSizeInBytes()
|
|
|
|
if bm.GetCardinality() > 0 {
|
|
|
|
require.True(t, lftSz > N-256 && lftSz < N+256)
|
|
|
|
} else {
|
|
|
|
require.True(t, lft.GetSerializedSizeInBytes() > 0)
|
|
|
|
require.True(t, lftSz < N+256)
|
2020-09-28 17:18:36 +00:00
|
|
|
}
|
|
|
|
}
|
2020-10-19 13:11:01 +00:00
|
|
|
|
|
|
|
bm = roaring.New()
|
|
|
|
bm.Add(1)
|
|
|
|
lft := bitmapdb.CutLeft(bm, N)
|
|
|
|
require.True(t, lft.GetSerializedSizeInBytes() > 0)
|
|
|
|
require.True(t, lft.GetCardinality() == 1)
|
|
|
|
require.True(t, bm.GetCardinality() == 0)
|
|
|
|
|
|
|
|
bm = roaring.New()
|
|
|
|
lft = bitmapdb.CutLeft(bm, N)
|
|
|
|
require.True(t, lft == nil)
|
|
|
|
require.True(t, bm.GetCardinality() == 0)
|
2020-09-28 17:18:36 +00:00
|
|
|
}
|