Skip to content

Commit 83790b2

Browse files
committed
add tests
1 parent 95831c2 commit 83790b2

File tree

3 files changed

+84
-2
lines changed

3 files changed

+84
-2
lines changed

node/branch_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,18 @@ func TestBranchNode_PutAndCheck(t *testing.T) {
5353
t.Errorf("invalid node: expected %#v got %#v", newNode, rNd)
5454
}
5555
}
56+
57+
func TestBranchNode_Cache(t *testing.T) {
58+
nd := NewBranchNode()
59+
nd.Hash = []byte("test")
60+
nd.Dirty = true
61+
62+
hash, dirty := nd.Cache()
63+
if !bytes.Equal(hash, nd.Hash) {
64+
t.Errorf("invalid hash: expected %s got %s", nd.Hash, hash)
65+
}
66+
67+
if nd.Dirty != dirty {
68+
t.Errorf("invalid dirty: expected %t got %t", nd.Dirty, dirty)
69+
}
70+
}

node/extension_test.go

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package node
2+
3+
import (
4+
"bytes"
5+
"reflect"
6+
"testing"
7+
8+
"github.com/begmaroman/mpt/enc"
9+
)
10+
11+
func TestExtensionNode_Find(t *testing.T) {
12+
key, value := enc.BytesToHex([]byte("key")), []byte("value")
13+
14+
nd := NewExtensionNode(key, NewLeafNode(value))
15+
16+
rVal, rNd, ok := nd.Find(key)
17+
if !ok {
18+
t.Error("added data not found")
19+
return
20+
}
21+
22+
if !bytes.Equal(rVal, value) {
23+
t.Errorf("invalid value: expected %s got %s", value, rVal)
24+
}
25+
26+
if !reflect.DeepEqual(nd, rNd) {
27+
t.Errorf("invalid node: expected %#v got %#v", nd, rNd)
28+
}
29+
}
30+
31+
func TestExtensionNode_PutAndCheck(t *testing.T) {
32+
key, value := enc.BytesToHex([]byte("key")), []byte("value")
33+
34+
nd := NewExtensionNode(enc.BytesToHex([]byte("k")), NewLeafNode([]byte("kat value")))
35+
newNode, ok := nd.Put(key, NewLeafNode(value))
36+
if !ok {
37+
t.Error("put failed")
38+
return
39+
}
40+
41+
rVal, rNd, ok := newNode.Find(key)
42+
if !ok {
43+
t.Error("added data not found")
44+
return
45+
}
46+
47+
if !bytes.Equal(rVal, value) {
48+
t.Errorf("invalid value: expected %s got %s", value, rVal)
49+
}
50+
51+
if !reflect.DeepEqual(newNode, rNd) {
52+
t.Errorf("invalid node: expected %#v got %#v", newNode, rNd)
53+
}
54+
}
55+
56+
func TestExtensionNode_Cache(t *testing.T) {
57+
nd := NewExtensionNode(nil, nil)
58+
nd.Hash = []byte("test")
59+
nd.Dirty = true
60+
61+
hash, dirty := nd.Cache()
62+
if !bytes.Equal(hash, nd.Hash) {
63+
t.Errorf("invalid hash: expected %s got %s", nd.Hash, hash)
64+
}
65+
66+
if nd.Dirty != dirty {
67+
t.Errorf("invalid dirty: expected %t got %t", nd.Dirty, dirty)
68+
}
69+
}

node/hash.go

-2
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,10 @@ func (h HashNode) Hash() enc.Hash {
1515
}
1616

1717
func (h HashNode) Find(key []byte) ([]byte, Node, bool) {
18-
// TODO: need to implement lookup logic
1918
return h, h, true
2019
}
2120

2221
func (h HashNode) Put(key []byte, value Node) (Node, bool) {
23-
// TODO: need to implement insertion logic
2422
return nil, false
2523
}
2624

0 commit comments

Comments
 (0)