added unit tests for hard-coded access to key/value offsets flatbuffers field

This commit is contained in:
Andy Arthur
2022-05-02 15:49:03 -07:00
parent 1fca9f0fee
commit 8a35593c89
2 changed files with 72 additions and 31 deletions

View File

@@ -0,0 +1,72 @@
// Copyright 2021 Dolthub, Inc.
//
// 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 message
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/dolthub/dolt/go/store/pool"
)
var sharedPool = pool.NewBuffPool()
func TestGetKeyValueOffsetsVectors(t *testing.T) {
for trial := 0; trial < 100; trial++ {
keys, values := randomByteSlices(t, (testRand.Int()%101)+50)
require.True(t, sumSize(keys)+sumSize(values) < MaxVectorOffset)
msg := SerializeProllyMap(sharedPool, keys, values, 0, nil)
// uses getKeyOffsetsVector with hard-coded vtable slot
keyBuf := getProllyMapKeys(msg)
// uses getValueOffsetsVector with hard-coded vtable slot
valBuf := getProllyMapValues(msg)
for i := range keys {
assert.Equal(t, keys[i], keyBuf.GetSlice(i))
}
for i := range values {
assert.Equal(t, values[i], valBuf.GetSlice(i))
}
}
}
func randomByteSlices(t *testing.T, count int) (keys, values [][]byte) {
keys = make([][]byte, count)
for i := range keys {
sz := (testRand.Int() % 41) + 10
keys[i] = make([]byte, sz)
_, err := testRand.Read(keys[i])
assert.NoError(t, err)
}
values = make([][]byte, count)
copy(values, keys)
testRand.Shuffle(len(values), func(i, j int) {
values[i], values[j] = values[j], values[i]
})
return
}
func sumSize(items [][]byte) (sz uint64) {
for _, item := range items {
sz += uint64(len(item))
}
return
}

View File

@@ -15,7 +15,6 @@
package tree
import (
"encoding/binary"
"math"
"math/rand"
"testing"
@@ -24,7 +23,6 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/dolthub/dolt/go/gen/fb/serial"
"github.com/dolthub/dolt/go/store/prolly/message"
"github.com/dolthub/dolt/go/store/val"
)
@@ -100,35 +98,6 @@ func sumTupleSize(items []val.Tuple) (sz uint64) {
return
}
func offsetsFromFlatbuffer(buf serial.ProllyTreeNode) (ko, vo []uint16) {
ko = make([]uint16, buf.KeyOffsetsLength())
for i := range ko {
ko[i] = buf.KeyOffsets(i)
}
vo = make([]uint16, buf.ValueOffsetsLength())
for i := range vo {
vo[i] = buf.ValueOffsets(i)
}
return
}
func offsetsFromSlicedBuffers(keys, values val.SlicedBuffer) (ko, vo []uint16) {
ko = deserializeOffsets(keys.Offs)
vo = deserializeOffsets(values.Offs)
return
}
func deserializeOffsets(buf []byte) (offs []uint16) {
offs = make([]uint16, len(buf)/2)
for i := range offs {
start, stop := i*2, (i+1)*2
offs[i] = binary.LittleEndian.Uint16(buf[start:stop])
}
return
}
func TestSamples(t *testing.T) {
tests := []struct {
data Samples