erigon-pulse/recsplit/eliasfano32/elias_fano_test.go
J1ang 7e68f20838
feat: implement eliasfano32.Min() (#682)
feat: implement eliasfano32.Min()
feature:
1. implement eliasfano32.Min() by calculating lower/upper bits.

refactor:
1. fix miscalculate of `superQSize` Annotation

relevant Issue in erigon:
https://github.com/ledgerwatch/erigon/issues/5623
2022-10-14 13:46:24 +07:00

82 lines
2.1 KiB
Go

/*
Copyright 2022 Erigon contributors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package eliasfano32
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
)
func TestEliasFano(t *testing.T) {
offsets := []uint64{1, 4, 6, 8, 10, 14, 16, 19, 22, 34, 37, 39, 41, 43, 48, 51, 54, 58, 62}
count := uint64(len(offsets))
maxOffset := offsets[0]
for _, offset := range offsets {
if offset > maxOffset {
maxOffset = offset
}
}
ef := NewEliasFano(count, maxOffset)
for _, offset := range offsets {
ef.AddOffset(offset)
}
ef.Build()
for i, offset := range offsets {
offset1 := ef.Get(uint64(i))
assert.Equal(t, offset, offset1, "offset")
}
v, ok := ef.Search(37)
assert.True(t, ok, "search1")
assert.Equal(t, uint64(37), v, "search1")
v, ok = ef.Search(0)
assert.True(t, ok, "search2")
assert.Equal(t, uint64(1), v, "search2")
_, ok = ef.Search(100)
assert.False(t, ok, "search3")
v, ok = ef.Search(11)
assert.True(t, ok, "search4")
assert.Equal(t, uint64(14), v, "search4")
buf := bytes.NewBuffer(nil)
ef.Write(buf)
assert.Equal(t, ef.Max(), Max(buf.Bytes()))
assert.Equal(t, ef.Min(), Min(buf.Bytes()))
}
func TestIterator(t *testing.T) {
offsets := []uint64{1, 4, 6, 8, 10, 14, 16, 19, 22, 34, 37, 39, 41, 43, 48, 51, 54, 58, 62}
count := uint64(len(offsets))
maxOffset := offsets[0]
for _, offset := range offsets {
if offset > maxOffset {
maxOffset = offset
}
}
ef := NewEliasFano(count, maxOffset)
for _, offset := range offsets {
ef.AddOffset(offset)
}
ef.Build()
efi := ef.Iterator()
i := 0
for efi.HasNext() {
assert.Equal(t, offsets[i], efi.Next(), "iter")
i++
}
}