erigon-pulse/recsplit/eliasfano32/elias_fano_test.go
Alex Sharov 9819f6af24
eliasfano: To fix checkptr fatal error (#697)
```
 go test -race -run=TestEliasFano ./recsplit/eliasfano32
fatal error: checkptr: converted pointer straddles multiple allocations

goroutine 35 [running]:
runtime.throw({0x13bb5bf?, 0x13446ca?})
	runtime/panic.go:1047 +0x5d fp=0xc000137790 sp=0xc000137760 pc=0x108a13d
runtime.checkptrAlignment(0xa9?, 0x0?, 0x0?)
	runtime/checkptr.go:26 +0x6c fp=0xc0001377b0 sp=0xc000137790 pc=0x1059f8c
github.com/ledgerwatch/erigon-lib/recsplit/eliasfano32.Min({0xc00012c280, 0x40, 0x40})
	github.com/ledgerwatch/erigon-lib/recsplit/eliasfano32/elias_fano.go:324 +0x36f fp=0xc000137868 sp=0xc0001377b0 pc=0x133e66f
github.com/ledgerwatch/erigon-lib/recsplit/eliasfano32.TestEliasFano(0x0?)
	github.com/ledgerwatch/erigon-lib/recsplit/eliasfano32/elias_fano_test.go:58
```
2022-10-22 10:55:19 +07:00

88 lines
2.3 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.AppendBytes(nil), buf.Bytes())
ef2, _ := ReadEliasFano(buf.Bytes())
assert.Equal(t, ef.Min(), ef2.Min())
assert.Equal(t, ef.Max(), ef2.Max())
assert.Equal(t, ef2.Max(), Max(buf.Bytes()))
assert.Equal(t, ef2.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++
}
}