Add tpage.write() test.

pull/34/head
Ben Johnson 2014-01-28 17:02:06 -05:00
parent bb7a93f750
commit da6d48abe9
2 changed files with 30 additions and 5 deletions

View File

@ -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):]
}

View File

@ -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.