MCPcopy
hub / github.com/etcd-io/bbolt / TestNode_read_LeafPage

Function TestNode_read_LeafPage

node_test.go:38–73  ·  view source on GitHub ↗

Ensure that a node can deserialize from a leaf page.

(t *testing.T)

Source from the content-addressed store, hash-verified

36
37// Ensure that a node can deserialize from a leaf page.
38func TestNode_read_LeafPage(t *testing.T) {
39 // Create a page.
40 var buf [4096]byte
41 page := (*common.Page)(unsafe.Pointer(&buf[0]))
42 page.SetFlags(common.LeafPageFlag)
43 page.SetCount(2)
44
45 // Insert 2 elements at the beginning. sizeof(leafPageElement) == 16
46 nodes := page.LeafPageElements()
47 //nodes := (*[3]leafPageElement)(unsafe.Pointer(uintptr(unsafe.Pointer(page)) + unsafe.Sizeof(*page)))
48 nodes[0] = *common.NewLeafPageElement(0, 32, 3, 4) // pos = sizeof(leafPageElement) * 2
49 nodes[1] = *common.NewLeafPageElement(0, 23, 10, 3) // pos = sizeof(leafPageElement) + 3 + 4
50
51 // Write data for the nodes at the end.
52 const s = "barfoozhelloworldbye"
53 data := common.UnsafeByteSlice(unsafe.Pointer(uintptr(unsafe.Pointer(page))+unsafe.Sizeof(*page)+common.LeafPageElementSize*2), 0, 0, len(s))
54 copy(data, s)
55
56 // Deserialize page into a leaf.
57 n := &node{}
58 n.read(page)
59
60 // Check that there are two inodes with correct data.
61 if !n.isLeaf {
62 t.Fatal("expected leaf")
63 }
64 if len(n.inodes) != 2 {
65 t.Fatalf("exp=2; got=%d", len(n.inodes))
66 }
67 if k, v := n.inodes[0].Key(), n.inodes[0].Value(); string(k) != "bar" || string(v) != "fooz" {
68 t.Fatalf("exp=<bar,fooz>; got=<%s,%s>", k, v)
69 }
70 if k, v := n.inodes[1].Key(), n.inodes[1].Value(); string(k) != "helloworld" || string(v) != "bye" {
71 t.Fatalf("exp=<helloworld,bye>; got=<%s,%s>", k, v)
72 }
73}
74
75// Ensure that a node can serialize into a leaf page.
76func TestNode_write_LeafPage(t *testing.T) {

Callers

nothing calls this directly

Calls 10

readMethod · 0.95
NewLeafPageElementFunction · 0.92
UnsafeByteSliceFunction · 0.92
SetCountMethod · 0.80
LeafPageElementsMethod · 0.80
FatalMethod · 0.65
FatalfMethod · 0.65
SetFlagsMethod · 0.45
KeyMethod · 0.45
ValueMethod · 0.45

Tested by

no test coverage detected