From da6d48abe99e23dc9544a5d6c97f69905ef44c66 Mon Sep 17 00:00:00 2001 From: Ben Johnson Date: Tue, 28 Jan 2014 17:02:06 -0500 Subject: [PATCH] Add tpage.write() test. --- tpage.go | 8 ++++---- tpage_test.go | 27 ++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/tpage.go b/tpage.go index 3701789..ded5b53 100644 --- a/tpage.go +++ b/tpage.go @@ -13,7 +13,7 @@ type tpage struct { } // allocator is a function that returns a set of contiguous pages. -type allocator func(count int) (*page, error) +type allocator func(size int) (*page, error) // put inserts or replaces a key on a leaf page. func (p *tpage) put(key []byte, value []byte) { @@ -69,14 +69,14 @@ func (p *tpage) write(pageSize int, allocate allocator) ([]*page, error) { for index, node := range nodes { // Write node. lnode := &lnodes[index] - lnode.pos = uint32(uintptr(unsafe.Pointer(&b[0])) - uintptr(unsafe.Pointer(&lnode))) + lnode.pos = uint32(uintptr(unsafe.Pointer(&b[0])) - uintptr(unsafe.Pointer(lnode))) lnode.ksize = uint32(len(node.key)) lnode.vsize = uint32(len(node.value)) // Write data to the end of the node. - copy(b[:], node.key) + copy(b[0:], node.key) b = b[len(node.key):] - copy(b[:], node.value) + copy(b[0:], node.value) b = b[len(node.value):] } diff --git a/tpage_test.go b/tpage_test.go index 5ca6d63..de2db6d 100644 --- a/tpage_test.go +++ b/tpage_test.go @@ -51,7 +51,32 @@ func TestTpageRead(t *testing.T) { // Ensure that a temporary page can serialize itself. func TestTpageWrite(t *testing.T) { - t.Skip("pending") + // Create a temp page. + p := &tpage{nodes: make(tnodes, 0)} + p.put([]byte("susy"), []byte("que")) + p.put([]byte("ricki"), []byte("lake")) + p.put([]byte("john"), []byte("johnson")) + + // Write it to a page. + var buf [4096]byte + allocate := func(size int) (*page, error) { + return (*page)(unsafe.Pointer(&buf[0])), nil + } + pages, err := p.write(4096, allocate) + assert.NoError(t, err) + + // Read the page back in. + p2 := &tpage{} + p2.read(pages[0]) + + // Check that the two pages are the same. + assert.Equal(t, len(p2.nodes), 3) + assert.Equal(t, p2.nodes[0].key, []byte("john")) + assert.Equal(t, p2.nodes[0].value, []byte("johnson")) + assert.Equal(t, p2.nodes[1].key, []byte("ricki")) + assert.Equal(t, p2.nodes[1].value, []byte("lake")) + assert.Equal(t, p2.nodes[2].key, []byte("susy")) + assert.Equal(t, p2.nodes[2].value, []byte("que")) } // Ensure that a temporary page can split into appropriate subgroups.