github.com/dolthub/dolt/go@v0.40.5-0.20240520175717-68db7794bea6/store/prolly/message/prolly_map_test.go (about)

     1  // Copyright 2021 Dolthub, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package message
    16  
    17  import (
    18  	"testing"
    19  	"unsafe"
    20  
    21  	"github.com/stretchr/testify/assert"
    22  	"github.com/stretchr/testify/require"
    23  
    24  	"github.com/dolthub/dolt/go/store/pool"
    25  	"github.com/dolthub/dolt/go/store/val"
    26  )
    27  
    28  var sharedPool = pool.NewBuffPool()
    29  
    30  func TestGetKeyValueOffsetsVectors(t *testing.T) {
    31  	for trial := 0; trial < 100; trial++ {
    32  		keys, values := randomByteSlices(t, (testRand.Int()%101)+50)
    33  		require.True(t, sumSize(keys)+sumSize(values) < MaxVectorOffset)
    34  		s := ProllyMapSerializer{valDesc: val.TupleDesc{}, pool: sharedPool}
    35  		msg := s.Serialize(keys, values, nil, 0)
    36  
    37  		// uses hard-coded vtable slot
    38  		keyBuf, valBuf, _, _, _ := getProllyMapKeysAndValues(msg)
    39  
    40  		for i := range keys {
    41  			assert.Equal(t, keys[i], keyBuf.GetItem(i, msg))
    42  		}
    43  		for i := range values {
    44  			assert.Equal(t, values[i], valBuf.GetItem(i, msg))
    45  		}
    46  	}
    47  }
    48  
    49  func TestItemAccessSize(t *testing.T) {
    50  	sz := unsafe.Sizeof(ItemAccess{})
    51  	assert.Equal(t, 10, int(sz))
    52  }
    53  
    54  func randomByteSlices(t *testing.T, count int) (keys, values [][]byte) {
    55  	keys = make([][]byte, count)
    56  	for i := range keys {
    57  		sz := (testRand.Int() % 41) + 10
    58  		keys[i] = make([]byte, sz)
    59  		_, err := testRand.Read(keys[i])
    60  		assert.NoError(t, err)
    61  	}
    62  
    63  	values = make([][]byte, count)
    64  	copy(values, keys)
    65  	testRand.Shuffle(len(values), func(i, j int) {
    66  		values[i], values[j] = values[j], values[i]
    67  	})
    68  
    69  	return
    70  }
    71  
    72  func sumSize(items [][]byte) (sz uint64) {
    73  	for _, item := range items {
    74  		sz += uint64(len(item))
    75  	}
    76  	return
    77  }