Skip to content

Commit

Permalink
Test data node (#325)
Browse files Browse the repository at this point in the history
  • Loading branch information
outofforest authored Dec 19, 2024
1 parent 9f3fe24 commit 3897b9a
Showing 1 changed file with 58 additions and 0 deletions.
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)
}

0 comments on commit 3897b9a

Please sign in to comment.