Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test data node #325

Merged
merged 2 commits into from
Dec 19, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions space/data_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package space

import (
"runtime"
"testing"
"unsafe"

"github.com/stretchr/testify/require"

"github.com/outofforest/quantum/types"
)

func TestDataNode(t *testing.T) {
requireT := require.New(t)
node := make([]byte, types.NodeLength)
defer runtime.KeepAlive(node)

nodeP := unsafe.Pointer(&node[0])

dataNode, err := NewDataNodeAssistant[uint32, uint16]()
requireT.NoError(err)

keyHashes := dataNode.KeyHashes(nodeP)
requireT.Equal(dataNode.NumOfItems(), uint64(len(keyHashes)))
requireT.Equal(nodeP, unsafe.Pointer(&keyHashes[0]))

requireT.GreaterOrEqual(uintptr(nodeP)+types.NodeLength,
uintptr(unsafe.Pointer(dataNode.Item(nodeP, dataNode.ItemOffset(dataNode.NumOfItems())))))

for i := range dataNode.NumOfItems() {
item := dataNode.Item(nodeP, dataNode.ItemOffset(i))
item.Key = uint32(i)
item.Value = uint16(i)
}

itemP := unsafe.Add(nodeP, dataNode.NumOfItems()*types.UInt64Length)
for i := range dataNode.NumOfItems() {
requireT.Equal(uint32(i), *(*uint32)(itemP)) //nolint:testifylint
requireT.Equal(uint16(i), *(*uint16)(unsafe.Add(itemP, 4))) //nolint:testifylint

itemP = unsafe.Add(itemP, 8)
}
}

func TestMaxDataNode(t *testing.T) {
requireT := require.New(t)

dataNode, err := NewDataNodeAssistant[uint64, [4080]byte]()
requireT.NoError(err)
requireT.Equal(uint64(1), dataNode.NumOfItems())
}

func TestTooBigNode(t *testing.T) {
requireT := require.New(t)

_, err := NewDataNodeAssistant[uint64, [4081]byte]()
requireT.Error(err)
}
Loading